-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.py
More file actions
410 lines (316 loc) · 12.1 KB
/
Copy pathCode.py
File metadata and controls
410 lines (316 loc) · 12.1 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/usr/bin/env python3
# ============================================================================
# EXAMPLE 1: Simplest Possible Usage (5 lines of code)
# ============================================================================
from env import TradingEnvironment, MarketDataLoader
from tasks import TaskFactory
from agents import RandomAgent
task = TaskFactory.create_easy_task()
env = TradingEnvironment(task.config, MarketDataLoader())
agent = RandomAgent(task.config.symbols)
state, _ = env.reset(seed=42)
for _ in range(50):
action = agent.act(state, {})
state, reward, done, info = env.step(action)
if done:
break
print(f"Final portfolio: ${info['portfolio_value']:.2f}")
# ============================================================================
# EXAMPLE 2: Full Training Run with Evaluation
# ============================================================================
from env import TradingEnvironment, MarketDataLoader, Config
from tasks import TaskFactory, Evaluator
from agents import RandomAgent, MomentumAgent, TechnicalAgent
def run_agent_on_task(agent, task, num_episodes=3):
"""Run agent and return metrics."""
results = []
for episode in range(num_episodes):
loader = MarketDataLoader()
env = TradingEnvironment(task.config, loader)
state, _ = env.reset(seed=42 + episode)
portfolio_values = [env.get_portfolio_value()]
returns = []
done = False
while not done:
action = agent.act(state, {})
state, reward, done, info = env.step(action)
portfolio_values.append(env.get_portfolio_value())
returns.append(info['metrics']['step_return'])
metrics = Evaluator.evaluate_episode(portfolio_values, returns, [])
results.append(metrics)
return results
# Test all agents on medium task
task = TaskFactory.create_medium_task()
agents = {
'Random': RandomAgent(task.config.symbols),
'Momentum': MomentumAgent(task.config.symbols),
'Technical': TechnicalAgent(task.config.symbols),
}
for agent_name, agent in agents.items():
results = run_agent_on_task(agent, task, num_episodes=3)
avg_return = sum(r.total_return for r in results) / len(results)
avg_sharpe = sum(r.sharpe_ratio for r in results) / len(results)
print(f"{agent_name:10}: Return={avg_return*100:6.2f}%, Sharpe={avg_sharpe:6.2f}")
# ============================================================================
# EXAMPLE 3: Custom Agent Implementation
# ============================================================================
from agents import BaseAgent
from env import Action, OrderType
class SimpleMovingAverageAgent(BaseAgent):
"""Buy on short MA above long MA, sell on cross below."""
def __init__(self, symbols):
super().__init__(symbols, "SMAAgent")
self.prev_signal = None
def act(self, state, info):
"""Generate action based on MA crossover."""
symbol = self.symbols[0]
step_return = info.get('metrics', {}).get('step_return', 0)
if step_return > 0.01 and not self.prev_signal:
action = Action(symbol=symbol, order_type=OrderType.BUY, quantity=20.0)
self.prev_signal = True
elif step_return < -0.01 and self.prev_signal:
action = Action(symbol=symbol, order_type=OrderType.SELL, quantity=20.0)
self.prev_signal = False
else:
action = Action(symbol=symbol, order_type=OrderType.HOLD, quantity=0.0)
return action
def reset(self):
self.prev_signal = None
# ============================================================================
# EXAMPLE 4: Custom Configuration and Task
# ============================================================================
from env import Config, TradingEnvironment, MarketDataLoader
# Create custom config
custom_config = Config(
symbols=['ASSET1', 'ASSET2'],
initial_cash=50000.0,
max_positions=2,
max_position_size=30.0,
slippage_bps=2.0,
commission_bps=2.0,
min_order_size=1.0,
max_leverage=2.5,
episode_length=150,
lookback_window=75,
)
# Create environment with custom config
loader = MarketDataLoader()
env = TradingEnvironment(custom_config, loader)
# Use with any agent
agent = RandomAgent(custom_config.symbols)
state, _ = env.reset(seed=42)
total_reward = 0
for _ in range(100):
action = agent.act(state, {})
state, reward, done, info = env.step(action)
total_reward += reward
if done:
break
print(f"Total Reward: {total_reward:.2f}, Portfolio: ${info['portfolio_value']:.2f}")
# ============================================================================
# EXAMPLE 5: Advanced: Loading Real Data and Analysis
# ============================================================================
from env import MarketDataLoader, TradingEnvironment, Config
from tasks import TaskFactory, Evaluator
import json
# Load custom data
loader = MarketDataLoader()
# Generate synthetic data for multiple assets
for symbol in ['ASSET1', 'ASSET2', 'ASSET3']:
loader.generate_synthetic_data(
symbol,
num_bars=2000,
volatility=0.02,
trend=0.0001
)
# Get task and environment
task = TaskFactory.create_medium_task()
env = TradingEnvironment(task.config, loader)
# Run episode
state, info = env.reset(seed=42)
agent = RandomAgent(task.config.symbols)
metrics_history = []
portfolio_values = [env.get_portfolio_value()]
returns_list = []
done = False
while not done:
action = agent.act(state, info)
state, reward, done, info = env.step(action)
portfolio_values.append(env.get_portfolio_value())
returns_list.append(info['metrics']['step_return'])
metrics_history.append(info['metrics'])
# Evaluate
final_metrics = Evaluator.evaluate_episode(
portfolio_values,
returns_list,
[]
)
print(f"\nEpisode Analysis:")
print(f" Duration: {len(portfolio_values)-1} steps")
print(f" Return: {final_metrics.total_return*100:.2f}%")
print(f" Sharpe: {final_metrics.sharpe_ratio:.2f}")
print(f" Max Drawdown: {final_metrics.max_drawdown*100:.2f}%")
print(f" Win Rate: {final_metrics.win_rate*100:.2f}%")
print(f" Score: {final_metrics.score():.1f}/100")
# Save results
results = {
'portfolio_values': portfolio_values,
'returns': returns_list,
'final_metrics': {
'total_return': float(final_metrics.total_return),
'sharpe_ratio': float(final_metrics.sharpe_ratio),
'max_drawdown': float(final_metrics.max_drawdown),
'score': float(final_metrics.score()),
}
}
with open('episode_results.json', 'w') as f:
json.dump(results, f, indent=2)
# ============================================================================
# EXAMPLE 6: Parallel Testing with Multiple Seeds
# ============================================================================
from env import TradingEnvironment, MarketDataLoader
from tasks import TaskFactory
from agents import MomentumAgent
import numpy as np
task = TaskFactory.create_hard_task()
seeds = [42, 123, 456, 789, 999]
results = []
for seed in seeds:
loader = MarketDataLoader()
env = TradingEnvironment(task.config, loader)
agent = MomentumAgent(task.config.symbols)
state, _ = env.reset(seed=seed)
portfolio_values = [env.get_portfolio_value()]
done = False
while not done:
action = agent.act(state, {})
state, reward, done, info = env.step(action)
portfolio_values.append(env.get_portfolio_value())
final_return = (portfolio_values[-1] - 100000) / 100000
results.append(final_return)
print(f"\nRobustness Analysis (5 seeds):")
print(f" Mean Return: {np.mean(results)*100:.2f}%")
print(f" Std Dev: {np.std(results)*100:.2f}%")
print(f" Min: {np.min(results)*100:.2f}%")
print(f" Max: {np.max(results)*100:.2f}%")
# ============================================================================
# EXAMPLE 7: Integration with RL Libraries (Pseudocode)
# ============================================================================
"""
# Using with Stable-Baselines3 (install: pip install stable-baselines3)
from stable_baselines3 import PPO
from env import TradingEnvironment, MarketDataLoader
from tasks import TaskFactory
import gymnasium as gym
from gymnasium import spaces
import numpy as np
class GymWrapper(gym.Env):
'''Wrap TradingEnvironment for gym compatibility'''
def __init__(self, task):
self.env = TradingEnvironment(task.config, MarketDataLoader())
self.observation_space = spaces.Box(
low=-np.inf, high=np.inf,
shape=(64,), dtype=np.float32
)
self.action_space = spaces.Discrete(
len(self.env.config.symbols) * 3 # symbol x [BUY, SELL, HOLD]
)
def reset(self, seed=None):
state, _ = self.env.reset(seed=seed)
return state, {}
def step(self, action_idx):
# Convert action index to Action object
num_symbols = len(self.env.config.symbols)
symbol_idx = action_idx // 3
order_type_idx = action_idx % 3
from env import Action, OrderType
symbol = self.env.config.symbols[symbol_idx]
order_types = [OrderType.BUY, OrderType.SELL, OrderType.HOLD]
action = Action(
symbol=symbol,
order_type=order_types[order_type_idx],
quantity=25.0
)
state, reward, done, _, info = self.env.step(action)
return state, reward, done, False, info
# Train agent
task = TaskFactory.create_medium_task()
env = GymWrapper(task)
model = PPO("MlpPolicy", env, verbose=1)
model.learn(total_timesteps=10000)
# Test trained model
obs, _ = env.reset()
for _ in range(250):
action, _ = model.predict(obs)
obs, reward, done, _, _ = env.step(action)
if done:
break
"""
# ============================================================================
# EXAMPLE 8: Debugging and Logging
# ============================================================================
import logging
# Setup detailed logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
from env import TradingEnvironment, MarketDataLoader
from tasks import TaskFactory
from agents import RandomAgent
task = TaskFactory.create_easy_task()
loader = MarketDataLoader()
env = TradingEnvironment(task.config, loader)
agent = RandomAgent(task.config.symbols)
state, info = env.reset(seed=42)
print("Starting episode debug run...")
for step in range(20):
action = agent.act(state, info)
state, reward, done, info = env.step(action)
print(f"\nStep {step}:")
print(f" Action: {action.symbol} {action.order_type.name} x{action.quantity}")
print(f" Reward: {reward:.4f}")
print(f" Portfolio: ${info['portfolio_value']:.2f}")
print(f" Positions: {list(info['positions'].keys())}")
if done:
print(f"\nEpisode ended at step {step}")
break
# ============================================================================
# TESTING CHECKLIST
# ============================================================================
"""
Before deploying your agent:
✓ Run quick tests:
python test_quick.py
✓ Verify imports:
from env import *
from agents import *
from tasks import *
✓ Test all difficulty levels:
- TaskFactory.create_easy_task()
- TaskFactory.create_medium_task()
- TaskFactory.create_hard_task()
✓ Test all baseline agents:
- RandomAgent
- MomentumAgent
- TechnicalAgent
✓ Test with multiple seeds:
env.reset(seed=42)
env.reset(seed=123)
env.reset(seed=456)
✓ Run full episode:
state, _ = env.reset()
done = False
while not done:
action = agent.act(state, info)
state, reward, done, info = env.step(action)
✓ Check metrics computation:
metrics = Evaluator.evaluate_episode(...)
score = metrics.score()
✓ Verify no errors:
python -c "import env; import agents; import tasks; print('✓ OK')"
"""
if __name__ == "__main__":
print("QuantitativeTrading-v1 - Quick Start Examples")
print("Copy-paste any example to get started!")
print("\nAll examples are self-contained and runnable.")