Beispiel #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));
        }
Beispiel #2
0
        public async Task <Binance.Blank> TestPlaceOrderAsync(string symbol, TradeSide side, Binance.OrderType type, decimal amount, decimal?price = null, decimal?stopPrice = null, Binance.TimeInForce tif = Binance.TimeInForce.GTC, string newClientOrderId = null)
        {
            var serverTime = await GetServerTime();

            var offset  = serverTime - DateTime.UtcNow;
            var request = new RestSharp.RestRequest(TestPlaceOrderEndpoint, RestSharp.Method.POST);

            request.AddQueryParameter("symbol", symbol);
            request.AddQueryParameter("side", side.ToString().ToUpper());
            request.AddQueryParameter("type", type.ToString().ToUpper());
            request.AddQueryParameter("quantity", amount.ToString());
            request.AddQueryParameter("newOrderRespType", "RESULT");
            request.AddQueryParameter("timeInForce", tif.ToString().ToUpper());
            //request.AddQueryParameter("recvWindow", 60000);
            if (price != null)
            {
                request.AddQueryParameter("price", price.Value.ToString());
            }
            if (newClientOrderId != null)
            {
                request.AddQueryParameter("newClientOrderId", newClientOrderId);
            }
            var result = await RequestSignedApiAsync <Binance.Blank>(request, TestPlaceOrderWeight, offset);

            return(result);
        }
        public static OpenPosition GetLatestPositionBySide(List <OpenPosition> positions, TradeSide tradeSide, string epic)
        {
            var marketPositions = positions.Where(x => x.Position.Direction == tradeSide.ToString() && x.Market.Epic == epic).ToList();

            if (marketPositions.Any())
            {
                return(tradeSide == TradeSide.BUY ?
                       marketPositions.OrderBy(x => x.Position.Level).FirstOrDefault() :
                       marketPositions.OrderByDescending(x => x.Position.Level).FirstOrDefault());
            }
            return(null);
        }