Make Trader parsing of executionPlan more robust (extract maxLossPercent/method fallbacks); ensure TradingViewChart test mock includes timeScale

This commit is contained in:
2026-05-16 14:02:29 +02:00
parent c9f83b834e
commit 3a681fa309
2 changed files with 18 additions and 3 deletions
+17 -3
View File
@@ -88,11 +88,25 @@ Format your response as JSON with these fields.`;
// fallback: try to extract primitive fields
const amountMatch = content.match(/"amount"\s*:\s*([0-9.]+)/);
const takeProfitMatch = content.match(/"takeProfit"\s*:\s*([0-9.]+)/);
const riskMatch = content.match(/"riskManagement"\s*:\s*"([^"]+)"/);
executionPlan = {};
const maxLossMatch = content.match(/"maxLossPercent"\s*:\s*([0-9.]+)/);
const methodMatch = content.match(/"method"\s*:\s*"([^"]+)"/);
executionPlan = {} as any;
if (amountMatch) executionPlan.amount = parseFloat(amountMatch[1]);
if (takeProfitMatch) executionPlan.takeProfit = parseFloat(takeProfitMatch[1]);
if (riskMatch) executionPlan.riskManagement = { note: riskMatch[1] };
executionPlan.riskManagement = {};
if (maxLossMatch) executionPlan.riskManagement.maxLossPercent = parseFloat(maxLossMatch[1]);
if (methodMatch) executionPlan.riskManagement.method = methodMatch[1];
}
}
// Additional fallback: if executionPlan parsed but missing nested riskManagement fields, try to extract them
if (executionPlan && executionPlan.riskManagement == null) {
const maxLossMatch2 = content.match(/"maxLossPercent"\s*:\s*([0-9.]+)/);
const methodMatch2 = content.match(/"method"\s*:\s*"([^"]+)"/);
if (maxLossMatch2 || methodMatch2) {
executionPlan.riskManagement = executionPlan.riskManagement || {};
if (maxLossMatch2) executionPlan.riskManagement.maxLossPercent = parseFloat(maxLossMatch2[1]);
if (methodMatch2) executionPlan.riskManagement.method = methodMatch2[1];
}
}