-
Notifications
You must be signed in to change notification settings - Fork 77
Account
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).
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.
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
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.
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
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
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
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
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
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. 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
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
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.
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
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
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
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.
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
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
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
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
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
These methods are utilized by the IUserDataWebSocketManager
service.
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
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 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
- Verify connection to the Binance server (minimal examples).
- Get the market depth (order book) for a symbol.
- Maintain a real-time order book cache for a symbol.
- Get the aggregate trades for a symbol.
- Maintain a real-time trade history cache for a symbol.
- Get the candlesticks for a symbol.
- Maintain a real-time price chart cache for a symbol.
- Get the 24-hour statistics for a symbol.
- Get current prices for all symbols for a price ticker.
- Get best price and quantity on the order book for all symbols.
- Get a list of all current symbols.
- Place a LIMIT order.
- Place a MARKET order.
- Place a TEST order to verify client order properties.
- Look-up an existing order to check status.
- Cancel an open order.
- Get all open orders for a symbol.
- Get all orders for a symbol.
- Get current account information.
- Get account trades for a symbol.
- Submit a withdraw request.
- Get deposit history.
- Get withdraw history.
- Donate BTC to the creator of this library.