🧮
PNL Calculations
On Lyra's dApp there are a couple of sections where you'll see an estimated profit and loss value. It's important to understand how these are calculated in order to make informed trading decisions.

## My Positions

The first place you might come across a profit/loss calculation is in the 'My Positions' section at the bottom of the trading page, shown below: The outline of the calculation for a long position is as follows:
1. 1.
When a long trade is opened (i.e. buy call or buy put), store the average price of the purchase (averagePrice), and the quantity of the position (balance).
2. 2.
The markPrice is equal to the Black Scholes price of the option, using the current tradeVolatility for the relevant listing. That is: estPNL = (markPrice - averagePrice) * balance.
3. 3.
As more trades are opened, update averagePrice and balance accordingly.
4. 4.
When a partial closing trade is performed (i.e. selling half of your position), closePrice is compared against averagePrice to determine a realizedPNL. That is, realizedPNL = (closePrice - averagePrice) * closeQuantity.
5. 5.
closeQuantity is then deducted from balance (balance -= closeQuantity), and averagePrice remains the same.

### Example

Walking through an example, imagine that we buy 10 ETH \$2000 calls for \$100:
1. 1.
averagePrice = \$100, balance = 10
If the price of calls increases to \$120 (i.e. markPrice = \$120):
2. estPNL = (120-100) * 10 = \$200.
If we buy another 5 calls at \$120:
3. averagePrice = (5 * \$120 + 10 * \$100) / (5 + 10) = \$106.67, balance = 10 + 5 = 15_
If the price of the option decreases to \$115, and we decide to close (i.e. sell) 5 of our calls:
4. realizedPNL = (\$115 - \$106.67) * 5 = \$41.65.
Our remaining position is long 10 calls (balance = 10), with averagePrice = \$106.67. If markPrice = \$116, estPNL = (116-\$106.67) * 10 = \$93.3. 