Add execution plan for sell decisions: amount, risk management, take-profit; include execution step in TradingGraph workflow

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-05-16 13:50:28 +02:00
parent 17c9ee27c0
commit 98c1e366a5
3 changed files with 61 additions and 6 deletions
+32 -3
View File
@@ -37,16 +37,21 @@ ${signalSummaries}
Debate Rounds:
${debateSummaries}
Based on all the information above, make a trading decision. Respond with:
Based on all the information above, make a trading decision. Respond with JSON containing these fields:
- action: "buy", "sell", or "hold"
- confidence: a number between 0 and 1
- reasoning: brief explanation
If the action is "sell", also include an "executionPlan" object with:
- amount: number (shares to sell)
- riskManagement: object (e.g., { maxLossPercent: 2 })
- takeProfit: number (target take-profit price)
Format your response as JSON with these fields.`;
const response = await this.client.createChatCompletion(
[
{ role: "system", content: "You are a trading agent that makes buy/sell/hold decisions based on all available signals." },
{ role: "system", content: "You are a trading agent that makes buy/sell/hold decisions and provides execution guidance when selling." },
{ role: "user", content: prompt },
],
this.model
@@ -57,6 +62,7 @@ Format your response as JSON with these fields.`;
let action: 'buy' | 'sell' | 'hold' = 'hold';
let confidence = 0.5;
let reasoning = content;
let executionPlan: any | undefined;
const actionMatch = content.match(/"action"\s*:\s*"(buy|sell|hold)"/);
if (actionMatch) {
@@ -73,12 +79,35 @@ Format your response as JSON with these fields.`;
reasoning = reasoningMatch[1];
}
return {
// Try to parse executionPlan if provided in JSON
const execMatch = content.match(/"executionPlan"\s*:\s*(\{[\s\S]*\})/);
if (execMatch) {
try {
executionPlan = JSON.parse(execMatch[1]);
} catch (err) {
// 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 = {};
if (amountMatch) executionPlan.amount = parseFloat(amountMatch[1]);
if (takeProfitMatch) executionPlan.takeProfit = parseFloat(takeProfitMatch[1]);
if (riskMatch) executionPlan.riskManagement = { note: riskMatch[1] };
}
}
const decision: TradingDecision = {
action,
confidence,
reasoning,
agentSignals: allSignals,
debateRounds: debates,
};
if (action === 'sell' && executionPlan) {
decision.executionPlan = executionPlan;
}
return decision;
}
}
+18 -3
View File
@@ -4,11 +4,11 @@ import { TechnicalAnalyst } from "./technical";
import { SentimentAnalyst } from "./sentiment";
import { BullishResearcher, BearishResearcher } from "./researchers";
import { Trader } from "./trader";
import type { AnalystReport, DebateRound, TradingDecision, AgentSignal } from "../types/agents";
import type { AnalystReport, DebateRound, TradingDecision, AgentSignal, ExecutionPlan } from "../types/agents";
export interface GraphStep {
step: "analysts" | "debate" | "trader";
data: AnalystReport[] | DebateRound[] | TradingDecision;
step: "analysts" | "debate" | "trader" | "execution";
data: AnalystReport[] | DebateRound[] | TradingDecision | ExecutionPlan;
}
export class TradingGraph {
@@ -50,6 +50,21 @@ export class TradingGraph {
console.log(`[TradingGraph] Analysis complete for ${ticker}`);
console.log(`[TradingGraph] Decision: ${decision.action} (confidence: ${decision.confidence})`);
// Build workflow steps for observability. Include an execution step when selling.
const steps: GraphStep[] = [
{ step: "analysts", data: reports },
{ step: "debate", data: debates },
{ step: "trader", data: decision },
];
if (decision.action === 'sell' && decision.executionPlan) {
steps.push({ step: "execution", data: decision.executionPlan });
console.log(`[TradingGraph] Execution plan: ${JSON.stringify(decision.executionPlan)}`);
}
// Log steps for debugging; external systems can be extended to consume GraphStep sequence.
console.log(`[TradingGraph] Workflow steps: ${JSON.stringify(steps)}`);
return decision;
}