/// <summary>
 /// Places a new test order. Test orders are not actually being executed and just test the functionality.
 /// </summary>
 /// <param name="symbol">The symbol the order is for</param>
 /// <param name="side">The order side (buy/sell)</param>
 /// <param name="type">The order type (limit/market)</param>
 /// <param name="timeInForce">Lifetime of the order (GoodTillCancel/ImmediateOrCancel)</param>
 /// <param name="quantity">The amount of the symbol</param>
 /// <param name="quoteOrderQuantity">The amount of the quote symbol. Only valid for market orders</param>
 /// <param name="price">The price to use</param>
 /// <param name="newClientOrderId">Unique id for order</param>
 /// <param name="stopPrice">Used for stop orders</param>
 /// <param name="icebergQty">User for iceberg orders</param>
 /// <param name="orderResponseType">Used for the response JSON</param>
 /// <param name="receiveWindow">The receive window for which this request is active. When the request takes longer than this to complete the server will reject the request</param>
 /// <param name="ct">Cancellation token</param>
 /// <returns>Id's for the placed test order</returns>
 public async Task <WebCallResult <BinancePlacedOrder> > PlaceTestOrderAsync(string symbol,
                                                                             OrderSide side,
                                                                             OrderType type,
                                                                             decimal?quantity           = null,
                                                                             decimal?quoteOrderQuantity = null,
                                                                             string?newClientOrderId    = null,
                                                                             decimal?price                       = null,
                                                                             TimeInForce?timeInForce             = null,
                                                                             decimal?stopPrice                   = null,
                                                                             decimal?icebergQty                  = null,
                                                                             OrderResponseType?orderResponseType = null,
                                                                             int?receiveWindow                   = null,
                                                                             CancellationToken ct                = default)
 {
     return(await _baseClient.PlaceOrderInternal(_baseClient.GetUrlSpot(newTestOrderEndpoint, api, signedVersion),
                                                 symbol,
                                                 side,
                                                 type,
                                                 quantity,
                                                 quoteOrderQuantity,
                                                 newClientOrderId,
                                                 price,
                                                 timeInForce,
                                                 stopPrice,
                                                 icebergQty,
                                                 null,
                                                 null,
                                                 orderResponseType,
                                                 receiveWindow,
                                                 ct).ConfigureAwait(false));
 }
