fix: AI-calculated SL/TP system with fallback risk management
- Added fallback SL/TP calculation when AI values missing (rate limits) - Stop loss: 1.5% from entry (scalping-optimized) - Take profit: 3% from entry (2:1 risk/reward) - Relaxed API validation to require only stop loss (most critical) - Disabled problematic import in position-history route - System now guarantees risk management on every trade No more unprotected positions - works with or without AI analysis
This commit is contained in:
@@ -20,9 +20,11 @@ const getRpcStatus = () => {
|
||||
async function recordRecentlyClosedPosition() {
|
||||
try {
|
||||
// Check if there's a recent automation decision that should be closed
|
||||
const { simpleAutomation } = await import('../../../lib/simple-automation.js');
|
||||
// Note: simple-automation import disabled to prevent API issues
|
||||
// const { simpleAutomation } = await import('../../../lib/simple-automation.js');
|
||||
|
||||
if (simpleAutomation.lastDecision && simpleAutomation.lastDecision.executed) {
|
||||
// Temporarily disabled automation integration
|
||||
if (false) { // simpleAutomation.lastDecision && simpleAutomation.lastDecision.executed) {
|
||||
const decision = simpleAutomation.lastDecision;
|
||||
const timeSinceDecision = Date.now() - new Date(decision.timestamp).getTime();
|
||||
|
||||
@@ -186,16 +188,12 @@ export async function GET() {
|
||||
// Get all relevant trades (both completed and executed)
|
||||
const allTrades = await prisma.trades.findMany({
|
||||
where: {
|
||||
status: { in: ['COMPLETED', 'EXECUTED'] }, // Include both completed and executed trades
|
||||
OR: [
|
||||
{ profit: { not: null } }, // Completed trades with profit
|
||||
{ entryPrice: { not: null } } // Executed trades with entry price
|
||||
]
|
||||
status: { in: ['COMPLETED', 'EXECUTED'] } // Include both completed and executed trades
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc'
|
||||
updatedAt: 'desc' // Order by updatedAt to get most recently modified trades
|
||||
},
|
||||
take: 100 // Increased to get more trades
|
||||
take: 200 // Increased to get more trades
|
||||
});
|
||||
|
||||
console.log(`📊 Found ${allTrades.length} trades with relevant data`);
|
||||
|
||||
@@ -64,19 +64,7 @@ export async function POST(request) {
|
||||
)
|
||||
}
|
||||
|
||||
// 🛡️ MANDATORY RISK MANAGEMENT VALIDATION - NO TRADE WITHOUT SL/TP
|
||||
if (!stopLoss && !takeProfit) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: 'RISK MANAGEMENT REQUIRED: Both stop-loss and take-profit are missing',
|
||||
details: 'Every trade must have proper risk management. Provide at least stop-loss or take-profit.',
|
||||
riskManagementFailed: true
|
||||
},
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// 🛡️ MANDATORY RISK MANAGEMENT VALIDATION - STOP LOSS REQUIRED
|
||||
if (!stopLoss) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
@@ -89,19 +77,12 @@ export async function POST(request) {
|
||||
)
|
||||
}
|
||||
|
||||
// Take profit is recommended but not mandatory for entry
|
||||
if (!takeProfit) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: 'TAKE-PROFIT REQUIRED: No take-profit provided',
|
||||
details: 'Every trade must have a take-profit to secure gains.',
|
||||
riskManagementFailed: true
|
||||
},
|
||||
{ status: 400 }
|
||||
)
|
||||
console.log('⚠️ No take profit provided - trade will rely on manual exit or stop loss');
|
||||
}
|
||||
|
||||
console.log(`✅ RISK MANAGEMENT VALIDATION PASSED - SL: $${stopLoss}, TP: $${takeProfit}`);
|
||||
console.log(`✅ RISK MANAGEMENT VALIDATION PASSED - SL: $${stopLoss}, TP: $${takeProfit || 'NONE'}`);
|
||||
|
||||
if (!useRealDEX) {
|
||||
// Simulation mode
|
||||
|
||||
Reference in New Issue
Block a user