forked from HKUDS/AI-Trader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_parrallel.py
More file actions
279 lines (231 loc) · 9.65 KB
/
main_parrallel.py
File metadata and controls
279 lines (231 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import os
import sys
import asyncio
from datetime import datetime
import json
from pathlib import Path
from dotenv import load_dotenv
import argparse
load_dotenv()
# Import tools and prompts
from tools.general_tools import write_config_value
from prompts.agent_prompt import all_nasdaq_100_symbols
# Agent class mapping table - for dynamic import and instantiation
AGENT_REGISTRY = {
"BaseAgent": {
"module": "agent.base_agent.base_agent",
"class": "BaseAgent"
},
"BaseAgent_Hour": {
"module": "agent.base_agent.base_agent_hour",
"class": "BaseAgent_Hour"
},
}
def get_agent_class(agent_type):
"""
Dynamically import and return the corresponding class based on agent type name
Args:
agent_type: Agent type name (e.g., "BaseAgent")
Returns:
Agent class
Raises:
ValueError: If agent type is not supported
ImportError: If unable to import agent module
"""
if agent_type not in AGENT_REGISTRY:
supported_types = ", ".join(AGENT_REGISTRY.keys())
raise ValueError(
f"❌ Unsupported agent type: {agent_type}\n"
f" Supported types: {supported_types}"
)
agent_info = AGENT_REGISTRY[agent_type]
module_path = agent_info["module"]
class_name = agent_info["class"]
try:
# Dynamic import module
import importlib
module = importlib.import_module(module_path)
agent_class = getattr(module, class_name)
print(f"✅ Successfully loaded Agent class: {agent_type} (from {module_path})")
return agent_class
except ImportError as e:
raise ImportError(f"❌ Unable to import agent module {module_path}: {e}")
except AttributeError as e:
raise AttributeError(f"❌ Class {class_name} not found in module {module_path}: {e}")
def load_config(config_path=None):
"""
Load configuration file from configs directory
Args:
config_path: Configuration file path, if None use default config
Returns:
dict: Configuration dictionary
"""
if config_path is None:
# Default configuration file path
config_path = Path(__file__).parent / "configs" / "default_config.json"
else:
config_path = Path(config_path)
if not config_path.exists():
print(f"❌ Configuration file does not exist: {config_path}")
exit(1)
try:
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
print(f"✅ Successfully loaded configuration file: {config_path}")
return config
except json.JSONDecodeError as e:
print(f"❌ Configuration file JSON format error: {e}")
exit(1)
except Exception as e:
print(f"❌ Failed to load configuration file: {e}")
exit(1)
async def _run_model_in_current_process(AgentClass, model_config, INIT_DATE, END_DATE, agent_config, log_config):
model_name = model_config.get("name", "unknown")
basemodel = model_config.get("basemodel")
signature = model_config.get("signature")
openai_base_url = model_config.get("openai_base_url", None)
openai_api_key = model_config.get("openai_api_key", None)
if not basemodel:
print(f"❌ Model {model_name} missing basemodel field")
return
if not signature:
print(f"❌ Model {model_name} missing signature field")
return
print("=" * 60)
print(f"🤖 Processing model: {model_name}")
print(f"📝 Signature: {signature}")
print(f"🔧 BaseModel: {basemodel}")
project_root = Path(__file__).resolve().parent
runtime_env_dir = project_root / "data" / "agent_data" / signature
runtime_env_dir.mkdir(parents=True, exist_ok=True)
runtime_env_path = runtime_env_dir / ".runtime_env.json"
os.environ["RUNTIME_ENV_PATH"] = str(runtime_env_path)
os.environ["SIGNATURE"] = signature
write_config_value("TODAY_DATE", END_DATE)
write_config_value("IF_TRADE", False)
max_steps = agent_config.get("max_steps", 10)
max_retries = agent_config.get("max_retries", 3)
base_delay = agent_config.get("base_delay", 0.5)
initial_cash = agent_config.get("initial_cash", 10000.0)
log_path = log_config.get("log_path", "./data/agent_data")
try:
agent = AgentClass(
signature=signature,
basemodel=basemodel,
stock_symbols=all_nasdaq_100_symbols,
log_path=log_path,
openai_base_url=openai_base_url,
openai_api_key=openai_api_key,
max_steps=max_steps,
max_retries=max_retries,
base_delay=base_delay,
initial_cash=initial_cash,
init_date=INIT_DATE
)
print(f"✅ {AgentClass.__name__} instance created successfully: {agent}")
await agent.initialize()
print("✅ Initialization successful")
await agent.run_date_range(INIT_DATE, END_DATE)
summary = agent.get_position_summary()
print(f"📊 Final position summary:")
print(f" - Latest date: {summary.get('latest_date')}")
print(f" - Total records: {summary.get('total_records')}")
print(f" - Cash balance: ${summary.get('positions', {}).get('CASH', 0):.2f}")
except Exception as e:
print(f"❌ Error processing model {model_name} ({signature}): {str(e)}")
print(f"📋 Error details: {e}")
raise
print("=" * 60)
print(f"✅ Model {model_name} ({signature}) processing completed")
print("=" * 60)
async def _spawn_model_subprocesses(config_path, enabled_models):
tasks = []
python_exec = sys.executable
this_file = str(Path(__file__).resolve())
for model in enabled_models:
signature = model.get("signature")
if not signature:
continue
cmd = [python_exec, this_file]
if config_path:
cmd.append(str(config_path))
cmd.extend(["--signature", signature])
print(f"🧩 Spawning subprocess for signature='{signature}': {' '.join(cmd)}")
proc = await asyncio.create_subprocess_exec(*cmd)
tasks.append(proc.wait())
if not tasks:
return
await asyncio.gather(*tasks)
async def main(config_path=None, only_signature: str | None = None):
"""Run trading experiment using Agent class (parallel runner)
Args:
config_path: Configuration file path, if None use default config
only_signature: If provided, run only this model signature
"""
# Load configuration file
config = load_config(config_path)
# Get Agent type
agent_type = config.get("agent_type", "BaseAgent")
try:
AgentClass = get_agent_class(agent_type)
except (ValueError, ImportError, AttributeError) as e:
print(str(e))
exit(1)
INIT_DATE = config["date_range"]["init_date"]
END_DATE = config["date_range"]["end_date"]
# Environment variables can override dates in configuration file
if os.getenv("INIT_DATE"):
INIT_DATE = os.getenv("INIT_DATE")
print(f"⚠️ Using environment variable to override INIT_DATE: {INIT_DATE}")
if os.getenv("END_DATE"):
END_DATE = os.getenv("END_DATE")
print(f"⚠️ Using environment variable to override END_DATE: {END_DATE}")
# Validate date range
# Support both YYYY-MM-DD and YYYY-MM-DD HH:MM:SS formats
if ' ' in INIT_DATE:
INIT_DATE_obj = datetime.strptime(INIT_DATE, "%Y-%m-%d %H:%M:%S")
else:
INIT_DATE_obj = datetime.strptime(INIT_DATE, "%Y-%m-%d")
if ' ' in END_DATE:
END_DATE_obj = datetime.strptime(END_DATE, "%Y-%m-%d %H:%M:%S")
else:
END_DATE_obj = datetime.strptime(END_DATE, "%Y-%m-%d")
if INIT_DATE_obj > END_DATE_obj:
print("❌ INIT_DATE is greater than END_DATE")
exit(1)
# Get model list from configuration file (only select enabled models)
enabled_models = [
model for model in config["models"]
if model.get("enabled", True)
]
if only_signature:
enabled_models = [m for m in enabled_models if m.get("signature") == only_signature]
# Get agent configuration
agent_config = config.get("agent_config", {})
log_config = config.get("log_config", {})
# Display enabled model information
model_names = [m.get("name", m.get("signature")) for m in enabled_models]
print("🚀 Starting trading experiment (parallel runner)")
print(f"🤖 Agent type: {agent_type}")
print(f"📅 Date range: {INIT_DATE} to {END_DATE}")
print(f"🤖 Model list: {model_names}")
if len(enabled_models) <= 1:
for model_config in enabled_models:
await _run_model_in_current_process(AgentClass, model_config, INIT_DATE, END_DATE, agent_config, log_config)
print("🎉 All models processing completed!")
else:
print("⚡ Multiple models enabled; running them in parallel using subprocesses...")
await _spawn_model_subprocesses(config_path, enabled_models)
print("🎉 All model subprocesses completed!")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="AI-Trader parallel runner")
parser.add_argument("config_path", nargs="?", default=None, help="Path to config JSON")
parser.add_argument("--signature", dest="signature", default=None, help="Run only this model signature")
args = parser.parse_args()
if args.config_path:
print(f"📄 Using specified configuration file: {args.config_path}")
else:
print(f"📄 Using default configuration file: configs/default_config.json")
if args.signature:
print(f"🎯 Filtering to single signature: {args.signature}")
asyncio.run(main(args.config_path, args.signature))