Private API
A python class for sending API requests that require authorization.
The private API is a class to send API requests that requires authorization, e.g. get my balance in Binance spot wallet, create a buy order of BTCUSDT, cancel an order, and etc.
It is initialized prior to your code as a variable named privateAPI
. When initializing, it automatically gathers the API keys that you have registered.
The private API class can only be used in terminal. In other words, it does not work in analytics.
If you didn't register the API key of some exchanges, then the functions which send API requests to them will throw exceptions.
Common Functions
The common functions are the methods of the private API, available for (almost) any exchanges we support. By using these functions, you can easily switch the exchanges you want to send API request to.
Every common function has its first argument as an exchange. It specifies which exchange should the request be sent to. For example, you can get the balance in your Binance spot wallet by privateAPI.get_balance('binance')
, while privateAPI.get_balance('binance_um_futures')
returns the balance in your Binance USDⓈ-M futures wallet in a same response structure.
For now, the available exchanges are,
binance
: Binance Spotbinance_um_futures
: Binance USDⓈ-M Futuresbinance_cm_futures
: Binance COIN-M Futures
1. Get Balance
It is a function to get the asset information in the exchange's wallet.
Arguments
exchange
: (string) The name of the exchange.
Returns
It returns the dictionary object with its key as a name of the asset in string, and value as the information on the asset. The information has three key:
amount
: (float) Current actual amount of the asset.unrealized_pnl
is not included.available
: (float) The amount of the asset available for trading.unrealized_pnl
: (float) The unrealized PnL(profit & loss) of the asset by the derivatives such as futures position. If the exchange is spot-only, then it is always 0.
Usually, amount = available + (locked amount because of open orders, maintenance margin, etc.)
holds.
2. Get Positions
It is a function to get the position information in the exchange's account.
This function merges all open positions by the symbol. For example, if you have both 0.1 BTCUSDT long and -0.2 BTCUSDT short position, then this method will return BTCUSDT short position with the amount = -0.1.
Only the exchanges which have futures are available. c.f. binance
(= Binance spot) cannot be the value of the exchange argument for this function.
Arguments
exchange
: (string) The name of the exchange.
Returns
It returns the dictionary object with its key as a name of the symbol in string, and value as the information on the position. The information has three key:
side
: (string)'long'
for long, or'short'
for short.amount
: (float) The amount of the position currently holding. Positive for long, negative for short.unrealized_pnl
: (float) The unrealized PnL of the position.
3. Buy
Create a buy/long order.
Arguments
exchange
: (string) The name of the exchange.symbol
: (string) The name of the symbol.amount
: (float) The amount of the order. It must be positive.price
: (float) The price of the order to be trade. Default value isNone
. It should beNone
when theorder_type == 'market'
.order_type
: (string) The type of the order.'limit'
for limit order,'market'
for market order. Default value is'limit'
.reduce_only
: (boolean) Futures only arguments. Enable reduce only option. If it's true, then the order works only to close the current position. Default value isFalse
.
Returns
None
4. Sell
Create a sell/short order.
Arguments
exchange
: (string) The name of the exchange.symbol
: (string) The name of the symbol.amount
: (float) The amount of the order. It must be positive.price
: (float) The price of the order to be trade. Default value isNone
. It should beNone
when theorder_type == 'market'
.order_type
: (string) The type of the order.'limit'
for limit order,'market'
for market order. Default value is'limit'
.reduce_only
: (boolean) Futures only arguments. Enable reduce only option. If it's true, then the order works only to close the current position. Default value isFalse
.
Returns
None
5. Get Open Orders
Get the list of the open orders' symbol and order ID.
Some exchanges may require additional arguments.
Arguments
exchange
: (string) The name of the exchange.**kwargs
: Additional arguments.symbol
: (string) The symbol of the orders.binance
,binance_um_futures
, andbinance_cm_futures
require symbol argument.
Returns
It returns a list of the order element. The order element is a dictionary of symbol
and order_id
.
symbol
: (string) The symbol of the order.order_id
: (string) The order ID of the order.
6. Cancel an Order
Cancel an open order by its order ID.
Some exchanges may require additional arguments.
Arguments
exchange
: (string) The name of the exchange.order_id
: An ID of the order to cancel.**kwargs
: Additional arguments.symbol
: (string) The symbol of the orders.binance
,binance_um_futures
, andbinance_cm_futures
require symbol argument.
Returns
None
7. Cancel All Orders
Cancel all the open orders.
Some exchanges may require additional arguments.
Arguments
exchange
: (string) The name of the exchange.**kwargs
: Additional arguments.symbol
: (string) The symbol of the orders.binance
,binance_um_futures
, andbinance_cm_futures
require symbol argument.
Returns
None
8. Set Leverage
Set leverage value.
Some exchanges may require additional arguments.
Only the exchanges which have futures are available. c.f. binance
(= Binance spot) cannot be the value of the exchange argument for this function.
Arguments
exchange
: (string) The name of the exchange.leverage
: The leverage value to set. It is usually a positive integer value.**kwargs
: Additional arguments.symbol
: (string) The symbol of the orders.binance_um_futures
, andbinance_cm_futures
require symbol argument.
Returns
None
9. Get API request result
This is a special function that can call any API requests that read/get the information.
If you are about the get public API requests such as getting orderbook, getting exchange info, and etc, then user publicAPI._get()
Arguments
exchange
: (string) The name of the exchange.api_url
: (string) Endpoint of the request, such as'/api/v1/exchangeIfo'
.**kwargs
: Additional arguments. They will be used as the parameters of the request.
Returns
None
Exchange-Only Functions
While common functions make it easy to switch exchanges, there are API requests which are specialized for the specific exchanges. Or, you may want to handle detailed options or values that the common functions cannot cover. In that case, the exchange-only functions can help you with that.
Most of the exchange-only functions have **kwargs
to support all the parameters that the API can have.
The returns are the object obtained by parsing the API response(Usually in JSON format). Therefore, the type of them are dict, list or None.
Binance
Last updated