fix: Use TRIGGER_LIMIT for stop loss orders

- Changed from LIMIT to TRIGGER_LIMIT for proper stop loss display
- SL now shows correctly on Drift UI with trigger line
- Added 0.5% buffer on limit price below trigger for safety
- Widened default SL to -2% for safer testing
- Tested and verified all 3 exit orders (TP1, TP2, SL) working
This commit is contained in:
mindesbunister
2025-10-26 20:15:12 +01:00
parent 4cc294baef
commit f733c11711
2 changed files with 13 additions and 7 deletions

4
.env
View File

@@ -61,7 +61,7 @@ PYTH_HERMES_URL=https://hermes.pyth.network
# Position sizing
# Base position size in USD (default: 50 for safe testing)
# Example: 50 with 10x leverage = $500 notional position
MAX_POSITION_SIZE_USD=10
MAX_POSITION_SIZE_USD=20
# Leverage multiplier (1-20, default: 10)
# Higher leverage = bigger gains AND bigger losses
@@ -70,7 +70,7 @@ LEVERAGE=5
# Risk parameters (as percentages)
# Stop Loss: Close 100% of position when price drops this much
# Example: -1.5% on 10x = -15% account loss
STOP_LOSS_PERCENT=-0.9
STOP_LOSS_PERCENT=-2.0
# Take Profit 1: Close 50% of position at this profit level
# Example: +0.7% on 10x = +7% account gain

View File

@@ -265,22 +265,28 @@ export async function placeExitOrders(options: {
}
}
// Place Stop-Loss LIMIT reduce-only (note: trigger-market would be preferable)
// Place Stop-Loss as TRIGGER_LIMIT order (proper stop loss that shows on Drift UI)
const slUSD = options.positionSizeUSD // place full-size SL
const slBaseAmount = usdToBase(slUSD, options.stopLossPrice)
if (slBaseAmount >= Math.floor(marketConfig.minOrderSize * 1e9)) {
const orderParams: any = {
orderType: OrderType.LIMIT,
orderType: OrderType.TRIGGER_LIMIT, // Use TRIGGER_LIMIT (not LIMIT with trigger)
marketIndex: marketConfig.driftMarketIndex,
direction: orderDirection,
baseAssetAmount: new BN(slBaseAmount),
price: new BN(Math.floor(options.stopLossPrice * 1e6)),
triggerPrice: new BN(Math.floor(options.stopLossPrice * 1e6)), // trigger price
price: new BN(Math.floor(options.stopLossPrice * 0.995 * 1e6)), // limit price slightly lower (0.5% buffer)
triggerCondition: options.direction === 'long'
? OrderTriggerCondition.BELOW // Long: trigger when price drops below
: OrderTriggerCondition.ABOVE, // Short: trigger when price rises above
reduceOnly: true,
}
console.log('🚧 Placing SL limit order (reduce-only)...')
console.log('🚧 Placing SL trigger-limit order (reduce-only)...')
console.log(` Trigger: ${options.direction === 'long' ? 'BELOW' : 'ABOVE'} $${options.stopLossPrice.toFixed(4)}`)
console.log(` Limit price: $${(options.stopLossPrice * 0.995).toFixed(4)}`)
const sig = await (driftClient as any).placePerpOrder(orderParams)
console.log('✅ SL order placed:', sig)
console.log('✅ SL trigger-limit order placed:', sig)
signatures.push(sig)
} else {
console.log('⚠️ SL size below market min, skipping on-chain SL')