- Add comprehensive setup guide (VIRTUAL_TRADING_SETUP_GUIDE.md) - Improve UI to clearly show required steps for AI learning - Make auto-execute toggle always visible with clear instructions - Add blue info panel explaining the learning setup process - User can now easily enable: Continuous Learning + Auto-Execute - Virtual trades will execute automatically and AI will learn from outcomes Resolves issue: AI analyzing without learning due to missing virtual trade execution
130 lines
4.3 KiB
JavaScript
130 lines
4.3 KiB
JavaScript
import { NextResponse } from "next/server";
|
||
|
||
export async function POST(request) {
|
||
try {
|
||
console.log("🛡️ SAFE PAPER TRADING API: Starting REAL analysis (paper trading only)...");
|
||
|
||
const body = await request.json();
|
||
const {
|
||
symbol = "SOLUSD",
|
||
selectedTimeframes = ["60"],
|
||
timeframe = "60",
|
||
mode,
|
||
paperTrading,
|
||
isolatedMode,
|
||
isContinuous = false
|
||
} = body;
|
||
|
||
const timeframesToAnalyze = selectedTimeframes.length > 0 ? selectedTimeframes : [timeframe];
|
||
|
||
if (mode !== "PAPER_ONLY" || !paperTrading || !isolatedMode) {
|
||
return NextResponse.json({
|
||
success: false,
|
||
error: "SAFETY VIOLATION: This API only supports isolated paper trading"
|
||
}, { status: 403 });
|
||
}
|
||
|
||
console.log(`📊 Getting REAL market analysis for ${symbol} on timeframes: ${timeframesToAnalyze.join(", ")} (${isContinuous ? "continuous learning" : "manual"})`);
|
||
|
||
const analysis = await getRealAnalysis({ symbol, selectedTimeframes: timeframesToAnalyze, isContinuous });
|
||
|
||
console.log("✅ Safe paper analysis complete - REAL DATA, NO TRADING RISK");
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
analysis: {
|
||
...analysis,
|
||
paperTrading: true,
|
||
isolated: true,
|
||
noRealTrading: true,
|
||
realData: true,
|
||
source: "REAL_MARKET_ANALYSIS",
|
||
timeframes: timeframesToAnalyze,
|
||
analysisMode: isContinuous ? "CONTINUOUS_LEARNING" : "MANUAL"
|
||
},
|
||
safety: {
|
||
paperTrading: true,
|
||
isolated: true,
|
||
noRealTrading: true,
|
||
realData: true,
|
||
source: "REAL_MARKET_ANALYSIS"
|
||
},
|
||
timestamp: new Date().toISOString()
|
||
});
|
||
|
||
} catch (error) {
|
||
console.error("❌ Safe paper trading API error:", error);
|
||
|
||
return NextResponse.json({
|
||
success: false,
|
||
error: `Real analysis failed: ${error.message}`,
|
||
details: "Paper trading requires real market data. Please try again.",
|
||
realDataOnly: true
|
||
}, { status: 500 });
|
||
}
|
||
}
|
||
|
||
async function getRealAnalysis(config) {
|
||
try {
|
||
const { symbol, selectedTimeframes, isContinuous = false } = config;
|
||
|
||
const primaryTimeframe = selectedTimeframes[0];
|
||
console.log(`<EFBFBD> Attempting to get real analysis for ${symbol} ${primaryTimeframe}m (${selectedTimeframes.length} timeframes)...`);
|
||
|
||
const analysisUrl = `http://localhost:3000/api/ai-analysis/latest?symbol=${symbol}&timeframe=${primaryTimeframe}`;
|
||
console.log(`📡 Calling: ${analysisUrl}`);
|
||
|
||
const response = await fetch(analysisUrl, {
|
||
headers: {
|
||
"Content-Type": "application/json"
|
||
},
|
||
signal: AbortSignal.timeout(180000)
|
||
});
|
||
|
||
console.log(`📡 Response status: ${response.status}`);
|
||
|
||
if (!response.ok) {
|
||
const errorText = await response.text();
|
||
console.error(`❌ Analysis API error: ${response.status} - ${errorText}`);
|
||
throw new Error(`Analysis API returned ${response.status}: ${errorText}`);
|
||
}
|
||
|
||
const data = await response.json();
|
||
console.log(`📊 Raw analysis response received`);
|
||
|
||
if (!data.success || !data.data?.analysis) {
|
||
console.error("❌ No analysis data in response:", data);
|
||
throw new Error("No analysis data received from API");
|
||
}
|
||
|
||
const realAnalysis = data.data.analysis;
|
||
console.log(`✅ Real analysis received: ${realAnalysis.recommendation} with ${realAnalysis.confidence}% confidence`);
|
||
|
||
return {
|
||
...realAnalysis,
|
||
paperTrading: true,
|
||
isolated: true,
|
||
noRealTrading: true,
|
||
realData: true,
|
||
source: "REAL_MARKET_ANALYSIS",
|
||
timeframes: selectedTimeframes,
|
||
primaryTimeframe: primaryTimeframe,
|
||
analysisMode: isContinuous ? "CONTINUOUS_LEARNING" : "MANUAL",
|
||
reasoning: `PAPER TRADING - REAL MARKET ANALYSIS${isContinuous ? " (Continuous Learning)" : ""}:
|
||
|
||
Multi-Timeframe Analysis: ${selectedTimeframes.join(", ")}
|
||
Primary Focus: ${primaryTimeframe}m
|
||
|
||
${realAnalysis.reasoning || "Real market analysis completed"}
|
||
|
||
SAFETY: This is paper trading only - no real trades will be executed.
|
||
${isContinuous ? "<22> LEARNING: System is continuously learning from market patterns." : ""}`
|
||
};
|
||
|
||
} catch (error) {
|
||
console.error("❌ Failed to get real analysis:", error.message);
|
||
console.error("❌ Error details:", error);
|
||
throw error;
|
||
}
|
||
}
|