Team Ai
Modelpublic

OggyCodes12/OpenEnvHackathon

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes
code.py419 linesDownload Raw Back to root
1#!/usr/bin/env python32"""3QUICK START GUIDE - QuantitativeTrading-v14 5Copy & paste examples to get started immediately.6"""7 8# ============================================================================9# EXAMPLE 1: Simplest Possible Usage (5 lines of code)10# ============================================================================11 12from env import TradingEnvironment, MarketDataLoader13from tasks import TaskFactory14from agents import RandomAgent15 16task = TaskFactory.create_easy_task()17env = TradingEnvironment(task.config, MarketDataLoader())18agent = RandomAgent(task.config.symbols)19state, _ = env.reset(seed=42)20 21for _ in range(50):22    action = agent.act(state, {})23    state, reward, done, info = env.step(action)24    if done:25        break26 27print(f"Final portfolio: ${info['portfolio_value']:.2f}")28 29 30# ============================================================================31# EXAMPLE 2: Full Training Run with Evaluation32# ============================================================================33 34from env import TradingEnvironment, MarketDataLoader, Config35from tasks import TaskFactory, Evaluator36from agents import RandomAgent, MomentumAgent, TechnicalAgent37 38def run_agent_on_task(agent, task, num_episodes=3):39    """Run agent and return metrics."""40    results = []41    42    for episode in range(num_episodes):43        loader = MarketDataLoader()44        env = TradingEnvironment(task.config, loader)45        state, _ = env.reset(seed=42 + episode)46        47        portfolio_values = [env.get_portfolio_value()]48        returns = []49        done = False50        51        while not done:52            action = agent.act(state, {})53            state, reward, done, info = env.step(action)54            portfolio_values.append(env.get_portfolio_value())55            returns.append(info['metrics']['step_return'])56        57        metrics = Evaluator.evaluate_episode(portfolio_values, returns, [])58        results.append(metrics)59    60    return results61 62# Test all agents on medium task63task = TaskFactory.create_medium_task()64 65agents = {66    'Random': RandomAgent(task.config.symbols),67    'Momentum': MomentumAgent(task.config.symbols),68    'Technical': TechnicalAgent(task.config.symbols),69}70 71for agent_name, agent in agents.items():72    results = run_agent_on_task(agent, task, num_episodes=3)73    avg_return = sum(r.total_return for r in results) / len(results)74    avg_sharpe = sum(r.sharpe_ratio for r in results) / len(results)75    print(f"{agent_name:10}: Return={avg_return*100:6.2f}%, Sharpe={avg_sharpe:6.2f}")76 77 78# ============================================================================79# EXAMPLE 3: Custom Agent Implementation80# ============================================================================81 82from agents import BaseAgent83from env import Action, OrderType84 85class SimpleMovingAverageAgent(BaseAgent):86    """Buy on short MA above long MA, sell on cross below."""87    88    def __init__(self, symbols):89        super().__init__(symbols, "SMAAgent")90        self.prev_signal = None91    92    def act(self, state, info):93        """Generate action based on MA crossover."""94        symbol = self.symbols[0]95        96        # In a real implementation, extract MA values from state97        # For now, use a simple heuristic98        step_return = info.get('metrics', {}).get('step_return', 0)99        100        if step_return > 0.01 and not self.prev_signal:101            action = Action(symbol=symbol, order_type=OrderType.BUY, quantity=20.0)102            self.prev_signal = True103        elif step_return < -0.01 and self.prev_signal:104            action = Action(symbol=symbol, order_type=OrderType.SELL, quantity=20.0)105            self.prev_signal = False106        else:107            action = Action(symbol=symbol, order_type=OrderType.HOLD, quantity=0.0)108        109        return action110    111    def reset(self):112        self.prev_signal = None113 114 115# ============================================================================116# EXAMPLE 4: Custom Configuration and Task117# ============================================================================118 119from env import Config, TradingEnvironment, MarketDataLoader120 121# Create custom config122custom_config = Config(123    symbols=['ASSET1', 'ASSET2'],124    initial_cash=50000.0,125    max_positions=2,126    max_position_size=30.0,127    slippage_bps=2.0,128    commission_bps=2.0,129    min_order_size=1.0,130    max_leverage=2.5,131    episode_length=150,132    lookback_window=75,133)134 135# Create environment with custom config136loader = MarketDataLoader()137env = TradingEnvironment(custom_config, loader)138 139# Use with any agent140agent = RandomAgent(custom_config.symbols)141state, _ = env.reset(seed=42)142 143total_reward = 0144for _ in range(100):145    action = agent.act(state, {})146    state, reward, done, info = env.step(action)147    total_reward += reward148    if done:149        break150 151print(f"Total Reward: {total_reward:.2f}, Portfolio: ${info['portfolio_value']:.2f}")152 153 154# ============================================================================155# EXAMPLE 5: Advanced: Loading Real Data and Analysis156# ============================================================================157 158from env import MarketDataLoader, TradingEnvironment, Config159from tasks import TaskFactory, Evaluator160import json161 162# Load custom data163loader = MarketDataLoader()164 165# Generate synthetic data for multiple assets166for symbol in ['ASSET1', 'ASSET2', 'ASSET3']:167    loader.generate_synthetic_data(168        symbol, 169        num_bars=2000, 170        volatility=0.02,171        trend=0.0001172    )173 174# Get task and environment175task = TaskFactory.create_medium_task()176env = TradingEnvironment(task.config, loader)177 178# Run episode179state, info = env.reset(seed=42)180agent = RandomAgent(task.config.symbols)181 182metrics_history = []183portfolio_values = [env.get_portfolio_value()]184returns_list = []185 186done = False187while not done:188    action = agent.act(state, info)189    state, reward, done, info = env.step(action)190    191    portfolio_values.append(env.get_portfolio_value())192    returns_list.append(info['metrics']['step_return'])193    metrics_history.append(info['metrics'])194 195# Evaluate196final_metrics = Evaluator.evaluate_episode(197    portfolio_values, 198    returns_list, 199    []200)201 202print(f"\nEpisode Analysis:")203print(f"  Duration: {len(portfolio_values)-1} steps")204print(f"  Return: {final_metrics.total_return*100:.2f}%")205print(f"  Sharpe: {final_metrics.sharpe_ratio:.2f}")206print(f"  Max Drawdown: {final_metrics.max_drawdown*100:.2f}%")207print(f"  Win Rate: {final_metrics.win_rate*100:.2f}%")208print(f"  Score: {final_metrics.score():.1f}/100")209 210# Save results211results = {212    'portfolio_values': portfolio_values,213    'returns': returns_list,214    'final_metrics': {215        'total_return': float(final_metrics.total_return),216        'sharpe_ratio': float(final_metrics.sharpe_ratio),217        'max_drawdown': float(final_metrics.max_drawdown),218        'score': float(final_metrics.score()),219    }220}221 222with open('episode_results.json', 'w') as f:223    json.dump(results, f, indent=2)224 225 226# ============================================================================227# EXAMPLE 6: Parallel Testing with Multiple Seeds228# ============================================================================229 230from env import TradingEnvironment, MarketDataLoader231from tasks import TaskFactory232from agents import MomentumAgent233import numpy as np234 235task = TaskFactory.create_hard_task()236seeds = [42, 123, 456, 789, 999]237results = []238 239for seed in seeds:240    loader = MarketDataLoader()241    env = TradingEnvironment(task.config, loader)242    agent = MomentumAgent(task.config.symbols)243    244    state, _ = env.reset(seed=seed)245    portfolio_values = [env.get_portfolio_value()]246    247    done = False248    while not done:249        action = agent.act(state, {})250        state, reward, done, info = env.step(action)251        portfolio_values.append(env.get_portfolio_value())252    253    final_return = (portfolio_values[-1] - 100000) / 100000254    results.append(final_return)255 256print(f"\nRobustness Analysis (5 seeds):")257print(f"  Mean Return: {np.mean(results)*100:.2f}%")258print(f"  Std Dev: {np.std(results)*100:.2f}%")259print(f"  Min: {np.min(results)*100:.2f}%")260print(f"  Max: {np.max(results)*100:.2f}%")261 262 263# ============================================================================264# EXAMPLE 7: Integration with RL Libraries (Pseudocode)265# ============================================================================266 267"""268# Using with Stable-Baselines3 (install: pip install stable-baselines3)269 270from stable_baselines3 import PPO271from env import TradingEnvironment, MarketDataLoader272from tasks import TaskFactory273import gymnasium as gym274from gymnasium import spaces275import numpy as np276 277class GymWrapper(gym.Env):278    '''Wrap TradingEnvironment for gym compatibility'''279    280    def __init__(self, task):281        self.env = TradingEnvironment(task.config, MarketDataLoader())282        self.observation_space = spaces.Box(283            low=-np.inf, high=np.inf, 284            shape=(64,), dtype=np.float32285        )286        self.action_space = spaces.Discrete(287            len(self.env.config.symbols) * 3  # symbol x [BUY, SELL, HOLD]288        )289    290    def reset(self, seed=None):291        state, _ = self.env.reset(seed=seed)292        return state, {}293    294    def step(self, action_idx):295        # Convert action index to Action object296        num_symbols = len(self.env.config.symbols)297        symbol_idx = action_idx // 3298        order_type_idx = action_idx % 3299        300        from env import Action, OrderType301        symbol = self.env.config.symbols[symbol_idx]302        order_types = [OrderType.BUY, OrderType.SELL, OrderType.HOLD]303        304        action = Action(305            symbol=symbol,306            order_type=order_types[order_type_idx],307            quantity=25.0308        )309        310        state, reward, done, _, info = self.env.step(action)311        return state, reward, done, False, info312 313# Train agent314task = TaskFactory.create_medium_task()315env = GymWrapper(task)316 317model = PPO("MlpPolicy", env, verbose=1)318model.learn(total_timesteps=10000)319 320# Test trained model321obs, _ = env.reset()322for _ in range(250):323    action, _ = model.predict(obs)324    obs, reward, done, _, _ = env.step(action)325    if done:326        break327"""328 329 330# ============================================================================331# EXAMPLE 8: Debugging and Logging332# ============================================================================333 334import logging335 336# Setup detailed logging337logging.basicConfig(338    level=logging.INFO,339    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'340)341 342from env import TradingEnvironment, MarketDataLoader343from tasks import TaskFactory344from agents import RandomAgent345 346task = TaskFactory.create_easy_task()347loader = MarketDataLoader()348env = TradingEnvironment(task.config, loader)349agent = RandomAgent(task.config.symbols)350 351state, info = env.reset(seed=42)352 353print("Starting episode debug run...")354for step in range(20):355    action = agent.act(state, info)356    state, reward, done, info = env.step(action)357    358    print(f"\nStep {step}:")359    print(f"  Action: {action.symbol} {action.order_type.name} x{action.quantity}")360    print(f"  Reward: {reward:.4f}")361    print(f"  Portfolio: ${info['portfolio_value']:.2f}")362    print(f"  Positions: {list(info['positions'].keys())}")363    364    if done:365        print(f"\nEpisode ended at step {step}")366        break367 368 369# ============================================================================370# TESTING CHECKLIST371# ============================================================================372 373"""374Before deploying your agent:375 376✓ Run quick tests:377  python test_quick.py378 379✓ Verify imports:380  from env import *381  from agents import *382  from tasks import *383 384✓ Test all difficulty levels:385  - TaskFactory.create_easy_task()386  - TaskFactory.create_medium_task()387  - TaskFactory.create_hard_task()388 389✓ Test all baseline agents:390  - RandomAgent391  - MomentumAgent392  - TechnicalAgent393 394✓ Test with multiple seeds:395  env.reset(seed=42)396  env.reset(seed=123)397  env.reset(seed=456)398 399✓ Run full episode:400  state, _ = env.reset()401  done = False402  while not done:403      action = agent.act(state, info)404      state, reward, done, info = env.step(action)405 406✓ Check metrics computation:407  metrics = Evaluator.evaluate_episode(...)408  score = metrics.score()409 410✓ Verify no errors:411  python -c "import env; import agents; import tasks; print('✓ OK')"412"""413 414 415if __name__ == "__main__":416    print("QuantitativeTrading-v1 - Quick Start Examples")417    print("Copy-paste any example to get started!")418    print("\nAll examples are self-contained and runnable.")419