Skip to content

Commit

Permalink
Avkr003/short fixes (#909)
Browse files Browse the repository at this point in the history
* fixing short stop loss price and long take profit price

* remove redundant check
  • Loading branch information
avkr003 authored Nov 4, 2024
1 parent f176c00 commit 7434315
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
12 changes: 9 additions & 3 deletions x/perpetual/keeper/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ func (k Keeper) Open(ctx sdk.Context, msg *types.MsgOpen, isBroker bool) (*types
if !msg.StopLossPrice.IsZero() && msg.StopLossPrice.GTE(tradingAssetPrice) {
return nil, fmt.Errorf("stop loss price cannot be greater than equal to tradingAssetPrice for long (Stop loss: %s, asset price: %s)", msg.StopLossPrice.String(), tradingAssetPrice.String())
}
// no need to override msg.TakeProfitPrice as the above ratio check it
}
if msg.Position == types.Position_SHORT {
if ratio.GT(params.MaximumShortTakeProfitPriceRatio) {
return nil, fmt.Errorf("take profit price should be less than %s times of current market price for short (current ratio: %s)", params.MaximumShortTakeProfitPriceRatio.String(), ratio.String())
}
if msg.StopLossPrice.IsZero() {
msg.StopLossPrice = types.MaxShortStopLossPrice
}
if !msg.StopLossPrice.IsZero() && msg.StopLossPrice.LTE(tradingAssetPrice) {
return nil, fmt.Errorf("stop loss price cannot be less than equal to tradingAssetPrice for short (Stop loss: %s, asset price: %s)", msg.StopLossPrice.String(), tradingAssetPrice.String())
}
Expand All @@ -63,14 +67,16 @@ func (k Keeper) Open(ctx sdk.Context, msg *types.MsgOpen, isBroker bool) (*types
existingMtp := k.CheckSameAssetPosition(ctx, msg)

if existingMtp == nil {
if msg.Leverage.Equal(math.LegacyOneDec()) {
return nil, fmt.Errorf("cannot open new position with leverage 1")
// opening new position
if msg.Leverage.LTE(math.LegacyOneDec()) {
return nil, fmt.Errorf("cannot open new position with leverage <= 1")
}
// Check if max positions are exceeded as we are opening new position, not updating old position
if err = k.CheckMaxOpenPositions(ctx); err != nil {
return nil, err
}
} else if msg.Leverage.Equal(math.LegacyOneDec()) {
} else if msg.Leverage.Equal(math.LegacyZeroDec()) {
// adding collateral to existing position (when leverage > 1, we leave the case for modifying old position)
// Enforce collateral addition (for leverage 1) without modifying take profit price
msg.TakeProfitPrice = existingMtp.TakeProfitPrice
}
Expand Down
1 change: 1 addition & 0 deletions x/perpetual/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
var (
TakeProfitPriceDefault = sdk.MustNewDecFromStr("10000000000000000000000000000000000000000") // 10^40
StopLossPriceDefault = sdk.ZeroDec()
MaxShortStopLossPrice = sdk.MustNewDecFromStr("10000000000000000000000000000000000000000") // 10^40
)

var (
Expand Down

0 comments on commit 7434315

Please sign in to comment.