feat: Add persistent settings and multiple layouts support
- Add settings manager to persist symbol, timeframe, and layouts - Support multiple layouts for comprehensive chart analysis - Remove debug screenshots for cleaner logs - Update AI analysis with professional trading prompt - Add multi-screenshot analysis for better trading insights - Update analyze API to use saved settings and multiple layouts
This commit is contained in:
53
app/api/analyze/route.ts
Normal file
53
app/api/analyze/route.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { aiAnalysisService } from '../../../lib/ai-analysis'
|
||||
import { tradingViewCapture } from '../../../lib/tradingview'
|
||||
import { settingsManager } from '../../../lib/settings'
|
||||
import path from 'path'
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const { symbol, layouts, timeframe } = await req.json()
|
||||
|
||||
// Load current settings
|
||||
const settings = await settingsManager.loadSettings()
|
||||
|
||||
// Use provided values or fall back to saved settings
|
||||
const finalSymbol = symbol || settings.symbol
|
||||
const finalTimeframe = timeframe || settings.timeframe
|
||||
const finalLayouts = layouts || settings.layouts
|
||||
|
||||
if (!finalSymbol) {
|
||||
return NextResponse.json({ error: 'Missing symbol' }, { status: 400 })
|
||||
}
|
||||
|
||||
const baseFilename = `${finalSymbol}_${finalTimeframe}_${Date.now()}`
|
||||
const screenshots = await tradingViewCapture.capture(finalSymbol, `${baseFilename}.png`, finalLayouts, finalTimeframe)
|
||||
|
||||
let result
|
||||
if (screenshots.length === 1) {
|
||||
// Single screenshot analysis
|
||||
const filename = path.basename(screenshots[0])
|
||||
result = await aiAnalysisService.analyzeScreenshot(filename)
|
||||
} else {
|
||||
// Multiple screenshots analysis
|
||||
const filenames = screenshots.map(screenshot => path.basename(screenshot))
|
||||
result = await aiAnalysisService.analyzeMultipleScreenshots(filenames)
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
return NextResponse.json({ error: 'Analysis failed' }, { status: 500 })
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
...result,
|
||||
settings: {
|
||||
symbol: finalSymbol,
|
||||
timeframe: finalTimeframe,
|
||||
layouts: finalLayouts
|
||||
},
|
||||
screenshots: screenshots.map(s => path.basename(s))
|
||||
})
|
||||
} catch (e: any) {
|
||||
return NextResponse.json({ error: e.message }, { status: 500 })
|
||||
}
|
||||
}
|
||||
33
app/api/auto-trading/route.ts
Normal file
33
app/api/auto-trading/route.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getAutoTradingService } from '../../../lib/auto-trading'
|
||||
|
||||
const autoTradingService = getAutoTradingService()
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const { action, config } = await req.json()
|
||||
if (action === 'start') {
|
||||
autoTradingService.start()
|
||||
return NextResponse.json({ status: 'started' })
|
||||
}
|
||||
if (action === 'stop') {
|
||||
autoTradingService.stop()
|
||||
return NextResponse.json({ status: 'stopped' })
|
||||
}
|
||||
if (action === 'config' && config) {
|
||||
autoTradingService.setConfig(config)
|
||||
return NextResponse.json({ status: 'config updated', config: autoTradingService })
|
||||
}
|
||||
return NextResponse.json({ error: 'Invalid action' }, { status: 400 })
|
||||
} catch (e: any) {
|
||||
return NextResponse.json({ error: e.message }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
// Return current config/status
|
||||
return NextResponse.json({
|
||||
config: autoTradingService,
|
||||
running: !!autoTradingService['intervalId']
|
||||
})
|
||||
}
|
||||
15
app/api/screenshot/route.ts
Normal file
15
app/api/screenshot/route.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { tradingViewCapture } from '../../../lib/tradingview'
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const { symbol, filename } = await req.json()
|
||||
if (!symbol || !filename) {
|
||||
return NextResponse.json({ error: 'Missing symbol or filename' }, { status: 400 })
|
||||
}
|
||||
const filePath = await tradingViewCapture.capture(symbol, filename)
|
||||
return NextResponse.json({ filePath })
|
||||
} catch (e: any) {
|
||||
return NextResponse.json({ error: e.message }, { status: 500 })
|
||||
}
|
||||
}
|
||||
14
app/api/trading-history/route.ts
Normal file
14
app/api/trading-history/route.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import prisma from '../../../lib/prisma'
|
||||
import { NextResponse } from 'next/server'
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const trades = await prisma.trade.findMany({
|
||||
orderBy: { executedAt: 'desc' },
|
||||
take: 50
|
||||
})
|
||||
return NextResponse.json(trades)
|
||||
} catch (e: any) {
|
||||
return NextResponse.json({ error: e.message }, { status: 500 })
|
||||
}
|
||||
}
|
||||
21
app/api/trading/route.ts
Normal file
21
app/api/trading/route.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { driftTradingService } from '../../../lib/drift-trading'
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const params = await req.json()
|
||||
const result = await driftTradingService.executeTrade(params)
|
||||
return NextResponse.json(result)
|
||||
} catch (e: any) {
|
||||
return NextResponse.json({ error: e.message }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const positions = await driftTradingService.getPositions()
|
||||
return NextResponse.json({ positions })
|
||||
} catch (e: any) {
|
||||
return NextResponse.json({ error: e.message }, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user