Home Bitcoin python – How is crossWalletBalance calculated with Binance perpetual futures in cross-margin mode?

python – How is crossWalletBalance calculated with Binance perpetual futures in cross-margin mode?

0
python – How is crossWalletBalance calculated with Binance perpetual futures in cross-margin mode?


I am attempting to breed and work out how crossWalletBalance worth, retrieved from Binance API, is calculated by Binance in USDⓈ-M perpetual futures. Significantly, I would want it for simulating the calculation of liquidation costs in cross-margin mode based on the method famous right here. Nonetheless, I am constantly failing to breed the worth from Binance API.

So far as I perceive, in cross-margin mode crossWalletBalance corresponds to the full steadiness, comprised of pockets steadiness and margin steadiness, which incorporates unrealized PnLs, as described right here.

So as to confirm my calculations, I take advantage of Binance Testnet, the place I place market orders utilizing Python’s CCXT bindings. Let’s suppose I open a number of quick positions. By fetching all positions with non-zero variety of contracts, I retrieve deserialized JSON with related data on all open positions grouped by symbols:

positions = trade.fetch_account_positions()
[pos for pos in positions if float(pos['contracts']) != 0]

Present free USDT steadiness is fetched utilizing one other HTTP-request wrapped into CCXT’s trade.fetch_free_balance().

Fields of strange curiosity among the many JSON are:

  • initMargin – place preliminary margin; I take advantage of it for calculating margin steadiness;
  • unrealizedProfit – UPnL; I additionally use it for calculating margin steadiness;
  • crossWalletBalance – floor fact worth for reference and checking whether or not my calculations are right;
  • crossMargin – for reference, to double-check that crossWalletBalance minus all unrealizedProfits equals to it.

I assume that the price taken is 0.0004, and have double-checked this reality utilizing the historical past of my trades on Binance Testnet web page.

I’ve tried a number of formulae modifications primarily based on beforehand talked about manuals. Significantly, I take advantage of pockets steadiness method from right here:

Pockets Stability = Complete Internet Switch + Complete Realized Revenue + Complete Internet Funding Charge - Complete Fee

…assuming that:

  • Complete Internet Switch equals to free USDT steadiness;
  • Complete Realized Revenue is zero, as quickly as positions are nonetheless open;
  • Complete Internet Funding Charge is zero, since positions are assumed to be closed nearly instantly, due to this fact, funding charges don’t relate;
  • Complete Fee is 0.0004. It’s taken as soon as, since positions are nonetheless open;

Significantly, amongst others, I’ve tried the next formulae:

  • crossWalletBalance == free USDT steadiness * Complete Fee + initMargins for all positions + unrealizedProfit for all positions, signal included;
  • crossWalletBalance == free USDT steadiness + (initMargins for all positions + unrealizedProfit) * Complete Fee for all positions, signal included.
  • Totally different variations that disregard charges or embrace margin charges.

Nonetheless, contemplating that collaterals are about 13000 digital “USDT”s, and the trades are throughout the leveraged vary of a number of 1000’s of USDTs, I constantly obtain values that differ from the bottom fact crossWalletBalance worth from a number of to tens-hundreds of “USDT”s, relying on the method.

I’d be grateful for any assist or hints on this respect.

LEAVE A REPLY

Please enter your comment!
Please enter your name here