fix: timeframe handling and progress tracking improvements
- Fix timeframe parameter handling in enhanced-screenshot API route - Support both 'timeframe' (singular) and 'timeframes' (array) parameters - Add proper sessionId propagation for real-time progress tracking - Enhance MACD analysis prompt with detailed crossover definitions - Add progress tracker service with Server-Sent Events support - Fix Next.js build errors in chart components (module variable conflicts) - Change dev environment port from 9000:3000 to 9001:3000 - Improve AI analysis layout detection logic - Add comprehensive progress tracking through all service layers
This commit is contained in:
@@ -3,6 +3,7 @@ import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { enhancedScreenshotService, ScreenshotConfig } from './enhanced-screenshot'
|
||||
import { TradingViewCredentials } from './tradingview-automation'
|
||||
import { progressTracker } from './progress-tracker'
|
||||
|
||||
const openai = new OpenAI({
|
||||
apiKey: process.env.OPENAI_API_KEY,
|
||||
@@ -70,16 +71,61 @@ export interface AnalysisResult {
|
||||
}
|
||||
|
||||
export class AIAnalysisService {
|
||||
async analyzeScreenshot(filename: string): Promise<AnalysisResult | null> {
|
||||
async analyzeScreenshot(filenameOrPath: string): Promise<AnalysisResult | null> {
|
||||
try {
|
||||
const screenshotsDir = path.join(process.cwd(), 'screenshots')
|
||||
const imagePath = path.join(screenshotsDir, filename)
|
||||
let imagePath: string
|
||||
|
||||
// Check if it's already a full path or just a filename
|
||||
if (path.isAbsolute(filenameOrPath)) {
|
||||
// It's already a full path
|
||||
imagePath = filenameOrPath
|
||||
} else {
|
||||
// It's just a filename, construct the full path
|
||||
const screenshotsDir = path.join(process.cwd(), 'screenshots')
|
||||
imagePath = path.join(screenshotsDir, filenameOrPath)
|
||||
}
|
||||
|
||||
// Read image file
|
||||
const imageBuffer = await fs.readFile(imagePath)
|
||||
const base64Image = imageBuffer.toString('base64')
|
||||
|
||||
const prompt = `You are now a professional trading assistant. You behave with the precision and decisiveness of a top proprietary desk trader. No vagueness, no fluff.
|
||||
|
||||
⚠️ CRITICAL RSI READING INSTRUCTION: The RSI indicator shows a numerical value AND a line position. IGNORE the number if it conflicts with the visual line position. If the RSI line appears in the top area of the indicator (above the 70 horizontal line), report it as OVERBOUGHT regardless of what number is displayed.
|
||||
|
||||
**CRITICAL: FIRST IDENTIFY THE LAYOUT TYPE**
|
||||
|
||||
Before analyzing any indicators, you MUST determine which layout you are looking at:
|
||||
|
||||
**AI Layout identification:**
|
||||
- Has RSI at the TOP of the chart
|
||||
- Has MACD at the BOTTOM of the chart
|
||||
- Has EMAs (9, 20, 50, 200) visible on the main chart
|
||||
- Does NOT have VWAP or OBV
|
||||
|
||||
**DIY Layout identification:**
|
||||
- Has Stochastic RSI at the TOP of the chart
|
||||
- Has OBV (On-Balance Volume) at the BOTTOM of the chart
|
||||
- Has VWAP (thick line) visible on the main chart
|
||||
- Does NOT have regular RSI or MACD
|
||||
|
||||
**LAYOUT-SPECIFIC INDICATOR INFORMATION:**
|
||||
|
||||
If this is an AI Layout screenshot, it contains:
|
||||
- TOP: RSI indicator (overbought above 70, oversold below 30)
|
||||
- MIDDLE (on chart): SVP, ATR Bands, EMA 9, EMA 20, EMA 50, EMA 200
|
||||
- BOTTOM: MACD indicator (NOT AT TOP - this is at the bottom of the chart)
|
||||
* MACD has two lines: MACD line (usually blue/faster) and Signal line (usually red/slower)
|
||||
* Bullish crossover = MACD line crosses ABOVE signal line (upward momentum)
|
||||
* Bearish crossover = MACD line crosses BELOW signal line (downward momentum)
|
||||
* Histogram bars: Green = bullish momentum, Red = bearish momentum
|
||||
* Zero line: Above = overall bullish trend, Below = overall bearish trend
|
||||
|
||||
If this is a DIY Module Layout screenshot, it contains:
|
||||
- TOP: Stochastic RSI indicator
|
||||
- MIDDLE (on chart): VWAP, Smart Money Concepts by Algo
|
||||
- BOTTOM: OBV (On-Balance Volume) indicator
|
||||
|
||||
**TRADING ANALYSIS REQUIREMENTS:**
|
||||
|
||||
1. **TIMEFRAME RISK ASSESSMENT**: Based on the timeframe shown in the screenshot, adjust risk accordingly:
|
||||
@@ -104,9 +150,23 @@ export class AIAnalysisService {
|
||||
|
||||
4. **CONFIRMATION TRIGGERS**: Exact signals to wait for:
|
||||
- Specific candle patterns, indicator crosses, volume confirmations
|
||||
- RSI behavior: "If RSI crosses above 70 while price is under resistance → wait"
|
||||
- RSI/Stoch RSI behavior:
|
||||
* MANDATORY: State if RSI is "OVERBOUGHT" (line above 70), "OVERSOLD" (line below 30), or "NEUTRAL" (between 30-70)
|
||||
* Do NOT say "above 50 line" - only report overbought/oversold/neutral status
|
||||
* If RSI line appears in upper area of indicator box, it's likely overbought regardless of number
|
||||
- VWAP: "If price retakes VWAP with bullish momentum → consider invalidation"
|
||||
- OBV: "If OBV starts climbing while price stays flat → early exit or reconsider bias"
|
||||
- MACD: Analyze MACD crossovers at the BOTTOM indicator panel.
|
||||
* Bullish crossover = MACD line (faster line) crosses ABOVE signal line (slower line) - indicates upward momentum
|
||||
* Bearish crossover = MACD line crosses BELOW signal line - indicates downward momentum
|
||||
* Histogram: Green bars = increasing bullish momentum, Red bars = increasing bearish momentum
|
||||
* Report specific crossover direction and current momentum state
|
||||
- EMA alignment: Check 9/20/50/200 EMA positioning and price relationship
|
||||
- Smart Money Concepts: Identify supply/demand zones and market structure
|
||||
|
||||
5. **LAYOUT-SPECIFIC ANALYSIS**:
|
||||
- AI Layout: Focus on RSI momentum (MUST identify overbought/oversold status), EMA alignment, MACD signals, and ATR bands for volatility
|
||||
- DIY Layout: Emphasize VWAP positioning, Stoch RSI oversold/overbought levels, OBV volume confirmation, and Smart Money Concepts structure
|
||||
|
||||
Examine the chart and identify:
|
||||
- Current price action and trend direction
|
||||
@@ -118,6 +178,7 @@ Examine the chart and identify:
|
||||
Provide your analysis in this exact JSON format (replace values with your analysis):
|
||||
|
||||
{
|
||||
"layoutDetected": "AI Layout|DIY Layout",
|
||||
"summary": "Objective technical analysis with timeframe risk assessment and specific trading setup",
|
||||
"marketSentiment": "BULLISH|BEARISH|NEUTRAL",
|
||||
"keyLevels": {
|
||||
@@ -153,10 +214,14 @@ Provide your analysis in this exact JSON format (replace values with your analys
|
||||
"riskToReward": "1:2",
|
||||
"confirmationTrigger": "Specific signal: Bearish engulfing candle on rejection from VWAP zone with RSI under 50",
|
||||
"indicatorAnalysis": {
|
||||
"rsi": "Specific RSI level and precise interpretation with action triggers",
|
||||
"vwap": "VWAP relationship to price with exact invalidation levels",
|
||||
"obv": "Volume analysis with specific behavioral expectations",
|
||||
"macd": "MACD signal line crosses and momentum analysis"
|
||||
"rsi": "ONLY if AI Layout detected: RSI status - MUST be 'OVERBOUGHT' (above 70 line), 'OVERSOLD' (below 30 line), or 'NEUTRAL' (30-70). Do NOT reference 50 line position.",
|
||||
"stochRsi": "ONLY if DIY Layout detected: Stochastic RSI oversold/overbought conditions - check both %K and %D lines",
|
||||
"vwap": "ONLY if DIY Layout detected: VWAP relationship to price with exact invalidation levels",
|
||||
"obv": "ONLY if DIY Layout detected: OBV volume analysis with specific behavioral expectations",
|
||||
"macd": "ONLY if AI Layout detected: MACD analysis - The MACD is located at the BOTTOM of the chart. Analyze: 1) Histogram bars (green = bullish momentum, red = bearish), 2) Signal line crossover (MACD line crossing ABOVE signal line = bullish crossover, BELOW = bearish crossover), 3) Zero line position. Report specific crossover direction and current momentum state.",
|
||||
"emaAlignment": "If AI Layout: EMA 9/20/50/200 positioning and price relationship - note stack order and price position",
|
||||
"atrBands": "If AI Layout: ATR bands for volatility and support/resistance",
|
||||
"smartMoney": "If DIY Layout: Smart Money Concepts supply/demand zones and structure"
|
||||
},
|
||||
"timeframeRisk": {
|
||||
"assessment": "Risk level based on detected timeframe",
|
||||
@@ -245,14 +310,23 @@ Return only the JSON object with your technical analysis.`
|
||||
}
|
||||
}
|
||||
|
||||
async analyzeMultipleScreenshots(filenames: string[]): Promise<AnalysisResult | null> {
|
||||
async analyzeMultipleScreenshots(filenamesOrPaths: string[]): Promise<AnalysisResult | null> {
|
||||
try {
|
||||
const screenshotsDir = path.join(process.cwd(), 'screenshots')
|
||||
|
||||
// Read all image files and convert to base64
|
||||
const images = await Promise.all(
|
||||
filenames.map(async (filename) => {
|
||||
const imagePath = path.join(screenshotsDir, filename)
|
||||
filenamesOrPaths.map(async (filenameOrPath) => {
|
||||
let imagePath: string
|
||||
|
||||
// Check if it's already a full path or just a filename
|
||||
if (path.isAbsolute(filenameOrPath)) {
|
||||
// It's already a full path
|
||||
imagePath = filenameOrPath
|
||||
} else {
|
||||
// It's just a filename, construct the full path
|
||||
const screenshotsDir = path.join(process.cwd(), 'screenshots')
|
||||
imagePath = path.join(screenshotsDir, filenameOrPath)
|
||||
}
|
||||
|
||||
const imageBuffer = await fs.readFile(imagePath)
|
||||
const base64Image = imageBuffer.toString('base64')
|
||||
return {
|
||||
@@ -265,15 +339,40 @@ Return only the JSON object with your technical analysis.`
|
||||
})
|
||||
)
|
||||
|
||||
const layoutInfo = filenames.map(f => {
|
||||
if (f.includes('_ai_')) return 'AI Layout'
|
||||
const layoutInfo = filenamesOrPaths.map(f => {
|
||||
const filename = path.basename(f) // Extract filename from path
|
||||
if (filename.includes('_ai_')) return 'AI Layout'
|
||||
if (f.includes('_diy_') || f.includes('_Diy module_')) return 'DIY Module Layout'
|
||||
return 'Unknown Layout'
|
||||
}).join(' and ')
|
||||
|
||||
const prompt = `You are now a professional trading assistant. You behave with the precision and decisiveness of a top proprietary desk trader. No vagueness, no fluff.
|
||||
|
||||
I'm providing you with ${filenames.length} TradingView chart screenshots from different layouts: ${layoutInfo}.
|
||||
I'm providing you with ${filenamesOrPaths.length} TradingView chart screenshots from different layouts: ${layoutInfo}.
|
||||
|
||||
⚠️ CRITICAL RSI READING INSTRUCTION: The RSI indicator shows a numerical value AND a line position. IGNORE the number if it conflicts with the visual line position. If the RSI line appears in the top area of the indicator (above the 70 horizontal line), report it as OVERBOUGHT regardless of what number is displayed.
|
||||
|
||||
**LAYOUT-SPECIFIC INDICATOR INFORMATION:**
|
||||
|
||||
**AI Layout Structure:**
|
||||
- TOP: RSI indicator (14-period) - Look for EXACT numerical value displayed and visual position relative to 30/50/70 levels
|
||||
- MIDDLE (on chart): SVP, ATR Bands, EMA 9 (yellow), EMA 20 (orange), EMA 50 (blue), EMA 200 (red)
|
||||
- BOTTOM: MACD indicator with signal line and histogram
|
||||
|
||||
**DIY Module Layout Structure:**
|
||||
- TOP: Stochastic RSI indicator - Check both %K and %D lines relative to 20/50/80 levels
|
||||
- MIDDLE (on chart): VWAP (thick line), Smart Money Concepts by Algo (supply/demand zones)
|
||||
- BOTTOM: OBV (On-Balance Volume) indicator showing volume flow
|
||||
|
||||
**CRITICAL: ACCURATE INDICATOR READING:**
|
||||
- RSI: IGNORE the numerical value if it conflicts with visual position. The RSI line position on the chart is what matters:
|
||||
* If RSI line is visually ABOVE the 70 horizontal line = OVERBOUGHT (regardless of number shown)
|
||||
* If RSI line is visually BELOW the 30 horizontal line = OVERSOLD (regardless of number shown)
|
||||
* If RSI line is between 30-70 = NEUTRAL zone
|
||||
* Example: If number shows "56.61" but line appears above 70 level, report as "RSI OVERBOUGHT at 70+ level"
|
||||
- MACD: Check histogram bars (green/red) and signal line crossovers
|
||||
- EMA Alignment: Note price position relative to each EMA and EMA stack order
|
||||
- VWAP: Identify if price is above/below VWAP and by how much
|
||||
|
||||
**TRADING ANALYSIS REQUIREMENTS:**
|
||||
|
||||
@@ -299,12 +398,23 @@ I'm providing you with ${filenames.length} TradingView chart screenshots from di
|
||||
|
||||
4. **CONFIRMATION TRIGGERS**: Exact signals to wait for:
|
||||
- Specific candle patterns, indicator crosses, volume confirmations
|
||||
- RSI behavior: "If RSI crosses above 70 while price is under resistance → wait"
|
||||
- RSI/Stoch RSI behavior: "If RSI crosses above 70 while price is under resistance → wait"
|
||||
* CRITICAL: Read RSI visually - if the line appears above 70 level regardless of numerical display, treat as overbought
|
||||
* If RSI line appears below 30 level visually, treat as oversold regardless of number shown
|
||||
- VWAP: "If price retakes VWAP with bullish momentum → consider invalidation"
|
||||
- OBV: "If OBV starts climbing while price stays flat → early exit or reconsider bias"
|
||||
- MACD: Analyze MACD crossovers at the BOTTOM indicator panel.
|
||||
* Bullish crossover = MACD line (faster line) crosses ABOVE signal line (slower line) - indicates upward momentum
|
||||
* Bearish crossover = MACD line crosses BELOW signal line - indicates downward momentum
|
||||
* Histogram: Green bars = increasing bullish momentum, Red bars = increasing bearish momentum
|
||||
* Report specific crossover direction and current momentum state
|
||||
- EMA alignment: Check 9/20/50/200 EMA positioning and price relationship
|
||||
- Smart Money Concepts: Identify supply/demand zones and market structure
|
||||
|
||||
5. **CROSS-LAYOUT ANALYSIS**:
|
||||
- Compare insights from different chart layouts for confirmations/contradictions
|
||||
- AI Layout insights: RSI momentum + EMA alignment + MACD signals
|
||||
- DIY Layout insights: VWAP positioning + Stoch RSI + OBV volume + Smart Money structure
|
||||
- Use multiple perspectives to increase confidence
|
||||
- Note which layout provides clearest signals
|
||||
|
||||
@@ -348,10 +458,14 @@ I'm providing you with ${filenames.length} TradingView chart screenshots from di
|
||||
"riskToReward": "1:2.5",
|
||||
"confirmationTrigger": "Specific signal: Bearish engulfing candle on rejection from VWAP zone with RSI under 50",
|
||||
"indicatorAnalysis": {
|
||||
"rsi": "Specific RSI level and precise interpretation with action triggers",
|
||||
"vwap": "VWAP relationship to price with exact invalidation levels",
|
||||
"obv": "Volume analysis with specific behavioral expectations",
|
||||
"macd": "MACD signal line crosses and momentum analysis",
|
||||
"rsi": "AI Layout: RSI status - MUST be 'OVERBOUGHT' (above 70 line), 'OVERSOLD' (below 30 line), or 'NEUTRAL' (30-70). Do NOT reference 50 line position.",
|
||||
"stochRsi": "DIY Layout: Stochastic RSI oversold/overbought conditions",
|
||||
"vwap": "DIY Layout: VWAP relationship to price with exact invalidation levels",
|
||||
"obv": "DIY Layout: OBV volume analysis with specific behavioral expectations",
|
||||
"macd": "AI Layout: MACD signal line crosses and histogram momentum analysis - green/red bars and signal line position",
|
||||
"emaAlignment": "AI Layout: EMA 9/20/50/200 positioning and price relationship - note stack order and price position",
|
||||
"atrBands": "AI Layout: ATR bands for volatility and support/resistance",
|
||||
"smartMoney": "DIY Layout: Smart Money Concepts supply/demand zones and structure",
|
||||
"crossLayoutConsensus": "Detailed comparison of how different layouts confirm or contradict signals"
|
||||
},
|
||||
"layoutComparison": {
|
||||
@@ -384,7 +498,7 @@ Analyze all provided screenshots comprehensively and return only the JSON respon
|
||||
}
|
||||
]
|
||||
|
||||
console.log(`🤖 Sending ${filenames.length} screenshots to OpenAI for multi-layout analysis...`)
|
||||
console.log(`🤖 Sending ${filenamesOrPaths.length} screenshots to OpenAI for multi-layout analysis...`)
|
||||
|
||||
const response = await openai.chat.completions.create({
|
||||
model: "gpt-4o-mini", // Cost-effective model with vision capabilities
|
||||
@@ -491,10 +605,12 @@ Analyze all provided screenshots comprehensively and return only the JSON respon
|
||||
screenshots: string[]
|
||||
analysis: AnalysisResult | null
|
||||
}> {
|
||||
const { sessionId } = config
|
||||
|
||||
try {
|
||||
console.log(`Starting automated capture with config for ${config.symbol} ${config.timeframe}`)
|
||||
|
||||
// Capture screenshots using enhanced service
|
||||
// Capture screenshots using enhanced service (this will handle its own progress)
|
||||
const screenshots = await enhancedScreenshotService.captureWithLogin(config)
|
||||
|
||||
if (screenshots.length === 0) {
|
||||
@@ -503,6 +619,11 @@ Analyze all provided screenshots comprehensively and return only the JSON respon
|
||||
|
||||
console.log(`${screenshots.length} screenshot(s) captured`)
|
||||
|
||||
// Add AI analysis step to progress if sessionId exists
|
||||
if (sessionId) {
|
||||
progressTracker.updateStep(sessionId, 'analysis', 'active', 'Running AI analysis on screenshots...')
|
||||
}
|
||||
|
||||
let analysis: AnalysisResult | null = null
|
||||
|
||||
if (screenshots.length === 1) {
|
||||
@@ -514,11 +635,20 @@ Analyze all provided screenshots comprehensively and return only the JSON respon
|
||||
}
|
||||
|
||||
if (!analysis) {
|
||||
if (sessionId) {
|
||||
progressTracker.updateStep(sessionId, 'analysis', 'error', 'AI analysis failed to generate results')
|
||||
}
|
||||
throw new Error('Failed to analyze screenshots')
|
||||
}
|
||||
|
||||
console.log(`Analysis completed for ${config.symbol} ${config.timeframe}`)
|
||||
|
||||
if (sessionId) {
|
||||
progressTracker.updateStep(sessionId, 'analysis', 'completed', 'AI analysis completed successfully!')
|
||||
// Mark session as complete
|
||||
setTimeout(() => progressTracker.deleteSession(sessionId), 1000)
|
||||
}
|
||||
|
||||
return {
|
||||
screenshots,
|
||||
analysis
|
||||
@@ -526,6 +656,20 @@ Analyze all provided screenshots comprehensively and return only the JSON respon
|
||||
|
||||
} catch (error) {
|
||||
console.error('Automated capture and analysis with config failed:', error)
|
||||
|
||||
if (sessionId) {
|
||||
// Find the active step and mark it as error
|
||||
const progress = progressTracker.getProgress(sessionId)
|
||||
if (progress) {
|
||||
const activeStep = progress.steps.find(step => step.status === 'active')
|
||||
if (activeStep) {
|
||||
progressTracker.updateStep(sessionId, activeStep.id, 'error', error instanceof Error ? error.message : 'Unknown error')
|
||||
}
|
||||
}
|
||||
// Clean up session
|
||||
setTimeout(() => progressTracker.deleteSession(sessionId), 5000)
|
||||
}
|
||||
|
||||
return {
|
||||
screenshots: [],
|
||||
analysis: null
|
||||
|
||||
Reference in New Issue
Block a user