public async Task CancelLimitOrderAsync(string assetId) { if (_hedgeLimitOrders.TryGetValue(assetId, out HedgeLimitOrder hedgeLimitOrder)) { await _lykkeExchangeService.CancelAsync(hedgeLimitOrder.AssetPairId); _hedgeLimitOrderService.Close(hedgeLimitOrder); _hedgeLimitOrders.Remove(assetId); } }
public async Task CancelLimitOrderAsync(string assetId) { ExternalOrder externalOrder = await _externalOrderRepository.GetAsync(Name, assetId); if (externalOrder == null) { return; } ISpotController spotController = _exchangeAdapterClientFactory.GetSpotController(Name); try { await spotController.CancelLimitOrderAsync(new CancelLimitOrderRequest { OrderId = externalOrder.Id }); HedgeLimitOrder hedgeLimitOrder = await _hedgeLimitOrderService.GetByIdAsync(externalOrder.HedgeLimitOrderId); OrderModel order = await spotController.LimitOrderStatusAsync(externalOrder.Id); if (order == null) { _log.WarningWithDetails("External order not found", externalOrder); return; } if (order.ExecutionStatus == OrderStatus.Fill || order.ExecutionStatus == OrderStatus.Canceled) { if (order.ExecutedVolume > 0) { await _positionService.UpdateAsync(hedgeLimitOrder.AssetId, hedgeLimitOrder.Exchange, hedgeLimitOrder.Type == LimitOrderType.Sell ?Domain.TradeType.Sell : Domain.TradeType.Buy, order.ExecutedVolume, order.ExecutedVolume *order.AvgExecutionPrice); await _externalTradeService.RegisterAsync(new ExternalTrade { Id = Guid.NewGuid().ToString("D"), Exchange = hedgeLimitOrder.Exchange, LimitOrderId = hedgeLimitOrder.Id, ExchangeOrderId = externalOrder.Id, AssetPairId = hedgeLimitOrder.AssetPairId, Type = hedgeLimitOrder.Type == LimitOrderType.Sell ? Domain.TradeType.Sell : Domain.TradeType.Buy, Timestamp = order.Timestamp, Price = order.AvgExecutionPrice, Volume = order.ExecutedVolume, Status = order.RemainingAmount > 0 ? TradeStatus.PartialFill : TradeStatus.Fill, OriginalVolume = order.OriginalVolume, RemainingVolume = order.RemainingAmount }); } await _externalOrderRepository.DeleteAsync(externalOrder.Exchange, externalOrder.Asset); _hedgeLimitOrderService.Close(hedgeLimitOrder); } else { _log.WarningWithDetails("Can not cancel external order in progress", externalOrder); } } catch (Exception exception) { _log.WarningWithDetails("An error occurred while canceling limit order", exception, externalOrder); } }