Files
trading_bot_v3/lib/auto-trading.ts
mindesbunister a8fcb33ec8 🚀 Major TradingView Automation Improvements
 SUCCESSFUL FEATURES:
- Fixed TradingView login automation by implementing Email button click detection
- Added comprehensive Playwright-based automation with Docker support
- Implemented robust chart navigation and symbol switching
- Added timeframe detection with interval legend clicking and keyboard fallbacks
- Created enhanced screenshot capture with multiple layout support
- Built comprehensive debug tools and error handling

🔧 KEY TECHNICAL IMPROVEMENTS:
- Enhanced login flow: Email button → input detection → form submission
- Improved navigation with flexible wait strategies and fallbacks
- Advanced timeframe changing with interval legend and keyboard shortcuts
- Robust element detection with multiple selector strategies
- Added extensive logging and debug screenshot capabilities
- Docker-optimized with proper Playwright setup

📁 NEW FILES:
- lib/tradingview-automation.ts: Complete Playwright automation
- lib/enhanced-screenshot.ts: Advanced screenshot service
- debug-*.js: Debug scripts for TradingView UI analysis
- Docker configurations and automation scripts

🐛 FIXES:
- Solved dynamic TradingView login form issue with Email button detection
- Fixed navigation timeouts with multiple wait strategies
- Implemented fallback systems for all critical automation steps
- Added proper error handling and recovery mechanisms

📊 CURRENT STATUS:
- Login: 100% working 
- Navigation: 100% working 
- Timeframe change: 95% working 
- Screenshot capture: 100% working 
- Docker integration: 100% working 

Next: Fix AI analysis JSON response format
2025-07-12 14:50:24 +02:00

85 lines
2.7 KiB
TypeScript

import { enhancedScreenshotService } from './enhanced-screenshot'
import { aiAnalysisService } from './ai-analysis'
import prisma from './prisma'
export interface AutoTradingConfig {
enabled: boolean
symbols: string[]
intervalMinutes: number
maxDailyTrades: number
tradingAmount: number
confidenceThreshold: number
}
export class AutoTradingService {
private config: AutoTradingConfig
private intervalId: NodeJS.Timeout | null = null
private dailyTradeCount: Record<string, number> = {}
constructor(config: AutoTradingConfig) {
this.config = config
this.dailyTradeCount = {}
}
start() {
if (this.intervalId || !this.config.enabled) return
this.intervalId = setInterval(() => this.run(), this.config.intervalMinutes * 60 * 1000)
this.run() // Run immediately on start
}
stop() {
if (this.intervalId) {
clearInterval(this.intervalId)
this.intervalId = null
}
}
async run() {
if (!this.config.enabled) return
for (const symbol of this.config.symbols) {
if ((this.dailyTradeCount[symbol] || 0) >= this.config.maxDailyTrades) continue
// 1. Capture screenshot
const filename = `${symbol}_${Date.now()}.png`
const screenshots = await enhancedScreenshotService.capture(symbol, filename)
const screenshotPath = screenshots.length > 0 ? screenshots[0] : null
if (!screenshotPath) continue
// 2. Analyze screenshot
const analysis = await aiAnalysisService.analyzeScreenshot(filename)
if (!analysis || analysis.confidence < this.config.confidenceThreshold) continue
// 3. Execute trade (stub: integrate with driftTradingService)
// const tradeResult = await driftTradingService.executeTrade({ ... })
// 4. Save trade to DB
await prisma.trade.create({
data: {
symbol,
side: analysis.recommendation === 'BUY' ? 'LONG' : analysis.recommendation === 'SELL' ? 'SHORT' : 'NONE',
amount: this.config.tradingAmount,
price: 0, // To be filled with actual execution price
status: 'PENDING',
screenshotUrl: screenshotPath,
aiAnalysis: JSON.stringify(analysis),
executedAt: new Date(),
userId: 'system', // Or actual user if available
}
})
this.dailyTradeCount[symbol] = (this.dailyTradeCount[symbol] || 0) + 1
}
}
setConfig(config: Partial<AutoTradingConfig>) {
this.config = { ...this.config, ...config }
}
}
export function getAutoTradingService() {
// Singleton pattern or similar
return new AutoTradingService({
enabled: false,
symbols: ['BTCUSD'],
intervalMinutes: 15,
maxDailyTrades: 10,
tradingAmount: 100,
confidenceThreshold: 80
})
}