Updated: 8 Nov 2021
Previous logic
Assuming that the wallet balance=3000USDT, Leverage in use is 100x cross margin and transaction fee is 0 for easier calculation.
1. The trader uses the cross margin mode to enter a buy long 2BTC BTCUSDT position at a price of 10,000 USDT, the initial margin=10,000*2/100=200USDT, the available margin is 2800USDT.
2. When the price drops to 9000 USDT, the user enters a sell short 4BTC BTCUSDT position, the initial margin is 9000*4/100=360USDT, the existing long position will currently have an unrealized loss of 2000USDT, and the available balance of the current account is 440 USDT( 3000-200-2000-360).
3. Now that the price has fallen to 8000 USDT, we only need to consider that the unhedged 2BTC short position has generated an unrealized profit of 2000USDT, and the available balance of the account is still 440USDT (the floating profit cannot be used as the available balance).
The existing logic can ensure that the hedging position has enough available margin to occupy, and the trader will not suffer excessive losses exceeding the available margin due to this, and there will be no situation where the position of the opposite direction is liquidated immediately after the unilateral position is closed.
New logic after optimization
Assuming that the wallet balance=3000USDT, Leverage in use is 100x cross margin and transaction fee is 0 for easier calculation.
1. The trader uses the cross margin mode to enter a buy long 2BTC BTCUSDT position at a price of 10,000 USDT.
The initial margin of position
=10,000*2/100
=200USDT.
The position margin of the position now will be
= initial margin + fee to close + unrealized loss of position
= 200 USDT+0+0 = 200 USDT.
The Available balance will now be
= Wallet balance - position margin - order margin
= 3000-200-0
= 2800 USDT
2. When the price drops to 9000 USDT, the user enters a sell short 4BTC BTCUSDT position. The existing long position will currently have an unrealized loss of 2000USDT.
The position margin of the long position will be
= 1.2* maintenance margin % * position value + fee to close
= 1.2*0.5%*(10000*2)+0
= 120USDT
The position margin of the short position will be
= (1.2*maintenance margin % * position value of the hedge 2 BTC position)+ initial margin of the unhedged 2 BTC position+ fee to close + unrealized loss of the unhedged 2 BTC position
= (1.2*0.5%*(9000*2))+(9000*2/100)+0+0
= 288 USDT
The Available balance will now be
= Wallet balance - position margin - order margin
= 3000- (288+120)-0
= 2592 USDT
3. Now that the price has fallen to 8000 USDT, we only need to consider that the unhedged 2BTC short position has generated an unrealized profit of 2000USDT, and the available balance of the account is still 2592 USDT (the floating profit cannot be used as the available balance). Where under the former logic traders available balance will be 440 USDT at the same stage.
4. Now that the price has fallen to 7000 USDT, and the trader close the short position with a realized profit of 8000 USDT. The Position margin of the Long position will now revert back to initial margin + fee to close + unrealized loss of position.
Position margin of long position
= 200+0+6000
=6200 USDT
The Available balance will now be
=3000-6200+8000
=4800 USDT
Compare to previous logic, the above optimization will release additional margin from the fully-hedge positions which traders can use to place new orders while ensuring that the hedging position has enough occupied position margin. There will be no situation where the position of the opposite direction is liquidated immediately after the unilateral position is closed.