# Trade lifecycle

This page contains a description of the life cycle of transactions, the main stages of trading on the platform and the calculation formulas used to determine the value of transactions, profit and loss, as well as commissions and spreads.

The page also provides examples of PnL (profit and loss) calculations, and an explanation of the terms used on the platform.

## Lifecycle of a long trade

Let's start by setting pool and position parameters for traders:

$Depth = 400\,\,ETH$

$Open\,fee = 0.03\%$

$Base\,spread = 0.05\%$

$Funding\,base\,rate = 1\%$

Suppose a trader has decided to open a long position for the ETH/USD pair, using 1000 USDT as their collateral and setting the leverage at x2. After the smart contract receives 1000 USDT, it will calculate and charge an open fee, which depends on the size of the open position and the leverage. For this trade, the commission can be calculated using the following formula:

$Open\,fee = Collateral*Leverage*Open \,fee\,rate = 1000 * 2 * 0.03\% = 0.6\, \,USDT$

Where:

**Collateral**- the amount a trader uses to open a position without taking leverage into account**Leverage**- a multiplier indicating how much a trader's position is increased relative to their collateral**Open fee rate**- the percentage rate charged by the exchange for opening a position. It may vary depending on the type of traded asset

Then the calculation of the amount of the trader's position takes place:

$Open\,position = (Collateral - Open\,Fee)*Leverage = (1000-0.6)*2= 1998.8$

The next step is to determine the opening price of the position.

Let's calculate the dynamic spread:

$Dynamic\,spread\,for\,long = max(0; \cfrac{(Long + 0.5*Long_{new} - Short)}{Depth} * 0.1\%) = max(0; \cfrac{(100 + 0.5*1.332533 - 90)}{400} * 0.1\%) = 0.002666566625\%$

Where:

**Long/Short**-$Long_{new}$ - the new long position size

**Depth**- the depth parameter of this asset

The final opening price of the position:

$Open\,long\,price = Current\,price*(1+Base\,spread+Dymamic\,spread) = 1500 * (1 + 0.05\% + 0.002666566625\%) = 1500.789998$

Where:

**Current price**- the current price of the underlying asset in the market**Base spread**- the fixed base spread

Funding calculations require the value of an open position in ETH:

$Long, ETH = \cfrac {Open\,position}{Open\,long\,price} = \cfrac {1998.8}{1500.789998} = 1.331831904\,ETH$

Where:

**Long, ETH**- the amount of Ethereum that was bought when opening a position**Open position**- the size of a trader's open position**Open long price**- the purchase price of Ethereum when opening a position

## Fees while holding a position

A borrow fee will be continuously debited from the traderโs account: $Borrow\,rate, per\,hour= Base\,rate * Leverage = 0.001\%*2 = 0.002\%$

Where:

**Borrow rate**- the interest rate that the trader pays for using borrowed funds**Base rate**- the base interest rate for borrowing, which depends on the type of asset used when opening a position

$Borrow\,fee, per\,hour= Borrow\,rate * (Collateral -Open\,fee) = 0.002\% * (1000-0.6) = 0.19988\,USDT$

Where:

**Borrow fee**- the commission that the trader pays for using borrowed funds

Since there are more long positions open than short positions, the funding rate will be debited from the traderโs account:

$Funding\,rate = Funding\,base\,rate * \cfrac {|Long - Short|}{Market\,depth} = 1\% * \cfrac {|100+1.331831904-90|}{400} = 0.02832957976\%$

Where:

**Funding rate**- the funding rate that a trader pays or receives depending on the direction of his open position**Funding base rate**- the base funding rate set by Gravix**Market Depth**- the depth of the market, which determines how large the open amount of assets must be in order to affect the cost of funding

$Long\,funding\,rate = funding\,rate = 0.02832957976\%$

The trader will be charged the following hourly amount:

$1.331831904\,ETH * ETH\,\,price * Long\,funding\,rate = 1.331831904\,ETH * 1500 \,ETH/USD * 0.0283313325\% = 0.56598858759\,USD$

Let's calculate the trader's liquidation price after one day:

$Liquidation\,Price\,Distance = \\ = \cfrac {Open\,Price * (Collateral * 0.9 - Borrow\,rates - Funding\,fees)} {Collateral * Leverage} = \\ = \cfrac {1500.789998 * (1000 * 0.9 - 0.19988 * 24 - 0.56598858759 * 24)} {1000 * 2} = 661.562604108$

Where:

**Liquidation Price Distance**- the distance between the current price and the liquidation price of the position. It is used to determine when the position will be liquidated

$Liquidation\,price = \cfrac {Open\,price - Liquidation\,Price\,Distance}{1-Close\,spread} = \\ = \cfrac {1500.789998 - 661.562604108}{1-0.05\%} = 839.6472175\,ETH/USD$

Where:

**Liquidation price**- the price at which the position will be liquidated

## Closing a position

We will assume that ETH has risen to 2,000 USD in 24 hours. Letโs calculate how much profit the trader will receive after closing their position:

$Close\,price = 2000*(1-Base\,spread) = 2000*(1-0.05\%) = 1999$

Where:

**Close price**- the price at which the trader closes their position

The traderโs profit excluding fees is then calculated:

$PnL_{\,Long} = ( \cfrac {Close\,price} {Open\,price} - 1) * (Collateral - Open\,fee) * Leverage = \\ = (\cfrac{1999} {1500.789998} - 1) * (1000 - 0.6) * 2 = 663.531975$

Where:

$PnL_{\,Long}$ - the profit or loss from an open position

We then calculate the close fee:

$ะกlose\,fee = ((Collateral-Open\,fee) * Leverage + PnL - Funding\,fee - Borrow\,fee) * Close\,fee\,rate = ((1000-0.6) * 2 + 663.531975 - 0.56598858759 * 24 - 0.19988*24) * 0.03\% = 0.793186$

Where:

**Close fee**- the commission charged for closing a position**Close fee rate**- equals the Open fee rate for this type of asset

Finally, we calculate the traderโs profit after payment of all fees:

$PnL_{\,Long} = ( \cfrac {Close\,price} {Open\,price} - 1) * (Collateral - Open\,fee) * Leverage - Funding fee - Borrow fee - Close\,fee = (\cfrac{1999} {1500.789998} - 1) * (1000 - 0.6) * 2 - 0.56598858759 * 24 - 0.19988*24 - 0.793186 = 644.3579433$

Where:

$PnL_{\,Long}$ - profit or loss from an open position

Last updated