Skip to content

Account

sonvister edited this page Mar 16, 2018 · 17 revisions

Account API (private)

To use the following features of the API you must have a Binance account and create an API-Key. If you haven't already created an account, please use my Referral ID: 10899093 when you Sign Up (it's an easy way to give back at no cost to you).

Client Orders

A ClientOrder serves as a mutable order placeholder (that can be validated and tested). Only after the ClientOrder is placed successfully does an immutable Order exist.

Validating

To validate a ClientOrder there are a couple of ClientOrder extension methods available: IsValid() and Validate(). The former returns a bool result and the later throws an exception with a message. Both use the cached Symbol information to determine, among other things, if the ClientOrder.Quantity will pass the 'LOT_SIZE' filter for a symbol.

NOTE: The static asset/symbol information is only as recent as the latest build, but it can be updated.

Reference: Official Binance API

Formatting Data

To modify the quantity or price in order to pass validation (and Binance filters) there are 3 extension methods on InclusiveRange (e.g. symbol.Quantity, symbol.Price): GetUpperValidValue(), GetValidValue(), and GetLowerValidValue(). The comments provide additional details on how each method works.

Test Placement

Create and TEST place a new order. An exception will be thrown if the order placement test fails.

try
{
    await api.TestPlaceAsync(new MarketOrder(user)
    {
        Symbol = Symbol.BTC_USDT,
        Side = OrderSide.Sell,
        Quantity = 0.1m
    });
}
catch (BinanceApiException) { }

NOTE: For simplicity, authentication is not shown in this example.

Sample console application example TEST Limit order, example TEST Market order.

Reference: Binance Official API

Limit Order

Create and place a new Limit order.

var order = await api.PlaceAsync(new LimitOrder(user)
{
    Symbol = Symbol.BTC_USDT,
    Side = OrderSide.Buy,
    Quantity = 0.1m,
    Price = 10000
});

A Limit-Maker order is a Limit order that will be rejected if it would immediately match and trade as a taker. It is the same as creating a 'post-only' limit order.

var order = await api.PlaceAsync(new LimitMakerOrder(user)
{
    Symbol = Symbol.BTC_USDT,
    Side = OrderSide.Buy,
    Quantity = 0.1m,
    Price = 10000
});

NOTE: For simplicity, exception handling and authentication are not shown in these examples.

Sample console application example.

Reference: Binance Official API

Stop Order (Limit)

Create and place a new Stop-Loss-Limit or Take-Profit-Limit order. Stop-Loss-Limit and Take-Profit-Limit will execute a Limit order when the StopPrice is reached.

var order = await api.PlaceAsync(new StopLossLimit(user)
{
    Symbol = Symbol.BTC_USDT,
    Side = OrderSide.Sell,
    Quantity = 0.1m,
    Price = 9000,
    StopPrice = 10000
});
var order = await api.PlaceAsync(new TakeProfitLimit(user)
{
    Symbol = Symbol.BTC_USDT,
    Side = OrderSide.Sell,
    Quantity = 0.1m,
    Price = 11000,
    StopPrice = 10000
});

NOTE: For simplicity, exception handling and authentication are not shown in these examples.

Sample console application example.

Reference: Binance Official API

Market Order

Create and place a new Market order. You do not set a price for Market orders, you take what is on the order book.

var order = await api.PlaceAsync(new MarketOrder(user)
{
    Symbol = Symbol.BTC_USDT,
    Side = OrderSide.Sell,
    Quantity = 0.1m
});

NOTE: For simplicity, exception handling and authentication are not shown in these examples.

Sample console application example.

Reference: Binance Official API

Stop Order (Market)

Create and place a new Stop-Loss or Take-Profit order. Stop-Loss and Take-Profit will execute a Market order when the StopPrice is reached.

var order = await api.PlaceAsync(new StopLoss(user)
{
    Symbol = Symbol.BTC_USDT,
    Side = OrderSide.Sell,
    Quantity = 0.1m,
    StopPrice = 10000
});
var order = await api.PlaceAsync(new TakeProfit(user)
{
    Symbol = Symbol.BTC_USDT,
    Side = OrderSide.Sell,
    Quantity = 0.1m,
    StopPrice = 10000
});

NOTE: For simplicity, exception handling and authentication are not shown in these examples.

Sample console application example.

Reference: Binance Official API

Query an Order

Get an order to determine current status. Order lookup requires an Order instance or the combination of a symbol and the order ID or the client order ID. If an Order instance is provided, it will be updated in place in addition to being returned.

var order = await api.GetAsync(order);
    // or...
