fix: address code review feedback
- Preserve and restore self.ticker and self.curr_state in propagate_portfolio() using try/finally to prevent side effects - Use pathlib.Path for log file construction in _log_portfolio() - Move traceback import to module level Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
dbd2c658e5
commit
85fbc48ede
|
|
@ -1,6 +1,7 @@
|
||||||
# TradingAgents/graph/portfolio_analysis.py
|
# TradingAgents/graph/portfolio_analysis.py
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import traceback
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Callable, Dict, List, Tuple
|
from typing import Any, Callable, Dict, List, Tuple
|
||||||
|
|
||||||
|
|
@ -88,7 +89,6 @@ class PortfolioAnalyzer:
|
||||||
"final_trade_decision": final_state["final_trade_decision"],
|
"final_trade_decision": final_state["final_trade_decision"],
|
||||||
}
|
}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
import traceback
|
|
||||||
if debug:
|
if debug:
|
||||||
print(f"Error analyzing {ticker}: {e}")
|
print(f"Error analyzing {ticker}: {e}")
|
||||||
individual_results[ticker] = {
|
individual_results[ticker] = {
|
||||||
|
|
@ -177,9 +177,6 @@ class PortfolioAnalyzer:
|
||||||
"portfolio_summary": portfolio_summary,
|
"portfolio_summary": portfolio_summary,
|
||||||
}
|
}
|
||||||
|
|
||||||
with open(
|
log_file = directory / f"portfolio_analysis_{trade_date}.json"
|
||||||
f"eval_results/portfolio/portfolio_analysis_{trade_date}.json",
|
with log_file.open("w", encoding="utf-8") as f:
|
||||||
"w",
|
|
||||||
encoding="utf-8",
|
|
||||||
) as f:
|
|
||||||
json.dump(log_data, f, indent=4)
|
json.dump(log_data, f, indent=4)
|
||||||
|
|
|
||||||
|
|
@ -278,13 +278,18 @@ class TradingAgentsGraph:
|
||||||
|
|
||||||
Delegates to PortfolioAnalyzer.analyze — see that class for full details.
|
Delegates to PortfolioAnalyzer.analyze — see that class for full details.
|
||||||
|
|
||||||
Note: Each call to propagate() overwrites self.ticker and self.curr_state,
|
This method preserves the instance's ticker and curr_state attributes,
|
||||||
so after this method returns, both reflect only the last ticker analyzed.
|
restoring them after the portfolio analysis is complete.
|
||||||
Calling reflect_and_remember() afterward will only apply to that last ticker.
|
|
||||||
"""
|
"""
|
||||||
return self.portfolio_analyzer.analyze(
|
original_ticker = self.ticker
|
||||||
tickers, trade_date, self.propagate, debug=self.debug
|
original_curr_state = self.curr_state
|
||||||
)
|
try:
|
||||||
|
return self.portfolio_analyzer.analyze(
|
||||||
|
tickers, trade_date, self.propagate, debug=self.debug
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
self.ticker = original_ticker
|
||||||
|
self.curr_state = original_curr_state
|
||||||
|
|
||||||
def reflect_and_remember(self, returns_losses):
|
def reflect_and_remember(self, returns_losses):
|
||||||
"""Reflect on decisions and update memory based on returns."""
|
"""Reflect on decisions and update memory based on returns."""
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue