Fix P&L calculation and update Copilot instructions

- Fix P&L calculation in Position Manager to use actual entry vs exit price instead of SDK's potentially incorrect realizedPnL
- Calculate actual profit percentage and apply to closed position size for accurate dollar amounts
- Update database record for last trade from incorrect 6.58 to actual .66 P&L
- Update .github/copilot-instructions.md to reflect TP2-as-runner system changes
- Document 25% runner system (5x larger than old 5%) with ATR-based trailing
- Add critical P&L calculation pattern to common pitfalls section
- Mark Phase 5 complete in development roadmap
This commit is contained in:
mindesbunister
2025-11-07 16:24:43 +01:00
parent 0c644ccabe
commit 5acc61cf66
4 changed files with 55 additions and 47 deletions

View File

@@ -795,9 +795,13 @@ export class PositionManager {
const wasForcedFullClose = !!result.fullyClosed && percentToClose < 100
const treatAsFullClose = percentToClose >= 100 || result.fullyClosed
// Calculate actual P&L based on entry vs exit price
const profitPercent = this.calculateProfitPercent(trade.entryPrice, closePriceForCalc, trade.direction)
const actualRealizedPnL = (closedUSD * profitPercent) / 100
// Update trade state
if (treatAsFullClose) {
trade.realizedPnL += result.realizedPnL || 0
trade.realizedPnL += actualRealizedPnL
trade.currentSize = 0
trade.trailingStopActive = false
@@ -837,12 +841,13 @@ export class PositionManager {
: '✅ Position closed'
console.log(`${closeLabel} | P&L: $${trade.realizedPnL.toFixed(2)} | Reason: ${reason}`)
} else {
// Partial close (TP1)
trade.realizedPnL += result.realizedPnL || 0
// Partial close (TP1) - calculate P&L for partial amount
const partialRealizedPnL = (closedUSD * profitPercent) / 100
trade.realizedPnL += partialRealizedPnL
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)}`
`✅ Partial close executed | Realized: $${partialRealizedPnL.toFixed(2)} | Closed (base): ${closedSizeBase.toFixed(6)} | Closed (USD): $${closedUSD.toFixed(2)} | Remaining USD: $${trade.currentSize.toFixed(2)}`
)
}