feat: Implement ATR-based dynamic TP2 system and fix P&L calculation

- Add ATR-based dynamic TP2 scaling from 0.7% to 3.0% based on volatility
- New config options: useAtrBasedTargets, atrMultiplierForTp2, minTp2Percent, maxTp2Percent
- Enhanced settings UI with ATR controls and updated risk calculator
- Fix external closure P&L calculation using unrealized P&L instead of volatile current price
- Update execute and test endpoints to use calculateDynamicTp2() function
- Maintain 25% runner system for capturing extended moves (4-5% targets)
- Add environment variables for ATR-based configuration
- Better P&L accuracy for manual position closures
This commit is contained in:
mindesbunister
2025-11-07 17:01:22 +01:00
parent 5acc61cf66
commit 6d5991172a
7 changed files with 208 additions and 19 deletions

View File

@@ -468,14 +468,38 @@ export class PositionManager {
// Calculate P&L first (set to 0 for phantom trades)
let realizedPnL = 0
let exitPrice = currentPrice
if (!wasPhantom) {
const profitPercent = this.calculateProfitPercent(
trade.entryPrice,
currentPrice,
trade.direction
)
const accountPnL = profitPercent * trade.leverage
realizedPnL = (sizeForPnL * accountPnL) / 100
// For external closures, try to estimate a more realistic exit price
// Manual closures may happen at significantly different prices than current market
const unrealizedPnL = trade.unrealizedPnL || 0
const positionSizeUSD = trade.positionSize
if (Math.abs(unrealizedPnL) > 1 && positionSizeUSD > 0) {
// If we have meaningful unrealized P&L, back-calculate the likely exit price
// This is more accurate than using volatile current market price
const impliedProfitPercent = (unrealizedPnL / positionSizeUSD) * 100 / trade.leverage
exitPrice = trade.direction === 'long'
? trade.entryPrice * (1 + impliedProfitPercent / 100)
: trade.entryPrice * (1 - impliedProfitPercent / 100)
console.log(`📊 Estimated exit price based on unrealized P&L:`)
console.log(` Unrealized P&L: $${unrealizedPnL.toFixed(2)}`)
console.log(` Market price: $${currentPrice.toFixed(6)}`)
console.log(` Estimated exit: $${exitPrice.toFixed(6)}`)
realizedPnL = unrealizedPnL
} else {
// Fallback to current price calculation
const profitPercent = this.calculateProfitPercent(
trade.entryPrice,
currentPrice,
trade.direction
)
const accountPnL = profitPercent * trade.leverage
realizedPnL = (sizeForPnL * accountPnL) / 100
}
}
// Determine exit reason from trade state and P&L
@@ -504,7 +528,7 @@ export class PositionManager {
try {
await updateTradeExit({
positionId: trade.positionId,
exitPrice: currentPrice,
exitPrice: exitPrice, // Use estimated exit price, not current market price
exitReason,
realizedPnL,
exitOrderTx: 'ON_CHAIN_ORDER',
@@ -516,7 +540,7 @@ export class PositionManager {
maxFavorablePrice: trade.maxFavorablePrice,
maxAdversePrice: trade.maxAdversePrice,
})
console.log(`💾 External closure recorded: ${exitReason} at $${currentPrice} | P&L: $${realizedPnL.toFixed(2)}`)
console.log(`💾 External closure recorded: ${exitReason} at $${exitPrice.toFixed(6)} | P&L: $${realizedPnL.toFixed(2)}`)
} catch (dbError) {
console.error('❌ Failed to save external closure:', dbError)
}