コード例 #1
0
        /// <summary>
        /// Starts a user stream by requesting a listen key. This listen key can be used in subsequent requests to BinanceSocketClient.Futures.SubscribeToUserDataUpdates. The stream will close after 60 minutes unless a keep alive is send.
        /// </summary>
        /// <param name="ct">Cancellation token</param>
        /// <returns>Listen key</returns>
        public async Task <WebCallResult <string> > StartUserStreamAsync(CancellationToken ct = default)
        {
            var timestampResult = await _futuresClient.CheckAutoTimestamp(ct).ConfigureAwait(false);

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

            var result = await _baseClient.SendRequestInternal <BinanceListenKey>(_baseClient.GetUrl(true, getFuturesListenKeyEndpoint, api, userDataStreamVersion), HttpMethod.Post, ct).ConfigureAwait(false);

            return(new WebCallResult <string>(result.ResponseStatusCode, result.ResponseHeaders, result.Data?.ListenKey, result.Error));
        }
コード例 #2
0
        /// <summary>.
        /// Gets account balances
        /// </summary>
        /// <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>The account information</returns>
        public async Task <WebCallResult <IEnumerable <BinanceFuturesAccountBalance> > > GetFuturesAccountBalanceAsync(long?receiveWindow = null, CancellationToken ct = default)
        {
            var timestampResult = await _futuresClient.CheckAutoTimestamp(ct).ConfigureAwait(false);

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

            var parameters = new Dictionary <string, object>
            {
                { "timestamp", _baseClient.GetTimestamp() }
            };

            parameters.AddOptionalParameter("recvWindow", receiveWindow?.ToString(CultureInfo.InvariantCulture) ?? _baseClient.DefaultReceiveWindow.TotalMilliseconds.ToString(CultureInfo.InvariantCulture));

            return(await _baseClient.SendRequestInternal <IEnumerable <BinanceFuturesAccountBalance> >(_baseClient.GetUrl(true, futuresAccountBalanceEndpoint, api, signedV2), HttpMethod.Get, ct, parameters, true).ConfigureAwait(false));
        }
コード例 #3
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 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="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,
            int?receiveWindow         = null,
            CancellationToken ct      = default)
        {
            symbol.ValidateBinanceSymbol();

            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");
                }
            }


            var timestampResult = await _futuresClient.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, 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;


            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("recvWindow", receiveWindow?.ToString(CultureInfo.InvariantCulture) ?? _baseClient.DefaultReceiveWindow.TotalMilliseconds.ToString(CultureInfo.InvariantCulture));

            return(await _baseClient.SendRequestInternal <BinanceFuturesPlacedOrder>(_baseClient.GetUrl(true, newOrderEndpoint, api, signedVersion), HttpMethod.Post, ct, parameters, true).ConfigureAwait(false));
        }