PnL calculations

To calculate the current PnL of a deal you may use the following example for ETH/DAI position:

Assume, we open an ETH/DAI in long using $1000 of DAI as collatral, with x5 leverage and slippage tolerance set to 0.5%, when DAI market price was at $0.99 and ETH market price at $2000 and ETH price rose to $2100

The amount of fees paid for opening a position on the market would be:

protocolFeeRate = 0.16%
flashLoanFeeRate = 0.09%

The amount of margin reserved for the loan would be:

margin = 1000 DAI
marginBaseOpenPrice = $0.99

Using the set order parameters we can calculate the Position Size in USD and ETH excluding the fees incurred, and the minimum size filled considering the current slippage tolerance applied:

collateralBaseOpenPrice = 2000
PositionSize = (margin × (leverage - 1) + margin) = 5000
totalSizeWithProtocolFee = positionSize - (positionSize × protocolFeeRate) = 4992
totalSizeWithSlippage = totalSizeWithProtocolFee - (totalSizeWithProtocolFee × slippage) = 4987.008
collateral = (totalSizeWithSlippage × marginBaseOpenPrice) / collateralBaseOpenPrice = 2.4685896 (ETH)

Now, we can calculate the amount of fees incurred to our position upon opening:

flashloanAmount = margin × (leverage - 1) = 4000
flashloanFee = (flashloanAmount × flashLoanFee) = 3.6

Next, we would have to calculate the funding rate and funding amounts. General formula to calculate the funding amount is the following:

fundingFee = (totalSizeWithSlippage - margin) × ratePerTime = 0.00648643

Where,

ratePerTime = (rateAPY / 100) × timePassed / 31536000 (seconds in 1 year) = 0.00000163, where

rateAPY = 1.43%
timePassed = 3600 (seconds in 1 hour)

For the simplicity of calculations, however, let's assume that the position hasn't been open for an hour yet, thus the funding fee has not yet been charged.

In order to find the PnL value, we would first need to calculate the current market prices for the position in DAI and ETH.

ethInUsdOpen = collateral × collateralBaseOpenPrice = 4937.13792
ethInUsdCurrent = collateral × collateralBaseCurrentPrice = 5183.994816
daiInUsd = (totalSizeWithSlippage - (flashloanFee - (fundingFee × hourPassed))) × marginBasePrice = 4983.74616

Now, in order to calculate the PnL of our position we have to find the difference between the current collateral market price and the open collateral market price:

TotalPnL = (ethInUsdCurrent / daiInUsd) × ethInUsdCurrent - ethInUsdOpen = $246.856896

Last updated