fix: correct entry prices and position sizing in trading system

- Fixed automation service to use real SOL price (~89) instead of hardcoded 00
- Updated position size calculation to properly convert USD investment to token amount
- Enhanced trade display to show separate entry/exit prices with price difference
- Added data quality warnings for trades with missing exit data
- Updated API to use current SOL price (189.50) and improved trade result determination
- Added detection and warnings for old trades with incorrect price data

Resolves issue where trades showed 9-100 entry prices instead of real SOL price of 89
and position sizes of 2.04 SOL instead of correct ~0.53 SOL for 00 investment
This commit is contained in:
mindesbunister
2025-07-21 09:26:48 +02:00
parent 55cea00e5e
commit 71e1a64b5d
13 changed files with 795 additions and 546 deletions

View File

@@ -46,7 +46,7 @@ export async function GET() {
const totalPnL = completedTrades.reduce((sum, trade) => sum + (trade.profit || 0), 0)
const winRate = completedTrades.length > 0 ? (successfulTrades.length / completedTrades.length * 100) : 0
const currentPrice = 175.82
const currentPrice = 189.50
const formattedTrades = recentTrades.map(trade => {
const priceChange = trade.side === 'BUY' ?
@@ -75,9 +75,51 @@ export async function GET() {
const tradingAmount = 100
const leverage = trade.leverage || 1
const correctTokenAmount = tradingAmount / trade.price
const displayAmount = correctTokenAmount
// Use real current price for calculation instead of stored wrong price
const correctTokenAmount = tradingAmount / currentPrice
const displayAmount = correctTokenAmount // Show corrected amount
const displayPositionSize = (tradingAmount * leverage).toFixed(2)
// Mark old trades with wrong data
const isOldWrongTrade = trade.price < 150 && trade.amount > 1.5 // Detect old wrong trades
// Enhanced entry/exit price handling
const entryPrice = trade.entryPrice || trade.price
let exitPrice = trade.exitPrice
let calculatedProfit = trade.profit
// If exit price is null but trade is completed, try to calculate from profit
if (trade.status === 'COMPLETED' && !exitPrice && calculatedProfit !== null && calculatedProfit !== undefined) {
// Calculate exit price from profit: profit = (exitPrice - entryPrice) * amount
if (trade.side === 'BUY') {
exitPrice = entryPrice + (calculatedProfit / trade.amount)
} else {
exitPrice = entryPrice - (calculatedProfit / trade.amount)
}
}
// If profit is null but we have both prices, calculate profit
if (trade.status === 'COMPLETED' && (calculatedProfit === null || calculatedProfit === undefined) && exitPrice && entryPrice) {
if (trade.side === 'BUY') {
calculatedProfit = (exitPrice - entryPrice) * trade.amount
} else {
calculatedProfit = (entryPrice - exitPrice) * trade.amount
}
}
// Determine result based on actual profit
let result = 'ACTIVE'
if (trade.status === 'COMPLETED') {
if (calculatedProfit === null || calculatedProfit === undefined) {
result = 'UNKNOWN' // When we truly don't have enough data
} else if (Math.abs(calculatedProfit) < 0.01) { // Within 1 cent
result = 'BREAKEVEN'
} else if (calculatedProfit > 0) {
result = 'WIN'
} else {
result = 'LOSS'
}
}
return {
id: trade.id,
@@ -98,21 +140,23 @@ export async function GET() {
actualDuration: durationMs,
durationText: formatDuration(durationMinutes) + (trade.status === 'OPEN' ? ' (Active)' : ''),
reason: `REAL: ${trade.side} signal with ${trade.confidence || 75}% confidence`,
entryPrice: trade.entryPrice || trade.price,
exitPrice: trade.exitPrice,
entryPrice: entryPrice,
exitPrice: exitPrice,
currentPrice: trade.status === 'OPEN' ? currentPrice : null,
unrealizedPnl: unrealizedPnL ? unrealizedPnL.toFixed(2) : null,
realizedPnl: realizedPnL ? realizedPnL.toFixed(2) : null,
calculatedProfit: calculatedProfit,
stopLoss: trade.stopLoss || (trade.side === 'BUY' ? (trade.price * 0.98).toFixed(2) : (trade.price * 1.02).toFixed(2)),
takeProfit: trade.takeProfit || (trade.side === 'BUY' ? (trade.price * 1.04).toFixed(2) : (trade.price * 0.96).toFixed(2)),
isActive: trade.status === 'OPEN' || trade.status === 'PENDING',
confidence: trade.confidence || 75,
result: trade.status === 'COMPLETED' ?
((trade.profit || 0) > 0 ? 'WIN' : (trade.profit || 0) < 0 ? 'LOSS' : 'BREAKEVEN') :
'ACTIVE',
result: result,
resultDescription: trade.status === 'COMPLETED' ?
`REAL: ${(trade.profit || 0) > 0 ? 'Profitable' : 'Loss'} ${trade.side} trade - Completed` :
`REAL: ${trade.side} position active - ${formatDuration(durationMinutes)}`
`REAL: ${result === 'WIN' ? 'Profitable' : result === 'LOSS' ? 'Loss' : result} ${trade.side} trade - Completed` :
`REAL: ${trade.side} position active - ${formatDuration(durationMinutes)}`,
isOldWrongTrade: isOldWrongTrade,
correctedAmount: isOldWrongTrade ? correctTokenAmount.toFixed(4) : null,
originalStoredPrice: trade.price
}
})