Beispiel #2
0
        public CallResult <BinancePlacedOrder> PlaceOrder(string symbol, OrderSide side, OrderType type,
                                                          decimal quantity, string newClientOrderId = null,
                                                          decimal?price      = null, TimeInForce?timeInForce = null, decimal?stopPrice = null,
                                                          decimal?icebergQty = null,
                                                          OrderResponseType?orderResponseType = null, int?receiveWindow = null)
        {
            var order = _implementation.PlaceTestOrder(symbol, side, type, quantity, newClientOrderId, price, timeInForce,
                                                       stopPrice, icebergQty, orderResponseType, receiveWindow);

            if (order.Success)
            {
                var data = order.Data;
                data.OrderId = _orderIdGenerator.Next();

                // PlaceTestOrder does not propagate this data, set it manually.
                data.Type = type;
                data.Side = side;
                var ev = new OrderUpdate(
                    orderId: data.OrderId,
                    tradeId: 0,
                    orderStatus: data.Type == OrderType.Market ? OrderUpdate.OrderStatus.Filled : OrderUpdate.OrderStatus.New,
                    orderType: BinanceUtilities.ToInternal(data.Type),
                    createdTimestamp: 0,
                    setPrice: data.Price,
                    side: BinanceUtilities.ToInternal(data.Side),
                    pair: TradingPair.Parse(symbol),
                    setQuantity: data.OriginalQuantity);
                _parent.ScheduleObserverEvent(ev);
            }

            return(order);
        }
        public static Task <WebCallResult <BinancePlacedOrder> > PlaceTestOrLiveOrderAsync(
            this IBinanceClient client,
            bool test,
            string symbol,
            OrderSide side,
            OrderType type,
            Decimal?quantity           = null,
            Decimal?quoteOrderQuantity = null,
            string?newClientOrderId    = null,
            Decimal?price                       = null,
            TimeInForce?timeInForce             = null,
            Decimal?stopPrice                   = null,
            Decimal?icebergQty                  = null,
            OrderResponseType?orderResponseType = null,
            int?receiveWindow                   = null,
            CancellationToken ct                = default(CancellationToken))
        {
            var spotOrderClient = client.Spot.Order;

            return(test
                ? spotOrderClient.PlaceTestOrderAsync(
                       symbol,
                       side,
                       type,
                       quantity,
                       quoteOrderQuantity,
                       newClientOrderId,
                       price,
                       timeInForce,
                       stopPrice,
                       icebergQty,
                       orderResponseType,
                       receiveWindow,
                       ct)
                : spotOrderClient.PlaceOrderAsync(
                       symbol,
                       side,
                       type,
                       quantity,
                       quoteOrderQuantity,
                       newClientOrderId,
                       price,
                       timeInForce,
                       stopPrice,
                       icebergQty,
                       orderResponseType,
                       receiveWindow,
                       ct));
        }
 /// <summary>
 /// Margin account new order
 /// </summary>
 /// <param name="symbol">The symbol the order is for</param>
 /// <param name="side">The order side (buy/sell)</param>
 /// <param name="type">The order type</param>
 /// <param name="timeInForce">Lifetime of the order (GoodTillCancel/ImmediateOrCancel/FillOrKill)</param>
 /// <param name="quantity">The amount of the symbol</param>
 /// <param name="quoteOrderQuantity">The amount of the quote symbol. Only valid for market orders</param>
 /// <param name="price">The price to use</param>
 /// <param name="newClientOrderId">Unique id for order</param>
 /// <param name="stopPrice">Used for stop orders</param>
 /// <param name="icebergQuantity">Used for iceberg orders</param>
 /// <param name="sideEffectType">Side effect type for this order</param>
 /// <param name="isIsolated">For isolated margin or not</param>
 /// <param name="orderResponseType">Used for the response JSON</param>
 /// <param name="receiveWindow">The receive window for which this request is active. When the request takes longer than this to complete the server will reject the request</param>
 /// <param name="ct">Cancellation token</param>
 /// <returns>Id's for the placed order</returns>
 public WebCallResult <BinancePlacedOrder> PlaceMarginOrder(string symbol,
                                                            OrderSide side,
                                                            OrderType type,
                                                            decimal?quantity           = null,
                                                            decimal?quoteOrderQuantity = null,
                                                            string?newClientOrderId    = null,
                                                            decimal?price                       = null,
                                                            TimeInForce?timeInForce             = null,
                                                            decimal?stopPrice                   = null,
                                                            decimal?icebergQuantity             = null,
                                                            SideEffectType?sideEffectType       = null,
                                                            bool?isIsolated                     = null,
                                                            OrderResponseType?orderResponseType = null,
                                                            int?receiveWindow                   = null,
                                                            CancellationToken ct                = default) => PlaceMarginOrderAsync(symbol, side, type, quantity, quoteOrderQuantity, newClientOrderId, price, timeInForce, stopPrice, icebergQuantity, sideEffectType, isIsolated, orderResponseType, receiveWindow, ct).Result;
 /// <summary>
 /// Places a new order
 /// </summary>
 /// <param name="symbol">The symbol the order is for</param>
 /// <param name="side">The order side (buy/sell)</param>
 /// <param name="type">The order type</param>
 /// <param name="timeInForce">Lifetime of the order (GoodTillCancel/ImmediateOrCancel/FillOrKill/GootTillCrossing)</param>
 /// <param name="positionSide">The position side</param>
 /// <param name="quantity">The amount of the base symbol</param>
 /// <param name="reduceOnly">Specify as true if the order is intended to only reduce the position</param>
 /// <param name="price">The price to use</param>
 /// <param name="newClientOrderId">A unique id among open orders. Automatically generated if not sent.</param>
 /// <param name="stopPrice">Used with STOP/STOP_MARKET or TAKE_PROFIT/TAKE_PROFIT_MARKET orders.</param>
 /// <param name="activationPrice">Used with TRAILING_STOP_MARKET orders, default as the latest price(supporting different workingType)</param>
 /// <param name="callbackRate">Used with TRAILING_STOP_MARKET orders</param>
 /// <param name="closePosition">Close-All,used with STOP_MARKET or TAKE_PROFIT_MARKET.</param>
 /// <param name="workingType">stopPrice triggered by: "MARK_PRICE", "CONTRACT_PRICE"</param>
 /// <param name="orderResponseType">The response type. Default Acknowledge</param>
 /// <param name="receiveWindow">The receive window for which this request is active. When the request takes longer than this to complete the server will reject the request</param>
 /// <param name="ct">Cancellation token</param>
 /// <returns>Id's for the placed order</returns>
 public WebCallResult <BinanceFuturesPlacedOrder> PlaceOrder(
     string symbol,
     OrderSide side,
     OrderType type,
     decimal?quantity,
     PositionSide?positionSide           = null,
     TimeInForce?timeInForce             = null,
     bool?reduceOnly                     = null,
     decimal?price                       = null,
     string?newClientOrderId             = null,
     decimal?stopPrice                   = null,
     decimal?activationPrice             = null,
     decimal?callbackRate                = null,
     WorkingType?workingType             = null,
     bool?closePosition                  = null,
     OrderResponseType?orderResponseType = null,
     int?receiveWindow                   = null,
     CancellationToken ct                = default) => PlaceOrderAsync(symbol, side, type, quantity, positionSide, timeInForce, reduceOnly, price, newClientOrderId, stopPrice, activationPrice, callbackRate, workingType, closePosition, orderResponseType, receiveWindow, ct).Result;
