/// <summary> /// Get current account information. /// </summary> /// <param name="client"></param> /// <param name="user"></param> /// <param name="recvWindow"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> GetAccountInfoAsync(this IBinanceHttpClient client, IBinanceApiUser user, 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/account") { ApiKey = user.ApiKey }; var timestamp = await client.GetTimestampAsync(token) .ConfigureAwait(false); request.AddParameter("timestamp", timestamp); if (recvWindow > 0) { request.AddParameter("recvWindow", recvWindow); } var signature = user.Sign(request.QueryString); request.AddParameter("signature", signature); return(await client.GetAsync(request, token) .ConfigureAwait(false)); }
/// <summary> /// Cancel an active order. /// </summary> /// <param name="client"></param> /// <param name="user"></param> /// <param name="symbol"></param> /// <param name="orderId"></param> /// <param name="origClientOrderId"></param> /// <param name="newClientOrderId">Used to uniquely identify this cancel. Automatically generated by default.</param> /// <param name="recvWindow"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> CancelOrderAsync(this IBinanceHttpClient client, IBinanceApiUser user, string symbol, long orderId = BinanceApi.NullId, string origClientOrderId = null, string newClientOrderId = 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") { ApiKey = user.ApiKey }; request.AddParameter("symbol", symbol.FormatSymbol()); if (orderId >= 0) { request.AddParameter("orderId", orderId); } if (!string.IsNullOrWhiteSpace(origClientOrderId)) { request.AddParameter("origClientOrderId", origClientOrderId); } if (!string.IsNullOrWhiteSpace(newClientOrderId)) { request.AddParameter("newClientOrderId", newClientOrderId); } 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.DeleteAsync(request, token, user.RateLimiter) .ConfigureAwait(false)); }
/// <summary> /// Submit a withdraw request. /// </summary> /// <param name="client"></param> /// <param name="user"></param> /// <param name="asset"></param> /// <param name="address"></param> /// <param name="addressTag"></param> /// <param name="amount"></param> /// <param name="name">A description of the address (optional).</param> /// <param name="recvWindow"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> WithdrawAsync(this IBinanceHttpClient client, IBinanceApiUser user, string asset, string address, string addressTag, decimal amount, string name = null, long recvWindow = default, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNull(user, nameof(user)); Throw.IfNullOrWhiteSpace(asset, nameof(asset)); Throw.IfNullOrWhiteSpace(address, nameof(address)); if (amount <= 0) { throw new ArgumentException("Withdraw amount must be greater than 0.", nameof(amount)); } if (recvWindow <= 0) { recvWindow = client.Options.RecvWindowDefault ?? 0; } var request = new BinanceHttpRequest($"/wapi/v3/withdraw.html") { ApiKey = user.ApiKey }; request.AddParameter("asset", asset.FormatSymbol()); request.AddParameter("address", address); request.AddParameter("amount", amount); if (!string.IsNullOrWhiteSpace(addressTag)) { request.AddParameter("addressTag", addressTag); } if (!string.IsNullOrWhiteSpace(name)) { request.AddParameter("name", name); } 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.PostAsync(request, token) .ConfigureAwait(false)); }
/// <summary> /// Get the withdrawal 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> GetWithdrawalsAsync(this IBinanceHttpClient client, IBinanceApiUser user, string asset = null, WithdrawalStatus?status = null, long startTime = default, long endTime = default, 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($"/wapi/v3/withdrawHistory.html") { ApiKey = user.ApiKey }; var timestamp = await client.GetTimestampAsync(token) .ConfigureAwait(false); request.AddParameter("timestamp", timestamp); if (!string.IsNullOrWhiteSpace(asset)) { request.AddParameter("asset", asset.FormatSymbol()); } if (status.HasValue) { request.AddParameter("status", (int)status); } if (startTime > 0) { request.AddParameter("startTime", startTime); } if (endTime > 0) { request.AddParameter("endTime", endTime); } if (recvWindow > 0) { request.AddParameter("recvWindow", recvWindow); } var signature = user.Sign(request.QueryString); request.AddParameter("signature", signature); 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 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. /// </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 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()); 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> /// Send in a new order. /// </summary> /// <param name="client"></param> /// <param name="user"></param> /// <param name="symbol"></param> /// <param name="side"></param> /// <param name="type"></param> /// <param name="quantity"></param> /// <param name="price"></param> /// <param name="newClientOrderId">A unique id for the order. Automatically generated if not sent.</param> /// <param name="timeInForce"></param> /// <param name="stopPrice">Used with STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, and TAKE_PROFIT_LIMIT orders.</param> /// <param name="icebergQty">Used with iceberg orders.</param> /// <param name="recvWindow"></param> /// <param name="isTestOnly">If true, test new order creation and signature/recvWindow; creates and validates a new order but does not send it into the matching engine.</param> /// <param name="newOrderResponseType">Set the response JSON.</param> /// <param name="token"></param> /// <returns></returns> public static async Task <string> PlaceOrderAsync(this IBinanceHttpClient client, IBinanceApiUser user, string symbol, OrderSide side, OrderType type, decimal quantity, decimal price, string newClientOrderId = null, TimeInForce?timeInForce = null, decimal stopPrice = 0, decimal icebergQty = 0, long recvWindow = default, bool isTestOnly = false, PlaceOrderResponseType newOrderRespType = PlaceOrderResponseType.Result, CancellationToken token = default) { Throw.IfNull(client, nameof(client)); Throw.IfNull(user, nameof(user)); Throw.IfNullOrWhiteSpace(symbol, nameof(symbol)); if (quantity <= 0) { throw new ArgumentException("Order quantity must be greater than 0.", nameof(quantity)); } if (recvWindow <= 0) { recvWindow = client.Options.RecvWindowDefault ?? 0; } var request = new BinanceHttpRequest($"/api/v3/order{(isTestOnly ? "/test" : string.Empty)}") { ApiKey = user.ApiKey }; request.AddParameter("symbol", symbol.FormatSymbol()); request.AddParameter("side", side.ToString().ToUpper()); request.AddParameter("type", type.AsString()); request.AddParameter("newOrderRespType", newOrderRespType.ToString().ToUpper()); request.AddParameter("quantity", quantity); if (price > 0) { request.AddParameter("price", price); } if (timeInForce.HasValue) { request.AddParameter("timeInForce", timeInForce.ToString().ToUpper()); } if (!string.IsNullOrWhiteSpace(newClientOrderId)) { request.AddParameter("newClientOrderId", newClientOrderId); } if (stopPrice > 0) { request.AddParameter("stopPrice", stopPrice); } if (icebergQty > 0) { // Automatically set time-in-force to GTC if not set. if (!timeInForce.HasValue) { timeInForce = TimeInForce.GTC; } if (timeInForce != TimeInForce.GTC) { throw new BinanceApiException("Any order with an icebergQty MUST have timeInForce set to GTC."); } request.AddParameter("icebergQty", icebergQty); } 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.PostAsync(request, token, user.RateLimiter) .ConfigureAwait(false)); }