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 in response to the components famous right here. Nevertheless, I am constantly 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 brief positions. By fetching all positions with non-zero variety of contracts, I retrieve deserialized JSON with related information 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 reality worth for reference and checking whether or not my calculations are right;crossMargin
– for reference, to double-check thatcrossWalletBalance
minus allunrealizedProfit
s equals to it.
I assume that the price 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. Significantly, I take advantage of pockets steadiness components from right here:
Pockets Steadiness = Complete Internet Switch + Complete Realized Revenue + Complete Internet Funding Payment - 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 Payment is zero, since positions are assumed to be closed nearly instantly, subsequently, 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 +initMargin
s for all positions +unrealizedProfit
for all positions, signal included;crossWalletBalance
== free USDT steadiness + (initMargin
s for all positions +unrealizedProfit
) * Complete Fee for all positions, signal included.- Completely different variations that disregard charges or embody margin charges.
Nevertheless, contemplating that collaterals are about 13000 digital “USDT”s, and the trades are inside the leveraged vary of a number of 1000’s of USDTs, I constantly obtain values that differ from the bottom reality crossWalletBalance
worth from a number of to tens-hundreds of “USDT”s, relying on the components.
I’d be pleased about any assist or hints on this respect.