Beispiel #6
0
        internal async Task <WebCallResult <BinancePlacedOrder> > PlaceOrderInternal(Uri uri,
                                                                                     string symbol,
                                                                                     OrderSide side,
                                                                                     OrderType type,
                                                                                     decimal?quantity           = null,
                                                                                     decimal?quoteOrderQuantity = null,
                                                                                     string?newClientOrderId    = null,
                                                                                     decimal?price                       = null,
                                                                                     TimeInForce?timeInForce             = null,
                                                                                     decimal?stopPrice                   = null,
                                                                                     decimal?icebergQty                  = null,
                                                                                     SideEffectType?sideEffectType       = null,
                                                                                     bool?isIsolated                     = null,
                                                                                     OrderResponseType?orderResponseType = null,
                                                                                     int?receiveWindow                   = null,
                                                                                     CancellationToken ct                = default)
        {
            symbol.ValidateBinanceSymbol();

            if (quoteOrderQuantity != null && type != OrderType.Market)
            {
                throw new ArgumentException("quoteOrderQuantity is only valid for market orders");
            }

            if ((quantity == null && quoteOrderQuantity == null) || (quantity != null && quoteOrderQuantity != null))
            {
                throw new ArgumentException("1 of either should be specified, quantity or quoteOrderQuantity");
            }


            var timestampResult = await CheckAutoTimestamp(ct).ConfigureAwait(false);

            if (!timestampResult)
            {
                return(new WebCallResult <BinancePlacedOrder>(timestampResult.ResponseStatusCode, timestampResult.ResponseHeaders, null, timestampResult.Error));
            }

            var rulesCheck = await CheckTradeRules(symbol, quantity, price, stopPrice, type, ct).ConfigureAwait(false);

            if (!rulesCheck.Passed)
            {
                log.Write(LogLevel.Warning, rulesCheck.ErrorMessage !);
                return(new WebCallResult <BinancePlacedOrder>(null, null, null, new ArgumentError(rulesCheck.ErrorMessage !)));
            }

            quantity  = rulesCheck.Quantity;
            price     = rulesCheck.Price;
            stopPrice = rulesCheck.StopPrice;

            var parameters = new Dictionary <string, object>
            {
                { "symbol", symbol },
                { "side", JsonConvert.SerializeObject(side, new OrderSideConverter(false)) },
                { "type", JsonConvert.SerializeObject(type, new OrderTypeConverter(false)) },
                { "timestamp", GetTimestamp() }
            };

            parameters.AddOptionalParameter("quantity", quantity?.ToString(CultureInfo.InvariantCulture));
            parameters.AddOptionalParameter("quoteOrderQty", quoteOrderQuantity?.ToString(CultureInfo.InvariantCulture));
            parameters.AddOptionalParameter("newClientOrderId", newClientOrderId);
            parameters.AddOptionalParameter("price", price?.ToString(CultureInfo.InvariantCulture));
            parameters.AddOptionalParameter("timeInForce", timeInForce == null ? null : JsonConvert.SerializeObject(timeInForce, new TimeInForceConverter(false)));
            parameters.AddOptionalParameter("stopPrice", stopPrice?.ToString(CultureInfo.InvariantCulture));
            parameters.AddOptionalParameter("icebergQty", icebergQty?.ToString(CultureInfo.InvariantCulture));
            parameters.AddOptionalParameter("sideEffectType", sideEffectType == null ? null : JsonConvert.SerializeObject(sideEffectType, new SideEffectTypeConverter(false)));
            parameters.AddOptionalParameter("isIsolated", isIsolated);
            parameters.AddOptionalParameter("newOrderRespType", orderResponseType == null ? null : JsonConvert.SerializeObject(orderResponseType, new OrderResponseTypeConverter(false)));
            parameters.AddOptionalParameter("recvWindow", receiveWindow?.ToString(CultureInfo.InvariantCulture) ?? DefaultReceiveWindow.TotalMilliseconds.ToString(CultureInfo.InvariantCulture));

            return(await SendRequestAsync <BinancePlacedOrder>(uri, HttpMethod.Post, ct, parameters, true).ConfigureAwait(false));
        }
