-9.5 C
New York
Wednesday, January 22, 2025

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. Significantly, I would want it for simulating the calculation of liquidation costs in cross-margin mode in line with the components famous right here. Nonetheless, I am persistently failing to breed the worth from Binance API.

So far as I perceive, in cross-margin mode crossWalletBalance corresponds to the overall 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 = 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 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 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 based mostly on beforehand talked about manuals. Significantly, I take advantage of pockets steadiness components from right here:

Pockets Stability = Whole Web Switch + Whole Realized Revenue + Whole Web Funding Payment - Whole Fee

…assuming that:

  • Whole Web Switch equals to free USDT steadiness;
  • Whole Realized Revenue is zero, as quickly as positions are nonetheless open;
  • Whole Web Funding Payment is zero, since positions are assumed to be closed virtually instantly, subsequently, funding charges don’t relate;
  • Whole 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 * 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.
  • Completely different variations that disregard charges or embody margin charges.

Nonetheless, contemplating that collaterals are about 13000 digital “USDT”s, and the trades are inside 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 components.

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

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles