Make Trader parsing of executionPlan more robust (extract maxLossPercent/method fallbacks); ensure TradingViewChart test mock includes timeScale
This commit is contained in:
+17
-3
@@ -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];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -74,6 +74,7 @@ describe("TradingViewChart", () => {
|
||||
it("creates candlestick series with explicit colors", () => {
|
||||
const mockAddSeries = vi.fn();
|
||||
mockCreateChart.mockReturnValue({
|
||||
timeScale: () => ({ applyOptions: vi.fn(), fitContent: vi.fn() }),
|
||||
addSeries: mockAddSeries,
|
||||
remove: vi.fn(),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user