diff --git a/app/agents/trader.ts b/app/agents/trader.ts index b74e6a3..d690521 100644 --- a/app/agents/trader.ts +++ b/app/agents/trader.ts @@ -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]; } } diff --git a/app/components/__tests__/TradingViewChart.test.tsx b/app/components/__tests__/TradingViewChart.test.tsx index c28de67..aae60ea 100644 --- a/app/components/__tests__/TradingViewChart.test.tsx +++ b/app/components/__tests__/TradingViewChart.test.tsx @@ -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(), });