From ce2e6d32cc6072d1bc4134a9ef9fdc1c2e594fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B0=91=E6=9D=B0?= Date: Thu, 9 Apr 2026 22:12:02 +0800 Subject: [PATCH] feat(orchestrator): example scripts for backtest and live mode --- orchestrator/examples/__init__.py | 0 orchestrator/examples/run_backtest.py | 41 +++++++++++++++++++++++++++ orchestrator/examples/run_live.py | 41 +++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 orchestrator/examples/__init__.py create mode 100644 orchestrator/examples/run_backtest.py create mode 100644 orchestrator/examples/run_live.py diff --git a/orchestrator/examples/__init__.py b/orchestrator/examples/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/orchestrator/examples/run_backtest.py b/orchestrator/examples/run_backtest.py new file mode 100644 index 00000000..f7f5bc49 --- /dev/null +++ b/orchestrator/examples/run_backtest.py @@ -0,0 +1,41 @@ +""" +Example: Run orchestrator backtest for 宁德时代 (300750.SZ) over 2023. + +Usage: + cd /path/to/TradingAgents + QUANT_BACKTEST_PATH=/path/to/quant_backtest python orchestrator/examples/run_backtest.py +""" +import json +import logging +import os +import sys + +# Add repo root to path +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) + +from orchestrator.config import OrchestratorConfig +from orchestrator.orchestrator import TradingOrchestrator +from orchestrator.backtest_mode import BacktestMode + +logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s") + +config = OrchestratorConfig( + quant_backtest_path=os.environ.get("QUANT_BACKTEST_PATH", ""), + cache_dir="orchestrator/cache", +) + +orchestrator = TradingOrchestrator(config) +backtest = BacktestMode(orchestrator) + +result = backtest.run( + tickers=["300750.SZ"], + start_date="2023-01-01", + end_date="2023-12-31", +) + +print(f"\n=== Backtest Summary ===") +print(json.dumps(result.summary, indent=2, ensure_ascii=False)) +print(f"\nTotal records: {len(result.records)}") +if result.records: + print(f"First record: {result.records[0]}") + print(f"Last record: {result.records[-1]}") diff --git a/orchestrator/examples/run_live.py b/orchestrator/examples/run_live.py new file mode 100644 index 00000000..4a652bfa --- /dev/null +++ b/orchestrator/examples/run_live.py @@ -0,0 +1,41 @@ +""" +Example: Run orchestrator live mode for a list of tickers. + +Usage: + cd /path/to/TradingAgents + QUANT_BACKTEST_PATH=/path/to/quant_backtest python orchestrator/examples/run_live.py +""" +import asyncio +import json +import logging +import os +import sys +from datetime import datetime, timezone + +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) + +from orchestrator.config import OrchestratorConfig +from orchestrator.orchestrator import TradingOrchestrator +from orchestrator.live_mode import LiveMode + +logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s") + +TICKERS = ["300750.SZ", "603259.SS"] + +config = OrchestratorConfig( + quant_backtest_path=os.environ.get("QUANT_BACKTEST_PATH", ""), + cache_dir="orchestrator/cache", +) + +orchestrator = TradingOrchestrator(config) +live = LiveMode(orchestrator) + + +async def main(): + today = datetime.now(timezone.utc).strftime("%Y-%m-%d") + print(f"\n=== Live Signals for {today} ===") + results = await live.run_once(TICKERS, date=today) + print(json.dumps(results, indent=2, ensure_ascii=False)) + + +asyncio.run(main())