public async Task ExecuteLimitOrderAsync(HedgeLimitOrder hedgeLimitOrder) { await _hedgeLimitOrderService.AddAsync(hedgeLimitOrder); AssetPairSettings assetPairSettings = await _instrumentService.GetAssetPairAsync(hedgeLimitOrder.AssetPairId, Name); if (assetPairSettings == null) { hedgeLimitOrder.Error = LimitOrderError.Unknown; hedgeLimitOrder.ErrorMessage = "Instrument not configured"; _log.WarningWithDetails("No settings for instrument", hedgeLimitOrder); return; } decimal price = hedgeLimitOrder.Price .TruncateDecimalPlaces(assetPairSettings.PriceAccuracy, hedgeLimitOrder.Type == LimitOrderType.Sell); decimal volume = Math.Round(hedgeLimitOrder.Volume, assetPairSettings.VolumeAccuracy); if (volume < assetPairSettings.MinVolume) { hedgeLimitOrder.Error = LimitOrderError.TooSmallVolume; return; } var limitOrder = new LimitOrder { Id = hedgeLimitOrder.Id, Price = price, Volume = volume, Type = hedgeLimitOrder.Type }; try { await _lykkeExchangeService.ApplyAsync(hedgeLimitOrder.AssetPairId, limitOrder); if (limitOrder.Error == LimitOrderError.None) { _hedgeLimitOrders[hedgeLimitOrder.AssetId] = hedgeLimitOrder; } hedgeLimitOrder.Error = limitOrder.Error; hedgeLimitOrder.ErrorMessage = limitOrder.ErrorMessage; } catch (ExchangeException exception) { hedgeLimitOrder.Error = LimitOrderError.Unknown; hedgeLimitOrder.ErrorMessage = exception.Message; } catch (Exception exception) { hedgeLimitOrder.Error = LimitOrderError.Unknown; hedgeLimitOrder.ErrorMessage = "Cannot create limit orders an unexpected error occurred"; _log.WarningWithDetails("An error occurred during creating limit orders", exception, hedgeLimitOrder); } }
public async Task ExecuteLimitOrderAsync(HedgeLimitOrder hedgeLimitOrder) { await _hedgeLimitOrderService.AddAsync(hedgeLimitOrder); ExternalOrder externalOrder = await _externalOrderRepository.GetAsync(hedgeLimitOrder.Exchange, hedgeLimitOrder.AssetId); if (externalOrder != null) { hedgeLimitOrder.Error = LimitOrderError.Unknown; hedgeLimitOrder.ErrorMessage = "Already exists"; return; } AssetPairSettings assetPairSettings = await _instrumentService.GetAssetPairAsync(hedgeLimitOrder.AssetPairId, hedgeLimitOrder.Exchange); if (assetPairSettings == null) { hedgeLimitOrder.Error = LimitOrderError.Unknown; hedgeLimitOrder.ErrorMessage = "Instrument not configured"; _log.WarningWithDetails("No settings for instrument", hedgeLimitOrder); return; } decimal price = hedgeLimitOrder.Price .TruncateDecimalPlaces(assetPairSettings.PriceAccuracy, hedgeLimitOrder.Type == LimitOrderType.Sell); decimal volume = Math.Round(hedgeLimitOrder.Volume, assetPairSettings.VolumeAccuracy); if (volume < assetPairSettings.MinVolume) { hedgeLimitOrder.Error = LimitOrderError.TooSmallVolume; return; } ISpotController spotController = _exchangeAdapterClientFactory.GetSpotController(Name); try { var assetPair = assetPairSettings.AssetPairId; // TODO: Remove this workaround if (Name == "NettingEngineDefault") { assetPair = GetAssetPair(assetPair); } OrderIdResponse response = await spotController.CreateLimitOrderAsync(new LimitOrderRequest { Instrument = assetPair, TradeType = hedgeLimitOrder.Type == LimitOrderType.Sell ? TradeType.Sell : TradeType.Buy, Price = price, Volume = volume }); externalOrder = new ExternalOrder(response.OrderId, hedgeLimitOrder.Exchange, hedgeLimitOrder.AssetId, hedgeLimitOrder.Id); await _externalOrderRepository.InsertAsync(externalOrder); _log.InfoWithDetails("External order created", externalOrder); } catch (Exception exception) { hedgeLimitOrder.Error = LimitOrderError.Unknown; hedgeLimitOrder.ErrorMessage = exception.Message; _log.WarningWithDetails("An error occurred while creating external order", exception, hedgeLimitOrder); } }