fix: correct PnL math and add health probe
This commit is contained in:
@@ -519,15 +519,15 @@ export async function closePosition(
|
||||
|
||||
// Calculate realized P&L with leverage (default 10x in dry run)
|
||||
const profitPercent = ((oraclePrice - position.entryPrice) / position.entryPrice) * 100 * (position.side === 'long' ? 1 : -1)
|
||||
const leverage = 10 // Use 10x for dry run
|
||||
const accountPnLPercent = profitPercent * leverage
|
||||
const closedNotional = sizeToClose * oraclePrice
|
||||
const realizedPnL = (closedNotional * accountPnLPercent) / 100
|
||||
const realizedPnL = (closedNotional * profitPercent) / 100
|
||||
const accountPnLPercent = profitPercent * 10 // display using default leverage
|
||||
|
||||
const mockTxSig = `DRY_RUN_CLOSE_${Date.now()}_${Math.random().toString(36).substring(7)}`
|
||||
|
||||
console.log(`💰 Simulated close:`)
|
||||
console.log(` Close price: $${oraclePrice.toFixed(4)}`)
|
||||
console.log(` Profit %: ${profitPercent.toFixed(3)}% → Account P&L (10x): ${accountPnLPercent.toFixed(2)}%`)
|
||||
console.log(` Realized P&L: $${realizedPnL.toFixed(2)}`)
|
||||
|
||||
return {
|
||||
@@ -584,11 +584,10 @@ export async function closePosition(
|
||||
console.log('⚠️ Could not determine leverage from account, using 10x default')
|
||||
}
|
||||
|
||||
const accountPnLPercent = profitPercent * leverage
|
||||
|
||||
// Calculate closed notional value (USD)
|
||||
const closedNotional = sizeToClose * oraclePrice
|
||||
const realizedPnL = (closedNotional * accountPnLPercent) / 100
|
||||
const realizedPnL = (closedNotional * profitPercent) / 100
|
||||
const accountPnLPercent = profitPercent * leverage
|
||||
|
||||
console.log(`💰 Close details:`)
|
||||
console.log(` Close price: $${oraclePrice.toFixed(4)}`)
|
||||
|
||||
@@ -350,15 +350,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 +380,10 @@ export class PositionManager {
|
||||
trade.lastPrice,
|
||||
trade.direction
|
||||
)
|
||||
const accountPnL = profitPercent * trade.leverage
|
||||
const estimatedPnL = (trade.currentSize * accountPnL) / 100
|
||||
const accountPnLPercent = profitPercent * trade.leverage
|
||||
const estimatedPnL = (trade.currentSize * profitPercent) / 100
|
||||
|
||||
console.log(`💰 Estimated P&L for lost trade: ${profitPercent.toFixed(2)}% price → ${accountPnL.toFixed(2)}% account → $${estimatedPnL.toFixed(2)} realized`)
|
||||
console.log(`💰 Estimated P&L for lost trade: ${profitPercent.toFixed(2)}% price → ${accountPnLPercent.toFixed(2)}% account → $${estimatedPnL.toFixed(2)} realized`)
|
||||
|
||||
try {
|
||||
await updateTradeExit({
|
||||
@@ -450,8 +450,7 @@ export class PositionManager {
|
||||
currentPrice,
|
||||
trade.direction
|
||||
)
|
||||
const accountPnL = profitPercent * trade.leverage
|
||||
realizedPnL = (sizeForPnL * accountPnL) / 100
|
||||
realizedPnL = (sizeForPnL * profitPercent) / 100
|
||||
}
|
||||
|
||||
// Determine exit reason from trade state and P&L
|
||||
@@ -708,7 +707,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
|
||||
|
||||
Reference in New Issue
Block a user