|
|
|
|
|
|
|
|
import json |
|
|
import traceback |
|
|
from typing import Dict, Any, TYPE_CHECKING |
|
|
from .schemas import TraceLog, ReflectorOutput |
|
|
from .memory_store import MemoryStore |
|
|
|
|
|
|
|
|
if TYPE_CHECKING: |
|
|
from LLM import LLMService |
|
|
|
|
|
class Reflector: |
|
|
def __init__(self, llm_service: 'LLMService', memory_store: MemoryStore): |
|
|
self.llm_service = llm_service |
|
|
self.memory_store = memory_store |
|
|
print("✅ Learning Hub Module: Reflector (Fast-Learner) loaded") |
|
|
|
|
|
async def analyze_trade_outcome(self, trade_object: Dict[str, Any], close_reason: str): |
|
|
""" |
|
|
Analyzes the trade outcome using LLM to generate a learning 'Delta' (rule). |
|
|
(Implements Point 2 & 4 of the 16-point plan) |
|
|
""" |
|
|
try: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
decision_data = trade_object.get('decision_data', {}) |
|
|
|
|
|
trace_log = TraceLog( |
|
|
decision_context=decision_data, |
|
|
market_context_at_decision=decision_data.get('market_context_at_decision', {}), |
|
|
indicators_at_decision=decision_data.get('indicators_at_decision', {}), |
|
|
closed_trade_object=trade_object, |
|
|
actual_outcome_reason=close_reason |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
news_text_at_decision = decision_data.get('news_text', 'No news data available at decision time.') |
|
|
news_score_at_decision = decision_data.get('news_score', 0.0) |
|
|
|
|
|
prompt = self._create_reflector_prompt( |
|
|
trace_log, |
|
|
news_text_at_decision, |
|
|
news_score_at_decision |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
response_text = await self.llm_service._call_llm(prompt) |
|
|
|
|
|
if not response_text: |
|
|
raise ValueError("Reflector LLM call returned no response.") |
|
|
|
|
|
|
|
|
|
|
|
reflector_json = self.llm_service._parse_llm_response_enhanced( |
|
|
response_text, |
|
|
fallback_strategy="reflection", |
|
|
symbol=trade_object.get('symbol', 'N/A') |
|
|
) |
|
|
|
|
|
if not reflector_json: |
|
|
raise ValueError(f"Failed to parse Reflector LLM response: {response_text}") |
|
|
|
|
|
|
|
|
reflector_output = ReflectorOutput(**reflector_json) |
|
|
|
|
|
|
|
|
strategy = trade_object.get('strategy', 'general') |
|
|
domain = self._determine_domain(strategy, reflector_output.error_mode) |
|
|
|
|
|
|
|
|
|
|
|
await self.memory_store.save_new_delta( |
|
|
reflector_output=reflector_output, |
|
|
trade_object=trade_object, |
|
|
domain=domain |
|
|
) |
|
|
|
|
|
print(f"✅ [Reflector] Successfully analyzed {trade_object.get('symbol')}. New Delta created.") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"❌ [Reflector] Failed to analyze trade outcome for {trade_object.get('symbol')}: {e}") |
|
|
traceback.print_exc() |
|
|
|
|
|
def _determine_domain(self, strategy: str, error_mode: str) -> str: |
|
|
"""Determines the domain the suggested Delta belongs to.""" |
|
|
error_mode = error_mode.lower() |
|
|
if "pattern" in error_mode or "triangle" in error_mode or "flag" in error_mode: |
|
|
return "pattern" |
|
|
if "indicator" in error_mode or "rsi" in error_mode or "macd" in error_mode: |
|
|
return "indicator" |
|
|
if "monte_carlo" in error_mode or "garch" in error_mode or "simulation" in error_mode: |
|
|
return "monte_carlo" |
|
|
|
|
|
|
|
|
if "news" in error_mode or "sentiment" in error_mode or "sec" in error_mode: |
|
|
return "general" |
|
|
|
|
|
|
|
|
if "strategy" in error_mode or "exit" in error_mode or "entry" in error_mode: |
|
|
return "strategy" |
|
|
|
|
|
|
|
|
if strategy in ["trend_following", "mean_reversion", "breakout_momentum"]: |
|
|
return "strategy" |
|
|
|
|
|
return "general" |
|
|
|
|
|
|
|
|
def _create_reflector_prompt( |
|
|
self, |
|
|
trace_log: TraceLog, |
|
|
news_text: str, |
|
|
news_score: float |
|
|
) -> str: |
|
|
|
|
|
""" |
|
|
Creates the (English-only) prompt for the LLM to act as a Reflector. |
|
|
(Implements Point 4 - Reflector prompt) |
|
|
""" |
|
|
|
|
|
trade = trace_log.closed_trade_object |
|
|
pnl_percent = trade.get('pnl_percent', 0) |
|
|
|
|
|
|
|
|
is_success = pnl_percent > 0.1 |
|
|
|
|
|
|
|
|
|
|
|
news_context_section = f""" |
|
|
4. **News Context (at entry):** |
|
|
* VADER Score (Raw): {news_score:.4f} |
|
|
* News Text: {news_text} |
|
|
""" |
|
|
|
|
|
|
|
|
prompt = f""" |
|
|
SYSTEM: You are an expert trading analyst Reflector. Your task is to analyze a completed trade "Trace" and determine the cause of success or failure. You must suggest a concise "Rule" (Delta) (max 25 words) to improve future performance. |
|
|
|
|
|
--- TRACE LOG START --- |
|
|
|
|
|
1. **Original Decision Context (What we decided):** |
|
|
* Strategy Used: {trade.get('strategy', 'N/A')} |
|
|
* Exit Profile: {trade.get('decision_data', {}).get('exit_profile', 'N/A')} |
|
|
* Reasoning (at entry): {trade.get('decision_data', {}).get('reasoning', 'N/A')[:200]}... |
|
|
* Entry Price: {trade.get('entry_price')} |
|
|
* Initial Stop Loss: {trade.get('stop_loss')} |
|
|
* Initial Take Profit: {trade.get('take_profit')} |
|
|
|
|
|
2. **Environment Context (When we decided):** |
|
|
* Market Context: {json.dumps(trace_log.market_context_at_decision)} |
|
|
* Key Indicators: {json.dumps(trace_log.indicators_at_decision)} |
|
|
|
|
|
3. **Actual Outcome (What happened):** |
|
|
* Close Price: {trade.get('close_price')} |
|
|
* Final PnL: {pnl_percent:+.2f}% |
|
|
* Close Reason: {trace_log.actual_outcome_reason} |
|
|
* Trade Duration: {trade.get('hold_duration_minutes', 'N/A')} minutes |
|
|
|
|
|
{news_context_section} |
|
|
--- TRACE LOG END --- |
|
|
|
|
|
TASK: Analyze the Trace above. |
|
|
1. Compare the "Actual Outcome" with the "Original Decision Context". |
|
|
2. **Crucially, review the "News Context".** Did the market react as the VADER score predicted? Did the news text contain critical information that was missed? |
|
|
3. Identify the primary "Error Mode" (e.g., 'ignored_negative_news', 'premature_exit') or "Success Factor" (e.g., 'correct_pattern_identification'). |
|
|
4. Suggest ONE concise "Rule" (Delta) (max 25 words) to improve performance. If the news was the cause, the rule MUST mention news. |
|
|
|
|
|
OUTPUT FORMAT (JSON Only - Adhere strictly to this schema): |
|
|
{{ |
|
|
"success": {str(is_success).lower()}, |
|
|
"score": 0.0, |
|
|
"error_mode": "Short description of the error mode (e.g., 'ignored_negative_news_SEC_investigation').", |
|
|
"suggested_rule": "The concise 25-word rule (e.g., 'If news contains 'SEC' or 'investigation', do not BUY regardless of technicals.').", |
|
|
"confidence": 0.0 |
|
|
}} |
|
|
""" |
|
|
return prompt |