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.
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:
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).
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.
As more trades are opened, update averagePrice and balance accordingly.
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.
closeQuantity is then deducted from balance (balance -= closeQuantity), and averagePrice remains the same.
Walking through an example, imagine that we buy 10 ETH $2000 calls for $100:
averagePrice = $100, balance = 10
If the price of calls increases to $120 (i.e. markPrice = $120):