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

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')