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:
@@ -568,7 +568,7 @@ ${validResults.map(r => `• ${r.timeframe}: ${r.analysis?.recommendation} (${r.
|
||||
}
|
||||
|
||||
// Calculate position size based on risk percentage
|
||||
const positionSize = this.calculatePositionSize(analysis)
|
||||
const positionSize = await this.calculatePositionSize(analysis)
|
||||
|
||||
return {
|
||||
direction: analysis.recommendation,
|
||||
@@ -585,12 +585,33 @@ ${validResults.map(r => `• ${r.timeframe}: ${r.analysis?.recommendation} (${r.
|
||||
}
|
||||
}
|
||||
|
||||
private calculatePositionSize(analysis: any): number {
|
||||
const baseAmount = this.config!.tradingAmount
|
||||
private async calculatePositionSize(analysis: any): Promise<number> {
|
||||
const baseAmount = this.config!.tradingAmount // This is the USD amount to invest
|
||||
const riskAdjustment = this.config!.riskPercentage / 100
|
||||
const confidenceAdjustment = analysis.confidence / 100
|
||||
|
||||
return baseAmount * riskAdjustment * confidenceAdjustment
|
||||
// Calculate the USD amount to invest
|
||||
const usdAmount = baseAmount * riskAdjustment * confidenceAdjustment
|
||||
|
||||
// Get current price to convert USD to token amount
|
||||
let currentPrice = analysis.entry?.price || analysis.currentPrice
|
||||
|
||||
if (!currentPrice) {
|
||||
try {
|
||||
const { default: PriceFetcher } = await import('./price-fetcher')
|
||||
currentPrice = await PriceFetcher.getCurrentPrice(this.config?.symbol || 'SOLUSD')
|
||||
console.log(`📊 Using current ${this.config?.symbol || 'SOLUSD'} price for position size: $${currentPrice}`)
|
||||
} catch (error) {
|
||||
console.error('Error fetching price for position size, using fallback:', error)
|
||||
currentPrice = this.config?.symbol === 'SOLUSD' ? 189 : 100
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate token amount: USD investment / token price
|
||||
const tokenAmount = usdAmount / currentPrice
|
||||
console.log(`💰 Position calculation: $${usdAmount} ÷ $${currentPrice} = ${tokenAmount.toFixed(4)} tokens`)
|
||||
|
||||
return tokenAmount
|
||||
}
|
||||
|
||||
private calculateStopLoss(analysis: any): number {
|
||||
@@ -599,7 +620,7 @@ ${validResults.map(r => `• ${r.timeframe}: ${r.analysis?.recommendation} (${r.
|
||||
return analysis.stopLoss.price
|
||||
}
|
||||
|
||||
const currentPrice = analysis.entry?.price || 150 // Default SOL price
|
||||
const currentPrice = analysis.entry?.price || 189 // Current SOL price
|
||||
const stopLossPercent = this.config!.stopLossPercent / 100
|
||||
|
||||
if (analysis.recommendation === 'BUY') {
|
||||
@@ -656,7 +677,21 @@ ${validResults.map(r => `• ${r.timeframe}: ${r.analysis?.recommendation} (${r.
|
||||
|
||||
private async executeSimulationTrade(decision: any): Promise<any> {
|
||||
// Simulate trade execution with realistic parameters
|
||||
const currentPrice = decision.currentPrice || 100 // Mock price
|
||||
let currentPrice = decision.currentPrice
|
||||
|
||||
// If no current price provided, fetch real price
|
||||
if (!currentPrice) {
|
||||
try {
|
||||
const { default: PriceFetcher } = await import('./price-fetcher')
|
||||
currentPrice = await PriceFetcher.getCurrentPrice(this.config?.symbol || 'SOLUSD')
|
||||
console.log(`📊 Fetched real ${this.config?.symbol || 'SOLUSD'} price: $${currentPrice}`)
|
||||
} catch (error) {
|
||||
console.error('Error fetching real price, using fallback:', error)
|
||||
// Use a more realistic fallback based on symbol
|
||||
currentPrice = this.config?.symbol === 'SOLUSD' ? 189 : 100
|
||||
}
|
||||
}
|
||||
|
||||
const slippage = Math.random() * 0.005 // 0-0.5% slippage
|
||||
const executionPrice = currentPrice * (1 + (Math.random() > 0.5 ? slippage : -slippage))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user