# Calculating PNL

### **Spot Markets**&#x20;

For spot markets, realized profit and loss (rPNL) is calculated based on the difference between the buy and sell prices of an asset. Using the **First In, First Out (FIFO)** method, the formula is:

$$
\text{rPNL} = \sum\limits\_{i=1}^{n} Q\_i \cdot (P\_{\text{sell},i} - P\_{\text{buy},i})
$$

Where:

* $$Q\_i$$: Quantity of the asset sold in the 𝑖-th transaction.
* $$P\_{\text{sell},i}$$: Price at which the asset was sold in the 𝑖-th transaction.
* $$P\_{\text{buy},i}$$: Price at which the corresponding quantity of the asset was purchased, based on FIFO.
* $$n$$: Total number of sell transactions considered.

**Example**

Let’s calculate rPNL step by step based on the following trades:

1. Date A: Buy 50 INJ at price $$P(A)$$.
2. Date B: Sell 200 INJ at price $$P(B)$$.
3. Date C: Sell 50 INJ at price $$P(C)$$.
4. Date D: Buy 10 INJ at price $$P(D)$$.
5. Date E: Sell 20 INJ at price $$P(E)$$.

**Step-by-Step rPNL Calculation**

1. **Initial rPNL**:
   * At Date A, nothing has been sold, so rPNL = 0.
2. **rPNL at Date B**:
   * The user sells 200 INJ but has only bought 50 INJ at $$P(A)$$, making them net short by 150 INJ.
   * $$rPNL = 50\times (P(B)-P(A))$$
3. **rPNL at Date C**:
   * The user sells another 50 INJ, remaining net short. Since no new purchases have been made, the rPNL calculation remains unchanged:
   * $$rPNL = 50\times(P(B) - P(A))$$
4. **rPNL at Date D**:
   * The user buys 10 INJ at $$P(D)$$, which does not immediately affect the rPNL.
   * The rPNL remains: $$50\times(P(B)-P(A))$$.
5. **rPNL at Date E**:
   * The user sells 20 INJ, of which 10 INJ were purchased at $$P(D)$$ and the remaining 10 INJ were part of the net short position.
   * The rPNL is calculated as: $$rPNL = \[50\times (P(B)-P(A))]+\[10\times(P(E)-P(D))]$$
     * $$50\times(P(B)-P(A))$$: Previously accrued PNL from the initial sale of 50 INJ.
     * $$10\times(P(E)-P(D))$$: Newly accrued PNL from selling 10 INJ bought at $$P(D)$$.
   * At this point, the user remains net short by 190 INJ, as they have sold more INJ than they purchased.

### **Derivatives Markets**

For derivatives markets, rPNL calculation is very straightforward. Herein, rPNL is calculated for each trade when a position is closed. The chain emits detailed trade logs that include the rPNL for each individual trade. These values are then aggregated to determine a trader’s total rPNL.

This approach ensures that rPNL is recorded accurately and in real-time, with additional adjustments made for scenarios like liquidations or funding payments.
