Prevent repeated TP2 cleanup loops
This commit is contained in:
@@ -682,7 +682,7 @@ export class PositionManager {
|
||||
}
|
||||
|
||||
// 5. Take profit 2 (remaining position)
|
||||
if (trade.tp1Hit && this.shouldTakeProfit2(currentPrice, trade)) {
|
||||
if (trade.tp1Hit && !trade.tp2Hit && this.shouldTakeProfit2(currentPrice, trade)) {
|
||||
console.log(`🎊 TP2 HIT: ${trade.symbol} at ${profitPercent.toFixed(2)}%`)
|
||||
|
||||
// Calculate how much to close based on TP2 size percent
|
||||
@@ -776,18 +776,31 @@ export class PositionManager {
|
||||
return
|
||||
}
|
||||
|
||||
const closePriceForCalc = result.closePrice || currentPrice
|
||||
const closedSizeBase = result.closedSize || 0
|
||||
const closedUSD = closedSizeBase * closePriceForCalc
|
||||
const wasForcedFullClose = !!result.fullyClosed && percentToClose < 100
|
||||
const treatAsFullClose = percentToClose >= 100 || result.fullyClosed
|
||||
|
||||
// Update trade state
|
||||
if (percentToClose >= 100) {
|
||||
// Full close - remove from monitoring
|
||||
if (treatAsFullClose) {
|
||||
trade.realizedPnL += result.realizedPnL || 0
|
||||
|
||||
// Save to database (only for valid exit reasons)
|
||||
trade.currentSize = 0
|
||||
trade.trailingStopActive = false
|
||||
|
||||
if (reason === 'TP2') {
|
||||
trade.tp2Hit = true
|
||||
}
|
||||
if (reason === 'TP1') {
|
||||
trade.tp1Hit = true
|
||||
}
|
||||
|
||||
if (reason !== 'error') {
|
||||
try {
|
||||
const holdTimeSeconds = Math.floor((Date.now() - trade.entryTime) / 1000)
|
||||
await updateTradeExit({
|
||||
positionId: trade.positionId,
|
||||
exitPrice: result.closePrice || currentPrice,
|
||||
exitPrice: closePriceForCalc,
|
||||
exitReason: reason as 'TP1' | 'TP2' | 'SL' | 'SOFT_SL' | 'HARD_SL' | 'manual' | 'emergency',
|
||||
realizedPnL: trade.realizedPnL,
|
||||
exitOrderTx: result.transactionSignature || 'MANUAL_CLOSE',
|
||||
@@ -802,22 +815,22 @@ export class PositionManager {
|
||||
console.log('💾 Trade saved to database')
|
||||
} catch (dbError) {
|
||||
console.error('❌ Failed to save trade exit to database:', dbError)
|
||||
// Don't fail the close if database fails
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
await this.removeTrade(trade.id)
|
||||
console.log(`✅ Position closed | P&L: $${trade.realizedPnL.toFixed(2)} | Reason: ${reason}`)
|
||||
const closeLabel = wasForcedFullClose
|
||||
? '✅ Forced full close (below Drift minimum)'
|
||||
: '✅ Position closed'
|
||||
console.log(`${closeLabel} | P&L: $${trade.realizedPnL.toFixed(2)} | Reason: ${reason}`)
|
||||
} else {
|
||||
// Partial close (TP1)
|
||||
trade.realizedPnL += result.realizedPnL || 0
|
||||
// result.closedSize is returned in base asset units (e.g., SOL), convert to USD using closePrice
|
||||
const closePriceForCalc = result.closePrice || currentPrice
|
||||
const closedSizeBase = result.closedSize || 0
|
||||
const closedUSD = closedSizeBase * closePriceForCalc
|
||||
trade.currentSize = Math.max(0, trade.currentSize - closedUSD)
|
||||
|
||||
console.log(`✅ Partial close executed | Realized: $${(result.realizedPnL || 0).toFixed(2)} | Closed (base): ${closedSizeBase.toFixed(6)} | Closed (USD): $${closedUSD.toFixed(2)} | Remaining USD: $${trade.currentSize.toFixed(2)}`)
|
||||
console.log(
|
||||
`✅ Partial close executed | Realized: $${(result.realizedPnL || 0).toFixed(2)} | Closed (base): ${closedSizeBase.toFixed(6)} | Closed (USD): $${closedUSD.toFixed(2)} | Remaining USD: $${trade.currentSize.toFixed(2)}`
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: Send notification
|
||||
|
||||
Reference in New Issue
Block a user