import time import json from tradingagents.agents.utils.korean_prompt import ( KOREAN_INVESTOR_GUIDE, KOREAN_DEBATE_GUIDE, KOREAN_FINAL_DECISION_GUIDE, ) 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["fundamentals_report"] sentiment_report = state["sentiment_report"] trader_plan = state["investment_plan"] 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" prompt = f"""As the Risk Management Judge and Debate Facilitator, your goal is to evaluate the debate between three risk analysts—Aggressive, Neutral, and Conservative—and determine whether to ENTER a NEW long position in this stock. Context: There is currently NO existing position. The trader is evaluating a fresh entry. The only valid decisions are: - **BUY**: Enter a new long position now — the risk/reward is favorable for a new entry. - **PASS**: Do not enter — the risks are too high or the timing is wrong; skip this trade. SELL is NOT a valid option since there is no existing position. Guidelines for Decision-Making: 1. **Summarize Key Arguments**: Extract the strongest points from each analyst about whether this is a good entry point, focusing on entry risk and reward. 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 about entry risk. 4. **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 BUY/PASS call. Deliverables: - A clear and actionable recommendation: Buy or Pass. - 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. {KOREAN_INVESTOR_GUIDE} {KOREAN_DEBATE_GUIDE} {KOREAN_FINAL_DECISION_GUIDE} """ response = llm.invoke(prompt) new_risk_debate_state = { "judge_decision": response.content, "history": risk_debate_state["history"], "aggressive_history": risk_debate_state["aggressive_history"], "conservative_history": risk_debate_state["conservative_history"], "neutral_history": risk_debate_state["neutral_history"], "latest_speaker": "Judge", "current_aggressive_response": risk_debate_state["current_aggressive_response"], "current_conservative_response": risk_debate_state["current_conservative_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": response.content, } return risk_manager_node