Skip to content

Commit

Permalink
fix(perpetual): update pnl formulas
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond committed Oct 4, 2024
1 parent 592a236 commit ed7ffa8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
23 changes: 17 additions & 6 deletions x/perpetual/keeper/query_open_estimation.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,27 @@ func (k Keeper) OpenEstimation(goCtx context.Context, req *types.QueryOpenEstima
req.TakeProfitPrice = sdk.MustNewDecFromStr(types.TakeProfitPriceDefault)
}

// calculate liabilities amount
liabilitiesAmountDec := sdk.NewDecFromBigInt(collateralAmountInBaseCurrency.Amount.BigInt()).Mul(req.Leverage.Sub(sdk.OneDec()))

// calculate estimated pnl
// estimated_pnl = (current_price - entry_price) * custody_amount
estimatedPnL := req.TakeProfitPrice.Sub(openPrice).Mul(sdk.NewDecFromBigInt(positionSize.Amount.BigInt()))
estimatedPnLDenom := req.TradingAsset
var estimatedPnL sdk.Dec
estimatedPnLDenom := baseCurrency

// if position is short then:
// estimated_pnl = (entry_price - current_price) * (custody_amount / open_price)
if req.Position == types.Position_SHORT {
estimatedPnL = openPrice.Sub(req.TakeProfitPrice).Mul(sdk.NewDecFromBigInt(positionSize.Amount.BigInt()).Quo(openPrice))
estimatedPnLDenom = baseCurrency
// estimated_pnl = custody_amount - liabilities_amount * take_profit_price - collateral_amount
estimatedPnL = sdk.NewDecFromBigInt(positionSize.Amount.BigInt()).Sub(liabilitiesAmountDec.Mul(req.TakeProfitPrice)).Sub(sdk.NewDecFromBigInt(req.Collateral.Amount.BigInt()))
} else {
// if position is long then:
// if collateral is not in base currency
if req.Collateral.Denom != baseCurrency {
// estimated_pnl = (custody_amount - collateral_amount) * take_profit_price - liabilities_amount
estimatedPnL = sdk.NewDecFromBigInt(positionSize.Amount.BigInt()).Sub(sdk.NewDecFromBigInt(req.Collateral.Amount.BigInt())).Mul(req.TakeProfitPrice).Sub(liabilitiesAmountDec)
} else {
// estimated_pnl = custody_amount * take_profit_price - liabilities_amount - collateral_amount
estimatedPnL = sdk.NewDecFromBigInt(positionSize.Amount.BigInt()).Mul(req.TakeProfitPrice).Sub(liabilitiesAmountDec).Sub(sdk.NewDecFromBigInt(req.Collateral.Amount.BigInt()))
}
}

// calculate liquidation price
Expand Down
14 changes: 7 additions & 7 deletions x/perpetual/keeper/query_open_estimation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func TestOpenEstimation_Long5XAtom100Usdc(t *testing.T) {
TakeProfitPrice: sdk.MustNewDecFromStr("20.00000000000000000"),
LiquidationPrice: sdk.MustNewDecFromStr("0.799653976372211738"),
InterestAmount: types.NewParams().MinBorrowInterestAmount,
EstimatedPnl: sdk.NewInt(9_483_592265),
EstimatedPnlDenom: ptypes.ATOM,
EstimatedPnl: sdk.NewInt(9_482_728700),
EstimatedPnlDenom: ptypes.BaseCurrency,
AvailableLiquidity: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(600000000000)),
Slippage: sdk.MustNewDecFromStr("0.000727856000000000"),
WeightBalanceRatio: sdk.MustNewDecFromStr("0.000000000000000000"),
Expand Down Expand Up @@ -225,8 +225,8 @@ func TestOpenEstimation_Long5XAtom10Atom(t *testing.T) {
OpenPrice: sdk.MustNewDecFromStr("1.000000000000000000"),
TakeProfitPrice: sdk.MustNewDecFromStr("20.00000000000000000"),
LiquidationPrice: sdk.MustNewDecFromStr("0.799662359570370484"),
EstimatedPnl: sdk.NewInt(947_320202),
EstimatedPnlDenom: ptypes.ATOM,
EstimatedPnl: sdk.NewInt(757_224656),
EstimatedPnlDenom: ptypes.BaseCurrency,
InterestAmount: types.NewParams().MinBorrowInterestAmount,
AvailableLiquidity: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(10000000000)),
Slippage: sdk.MustNewDecFromStr("0.000686040302239768"),
Expand Down Expand Up @@ -347,8 +347,8 @@ func TestOpenEstimation_Long10XAtom1000Usdc(t *testing.T) {
TakeProfitPrice: sdk.MustNewDecFromStr("5.000000000000000000"),
LiquidationPrice: sdk.MustNewDecFromStr("3.944975514993148154"),
InterestAmount: types.NewParams().MinBorrowInterestAmount,
EstimatedPnl: sdk.NewInt(1_370_711096),
EstimatedPnlDenom: ptypes.ATOM,
EstimatedPnl: sdk.NewInt(1_235_336860),
EstimatedPnlDenom: ptypes.BaseCurrency,
AvailableLiquidity: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(600_000_000000)),
Slippage: sdk.MustNewDecFromStr("0.012549973525000000"),
WeightBalanceRatio: sdk.MustNewDecFromStr("0.000000000000000000"),
Expand Down Expand Up @@ -463,7 +463,7 @@ func TestOpenEstimation_Short5XAtom10Usdc(t *testing.T) {
OpenPrice: sdk.MustNewDecFromStr("1.000000000000000000"),
TakeProfitPrice: sdk.MustNewDecFromStr("2.000000000000000000"),
LiquidationPrice: sdk.MustNewDecFromStr("1.200000000000000000"),
EstimatedPnl: sdk.NewInt(-500000000),
EstimatedPnl: sdk.NewInt(-400000000),
EstimatedPnlDenom: ptypes.BaseCurrency,
InterestAmount: types.NewParams().MinBorrowInterestAmount,
AvailableLiquidity: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(10000000000)),
Expand Down

0 comments on commit ed7ffa8

Please sign in to comment.