# Calculations

1. **Average entry price calculation for positions**

<table><thead><tr><th width="249.33333333333331">Scenario</th><th>Sub-scenario</th><th>Average Entry Price</th></tr></thead><tbody><tr><td>New position opened</td><td></td><td><em>AvgEntryPrice = TradePrice</em></td></tr><tr><td>New trade for an existing position</td><td>Side changed, i.e. long became short or short became long</td><td><em>AvgEntryPrice = TradePrice</em></td></tr><tr><td></td><td>Size reduced, i.e. long reduces in size but remains long, or, short reduces in size but remains short</td><td>AvgEntryPrice = LastAvgEntryPrice</td></tr><tr><td></td><td>Size increased, i.e. long increase in size but remains long, or, short increases in size but remains short</td><td>AvgEntryPrice = <em>(PreviousSize * LastAvgEntryPrice + TradedSize * TradePrice) / NewSize</em></td></tr></tbody></table>

**P.S.: Fees is not considered while calculating average entry price of positions, total fees for a position is shown separately**

2. **Realised Profit and Loss calculation for trades**

| Scenario                           | Sub-scenario                                                                                                | Realised Pnl                                                                                           |
| ---------------------------------- | ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
| New position opened                |                                                                                                             | Realised Pnl = 0                                                                                       |
| New trade for an existing position | Side changed, i.e. long became short or short became long                                                   | <p>Realised Pnl = Pnl for the side which was closed<br><br>Unrealised Pnl for position is set to 0</p> |
|                                    | Size reduced, i.e. long reduces in size but remains long, or, short reduces in size but remains short       | Realised Pnl = (InitialSize - FinalSize) \* (TradePrice - LastAvgEntryPrice)                           |
|                                    | Size increased, i.e. long increases in size but remains long, or, short increases in size but remains short | Realised Pnl = 0                                                                                       |

3. **Realised funding calculation for trades**

| Scenario                           | Sub-scenario                                                                                                | Realised funding                                                                                                                    |
| ---------------------------------- | ----------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| New position opened                |                                                                                                             | Realised funding = 0                                                                                                                |
| New trade for an existing position | Side changed, i.e. long became short or short became long                                                   | <p>Realised funding = Funding accumulated since the last trade<br><br>Unrealised funding for corresponding position is set to 0</p> |
|                                    | Size reduced, i.e. long reduces in size but remains long, or, short reduces in size but remains short       | <p>Realised funding = Funding accumulated since last trade<br><br>Unrealised funding for corresponding position is set to 0</p>     |
|                                    | Size increased, i.e. long increases in size but remains long, or, short increases in size but remains short | <p>Realised funding = Funding accumulated since last trade<br><br>Unrealised funding for corresponding position is set to 0</p>     |

4. **Fees calculation for orders**\
   The fee displayed for orders covers the total fee incurred from placing the order to creating the position (There is a delay between aforementioned events due to delayed order mechanism of Synthetix)

| Type of order  | Fees calculation                                     |
| -------------- | ---------------------------------------------------- |
| Standard order | Fees = keeper fee + execution fee                    |
| Limit order    | Fees = limit order fees + keeper fee + execution fee |

5. **Unrealised funding calculation for positions**

Funding shown in positions tab is the unrealised funding accrued for the position since the last trade placed by the user. \
\
P.S.: There is a cron which updates the unrealised funding for a position every 3 minutes, so the unrealised funding shown is not always accurate.

6. **Unrealized Pnl calculation**

Unrealized Pnl = (currentPrice - latestInteractionPrice) \* size + accruedFunding

7. **Realised Pnl calculation for liquidations**

Realised Pnl for a liquidation can be seen in the **`History`** tab. A new trade is created with side field set to **Liquidation.** Realised pnl for a liquidation trade is set to the total margin present in the market when the position was liquidated.
