Position: A position in trading refers to holding a specific amount of money with the expectation to with the expectation to change its value. When you buy an asset with the expectation that its price will rise then you are in a long position.When you sell an asset you do not own, with the expectation of buying it back at a lower price
Long Position: Imagine you believe that the price of gold is going to increase. To capitalize on this belief, you decide to buy 10 ounces of gold at $1,800 per ounce, investing a total of $18,000. By purchasing this gold, you are now holding a long position in gold. If the price of gold rises to $1,900 per ounce and you decide to sell, you would be selling your 10 ounces for $19,000, making a profit of $1,000 on your position.
Short Position: On the flip side, if you believed that the price of gold was going to decrease, you could take a short position on gold. This would involve borrowing 10 ounces of gold and selling them at the current price of $1,800 per ounce. If the price then falls to $1,700 per ounce, you could buy back the 10 ounces for $17,000, return the borrowed gold, and keep the $1,000 difference as profit.
Futures Mode
USDT-M: In USDT-M all the balance & fee calculation ( addition, deduction, representation etc) will be in USDT currency
COIN-M: In COIN-M all the balance & fee calculation (addition, deduction, representation etc) will be in Asset (BTC, ETH, MATIC) currency
Perpetual: In perpetual mode there will be no time limit for closing a position rather users close their position by themselves or close their position by liquidation
Quarterly: In quarterly mode there will be a time limit (3,6,12 months etc). The position will be closed automatically after the time expires whether it will reach liquidation or not.
Margin Mode
Cross Margin Mode: In cross margin mode instead of assigning a specific amount of money to support a single trade where your entire balance is available to backup for your position to avoid liquidation.
Isolated Margin Mode: In isolated margin mode you must have to assign a specific amount of money to support a single trade where your specific assigned amount will be accounted for keeping your position running. If you assign more balance you will be at lower risk to liquidate and vice versa.
Example for Isolated and Cross Mode: Think of isolated margin like individual projects where each project has its own budget. If a project goes over budget, it can't borrow money from other projects. In cross margin, it's like a team project where the total budget is shared. If one part of the project needs more resources, it can use funds from the common pot.
Leverage: Leverage in trading refers to the use of borrowed funds to increase the potential return of an investment. It allows traders to open larger positions with a smaller amount of actual capital. While leverage can amplify gains, it also increases the potential for losses.
- Suppose you have $1,000 to invest in crypto trading, and you use 10 leverage. This means you can open a position worth $10,000
Balances:
Asset Balance: User’s actual wallet balance (DB Balance)
Unrealized PNL: Summation of user’s total position’s PNL
Total Order Cost: Summation of the cost of all (open + pending) orders
Total Margin: Summation of the all positions margin (Cross + Isolated)
Available Balance: Asset Balance - Unrealized PNL - Total Order Cost - Total Margin
Margin: Margin refers to the amount of money that must be deposited by a trader for the exchange as collateral to open a position. It acts as a guarantee for meeting any potential losses incurred by the trader.
Initial Margin: This is the initial deposit required to open a futures position. It is not a payment but rather a security deposit that must be maintained as long as the position is open. Finally, the margin amount will be deducted from the user wallet for the position liquidation to recover platform loss. For the isolated mode margin doesn’t change for the mark price changes rather margin can be adjusted (add/remove) for protecting liquidation where For the cross mode margin changes for the mark price changes
Maintenance Margin: After a position is opened, traders must maintain a certain level of funds in their account, known as the maintenance margin. This is typically lower than the initial margin and this is calculated considering some of the risk management settings. For the isolated mode a single position maintenance margin is responsible for margin ratio calculation, while for the cross mode the all position maintenance margin is responsible for calculating margin ratio.
Margin Balance: The margin balance refers to the amount of money in a trader's account that is allocated as margin for holding open positions. It's essential to understand the concept of margin balance as it directly impacts a trader’s ability to hold positions in the futures market.
For the isolated mode the fixed allocated margin is the margin balance. Users can adjust the position margin balance.
For the cross mode margin balance:
Asset Balance - Sum of Isolated Position Margin - Sum of Order Margin + PNL
Margin Ratio: Margin ratio is the ratio of maintenance margin and margin balance. When a position's margin balance goes close to the maintenance margin then the margin ratio goes close to 100% and the position will be at a high risk of liquidation. When margin balance becomes less than or equal to maintenance margin then margin ratio crosses 100% and the liquidation happens.
Liquidation Price: Liquidation price is the price if a currency pair’s mark price goes to the liquidation price that position will be liquidated.
In isolated mode when the position’s mark price goes close to the liquidation price then the neg PNL of the position goes close to the margin then margin ratio will go close to the 100%
In cross mode when position mark price goes to liquidation price then the deducting total pnl from the margin balance goes close to the maintenance margin and the margin ratio then goes to the 100%.
Funding Rate: The payment rate exchanged between the long and short positions for the next funding. If the funding rate is positive, longs pay shorts. If negative, shorts pay longs.
Index Price: Current Crypto Price from different marketplace (Avg price)
Mark Price: Is calculated from Index price and market price
Entry Price: After creating buy/sell order, the order first trades with the current order book. Entry price is the trade price (Maker price)
Take Profit: Take Profit is an order placed by a trader to automatically close a position when the market reaches a specific favorable price level
- If a trader goes long (buys) a futures contract at $100 and sets a Take Profit order at $110, the position will be automatically closed when the market price reaches $110, securing a profit for the trader
Stop Loss: Stop Loss is an order placed by a trader to automatically close a position when the market reaches a specified unfavorable price level
- If a trader goes long (buys) a futures contract at $100 and sets a Stop Loss order at $90, the position will be automatically closed when the market price reaches $90, limiting the potential loss.
Reduce Only: Reduce-Only order serves to strictly reduce your open position.
If you have an open long (buy) position, a "ReduceOnly" order associated with that position would be a sell order. This order would only be allowed to reduce or close your existing long position; it cannot open a new short position.
Similarly, if you have an open short (sell) position, a "ReduceOnly" order associated with that position would be a buy order. This order would only be allowed to reduce or close your existing short position; it cannot open a new long position.
Final Formulas
Calculate Amount With Size Percent = Max * (Size / 100)
Initial Margin = (Price * Amount) / Leverage
PNL Long / Short \= Position Size Direction(1 or -1) (Market Price - Entry Price)
UnRealized PnL = Position Size Direction (1 or -1 ) (Mark Price - Entry Price)
Realized PnL = Position Size Direction(1 or -1) (Close Price - Entry Price)
ROI% = ( PNL / Initial Margin ) * 100%
Target price:
Long target price = entry price * ( ROE% / leverage + 1 )
Short target price = entry price * ( 1 - ROE% / leverage )
Available Balance = Wallet Balance - Sum(PNL) - Sum(Margin)
- Margin = (amount * mark price) / leverage
Buy / Sell Max/Min price = There will be a percentage setting from admin. Max / min price will be with market price and setting percentage.
Maintenance Margin = Amount Price Maintenance Margin Rate - Maintenance Amount
Margin Balance = Available Balance + Sum(Margin)
- Margin = ( Mark Price * Amount ) / Leverage
Margin Ratio = ( Maintenance Margin / Margin Balance ) * 100
Cost For Limit Or Stop Order = Initial Margin + Open Loss
Long Open Loss = abs {min[0,size * (mark price - order price)]}
Short Open Loss = abs{min[0, -size*(mark price - order price)]}
Cost For Market Order = Initial Margin + Open Loss
Initial Margin = (Assuming Price * Number of Contract) / Leverage
Long Assuming Price = ask[0] * (1 + 0.05%)
Short Assuming Price = bid [0]
Open Loss = Number of Contract abs{min[0, dir (mark price - assuming price)]}
Max(Base Currency) = (Available Balance * Leverage)
- If Max(Trade Currency) > leverage wise max setting
then Max(Trade Currency) \= leverage wise max setting
Max(Trade Currency) = Max(Trade Currency) - (MA * MMR%)
Max(Base Currency) \= Max(Trade Currency) / Order Price
Price < Mark Price then Buy is stable
Price > Mark Price then Sell is stable
Mark price = Median (Price 1, Price 2, Contract Price) **
Price 1 = Price Index (1 + Last Funding Rate (Time until next Funding / Funding period) )
Price 2 = Price Index + Moving Average (5-minute Basis) **
Moving Average (5-minute Basis) = sum of [(Bid1_i + Ask1_i)/2 - PI_i] /60
Bid1_i 5 min period buy order
Ask_i 5 min period sell order
PI: Price Index at the time of calculation
If Price 1 < Price 2 < Contract Price, then Price 2 will be taken as the Mark Price
Funding Calculation
Funding Rate = Premium Index + Clamp(Interest Rate − Premium Index, Min Rate, Max Rate)
Premium Index = ((Future Price - Spot Price) / Spot Price) * 100
Clamp = max(min, min(max, x))
Interest Rate = Set by exchange
Min Rate = Set by exchange
Max Rate = Set by exchanged
Funding Payment = Position Size × Funding Rate
If the funding rate is positive, long positions pay funding to short positions, and if it's negative, short positions pay funding to long positions.
Time until next Funding = Funding expiration Time - Current Time
Funding period = 8 hrs
Funding Countdown = The funding countdown is the time remaining until the next funding period, which typically occurs every 8 hours
Liquidation Price
LQ for Long: Entry Price - ( Available Balance / Size )
LQ for Short: ( Available Balance / Size ) + Entry Price
LQ = (WB - OTHER_MAIN_BAL + OTHER_PNL + SIZE EP) / (SIZE (MMR/100) - SIDE * SIZE)
N.B: Liquidity price can be minus value. If minus add only minus before the price
Liquidation Process:
If mark price goes to liquidation price
- Liquidate the specific position and call the margin ratio check event of user to check if margin ratio cross or not after liquidation
If margin ratio goes to 100%
If margin mode is isolated then liquidate the only isolated position and call the margin ratio check event of the user to check if margin ratio crosses or not after liquidation.
If margin mode is cross then liquidate all currency pair cross positions
If margin ratio >= 80 % and < 100 %
- Send notification to the user to close amount or add balance