Esempio n. 1
0
        public async Task <string> CreateLimitOrderAsync(string assetPair, decimal price, decimal volume,
                                                         OrderType orderType)
        {
            try
            {
                OrderIdResponse response = await _client.CreateLimitOrderAsync(new LimitOrderRequest
                {
                    Instrument = GetAssetPairId(assetPair),
                    TradeType  = orderType == OrderType.Sell
                        ? TradeType.Sell
                        : TradeType.Buy,
                    Price  = price,
                    Volume = volume
                });

                return(response.OrderId);
            }
            catch (FailedOperationException)
            {
                throw;
            }
            catch (Exception exception)
            {
                throw new Exception("An error occurred while creating limit order", exception);
            }
        }
        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);
            }
        }