Beispiel #7
0
 /// <summary>
 /// Places a new order
 /// </summary>
 /// <param name="symbol">The symbol the order is for</param>
 /// <param name="side">The order side (buy/sell)</param>
 /// <param name="type">The order type</param>
 /// <param name="timeInForce">Lifetime of the order (GoodTillCancel/ImmediateOrCancel/FillOrKill)</param>
 /// <param name="quantity">The amount of the symbol</param>
 /// <param name="quoteOrderQuantity">The amount of the quote symbol. Only valid for market orders</param>
 /// <param name="price">The price to use</param>
 /// <param name="newClientOrderId">Unique id for order</param>
 /// <param name="stopPrice">Used for stop orders</param>
 /// <param name="icebergQty">Used for iceberg orders</param>
 /// <param name="orderResponseType">The type of response to receive</param>
 /// <param name="receiveWindow">The receive window for which this request is active. When the request takes longer than this to complete the server will reject the request</param>
 /// <param name="ct">Cancellation token</param>
 /// <param name="futures"></param>
 /// <returns>Id's for the placed order</returns>
 public async Task <WebCallResult <BinancePlacedOrder> > PlaceOrderAsync(string symbol, OrderSide side, OrderType type, decimal?quantity = null, decimal?quoteOrderQuantity = null, string?newClientOrderId = null, decimal?price = null, TimeInForce?timeInForce = null, decimal?stopPrice = null, decimal?icebergQty = null, OrderResponseType?orderResponseType = null, int?receiveWindow = null, CancellationToken ct = default, bool futures = true)
 {
     return(await PlaceOrderInternal(GetUrl(NewOrderEndpoint, FuturesApi, FuturesVersion, true),
                                     symbol,
                                     side,
                                     type,
                                     quantity,
                                     quoteOrderQuantity,
                                     newClientOrderId,
                                     price,
                                     timeInForce,
                                     stopPrice,
                                     icebergQty,
                                     null,
                                     orderResponseType,
                                     receiveWindow,
                                     ct).ConfigureAwait(false));
 }
