File size: 11,294 Bytes
ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db 27f1775 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db ee338ff 746b5db |
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 |
---
library_name: stable-baselines3
tags:
- reinforcement-learning
- trading
- finance
- stock-market
- ppo
- quantitative-finance
- algorithmic-trading
- deep-reinforcement-learning
- portfolio-management
- financial-ai
license: mit
base_model: PPO
model-index:
- name: Stock Trading RL Agent
results:
- task:
type: reinforcement-learning
name: Stock Trading
dataset:
name: FAANG Stocks (5Y Historical Data)
type: financial-time-series
metrics:
- type: total_return
value: 162.87
name: Best Total Return (AMZN)
- type: sharpe_ratio
value: 0.74
name: Best Sharpe Ratio (AMZN)
- type: max_drawdown
value: 145.29
name: Best Max Drawdown (TSLA)
- type: win_rate
value: 52.11
name: Best Win Rate (MSFT)
datasets:
- yahoo-finance
pipeline_tag: reinforcement-learning
widget:
- text: "Technical Analysis Trading Agent"
example_title: "Stock Trading Decision"
---
# π Stock Trading RL Agent - Advanced PPO Implementation
<div align="center">




**A state-of-the-art reinforcement learning agent for algorithmic stock trading using Proximal Policy Optimization (PPO)**
[π₯ **Quick Start**](#quick-start) β’ [π **Performance**](#performance-metrics) β’ [π‘ **Usage**](#usage) β’ [π οΈ **Technical Details**](#technical-details)
</div>
## π Model Overview
This model represents a sophisticated **reinforcement learning trading agent** trained using the **Proximal Policy Optimization (PPO)** algorithm. The agent learns to make optimal trading decisions across multiple stocks by analyzing technical indicators, market patterns, and portfolio states.
### π― Key Highlights
- **π§ Algorithm**: PPO with Multi-Layer Perceptron policy
- **π° Action Space**: Hybrid continuous/discrete (Action Type + Position Sizing)
- **π Observation Space**: 60-day lookback window with technical indicators
- **π Training**: 500,000 timesteps across 5 major stocks
- **β‘ Performance**: Up to 7,243% returns with risk management
## π Quick Start
### Installation
```bash
pip install stable-baselines3 yfinance pandas numpy scikit-learn
```
### For data preparation, you can use Enhanced Enviroment and Stock data processor automated classes for data and enviroment preparation in python files provided in directory
### Load and Use the Model
```python
from stable_baselines3 import PPO
import pickle
import numpy as np
# Load the trained model
model = PPO.load("best_model.zip")
# Load the data scaler
with open("scaler.pkl", "rb") as f:
scaler = pickle.load(f)
# Example prediction
obs = your_observation_data # Shape: (n_features,)
action, _states = model.predict(obs, deterministic=True)
# Interpret action
action_type = int(action[0]) # 0: Hold, 1: Buy, 2: Sell
position_size = action[1] # 0-1: Fraction of available capital
```
## π Performance Metrics
### π Evaluation Results
| Stock | Total Return | Sharpe Ratio | Max Drawdown | Win Rate | Status |
|-------|-------------|-------------|-------------|----------|--------|
| **MSFT** | **7,243.44%** | 0.56 | 164.60% | **52.11%** | π Best Overall |
| **AMZN** | **162.87%** | **0.74** | 187.11% | 6.72% | π Best Risk-Adj. |
| **TSLA** | 109.91% | -0.22 | **145.29%** | 44.76% | β‘ Volatile |
| **AAPL** | -74.02% | 0.65 | 157.07% | 7.01% | β οΈ Underperform |
| **GOOGL** | 0.00% | 0.00 | 0.00% | 0.00% | π No Activity |
### π― Key Performance Indicators
- **π Maximum Return**: 7,243.44% (MSFT)
- **βοΈ Best Risk-Adjusted Return**: 0.74 Sharpe Ratio (AMZN)
- **π― Highest Win Rate**: 52.11% (MSFT)
- **π Lowest Drawdown**: 145.29% (TSLA)
- **πΌ Portfolio Coverage**: 5 major stocks
## π οΈ Technical Details
### π§ Model Architecture
```yaml
Algorithm: PPO (Proximal Policy Optimization)
Policy Network: Multi-Layer Perceptron
Action Space:
- Action Type: Discrete(3) [Hold, Buy, Sell]
- Position Size: Continuous[0,1]
Observation Space: Technical indicators + Portfolio state
Training Steps: 500,000
Batch Size: 64
Learning Rate: 0.0003
```
### π Data Configuration
```json
{
"tickers": ["AAPL", "MSFT", "GOOGL", "AMZN", "TSLA"],
"period": "5y",
"interval": "1d",
"use_sp500": false,
"lookback_window": 60
}
```
### π Environment Setup
```json
{
"initial_balance": 10000,
"transaction_cost": 0.001,
"max_position_size": 1.0,
"reward_type": "return",
"risk_adjustment": true
}
```
### π Training Configuration
```json
{
"algorithm": "PPO",
"total_timesteps": 500000,
"learning_rate": 0.0003,
"batch_size": 64,
"n_epochs": 10,
"gamma": 0.99,
"eval_freq": 1000,
"n_eval_episodes": 5,
"save_freq": 10000,
"seed": 42
}
```
## π State Space & Features
### π Technical Indicators
The agent observes the following features for each stock:
- **π Trend Indicators**: SMA (20, 50), EMA (12, 26)
- **π Momentum**: RSI, MACD, MACD Signal, MACD Histogram
- **π― Volatility**: Bollinger Bands (Upper, Lower, %B)
- **πΉ Price/Volume**: Open, High, Low, Close, Volume
- **π° Portfolio State**: Balance, Position, Net Worth, Returns
### π Action Space
The agent outputs a 2-dimensional action:
1. **Action Type** (Discrete):
- `0`: Hold position
- `1`: Buy signal
- `2`: Sell signal
2. **Position Size** (Continuous):
- Range: `[0, 1]`
- Represents fraction of available capital to use
## π― Usage Examples
### π Basic Trading Loop
```python
import yfinance as yf
import pandas as pd
from stable_baselines3 import PPO
# Load model and scaler
model = PPO.load("best_model.zip")
with open("scaler.pkl", "rb") as f:
scaler = pickle.load(f)
# Get live data
ticker = "AAPL"
data = yf.download(ticker, period="3mo", interval="1d")
# Prepare observation (implement your feature engineering)
obs = prepare_observation(data, scaler) # Your preprocessing function
# Get trading decision
action, _states = model.predict(obs, deterministic=True)
action_type = ["HOLD", "BUY", "SELL"][int(action[0])]
position_size = action[1]
print(f"Action: {action_type}, Size: {position_size:.2%}")
```
### π Backtesting Framework
```python
def backtest_strategy(model, data, initial_balance=10000):
"""
Backtest the trained model on historical data
"""
balance = initial_balance
position = 0
for i in range(len(data)):
obs = prepare_observation(data[:i+1])
action, _ = model.predict(obs, deterministic=True)
# Execute trading logic
action_type = int(action[0])
position_size = action[1]
if action_type == 1: # Buy
shares_to_buy = (balance * position_size) // data.iloc[i]['Close']
position += shares_to_buy
balance -= shares_to_buy * data.iloc[i]['Close']
elif action_type == 2: # Sell
shares_to_sell = position * position_size
position -= shares_to_sell
balance += shares_to_sell * data.iloc[i]['Close']
return balance + position * data.iloc[-1]['Close']
```
## π Model Files
| File | Description | Size |
|------|-------------|------|
| `best_model.zip` | π Best performing model checkpoint | ~2.5MB |
| `final_model.zip` | π― Final trained model | ~2.5MB |
| `scaler.pkl` | π§ Data preprocessing scaler | ~50KB |
| `config.json` | βοΈ Complete training configuration | ~5KB |
| `evaluation_results.json` | π Detailed evaluation metrics | ~10KB |
| `training_summary.json` | π Training statistics | ~8KB |
## π Training Details
### π Training Process
- **π― Evaluation Frequency**: Every 1,000 steps
- **πΎ Checkpoint Saving**: Every 10,000 steps
- **π² Random Seed**: 42 (reproducible results)
- **β±οΈ Training Time**: ~6 hours on modern GPU
- **π Convergence**: Achieved after ~400,000 steps
### π Performance During Training
The model showed consistent improvement during training:
- **Early Stage** (0-100k steps): Learning basic market patterns
- **Mid Stage** (100k-300k steps): Developing risk management
- **Late Stage** (300k-500k steps): Fine-tuning position sizing
## β οΈ Important Disclaimers
> **π¨ Risk Warning**: This model is for educational and research purposes only. Past performance does not guarantee future results. Cryptocurrency and stock trading involves substantial risk of loss.
> **π Data Limitations**: The model was trained on historical data from 2019-2024. Market conditions may change, affecting model performance.
> **π§ Technical Limitations**: The model requires proper preprocessing and feature engineering to work effectively in live trading environments.
## π Advanced Usage
### π― Custom Environment Integration
```python
# Create custom trading environment
from stable_baselines3.common.env_checker import check_env
from your_trading_env import StockTradingEnv
env = StockTradingEnv(
tickers=["AAPL", "MSFT", "GOOGL"],
initial_balance=10000,
transaction_cost=0.001
)
# Verify environment
check_env(env)
# Load and test model
model = PPO.load("best_model.zip")
obs = env.reset()
action, _states = model.predict(obs)
```
### π Real-time Trading Integration
```python
import asyncio
import websocket
async def live_trading_loop():
"""
Example live trading implementation
"""
while True:
# Get real-time market data
market_data = await get_market_data()
# Prepare observation
obs = prepare_observation(market_data)
# Get model prediction
action, _ = model.predict(obs)
# Execute trade (implement your broker API)
if int(action[0]) != 0: # Not hold
await execute_trade(action)
await asyncio.sleep(60) # Wait 1 minute
```
## π€ Contributing
We welcome contributions! Please feel free to:
- π Report bugs and issues
- π‘ Suggest new features
- π Improve documentation
- π§ Submit pull requests
## π License
This project is licensed under the **MIT License** - see the [LICENSE](LICENSE) file for details.
## π Links & Resources
- **π Hugging Face Model**: [Adilbai/stock-trading-rl-20250704-171446](https://huggingface.co/Adilbai/stock-trading-rl-20250704-171446)
- **π Stable-Baselines3**: [Documentation](https://stable-baselines3.readthedocs.io/)
- **πΉ Yahoo Finance**: [API Documentation](https://github.com/ranaroussi/yfinance)
- **π PPO Paper**: [Proximal Policy Optimization](https://arxiv.org/abs/1707.06347)
## π Citation
If you use this model in your research, please cite:
```bibtex
@misc{stock-trading-rl-2025,
title={Stock Trading RL Agent using PPO},
author={Adilbai},
year={2025},
url={https://huggingface.co/Adilbai/stock-trading-rl-20250704-171446}
}
```
---
<div align="center">
**π Ready to revolutionize your trading strategy?**
[Get Started](#quick-start) β’ [View Performance](#performance-metrics) β’ [Technical Details](#technical-details)
</div> |