/// <summary> /// Cancels any orders that have been buying for an entire cycle. /// </summary> /// <returns></returns> private async Task CancelUnboughtOrders() { // Only trigger if there are orders still buying. if (_activeTrades.Any(x => x.IsBuying)) { // Loop our current buying trades if there are any. foreach (var trade in _activeTrades.Where(x => x.IsBuying)) { var exchangeOrder = await _api.GetOrder(trade.BuyOrderId, trade.Market); // if this order is PartiallyFilled, don't cancel if (exchangeOrder?.Status == OrderStatus.PartiallyFilled) { continue; // not yet completed so wait } // Cancel our open buy order. await _api.CancelOrder(trade.BuyOrderId, trade.Market); // Update the buy order. trade.IsBuying = false; trade.OpenOrderId = null; trade.IsOpen = false; trade.SellType = SellType.Cancelled; trade.CloseDate = DateTime.UtcNow; // Update the order in our batch. _orderBatch.Add(TableOperation.Replace(trade)); // Handle the trader that was dedicated to this order. var currentTrader = _currentTraders.FirstOrDefault(x => x.RowKey == trade.TraderId); if (currentTrader != null) { currentTrader.IsBusy = false; currentTrader.LastUpdated = DateTime.UtcNow; // Update the trader to indicate that we're not busy anymore. await _traderTable.ExecuteAsync(TableOperation.Replace(currentTrader)); } await SendNotification($"Cancelled {trade.Market} buy order."); } } }
/// <summary> /// Cancels any orders that have been buying for an entire cycle. /// </summary> /// <returns></returns> private async Task CancelUnboughtOrders() { // Only trigger if there are orders still buying. if (_activeTrades.Any(x => x.IsBuying)) { // Loop our current trades that are still looking to buy if there are any. foreach (var trade in _activeTrades.Where(x => x.IsBuying)) { var exchangeOrder = await _api.GetOrder(trade.BuyOrderId, trade.Market); // if this order is PartiallyFilled, don't cancel if (exchangeOrder?.Status == OrderStatus.PartiallyFilled) { continue; // not yet completed so wait } await _api.CancelOrder(trade.BuyOrderId, trade.Market); // Update the buy order in our data storage. trade.IsBuying = false; trade.OpenOrderId = null; trade.IsOpen = false; trade.SellType = SellType.Cancelled; trade.CloseDate = DateTime.UtcNow; // Update the order await _dataStore.SaveTradeAsync(trade); // Handle the trader that was dedicated to this order. var currentTrader = _currentTraders.FirstOrDefault(x => x.Identifier == trade.TraderId); // If there is a trader, update that as well... if (currentTrader != null) { currentTrader.IsBusy = false; currentTrader.LastUpdated = DateTime.UtcNow; // Update the trader to indicate that we're not busy anymore. await _dataStore.SaveTraderAsync(currentTrader); } await SendNotification($"Cancelled {trade.Market} buy order because it wasn't filled in time."); } } }