Tuesday, January 21, 2025
HomeBitcoinpython - How is crossWalletBalance calculated with Binance perpetual futures in cross-margin...

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


I am making an attempt to breed and determine how crossWalletBalance worth, retrieved from Binance API, is calculated by Binance in USDⓈ-M perpetual futures. Notably, I would want it for simulating the calculation of liquidation costs in cross-margin mode in keeping with the method famous right here. Nevertheless, I am persistently 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.

As a way to confirm my calculations, I exploit 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 = change.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 change.fetch_free_balance().

Fields of strange curiosity among the many JSON are:

  • initMargin – place preliminary margin; I exploit 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 appropriate;
  • crossMargin – for reference, to double-check that crossWalletBalance minus all unrealizedProfits equals to it.

I assume that the payment taken is 0.0004, and have double-checked this truth 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. Notably, I exploit pockets steadiness method from right here:

Pockets Stability = Whole Internet Switch + Whole Realized Revenue + Whole Internet Funding Price - Whole Fee

…assuming that:

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

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

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

Nevertheless, contemplating that collaterals are about 13000 digital “USDT”s, and the trades are throughout the leveraged vary of a number of hundreds of USDTs, I persistently 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 glad about any assist or hints on this respect.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments