/// <summary> /// Get candlesticks for a symbol. Candlesticks/K-Lines are uniquely identified by their open time. /// If startTime and endTime are not sent, the most recent candlesticks are returned. /// </summary> /// <param name="client"></param> /// <param name="symbol"></param> /// <param name="interval"></param> /// <param name="limit">Default 500; max 500.</param> /// <param name="startTime"></param> /// <param name="endTime"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> GetCandlesticksAsync(this IBinanceHttpClient client, string symbol, CandlestickInterval interval, int limit = default, DateTime startTime = default, DateTime endTime = default, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNullOrWhiteSpace(symbol, nameof(symbol)); if (client.RateLimiter != null) { await client.RateLimiter.DelayAsync(token : token) .ConfigureAwait(false); } var request = new BinanceHttpRequest("/api/v1/klines"); request.AddParameter("symbol", symbol.FormatSymbol()); request.AddParameter("interval", interval.AsString()); if (limit > 0) { request.AddParameter("limit", limit); } if (startTime != default) { if (startTime.Kind != DateTimeKind.Utc) { throw new ArgumentException("Date/Time must be UTC.", nameof(startTime)); } request.AddParameter("startTime", startTime.ToTimestamp()); } // ReSharper disable once InvertIf if (endTime != default) { if (endTime.Kind != DateTimeKind.Utc) { throw new ArgumentException("Date/Time must be UTC.", nameof(endTime)); } request.AddParameter("endTime", endTime.ToTimestamp()); } return(await client.GetAsync(request, token) .ConfigureAwait(false)); }
/// <summary> /// Get trades for a specific account and symbol. /// </summary> /// <param name="client"></param> /// <param name="user"></param> /// <param name="symbol"></param> /// <param name="fromId">TradeId to fetch from. Default gets most recent trades.</param> /// <param name="limit">Default 500; max 500.</param> /// <param name="recvWindow"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> GetAccountTradesAsync(this IBinanceHttpClient client, IBinanceApiUser user, string symbol, long fromId = BinanceApi.NullId, int limit = default, long recvWindow = default, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNull(user, nameof(user)); Throw.IfNullOrWhiteSpace(symbol, nameof(symbol)); if (recvWindow == default) { recvWindow = client.DefaultRecvWindow; } if (client.RateLimiter != null) { await client.RateLimiter.DelayAsync(5, token) .ConfigureAwait(false); } var request = new BinanceHttpRequest("/api/v3/myTrades") { ApiKey = user.ApiKey }; request.AddParameter("symbol", symbol.FormatSymbol()); if (fromId >= 0) { request.AddParameter("fromId", fromId); } if (limit > 0) { request.AddParameter("limit", limit); } if (recvWindow > 0) { request.AddParameter("recvWindow", recvWindow); } await client.SignAsync(request, user, token) .ConfigureAwait(false); return(await client.GetAsync(request, token) .ConfigureAwait(false)); }
/// <summary> /// Get trades for a specific account and symbol. /// </summary> /// <param name="client"></param> /// <param name="user"></param> /// <param name="symbol"></param> /// <param name="fromId">TradeId to fetch from. Default gets most recent trades.</param> /// <param name="limit">Default 500; max 500.</param> /// <param name="recvWindow"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> GetAccountTradesAsync(this IBinanceHttpClient client, IBinanceApiUser user, string symbol, long fromId = BinanceApi.NullId, int limit = default, long recvWindow = default, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNull(user, nameof(user)); Throw.IfNullOrWhiteSpace(symbol, nameof(symbol)); if (recvWindow <= 0) { recvWindow = client.Options.RecvWindowDefault ?? 0; } var request = new BinanceHttpRequest($"/api/v3/myTrades") { ApiKey = user.ApiKey }; request.AddParameter("symbol", symbol.FormatSymbol()); if (fromId >= 0) { request.AddParameter("fromId", fromId); } if (limit > 0) { request.AddParameter("limit", limit); } if (recvWindow > 0) { request.AddParameter("recvWindow", recvWindow); } var timestamp = await client.GetTimestampAsync(token) .ConfigureAwait(false); request.AddParameter("timestamp", timestamp); var signature = user.Sign(request.QueryString); request.AddParameter("signature", signature); return(await client.GetAsync(request, token) .ConfigureAwait(false)); }
/// <summary> /// Get best price/quantity on the order book for a symbol. /// </summary> /// <param name="client"></param> /// <param name="symbol"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> GetOrderBookTopAsync(this IBinanceHttpClient client, string symbol, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNullOrWhiteSpace(symbol, nameof(symbol)); if (client.RateLimiter != null) { await client.RateLimiter.DelayAsync(token : token) .ConfigureAwait(false); } var request = new BinanceHttpRequest("/api/v1/ticker/bookTicker"); request.AddParameter("symbol", symbol.FormatSymbol()); return(await client.GetAsync(request, token) .ConfigureAwait(false)); }
/// <summary> /// Get the deposit address for an asset. /// </summary> /// <param name="client"></param> /// <param name="user"></param> /// <param name="asset"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> GetDepositAddressAsync(this IBinanceHttpClient client, IBinanceApiUser user, string asset, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNullOrWhiteSpace(asset, nameof(asset)); var request = new BinanceHttpRequest("/wapi/v3/depositAddress.html") { ApiKey = user.ApiKey }; request.AddParameter("asset", asset.FormatSymbol()); await client.SignAsync(request, user, token) .ConfigureAwait(false); return(await client.GetAsync(request, token) .ConfigureAwait(false)); }
/// <summary> /// Get compressed, aggregate trades. Trades that fill at the time, from the same order, with the same price will have the quantity aggregated. /// If fromdId, startTime, and endTime are not sent, the most recent aggregate trades will be returned. /// </summary> /// <param name="client"></param> /// <param name="symbol"></param> /// <param name="fromId">ID to get aggregate trades from INCLUSIVE.</param> /// <param name="limit">Default 500; max 500.</param> /// <param name="startTime">Timestamp in ms to get aggregate trades from INCLUSIVE.</param> /// <param name="endTime">Timestamp in ms to get aggregate trades until INCLUSIVE.</param> /// <param name="token"></param> /// <returns></returns> public static Task <string> GetAggregateTradesAsync(this IBinanceHttpClient client, string symbol, long fromId = BinanceApi.NullId, int limit = default, long startTime = default, long endTime = default, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNullOrWhiteSpace(symbol, nameof(symbol)); var request = new BinanceHttpRequest("/api/v1/aggTrades", 2); request.AddParameter("symbol", symbol.FormatSymbol()); if (fromId >= 0) { request.AddParameter("fromId", fromId); } if (startTime > 0) { request.AddParameter("startTime", startTime); } if (endTime > 0) { request.AddParameter("endTime", endTime); } if (startTime <= 0 || endTime <= 0) { if (limit > 0) { request.AddParameter("limit", limit); } } else { var start = DateTimeOffset.FromUnixTimeMilliseconds(startTime); var end = DateTimeOffset.FromUnixTimeMilliseconds(endTime); if ((end - start).Duration() >= TimeSpan.FromHours(24)) { throw new ArgumentException($"The interval between {nameof(startTime)} and {nameof(endTime)} must be less than 24 hours.", nameof(endTime)); } } return(client.GetAsync(request, token)); }
/// <summary> /// Check an order's status. Either orderId or origClientOrderId must be sent. /// </summary> /// <param name="client"></param> /// <param name="user"></param> /// <param name="symbol"></param> /// <param name="orderId"></param> /// <param name="origClientOrderId"></param> /// <param name="recvWindow"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> GetOrderAsync(this IBinanceHttpClient client, IBinanceApiUser user, string symbol, long orderId = BinanceApi.NullId, string origClientOrderId = null, long recvWindow = default, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNull(user, nameof(user)); Throw.IfNullOrWhiteSpace(symbol, nameof(symbol)); if (orderId < 0 && string.IsNullOrWhiteSpace(origClientOrderId)) { throw new ArgumentException($"Either '{nameof(orderId)}' or '{nameof(origClientOrderId)}' must be provided, but both were invalid."); } if (recvWindow <= 0) { recvWindow = client.Options.RecvWindowDefault ?? 0; } var request = new BinanceHttpRequest("/api/v3/order", 2) { ApiKey = user.ApiKey }; request.AddParameter("symbol", symbol.FormatSymbol()); if (orderId >= 0) { request.AddParameter("orderId", orderId); } if (!string.IsNullOrWhiteSpace(origClientOrderId)) { request.AddParameter("origClientOrderId", origClientOrderId); } if (recvWindow > 0) { request.AddParameter("recvWindow", recvWindow); } await client.SignAsync(request, user, token) .ConfigureAwait(false); return(await client.GetAsync(request, token, user.RateLimiter) .ConfigureAwait(false)); }
/// <summary> /// Get the account status. /// </summary> /// <param name="client"></param> /// <param name="user"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> GetAccountStatusAsync(this IBinanceHttpClient client, IBinanceApiUser user, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); if (client.RateLimiter != null) { await client.RateLimiter.DelayAsync(token : token) .ConfigureAwait(false); } var request = new BinanceHttpRequest("/wapi/v3/accountStatus.html") { ApiKey = user.ApiKey }; await client.SignAsync(request, user, token) .ConfigureAwait(false); return(await client.GetAsync(request, token) .ConfigureAwait(false)); }
/// <summary> /// Get the account status. /// </summary> /// <param name="client"></param> /// <param name="user"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> GetAccountStatusAsync(this IBinanceHttpClient client, IBinanceApiUser user, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); var request = new BinanceHttpRequest($"/wapi/v3/accountStatus.html") { ApiKey = user.ApiKey }; var timestamp = await client.GetTimestampAsync(token) .ConfigureAwait(false); request.AddParameter("timestamp", timestamp); var signature = user.Sign(request.QueryString); request.AddParameter("signature", signature); return(await client.GetAsync(request, token) .ConfigureAwait(false)); }
/// <summary> /// Get all open orders on a symbol or all symbols. /// </summary> /// <param name="client"></param> /// <param name="user"></param> /// <param name="symbol"></param> /// <param name="recvWindow"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> GetOpenOrdersAsync(this IBinanceHttpClient client, IBinanceApiUser user, string symbol = null, long recvWindow = default, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNull(user, nameof(user)); if (recvWindow == default) { recvWindow = client.DefaultRecvWindow; } if (client.RateLimiter != null) { var tradingSymbols = Symbol.Cache.Values.Count(s => s.Status == SymbolStatus.Trading); await client.RateLimiter .DelayAsync(string.IsNullOrWhiteSpace(symbol) && tradingSymbols > 1?tradingSymbols / 2 : 1, token) .ConfigureAwait(false); } var request = new BinanceHttpRequest("/api/v3/openOrders") { ApiKey = user.ApiKey }; if (!string.IsNullOrWhiteSpace(symbol)) { request.AddParameter("symbol", symbol.FormatSymbol()); } if (recvWindow > 0) { request.AddParameter("recvWindow", recvWindow); } await client.SignAsync(request, user, token) .ConfigureAwait(false); return(await client.GetAsync(request, token) .ConfigureAwait(false)); }
/// <summary> /// Get all account orders; active, canceled, or filled. /// If orderId is set, this will return orders >= orderId; otherwise return most recent orders. /// </summary> /// <param name="client"></param> /// <param name="user"></param> /// <param name="symbol"></param> /// <param name="orderId"></param> /// <param name="limit">Default 500; max 500.</param> /// <param name="recvWindow"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> GetOrdersAsync(this IBinanceHttpClient client, IBinanceApiUser user, string symbol, long orderId = BinanceApi.NullId, int limit = default, long recvWindow = default, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNull(user, nameof(user)); Throw.IfNullOrWhiteSpace(symbol, nameof(symbol)); if (recvWindow <= 0) { recvWindow = client.Options.RecvWindowDefault ?? 0; } var request = new BinanceHttpRequest("/api/v3/allOrders", 20) { ApiKey = user.ApiKey }; request.AddParameter("symbol", symbol.FormatSymbol()); if (orderId >= 0) { request.AddParameter("orderId", orderId); } if (limit > 0) { request.AddParameter("limit", limit); } if (recvWindow > 0) { request.AddParameter("recvWindow", recvWindow); } await client.SignAsync(request, user, token) .ConfigureAwait(false); return(await client.GetAsync(request, token) .ConfigureAwait(false)); }
/// <summary> /// Get all open orders on a symbol. /// </summary> /// <param name="client"></param> /// <param name="user"></param> /// <param name="symbol"></param> /// <param name="recvWindow"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> GetOpenOrdersAsync(this IBinanceHttpClient client, IBinanceApiUser user, string symbol = null, long recvWindow = default, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNull(user, nameof(user)); if (recvWindow <= 0) { recvWindow = client.Options.RecvWindowDefault ?? 0; } var request = new BinanceHttpRequest($"/api/v3/openOrders") { ApiKey = user.ApiKey }; if (!string.IsNullOrWhiteSpace(symbol)) { request.AddParameter("symbol", symbol.FormatSymbol()); } if (recvWindow > 0) { request.AddParameter("recvWindow", recvWindow); } var timestamp = await client.GetTimestampAsync(token) .ConfigureAwait(false); request.AddParameter("timestamp", timestamp); var signature = user.Sign(request.QueryString); request.AddParameter("signature", signature); return(await client.GetAsync(request, token) .ConfigureAwait(false)); }
/// <summary> /// Get order book (market depth) of a symbol. /// </summary> /// <param name="client"></param> /// <param name="symbol"></param> /// <param name="limit">Valid values: [5, 10, 20, 50, 100, 500, 1000] (default: 100).</param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> GetOrderBookAsync(this IBinanceHttpClient client, string symbol, int limit = default, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNullOrWhiteSpace(symbol, nameof(symbol)); if (client.RateLimiter != null) { await client.RateLimiter .DelayAsync(limit >= 1000? 10 : limit >= 500? 5 : 1, token) .ConfigureAwait(false); } var request = new BinanceHttpRequest("/api/v1/depth"); request.AddParameter("symbol", symbol.FormatSymbol()); if (limit > 0) { request.AddParameter("limit", limit); } return(await client.GetAsync(request, token) .ConfigureAwait(false)); }
/// <summary> /// Get older (non-compressed) trades. /// </summary> /// <param name="client"></param> /// <param name="apiKey"></param> /// <param name="symbol"></param> /// <param name="fromId">TradeId to fetch from. Default gets most recent trades.</param> /// <param name="limit">Default 500; max 500.</param> /// <param name="token"></param> /// <returns></returns> public static Task <string> GetTradesAsync(this IBinanceHttpClient client, string apiKey, string symbol, long fromId = BinanceApi.NullId, int limit = default, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNullOrWhiteSpace(symbol, nameof(symbol)); var request = new BinanceHttpRequest("/api/v1/historicalTrades", 20) { ApiKey = apiKey }; request.AddParameter("symbol", symbol.FormatSymbol()); if (fromId >= 0) { request.AddParameter("fromId", fromId); } if (limit > 0) { request.AddParameter("limit", limit); } return(client.GetAsync(request, token)); }
/// <summary> /// Get 24 hour price change statistics for a symbol or all symbols. /// </summary> /// <param name="client"></param> /// <param name="symbol"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> Get24HourStatisticsAsync(this IBinanceHttpClient client, string symbol = null, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); if (client.RateLimiter != null) { var tradingSymbols = Symbol.Cache.Values.Count(s => s.Status == SymbolStatus.Trading); await client.RateLimiter .DelayAsync(string.IsNullOrWhiteSpace(symbol) && tradingSymbols > 1?tradingSymbols / 2 : 1, token) .ConfigureAwait(false); } // When symbol is not provided use number of symbols that are TRADING for weight. var request = new BinanceHttpRequest("/api/v1/ticker/24hr"); if (!string.IsNullOrWhiteSpace(symbol)) { request.AddParameter("symbol", symbol.FormatSymbol()); } return(await client.GetAsync(request, token) .ConfigureAwait(false)); }
/// <summary> /// Test connectivity to the server and get the current time. /// </summary> /// <param name="client"></param> /// <param name="token"></param> /// <returns></returns> public static Task <string> GetServerTimeAsync(this IBinanceHttpClient client, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); return(client.GetAsync("/api/v1/time", token)); }
/// <summary> /// Get exchange information. /// </summary> /// <param name="client"></param> /// <param name="token"></param> /// <returns></returns> public static Task <string> GetExchangeInfoAsync(this IBinanceHttpClient client, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); return(client.GetAsync("/api/v1/exchangeInfo", token)); }
/// <summary> /// Get best price/quantity on the order book for all symbols. /// </summary> /// <param name="client"></param> /// <param name="token"></param> /// <returns></returns> public static Task <string> GetOrderBookTopsAsync(this IBinanceHttpClient client, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); return(client.GetAsync("/api/v1/ticker/bookTicker", token)); }
/// <summary> /// Get compressed, aggregate trades. Trades that fill at the time, from the same order, with the same price will have the quantity aggregated. /// If fromdId, startTime, and endTime are not sent, the most recent aggregate trades will be returned. /// </summary> /// <param name="client"></param> /// <param name="symbol"></param> /// <param name="fromId">ID to get aggregate trades from INCLUSIVE.</param> /// <param name="limit">Default 500; max 500.</param> /// <param name="startTime">Timestamp in ms to get aggregate trades from INCLUSIVE.</param> /// <param name="endTime">Timestamp in ms to get aggregate trades until INCLUSIVE.</param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> GetAggregateTradesAsync(this IBinanceHttpClient client, string symbol, long fromId = BinanceApi.NullId, int limit = default, DateTime startTime = default, DateTime endTime = default, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNullOrWhiteSpace(symbol, nameof(symbol)); if (client.RateLimiter != null) { await client.RateLimiter.DelayAsync(token : token) .ConfigureAwait(false); } var request = new BinanceHttpRequest("/api/v1/aggTrades"); request.AddParameter("symbol", symbol.FormatSymbol()); if (fromId >= 0) { request.AddParameter("fromId", fromId); } if (startTime != default) { if (startTime.Kind != DateTimeKind.Utc) { throw new ArgumentException("Date/Time must be UTC.", nameof(startTime)); } request.AddParameter("startTime", startTime.ToTimestamp()); } if (endTime != default) { if (endTime.Kind != DateTimeKind.Utc) { throw new ArgumentException("Date/Time must be UTC.", nameof(endTime)); } request.AddParameter("endTime", endTime.ToTimestamp()); } if (startTime == default || endTime == default) { if (limit > 0) { request.AddParameter("limit", limit); } } else { if (endTime < startTime) { throw new ArgumentException($"Time ({nameof(endTime)}) must not be less than {nameof(startTime)} ({startTime}).", nameof(endTime)); } if ((endTime - startTime).Duration() >= TimeSpan.FromHours(24)) { throw new ArgumentException($"The interval between {nameof(startTime)} and {nameof(endTime)} must be less than 24 hours.", nameof(endTime)); } } return(await client.GetAsync(request, token) .ConfigureAwait(false)); }
/// <summary> /// Get the deposit history. /// </summary> /// <param name="client"></param> /// <param name="user"></param> /// <param name="asset"></param> /// <param name="status"></param> /// <param name="startTime"></param> /// <param name="endTime"></param> /// <param name="recvWindow"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> GetDepositsAsync(this IBinanceHttpClient client, IBinanceApiUser user, string asset = null, DepositStatus?status = null, DateTime startTime = default, DateTime endTime = default, long recvWindow = default, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNull(user, nameof(user)); if (recvWindow == default) { recvWindow = client.DefaultRecvWindow; } if (client.RateLimiter != null) { await client.RateLimiter.DelayAsync(token : token) .ConfigureAwait(false); } var request = new BinanceHttpRequest("/wapi/v3/depositHistory.html") { ApiKey = user.ApiKey }; if (!string.IsNullOrWhiteSpace(asset)) { request.AddParameter("asset", asset.FormatSymbol()); } if (status.HasValue) { request.AddParameter("status", (int)status); } if (startTime != default) { if (startTime.Kind != DateTimeKind.Utc) { throw new ArgumentException("Date/Time must be UTC.", nameof(startTime)); } request.AddParameter("startTime", startTime.ToTimestamp()); } if (endTime != default) { if (endTime.Kind != DateTimeKind.Utc) { throw new ArgumentException("Date/Time must be UTC.", nameof(endTime)); } request.AddParameter("endTime", endTime.ToTimestamp()); } if (recvWindow > 0) { request.AddParameter("recvWindow", recvWindow); } await client.SignAsync(request, user, token) .ConfigureAwait(false); return(await client.GetAsync(request, token) .ConfigureAwait(false)); }