var order = await api.GetOrderAsync(user, Symbol.BTC_USDT, orderId);
    // or...
var order = await api.GetOrderAsync(user, Symbol.BTC_USDT, clientOrderId);

NOTE: For simplicity, authentication is not shown in this example.

Sample console application example.

Reference: Binance Official API

Cancel an Order

Cancel an order. Order lookup requires an Order instance or the combination of a symbol and the order ID or the client order ID.

await api.CancelAsync(order);
    // or...
await api.CancelOrderAsync(user, Symbol.BTC_USDT, orderId);
    // or...
await api.CancelOrderAsync(user, Symbol.BTC_USDT, clientOrderId);

NOTE: For simplicity, authentication is not shown in this example.

Sample console application example.

Reference: Binance Official API

Open Orders

Get all open orders for a symbol with optional limit [1-500].

var orders = await api.GetOpenOrdersAsync(user, Symbol.BTC_USDT);

NOTE: For simplicity, authentication is not shown in this example.

Sample console application example.

Reference: Binance Official API

Cancel All Open Orders

Extension that combines get all open orders for a symbol or all symbols with cancel.

var cancelOrderIds = await api.CancelAllOrdersAsync(user, Symbol.BTC_USDT);
    // or...
var cancelOrderIds = await api.CancelAllOrdersAsync(user);

NOTE: For simplicity, authentication is not shown in this example.

Sample console application example.

Orders

Get all orders active, canceled, or filled with optional limit [1-500].

var orders = await api.GetOrdersAsync(user, Symbol.BTC_USDT);

NOTE: For simplicity, authentication is not shown in this example.

Sample console application example.

Reference: Binance Official API

Account Information

Get current account information.

var account = await api.GetAccountInfoAsync(user);

NOTE: For simplicity, authentication is not shown in this example.

Sample console application example.

Reference: Binance Official API

Account Trades

Get account trades for a specific account and symbol with optional limit [1-500].

var account = await api.GetAccountTradesAsync(user, Symbol.BTC_USDT);

NOTE: For simplicity, authentication is not shown in this example.

Sample console application example.

Reference: Binance Official API

Order Account Trades

Extension that uses get account trades to lookup account trades associated with an Order.

NOTE: This method must query all account trades for a symbol.

var account = await api.GetAccountTradesAsync(user, order);

NOTE: For simplicity, authentication is not shown in this example.

Sample console application example.

Withdraw

Submit a withdraw request ...optionally donate to me :)

await api.WithdrawAsync(new WithdrawRequest(user)
{
    Asset = Asset.BTC,
    Address = "3JjG3tRR1dx98UJyNdpzpkrxRjXmPfQHk9",
    Amount = 0.01m,
    Name = "Donate"
});

NOTE: For simplicity, authentication is not shown in this example.

Sample console application example.

Reference: Binance Official API

Deposit History

Get deposit history.

var deposits = await api.GetDepositsAsync(user);

NOTE: For simplicity, authentication is not shown in this example.

Sample console application example.

Reference: Binance Official API

Withdrawal History

Get withdrawal history.

var withdrawals = await api.GetWithdrawalsAsync(user);

NOTE: For simplicity, authentication is not shown in this example.

Sample console application example.

Reference: Binance Official API

Deposit Address

Get deposit address for an asset.

var address = await api.GetDepositAddressAsync(user, Asset.BTC);

NOTE: For simplicity, authentication is not shown in this example.

Sample console application example.

Reference: Binance Official API

Account Status

Get account status.

var status = await api.GetAccountStatusAsync(user);

NOTE: For simplicity, authentication is not shown in this example.

Sample console application example.

Reference: Binance Official API

User Stream Control

These methods are utilized by the IUserDataWebSocketManager service.

Start User Stream

Start a new user data stream.

var listenKey = await api.UserStreamStartAsync(user);
    // or...
var listenKey = await api.UserStreamStartAsync("API-Key");

NOTE: For simplicity, authentication is not shown in this example.

Reference: Binance Official API
Reference: Binance Official API

Keepalive User Stream

Ping a user data stream to prevent a timeout.

await api.UserStreamKeepAliveAsync(user, listenKey);
    // or...
await api.UserStreamKeepAliveAsync("API-Key", listenKey);

NOTE: For simplicity, authentication is not shown in this example.

Reference: Binance Official API
Reference: Binance Official API

Close User Stream

Close a user data stream.

await api.UserStreamCloseAsync(user, listenKey);
    // or...
await api.UserStreamCloseAsync("API-Key", listenKey);

NOTE: For simplicity, authentication is not shown in this example.

Reference: Binance Official API
Reference: Binance Official API

Clone this wiki locally