Beispiel #8
0
 public WebCallResult <BinancePlacedOrder> PlaceOrder(string symbol, OrderSide side, OrderType type, decimal?quantity = null, decimal?quoteOrderQuantity = null, string?newClientOrderId = null, decimal?price = null, TimeInForce?timeInForce = null, decimal?stopPrice = null, decimal?icebergQty = null, OrderResponseType?orderResponseType = null, int?receiveWindow = null, CancellationToken ct = default, bool futures = true)
 => PlaceOrderAsync(symbol, side, type, quantity, quoteOrderQuantity, newClientOrderId, price, timeInForce, stopPrice, icebergQty, orderResponseType, receiveWindow, ct, futures).Result;
        /// <summary>
        /// Places a new order
        /// </summary>
        /// <param name="symbol">The symbol the order is for</param>
        /// <param name="side">The order side (buy/sell)</param>
        /// <param name="type">The order type</param>
        /// <param name="timeInForce">Lifetime of the order (GoodTillCancel/ImmediateOrCancel/FillOrKill)</param>
        /// <param name="quantity">The amount of the base symbol</param>
        /// <param name="positionSide">The position side</param>
        /// <param name="reduceOnly">Specify as true if the order is intended to only reduce the position</param>
        /// <param name="price">The price to use</param>
        /// <param name="newClientOrderId">Unique id for order</param>
        /// <param name="stopPrice">Used for stop orders</param>
        /// <param name="activationPrice">Used with TRAILING_STOP_MARKET orders, default as the latest price(supporting different workingType)</param>
        /// <param name="callbackRate">Used with TRAILING_STOP_MARKET orders</param>
        /// <param name="workingType">stopPrice triggered by: "MARK_PRICE", "CONTRACT_PRICE"</param>
        /// <param name="closePosition">Close-All,used with STOP_MARKET or TAKE_PROFIT_MARKET.</param>
        /// <param name="orderResponseType">The response type. Default Acknowledge</param>
        /// <param name="receiveWindow">The receive window for which this request is active. When the request takes longer than this to complete the server will reject the request</param>
        /// <param name="ct">Cancellation token</param>
        /// <returns>Id's for the placed order</returns>
        public async Task <WebCallResult <BinanceFuturesPlacedOrder> > PlaceOrderAsync(
            string symbol,
            OrderSide side,
            OrderType type,
            decimal?quantity,
            PositionSide?positionSide           = null,
            TimeInForce?timeInForce             = null,
            bool?reduceOnly                     = null,
            decimal?price                       = null,
            string?newClientOrderId             = null,
            decimal?stopPrice                   = null,
            decimal?activationPrice             = null,
            decimal?callbackRate                = null,
            WorkingType?workingType             = null,
            bool?closePosition                  = null,
            OrderResponseType?orderResponseType = null,
            int?receiveWindow                   = null,
            CancellationToken ct                = default)
        {
            if (closePosition == true && positionSide != null)
            {
                if (positionSide == PositionSide.Short && side == OrderSide.Sell)
                {
                    throw new ArgumentException("Can't close short position with order side sell");
                }
                if (positionSide == PositionSide.Long && side == OrderSide.Buy)
                {
                    throw new ArgumentException("Can't close long position with order side buy");
                }
            }

            if (orderResponseType == OrderResponseType.Full)
            {
                throw new ArgumentException("OrderResponseType.Full is not supported in Futures");
            }


            var timestampResult = await BaseClient.CheckAutoTimestamp(ct).ConfigureAwait(false);

            if (!timestampResult)
            {
                return(new WebCallResult <BinanceFuturesPlacedOrder>(timestampResult.ResponseStatusCode, timestampResult.ResponseHeaders, null, timestampResult.Error));
            }

            var rulesCheck = await FuturesClient.CheckTradeRules(symbol, quantity, price, stopPrice, type, ct).ConfigureAwait(false);

            if (!rulesCheck.Passed)
            {
                _log.Write(LogVerbosity.Warning, rulesCheck.ErrorMessage !);
                return(new WebCallResult <BinanceFuturesPlacedOrder>(null, null, null, new ArgumentError(rulesCheck.ErrorMessage !)));
            }

            quantity  = rulesCheck.Quantity;
            price     = rulesCheck.Price;
            stopPrice = rulesCheck.StopPrice;

            var parameters = new Dictionary <string, object>
            {
                { "symbol", symbol },
                { "side", JsonConvert.SerializeObject(side, new OrderSideConverter(false)) },
                { "type", JsonConvert.SerializeObject(type, new OrderTypeConverter(false)) },
                { "timestamp", BaseClient.GetTimestamp() }
            };

            parameters.AddOptionalParameter("quantity", quantity?.ToString(CultureInfo.InvariantCulture));
            parameters.AddOptionalParameter("newClientOrderId", newClientOrderId);
            parameters.AddOptionalParameter("price", price?.ToString(CultureInfo.InvariantCulture));
            parameters.AddOptionalParameter("timeInForce", timeInForce == null ? null : JsonConvert.SerializeObject(timeInForce, new TimeInForceConverter(false)));
            parameters.AddOptionalParameter("positionSide", positionSide == null ? null : JsonConvert.SerializeObject(positionSide, new PositionSideConverter(false)));
            parameters.AddOptionalParameter("stopPrice", stopPrice?.ToString(CultureInfo.InvariantCulture));
            parameters.AddOptionalParameter("activationPrice", activationPrice?.ToString(CultureInfo.InvariantCulture));
            parameters.AddOptionalParameter("callbackRate", callbackRate?.ToString(CultureInfo.InvariantCulture));
            parameters.AddOptionalParameter("workingType", workingType == null ? null : JsonConvert.SerializeObject(workingType, new WorkingTypeConverter(false)));
            parameters.AddOptionalParameter("reduceOnly", reduceOnly?.ToString().ToLower());
            parameters.AddOptionalParameter("closePosition", closePosition?.ToString().ToLower());
            parameters.AddOptionalParameter("newOrderRespType", orderResponseType == null ? null : JsonConvert.SerializeObject(orderResponseType, new OrderResponseTypeConverter(false)));
            parameters.AddOptionalParameter("recvWindow", receiveWindow?.ToString(CultureInfo.InvariantCulture) ?? BaseClient.DefaultReceiveWindow.TotalMilliseconds.ToString(CultureInfo.InvariantCulture));

            return(await BaseClient.SendRequestInternal <BinanceFuturesPlacedOrder>(FuturesClient.GetUrl(newOrderEndpoint, Api, SignedVersion), HttpMethod.Post, ct, parameters, true).ConfigureAwait(false));
        }
