diff --git a/.env b/.env index 36f3bd7..c6aaf32 100644 --- a/.env +++ b/.env @@ -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 diff --git a/lib/drift/orders.ts b/lib/drift/orders.ts index e59d4e0..8109cb0 100644 --- a/lib/drift/orders.ts +++ b/lib/drift/orders.ts @@ -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')