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:
+32
-3
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user