示例#1
0
        public async Task <OrderDto> PlaceOrderAsync(Instrument instrument, double price, double quantity, TradeSide side, OrderType orderType)
        {
            if (!ExchangeConfig.SupportedInstruments.ContainsKey(instrument))
            {
                _logger.Log(LogLevel.Error, $"{ExchangeConfig.ExchangeName} does not support the {instrument} instrument");

                return(null);
            }

            if (!ExchangeConfig.SupportedOrderTypes.ContainsKey(orderType))
            {
                _logger.Log(LogLevel.Error, $"{ExchangeConfig.ExchangeName} does not support orders of type {orderType}");

                return(null);
            }

            IRequest request = null;
            var      ot      = ExchangeConfig.SupportedOrderTypes[orderType];

            switch (orderType)
            {
            case OrderType.Market:
                request = new PlaceMarketOrderRequest
                {
                    Symbol    = _exchangeApi.ToSymbol(instrument),
                    OrderQty  = quantity,
                    Side      = side.ToString(),
                    OrderType = ot
                };
                break;

            case OrderType.Limit:
                request = new PlaceLimitOrderRequest
                {
                    Symbol    = _exchangeApi.ToSymbol(instrument),
                    OrderQty  = quantity,
                    Side      = side.ToString(),
                    Price     = price,
                    OrderType = ot
                };
                break;

            case OrderType.LimitMarket:
                break;

            case OrderType.Stop:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(orderType), orderType, null);
            }

            var channelPath = ExchangeConfig.SupportedRestChannels[ExchangeChannel.Order];

            return(await _exchangeApi.PostAsync <OrderDto, OrderResponse>(channelPath, request));
        }
        public async Task <IActionResult> PlaceMarketOrder(PlaceMarketOrderRequest request)
        {
            var result = await _validationService.ValidateMarketOrderAsync(request.AssetPairId, request.Volume);

            if (result != null)
            {
                throw HftApiException.Create(result.Code, result.Message).AddField(result.FieldName);
            }

            var walletId = User.GetWalletId();

            var order = new MarketOrderModel
            {
                Id          = Guid.NewGuid().ToString(),
                AssetPairId = request.AssetPairId,
                ClientId    = walletId,
                Volume      = (double)request.Volume,
                OrderAction = request.Side,
                Straight    = true
            };

            var response = await _matchingEngineClient.HandleMarketOrderAsync(order);

            if (response == null)
            {
                throw HftApiException.Create(HftApiErrorCode.MeRuntime, "ME not available");
            }

            (HftApiErrorCode code, string message) = response.Status.ToHftApiError();

            if (code == HftApiErrorCode.Success)
            {
                return(Ok(ResponseModel <MarketOrderResponse> .Ok(new MarketOrderResponse {
                    OrderId = order.Id, Price = (decimal)response.Price
                })));
            }

            throw HftApiException.Create(code, message);
        }