Tangem Wallet Pack of 2 - Secure Crypto Wallet - Trusted Cold Storage for Bitcoin, Ethereum, NFT's &

Tangem Wallet Pack of 2 - Secure Crypto Wallet - Trusted Cold Storage for Bitcoin, Ethereum, NFT's &
Key item features Ultimate Security: Generates a private key that remains on the card, safeguarding crypto and NFTs from hackers with EAL6+ certification and audited firmware. Versatile Compatibility: Manages over 13,000 tokens across 70+ blockchains, supporting DeFi, NFTs, and DeEx without wires, Bluetooth, or USB. Effortless Operation: Utilizes NFC for secure transactions via a mobile device and the Tangem app, enabling buying and selling crypto with various payment methods. Smart Backup: Features a second Tangem Wallet as a backup, eliminating the need for paper, pictures, or seed phrases for recovery. Durable Design: Boasts IP68 protection against environmental conditions, ensuring longevity and robust physical security. Comprehensive Support: Compatible with Bitcoin, Ethereum, Solana, XRP, USDT, and over 6,000 cryptocurrencies, integrating with dApps and WalletConnect.

LBANK

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

I'm trying to reproduce and figure out how crossWalletBalance value, retrieved from Binance API, is calculated by Binance in USDⓈ-M perpetual futures. Particularly, I would need it for simulating the calculation of liquidation prices in cross-margin mode according to the formula noted here. However, I'm consistently failing to reproduce the value from Binance API.

As far as I understand, in cross-margin mode crossWalletBalance corresponds to the total balance, comprised of wallet balance and margin balance, which includes unrealized PnLs, as described here.

In order to verify my calculations, I use Binance Testnet, where I place market orders using Python's CCXT bindings. Let's suppose I open several short positions. By fetching all positions with non-zero number of contracts, I retrieve deserialized JSON with relevant info on all open positions grouped by symbols:

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

Current free USDT balance is fetched using another HTTP-request wrapped into CCXT's exchange.fetch_free_balance().

Fields of peculiar interest among the JSON are:

  • initMargin - position initial margin; I use it for calculating margin balance;
  • unrealizedProfit - UPnL; I also use it for calculating margin balance;
  • crossWalletBalance - ground truth value for reference and checking whether my calculations are correct;
  • crossMargin - for reference, to double-check that crossWalletBalance minus all unrealizedProfits equals to it.

I assume that the fee taken is 0.0004, and have double-checked this fact using the history of my trades on Binance Testnet page.

I have tried several formulae modifications based on previously mentioned manuals. Particularly, I use wallet balance formula from here:

Wallet Balance = Total Net Transfer + Total Realized Profit + Total Net Funding Fee - Total Commission

...assuming that:

  • Total Net Transfer equals to free USDT balance;
  • Total Realized Profit is zero, as soon as positions are still open;
  • Total Net Funding Fee is zero, since positions are assumed to be closed almost immediately, therefore, funding rates do not relate;
  • Total Commission is 0.0004. It is taken once, since positions are still open;

Particularly, among others, I have tried the following formulae:

  • crossWalletBalance == free USDT balance * Total Commission + initMargins for all positions + unrealizedProfit for all positions, sign included;
  • crossWalletBalance == free USDT balance + (initMargins for all positions + unrealizedProfit) * Total Commission for all positions, sign included.
  • Different variations that disregard fees or include margin rates.

However, considering that collaterals are about 13000 virtual "USDT"s, and the trades are within the leveraged range of several thousands of USDTs, I consistently achieve values that differ from the ground truth crossWalletBalance value from several to tens-hundreds of "USDT"s, depending on the formula.

As a result, I assume two possible reasons for this:

  • Underlying assets mark prices slippages occur during those one-two seconds between fetching free USDT balance and fetching positions. In this turn, it affects UPnLs and margin balance calculation;
  • All my calculations are flawed.

I would be thankful for any help or hints in this respect.



from Recent Questions - Bitcoin Stack Exchange https://ift.tt/5EcjHYI
via IFTTT

Popular posts from this blog

Crypto Exec Warns Tokenization Is Moving Faster Than Expected

Bitcoin Mining Could Be Strengthening The Ruble, Russian Central Bank Says

Nigerian SEC Partners With Police To Tackle Crypto Ponzi Schemes – Details