From ed7ffa8f13d8d4209357916e42e487977d32c81f Mon Sep 17 00:00:00 2001 From: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com> Date: Fri, 4 Oct 2024 15:45:25 +0200 Subject: [PATCH] fix(perpetual): update pnl formulas --- x/perpetual/keeper/query_open_estimation.go | 23 ++++++++++++++----- .../keeper/query_open_estimation_test.go | 14 +++++------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/x/perpetual/keeper/query_open_estimation.go b/x/perpetual/keeper/query_open_estimation.go index 401f8edc..d549c85f 100644 --- a/x/perpetual/keeper/query_open_estimation.go +++ b/x/perpetual/keeper/query_open_estimation.go @@ -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 diff --git a/x/perpetual/keeper/query_open_estimation_test.go b/x/perpetual/keeper/query_open_estimation_test.go index bb4fabf6..8fc0d935 100644 --- a/x/perpetual/keeper/query_open_estimation_test.go +++ b/x/perpetual/keeper/query_open_estimation_test.go @@ -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"), @@ -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"), @@ -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"), @@ -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)),