feat: refresh exit orders after TP1 and add dry-run harness
This commit is contained in:
@@ -333,12 +333,7 @@ export class PositionManager {
|
||||
trade.tp1Hit = true
|
||||
trade.currentSize = positionSizeUSD
|
||||
|
||||
// Move SL to breakeven after TP1
|
||||
trade.stopLossPrice = trade.entryPrice
|
||||
trade.slMovedToBreakeven = true
|
||||
console.log(`🛡️ Stop loss moved to breakeven: $${trade.stopLossPrice.toFixed(4)}`)
|
||||
|
||||
await this.saveTradeState(trade)
|
||||
await this.handlePostTp1Adjustments(trade, 'on-chain TP1 detection')
|
||||
|
||||
} else if (trade.tp1Hit && !trade.tp2Hit && reductionPercent >= 85) {
|
||||
// TP2 fired (total should be ~95% closed, 5% runner left)
|
||||
@@ -350,15 +345,15 @@ export class PositionManager {
|
||||
|
||||
await this.saveTradeState(trade)
|
||||
|
||||
// CRITICAL: Don't return early! Continue monitoring the runner position
|
||||
// The trailing stop logic at line 732 needs to run
|
||||
|
||||
} else {
|
||||
// Partial fill detected but unclear which TP - just update size
|
||||
console.log(`⚠️ Unknown partial fill detected - updating tracked size to $${positionSizeUSD.toFixed(2)}`)
|
||||
trade.currentSize = positionSizeUSD
|
||||
await this.saveTradeState(trade)
|
||||
}
|
||||
|
||||
// Continue monitoring the remaining position
|
||||
return
|
||||
}
|
||||
|
||||
// CRITICAL: Check for entry price mismatch (NEW position opened)
|
||||
@@ -380,10 +375,10 @@ export class PositionManager {
|
||||
trade.lastPrice,
|
||||
trade.direction
|
||||
)
|
||||
const accountPnLPercent = profitPercent * trade.leverage
|
||||
const estimatedPnL = (trade.currentSize * profitPercent) / 100
|
||||
const accountPnL = profitPercent * trade.leverage
|
||||
const estimatedPnL = (trade.currentSize * accountPnL) / 100
|
||||
|
||||
console.log(`💰 Estimated P&L for lost trade: ${profitPercent.toFixed(2)}% price → ${accountPnLPercent.toFixed(2)}% account → $${estimatedPnL.toFixed(2)} realized`)
|
||||
console.log(`💰 Estimated P&L for lost trade: ${profitPercent.toFixed(2)}% price → ${accountPnL.toFixed(2)}% account → $${estimatedPnL.toFixed(2)} realized`)
|
||||
|
||||
try {
|
||||
await updateTradeExit({
|
||||
@@ -450,7 +445,8 @@ export class PositionManager {
|
||||
currentPrice,
|
||||
trade.direction
|
||||
)
|
||||
realizedPnL = (sizeForPnL * profitPercent) / 100
|
||||
const accountPnL = profitPercent * trade.leverage
|
||||
realizedPnL = (sizeForPnL * accountPnL) / 100
|
||||
}
|
||||
|
||||
// Determine exit reason from trade state and P&L
|
||||
@@ -632,56 +628,7 @@ export class PositionManager {
|
||||
// Move SL based on breakEvenTriggerPercent setting
|
||||
trade.tp1Hit = true
|
||||
trade.currentSize = trade.positionSize * ((100 - this.config.takeProfit1SizePercent) / 100)
|
||||
const newStopLossPrice = this.calculatePrice(
|
||||
trade.entryPrice,
|
||||
this.config.breakEvenTriggerPercent, // Use configured breakeven level
|
||||
trade.direction
|
||||
)
|
||||
trade.stopLossPrice = newStopLossPrice
|
||||
trade.slMovedToBreakeven = true
|
||||
|
||||
console.log(`🔒 SL moved to +${this.config.breakEvenTriggerPercent}% (${this.config.takeProfit1SizePercent}% closed, ${100 - this.config.takeProfit1SizePercent}% remaining): ${newStopLossPrice.toFixed(4)}`)
|
||||
|
||||
// CRITICAL: Cancel old on-chain SL orders and place new ones at updated price
|
||||
try {
|
||||
console.log('🗑️ Cancelling old stop loss orders...')
|
||||
const { cancelAllOrders, placeExitOrders } = await import('../drift/orders')
|
||||
const cancelResult = await cancelAllOrders(trade.symbol)
|
||||
if (cancelResult.success) {
|
||||
console.log(`✅ Cancelled ${cancelResult.cancelledCount || 0} old orders`)
|
||||
|
||||
// Place new SL orders at breakeven/profit level for remaining position
|
||||
console.log(`🛡️ Placing new SL orders at $${newStopLossPrice.toFixed(4)} for remaining position...`)
|
||||
const exitOrdersResult = await placeExitOrders({
|
||||
symbol: trade.symbol,
|
||||
positionSizeUSD: trade.currentSize,
|
||||
entryPrice: trade.entryPrice,
|
||||
tp1Price: trade.tp2Price, // Only TP2 remains
|
||||
tp2Price: trade.tp2Price, // Dummy, won't be used
|
||||
stopLossPrice: newStopLossPrice,
|
||||
tp1SizePercent: 100, // Close remaining 25% at TP2
|
||||
tp2SizePercent: 0,
|
||||
direction: trade.direction,
|
||||
useDualStops: this.config.useDualStops,
|
||||
softStopPrice: trade.direction === 'long'
|
||||
? newStopLossPrice * 1.005 // 0.5% above for long
|
||||
: newStopLossPrice * 0.995, // 0.5% below for short
|
||||
hardStopPrice: newStopLossPrice,
|
||||
})
|
||||
|
||||
if (exitOrdersResult.success) {
|
||||
console.log('✅ New SL orders placed on-chain at updated price')
|
||||
} else {
|
||||
console.error('❌ Failed to place new SL orders:', exitOrdersResult.error)
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to update on-chain SL orders:', error)
|
||||
// Don't fail the TP1 exit if SL update fails - software monitoring will handle it
|
||||
}
|
||||
|
||||
// Save state after TP1
|
||||
await this.saveTradeState(trade)
|
||||
await this.handlePostTp1Adjustments(trade, 'software TP1 execution')
|
||||
return
|
||||
}
|
||||
|
||||
@@ -707,7 +654,7 @@ export class PositionManager {
|
||||
}
|
||||
|
||||
// 5. Take profit 2 (remaining position)
|
||||
if (trade.tp1Hit && !trade.tp2Hit && this.shouldTakeProfit2(currentPrice, trade)) {
|
||||
if (trade.tp1Hit && this.shouldTakeProfit2(currentPrice, trade)) {
|
||||
console.log(`🎊 TP2 HIT: ${trade.symbol} at ${profitPercent.toFixed(2)}%`)
|
||||
|
||||
// Calculate how much to close based on TP2 size percent
|
||||
@@ -926,6 +873,103 @@ export class PositionManager {
|
||||
console.log('✅ All positions closed')
|
||||
}
|
||||
|
||||
private async handlePostTp1Adjustments(trade: ActiveTrade, context: string): Promise<void> {
|
||||
if (trade.currentSize <= 0) {
|
||||
console.log(`⚠️ Skipping TP1 adjustments for ${trade.symbol} (${context}) because current size is $${trade.currentSize.toFixed(2)}`)
|
||||
await this.saveTradeState(trade)
|
||||
return
|
||||
}
|
||||
|
||||
const newStopLossPrice = this.calculatePrice(
|
||||
trade.entryPrice,
|
||||
this.config.breakEvenTriggerPercent,
|
||||
trade.direction
|
||||
)
|
||||
|
||||
trade.stopLossPrice = newStopLossPrice
|
||||
trade.slMovedToBreakeven = true
|
||||
|
||||
console.log(`🔒 (${context}) SL moved to +${this.config.breakEvenTriggerPercent}% (${this.config.takeProfit1SizePercent}% closed, ${100 - this.config.takeProfit1SizePercent}% remaining): ${newStopLossPrice.toFixed(4)}`)
|
||||
|
||||
await this.refreshExitOrders(trade, {
|
||||
stopLossPrice: newStopLossPrice,
|
||||
tp1Price: trade.tp2Price,
|
||||
tp1SizePercent: 100,
|
||||
tp2Price: trade.tp2Price,
|
||||
tp2SizePercent: 0,
|
||||
context,
|
||||
})
|
||||
|
||||
await this.saveTradeState(trade)
|
||||
}
|
||||
|
||||
private async refreshExitOrders(
|
||||
trade: ActiveTrade,
|
||||
options: {
|
||||
stopLossPrice: number
|
||||
tp1Price: number
|
||||
tp1SizePercent: number
|
||||
tp2Price?: number
|
||||
tp2SizePercent?: number
|
||||
context: string
|
||||
}
|
||||
): Promise<void> {
|
||||
if (trade.currentSize <= 0) {
|
||||
console.log(`⚠️ Skipping exit order refresh for ${trade.symbol} (${options.context}) because tracked size is zero`)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
console.log(`🗑️ (${options.context}) Cancelling existing exit orders before refresh...`)
|
||||
const { cancelAllOrders, placeExitOrders } = await import('../drift/orders')
|
||||
const cancelResult = await cancelAllOrders(trade.symbol)
|
||||
if (cancelResult.success) {
|
||||
console.log(`✅ (${options.context}) Cancelled ${cancelResult.cancelledCount || 0} old orders`)
|
||||
} else {
|
||||
console.warn(`⚠️ (${options.context}) Failed to cancel old orders: ${cancelResult.error}`)
|
||||
}
|
||||
|
||||
const tp2Price = options.tp2Price ?? options.tp1Price
|
||||
const tp2SizePercent = options.tp2SizePercent ?? 0
|
||||
|
||||
const refreshParams: any = {
|
||||
symbol: trade.symbol,
|
||||
positionSizeUSD: trade.currentSize,
|
||||
entryPrice: trade.entryPrice,
|
||||
tp1Price: options.tp1Price,
|
||||
tp2Price,
|
||||
stopLossPrice: options.stopLossPrice,
|
||||
tp1SizePercent: options.tp1SizePercent,
|
||||
tp2SizePercent,
|
||||
direction: trade.direction,
|
||||
useDualStops: this.config.useDualStops,
|
||||
}
|
||||
|
||||
if (this.config.useDualStops) {
|
||||
const softStopBuffer = this.config.softStopBuffer ?? 0.4
|
||||
const softStopPrice = trade.direction === 'long'
|
||||
? options.stopLossPrice * (1 + softStopBuffer / 100)
|
||||
: options.stopLossPrice * (1 - softStopBuffer / 100)
|
||||
|
||||
refreshParams.softStopPrice = softStopPrice
|
||||
refreshParams.softStopBuffer = softStopBuffer
|
||||
refreshParams.hardStopPrice = options.stopLossPrice
|
||||
}
|
||||
|
||||
console.log(`🛡️ (${options.context}) Placing refreshed exit orders: size=$${trade.currentSize.toFixed(2)} SL=${options.stopLossPrice.toFixed(4)} TP=${options.tp1Price.toFixed(4)}`)
|
||||
const exitOrdersResult = await placeExitOrders(refreshParams)
|
||||
|
||||
if (exitOrdersResult.success) {
|
||||
console.log(`✅ (${options.context}) Exit orders refreshed on-chain`)
|
||||
} else {
|
||||
console.error(`❌ (${options.context}) Failed to place refreshed exit orders: ${exitOrdersResult.error}`)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`❌ (${options.context}) Error refreshing exit orders:`, error)
|
||||
// Monitoring loop will still enforce SL logic even if on-chain refresh fails
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save trade state to database (for persistence across restarts)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user