81 lines
4.4 KiB
Python
81 lines
4.4 KiB
Python
import time
|
|
import json
|
|
|
|
|
|
def create_risk_manager(llm, memory):
|
|
def risk_manager_node(state) -> dict:
|
|
|
|
company_name = state["company_of_interest"]
|
|
|
|
history = state["risk_debate_state"]["history"]
|
|
risk_debate_state = state["risk_debate_state"]
|
|
market_research_report = state["market_report"]
|
|
news_report = state["news_report"]
|
|
fundamentals_report = state["news_report"]
|
|
sentiment_report = state["sentiment_report"]
|
|
trader_plan = state["investment_plan"]
|
|
stop_loss = state.get("stop_loss")
|
|
take_profit = state.get("take_profit")
|
|
|
|
curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}"
|
|
past_memories = memory.get_memories(curr_situation, n_matches=2)
|
|
|
|
past_memory_str = ""
|
|
for i, rec in enumerate(past_memories, 1):
|
|
past_memory_str += rec["recommendation"] + "\n\n"
|
|
|
|
user_position = state.get("user_position", "none")
|
|
cost_per_trade = state.get("cost_per_trade", 0.0)
|
|
|
|
prompt = f"""As the Risk Management Judge and Debate Facilitator, your goal is to evaluate the debate between three risk analysts—Risky, Neutral, and Safe/Conservative—and determine the best course of action for the trader. Your recommendation will depend on the user's current position on the ticker and the trading cost per operation.
|
|
|
|
- The user has a current position of '{user_position}' and the cost per trade is {cost_per_trade}.
|
|
- If the user has an open long position, your recommendation can be to maintain the long position, close the long position, or close the long position and open a short position.
|
|
- If the user has an open short position, your recommendation can be to maintain the short position, close the short position, or close the short position and open a long position.
|
|
- If the user has no open position, your recommendation can be to do nothing, open a long position, or open a short position.
|
|
|
|
Your decision must result in a clear recommendation. Choose a neutral stance only if strongly justified by specific arguments, not as a fallback when all sides seem valid. Strive for clarity and decisiveness. Take into account that any transaction will incur a cost of {cost_per_trade}, so the potential profit of a transaction must be greater than this cost.
|
|
|
|
Guidelines for Decision-Making:
|
|
1. **Summarize Key Arguments**: Extract the strongest points from each analyst, focusing on relevance to the context.
|
|
2. **Provide Rationale**: Support your recommendation with direct quotes and counterarguments from the debate.
|
|
3. **Refine the Trader's Plan**: Start with the trader's original plan, **{trader_plan}**, and adjust it based on the analysts' insights.
|
|
4. **Incorporate Technical Analysis**: The Trade Planner has proposed a stop-loss of **{stop_loss}** and a take-profit of **{take_profit}**. You must consider these levels in your final recommendation.
|
|
5. **Learn from Past Mistakes**: Use lessons from **{past_memory_str}** to address prior misjudgments and improve the decision you are making now to make sure you don't make a wrong decision that loses money.
|
|
|
|
Deliverables:
|
|
- A clear and actionable recommendation.
|
|
- Detailed reasoning anchored in the debate and past reflections.
|
|
|
|
---
|
|
|
|
**Analysts Debate History:**
|
|
{history}
|
|
|
|
---
|
|
|
|
Focus on actionable insights and continuous improvement. Build on past lessons, critically evaluate all perspectives, and ensure each decision advances better outcomes."""
|
|
|
|
response = llm.invoke(prompt)
|
|
|
|
final_decision_content = response.content
|
|
new_risk_debate_state = {
|
|
"judge_decision": response.content,
|
|
"history": risk_debate_state["history"],
|
|
"risky_history": risk_debate_state["risky_history"],
|
|
"safe_history": risk_debate_state["safe_history"],
|
|
"neutral_history": risk_debate_state["neutral_history"],
|
|
"latest_speaker": "Judge",
|
|
"current_risky_response": risk_debate_state["current_risky_response"],
|
|
"current_safe_response": risk_debate_state["current_safe_response"],
|
|
"current_neutral_response": risk_debate_state["current_neutral_response"],
|
|
"count": risk_debate_state["count"],
|
|
}
|
|
|
|
return {
|
|
"risk_debate_state": new_risk_debate_state,
|
|
"final_trade_decision": final_decision_content,
|
|
}
|
|
|
|
return risk_manager_node
|