public async Task Pulse() { foreach (var level in _levels) { await _semaphoreSlim.WaitAsync(); try { bool shouldPulse; string currentOrderId; currentOrderId = level.CurrentOrderId; shouldPulse = level.Status == MarketLevelStatus.Placed; if (shouldPulse) { var size = await _trader.GetRemainingSizeOfActiveOrder(currentOrderId, _symbol); if (size.HasValue) { var price = level.ActualOrderPrice; var orderId = _trader.RePlaceOrderAsync( currentOrderId, _symbol, price, size.Value, level.ActualOrderSide).GetAwaiter().GetResult(); Console.WriteLine($"Pulse {level.PriceBuy}, {level.Side}: Price: {price}, Side: {level.ActualOrderSide}, Size: {size.Value}"); if (!string.IsNullOrEmpty(orderId) && level.CurrentOrderId == currentOrderId) { level.CurrentOrderId = orderId; _levelsByOrder[orderId] = level; } } } } catch (Exception ex) { Console.WriteLine($"ERROR: cannot exec pulse for {level.PriceBuy} ({level.Side})"); Console.WriteLine(ex); } finally { _semaphoreSlim.Release(); } } }