Beispiel #10
0
        /// <summary>
        /// Send new order.
        /// </summary>
        /// <param name="symbol">Symbol of Coin.</param>
        /// <param name="side">Order Side [BUY, SELL]</param>
        /// <param name="type">Order Type [LIMIT, MARKET, STOP_LOSS , STOP_LOSS_LIMIT, TAKE_PROFIT, TAKE_PROFIT_LIMIT, LIMIT_MAKER]</param>
        /// <param name="price">The price of the coin at the time the order is entered.</param>
        /// <param name="quantity">The quantity of assets to buy or sell in the limit order.</param>
        /// <param name="quoteOrderQty">MARKET orders using quoteOrderQty specifies the amount the user wants to spend (when buying) or receive (when selling) the quote asset; the correct quantity will be determined based on the market liquidity and quoteOrderQty.
        /// Using BTCUSDT as an example:
        /// On the BUY side, the order will buy as many BTC as quoteOrderQty USDT can.
        /// On the SELL side, the order will sell as much BTC needed to receive quoteOrderQty USDT.
        /// </param>
        /// <param name="icebergQty">A conditional order to buy or sell a large amount of assets in smaller predetermined quantities in order to conceal the total order quantity.</param>
        /// <param name="stopPrice">When the asset’s price reaches the given stop price, the stop-limit order is executed to buy or sell the asset at the given limit price or better.</param>
        /// <param name="orderResponseType">ACK, RESULT or FULL; MARKET and LIMIT order types default to FULL, all other orders default to ACK.</param>
        /// <param name="timeInForce">Any order with an icebergQty MUST have timeInForce set to GTC.</param>
        /// <param name="recvWindow">The value cannot be greater than 60000.</param>
        /// <returns></returns>
        public async Task <Order> NewOrder(string symbol, OrderSide side, OrderType type, decimal?price = null, decimal?quantity = null, decimal?quoteOrderQty = null, decimal?icebergQty = null, decimal?stopPrice = null, OrderResponseType?orderResponseType = OrderResponseType.ACK, TimeInForce timeInForce = TimeInForce.GTC, long?recvWindow = 5000)
        {
            var parameters = $"symbol={symbol.ToUpper()}&side={side.GetMemberAttr()}&type={type.GetMemberAttr()}&recvWindow={recvWindow}"
                             + ((type == OrderType.LIMIT || type == OrderType.STOP_LOSS_LIMIT || type == OrderType.TAKE_PROFIT_LIMIT) ? $"&timeInForce={timeInForce.GetMemberAttr()}" : "")
                             + ((type == OrderType.LIMIT || type == OrderType.MARKET || type == OrderType.STOP_LOSS || type == OrderType.STOP_LOSS_LIMIT || type == OrderType.TAKE_PROFIT || type == OrderType.TAKE_PROFIT_LIMIT || type == OrderType.LIMIT_MAKER) && quantity > 0m ? $"&quantity={quantity}" : "")
                             + ((type == OrderType.MARKET) && quoteOrderQty > 0m ? $"&quoteOrderQty={quoteOrderQty}" : "")
                             + ((type == OrderType.LIMIT || type == OrderType.STOP_LOSS_LIMIT || type == OrderType.TAKE_PROFIT_LIMIT || type == OrderType.LIMIT_MAKER) && price > 0m ? $"&price={price}" : "")
                             + ((type == OrderType.STOP_LOSS || type == OrderType.STOP_LOSS_LIMIT || type == OrderType.TAKE_PROFIT || type == OrderType.TAKE_PROFIT_LIMIT) && stopPrice > 0m ? $"&stopPrice={stopPrice}" : "")
                             + ((type == OrderType.LIMIT || type == OrderType.STOP_LOSS_LIMIT || type == OrderType.TAKE_PROFIT_LIMIT) && icebergQty > 0m ? $"&icebergQty={icebergQty}" : "")
                             + ((orderResponseType.HasValue) ? $"&newOrderRespType={orderResponseType.GetMemberAttr()}" : "");


            switch (orderResponseType)
            {
            case OrderResponseType.RESULT:
                return(await _httpConfiguration.SendAsyncRequest <ResponseResult>(HttpMethod.Post, Endpoint.Order, parameters, true));

            case OrderResponseType.FULL:
                return(await _httpConfiguration.SendAsyncRequest <ResponseFull>(HttpMethod.Post, Endpoint.Order, parameters, true));

            default:
                return(await _httpConfiguration.SendAsyncRequest <ResponseAcknowledge>(HttpMethod.Post, Endpoint.Order, parameters, true));
            }
        }
        /// <summary>
        /// Places a new margin OCO(One cancels other) order
        /// </summary>
        /// <param name="symbol">The symbol the order is for</param>
        /// <param name="side">The order side (buy/sell)</param>
        /// <param name="stopLimitTimeInForce">Lifetime of the stop order (GoodTillCancel/ImmediateOrCancel/FillOrKill)</param>
        /// <param name="quantity">The amount of the symbol</param>
        /// <param name="price">The price to use</param>
        /// <param name="stopPrice">The stop price</param>
        /// <param name="stopLimitPrice">The price for the stop limit order</param>
        /// <param name="stopClientOrderId">Client id for the stop order</param>
        /// <param name="limitClientOrderId">Client id for the limit order</param>
        /// <param name="listClientOrderId">Client id for the order list</param>
        /// <param name="limitIcebergQuantity">Iceberg quantity for the limit order</param>
        /// <param name="sideEffectType">Side effect type</param>
        /// <param name="isIsolated">Is isolated</param>
        /// <param name="orderResponseType">Order response type</param>
        /// <param name="stopIcebergQuantity">Iceberg quantity for the stop order</param>
        /// <param name="receiveWindow">The receive window for which this request is active. When the request takes longer than this to complete the server will reject the request</param>
        /// <param name="ct">Cancellation token</param>
        /// <returns>Order list info</returns>
        public async Task <WebCallResult <BinanceMarginOrderOcoList> > PlaceMarginOCOOrderAsync(string symbol,
                                                                                                OrderSide side,
                                                                                                decimal price,
                                                                                                decimal stopPrice,
                                                                                                decimal quantity,
                                                                                                decimal?stopLimitPrice           = null,
                                                                                                TimeInForce?stopLimitTimeInForce = null,
                                                                                                decimal?stopIcebergQuantity      = null,
                                                                                                decimal?limitIcebergQuantity     = null,
                                                                                                SideEffectType?sideEffectType    = null,
                                                                                                bool?isIsolated                     = null,
                                                                                                string?listClientOrderId            = null,
                                                                                                string?limitClientOrderId           = null,
                                                                                                string?stopClientOrderId            = null,
                                                                                                OrderResponseType?orderResponseType = null,
                                                                                                int?receiveWindow                   = null,
                                                                                                CancellationToken ct                = default)
        {
            symbol.ValidateBinanceSymbol();
            var timestampResult = await _baseClient.CheckAutoTimestamp(ct).ConfigureAwait(false);

            if (!timestampResult)
            {
                return(new WebCallResult <BinanceMarginOrderOcoList>(timestampResult.ResponseStatusCode, timestampResult.ResponseHeaders, null, timestampResult.Error));
            }

            var rulesCheck = await _baseClient.CheckTradeRules(symbol, quantity, price, stopPrice, null, ct).ConfigureAwait(false);

            if (!rulesCheck.Passed)
            {
                _log.Write(LogLevel.Warning, rulesCheck.ErrorMessage !);
                return(new WebCallResult <BinanceMarginOrderOcoList>(null, null, null, new ArgumentError(rulesCheck.ErrorMessage !)));
            }

            quantity  = rulesCheck.Quantity !.Value;
            price     = rulesCheck.Price !.Value;
            stopPrice = rulesCheck.StopPrice !.Value;

            var parameters = new Dictionary <string, object>
            {
                { "symbol", symbol },
                { "side", JsonConvert.SerializeObject(side, new OrderSideConverter(false)) },
                { "quantity", quantity.ToString(CultureInfo.InvariantCulture) },
                { "price", price.ToString(CultureInfo.InvariantCulture) },
                { "stopPrice", stopPrice.ToString(CultureInfo.InvariantCulture) },
                { "timestamp", _baseClient.GetTimestamp() }
            };

            parameters.AddOptionalParameter("stopLimitPrice", stopLimitPrice?.ToString(CultureInfo.InvariantCulture));
            parameters.AddOptionalParameter("isIsolated", isIsolated?.ToString());
            parameters.AddOptionalParameter("sideEffectType", sideEffectType == null ? null : JsonConvert.SerializeObject(sideEffectType, new SideEffectTypeConverter(false)));
            parameters.AddOptionalParameter("listClientOrderId", listClientOrderId);
            parameters.AddOptionalParameter("limitClientOrderId", limitClientOrderId);
            parameters.AddOptionalParameter("stopClientOrderId", stopClientOrderId);
            parameters.AddOptionalParameter("limitIcebergQty", limitIcebergQuantity?.ToString(CultureInfo.InvariantCulture));
            parameters.AddOptionalParameter("newOrderRespType", orderResponseType == null ? null : JsonConvert.SerializeObject(orderResponseType, new OrderResponseTypeConverter(false)));
            parameters.AddOptionalParameter("stopIcebergQty", stopIcebergQuantity?.ToString(CultureInfo.InvariantCulture));
            parameters.AddOptionalParameter("stopLimitTimeInForce", stopLimitTimeInForce == null ? null : JsonConvert.SerializeObject(stopLimitTimeInForce, new TimeInForceConverter(false)));
            parameters.AddOptionalParameter("recvWindow", receiveWindow?.ToString(CultureInfo.InvariantCulture) ?? _baseClient.DefaultReceiveWindow.TotalMilliseconds.ToString(CultureInfo.InvariantCulture));

            return(await _baseClient.SendRequestInternal <BinanceMarginOrderOcoList>(_baseClient.GetUrlSpot(newMarginOCOOrderEndpoint, marginApi, marginVersion), HttpMethod.Post, ct, parameters, true).ConfigureAwait(false));
        }
Beispiel #12
0
 public Task <CallResult <BinancePlacedOrder> > PlaceTestOrderAsync(string symbol, OrderSide side, OrderType type, decimal quantity,
                                                                    string newClientOrderId = null, decimal?price = null, TimeInForce?timeInForce                 = null, decimal?stopPrice = null,
                                                                    decimal?icebergQty      = null, OrderResponseType?orderResponseType = null, int?receiveWindow = null) => throw new NotImplementedException();