Update README.md
Browse files
README.md
CHANGED
|
@@ -1,145 +1,147 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: mit
|
| 3 |
-
language: en
|
| 4 |
-
library_name: stable-baselines3
|
| 5 |
-
tags:
|
| 6 |
-
- reinforcement-learning
|
| 7 |
-
- finance
|
| 8 |
-
- gold-trading
|
| 9 |
-
- xauusd
|
| 10 |
-
- ppo
|
| 11 |
-
metrics:
|
| 12 |
-
- sharpe_ratio
|
| 13 |
-
- win_rate
|
| 14 |
-
pipeline_tag: reinforcement-learning
|
| 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 |
-
import
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
vec
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
from
|
| 83 |
-
|
| 84 |
-
import
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
vec
|
| 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 |
This model is for educational and research purposes only. Trading involves risk, and past performance does not guarantee future results. Always backtest and validate before using in live trading.
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
language: en
|
| 4 |
+
library_name: stable-baselines3
|
| 5 |
+
tags:
|
| 6 |
+
- reinforcement-learning
|
| 7 |
+
- finance
|
| 8 |
+
- gold-trading
|
| 9 |
+
- xauusd
|
| 10 |
+
- ppo
|
| 11 |
+
metrics:
|
| 12 |
+
- sharpe_ratio
|
| 13 |
+
- win_rate
|
| 14 |
+
pipeline_tag: reinforcement-learning
|
| 15 |
+
datasets:
|
| 16 |
+
- ZombitX64/xauusd-gold-price-historical-data-2004-2025
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
# PPO Model for XAUUSD Gold Trading
|
| 20 |
+
|
| 21 |
+
This repository contains a Reinforcement Learning model trained using Proximal Policy Optimization (PPO) for trading XAUUSD (Gold vs US Dollar) on 15-minute timeframes.
|
| 22 |
+
|
| 23 |
+
## Model Details
|
| 24 |
+
|
| 25 |
+
- **Model Type**: PPO (Proximal Policy Optimization)
|
| 26 |
+
- **Framework**: Stable-Baselines3
|
| 27 |
+
- **Environment**: Custom Gym environment for XAUUSD trading
|
| 28 |
+
- **Training Data**: Historical XAUUSD data from 2004 to 2025 (resampled to 15-min bars)
|
| 29 |
+
- **Total Timesteps**: 1,000,000
|
| 30 |
+
- **Position Sizing**: Base 5.0 oz, Max 7.5 oz
|
| 31 |
+
- **Initial Capital**: 200 USD
|
| 32 |
+
- **Transaction Cost**: 0.65 USD per oz
|
| 33 |
+
|
| 34 |
+
## Performance Metrics (Test Set)
|
| 35 |
+
|
| 36 |
+
- **Average Daily Profit**: 51.46 USD
|
| 37 |
+
- **Win Rate**: 69.0%
|
| 38 |
+
- **Max Drawdown**: 12.0%
|
| 39 |
+
- **Sharpe Ratio**: 7.56
|
| 40 |
+
- **Average Trades per Day**: 2.66
|
| 41 |
+
|
| 42 |
+
## Features Used
|
| 43 |
+
|
| 44 |
+
- Log Return
|
| 45 |
+
- RSI (14-period)
|
| 46 |
+
- Moving Averages (short/long)
|
| 47 |
+
- Bollinger Bands
|
| 48 |
+
- MACD
|
| 49 |
+
- Volume indicators
|
| 50 |
+
|
| 51 |
+
## Usage
|
| 52 |
+
|
| 53 |
+
### Loading the Model
|
| 54 |
+
|
| 55 |
+
Below are two safe ways to load the trained policy depending on what you have available.
|
| 56 |
+
|
| 57 |
+
Option A — Load the full Stable-Baselines3 model (.zip)
|
| 58 |
+
|
| 59 |
+
```python
|
| 60 |
+
from stable_baselines3 import PPO
|
| 61 |
+
from stable_baselines3.common.vec_env import VecNormalize
|
| 62 |
+
import os
|
| 63 |
+
|
| 64 |
+
# Create or reconstruct an environment similar to the one used for training
|
| 65 |
+
# e.g. `env = make_your_env(...)` — replace with your env factory
|
| 66 |
+
env = ...
|
| 67 |
+
|
| 68 |
+
# If you saved VecNormalize separately, load and wrap your env first
|
| 69 |
+
if os.path.exists("models/vecnormalize.pkl"):
|
| 70 |
+
vec = VecNormalize.load("models/vecnormalize.pkl", env)
|
| 71 |
+
vec.training = False
|
| 72 |
+
vec.norm_reward = False
|
| 73 |
+
env = vec
|
| 74 |
+
|
| 75 |
+
# Load the full model (policy + optimizer state)
|
| 76 |
+
model = PPO.load("models/ppo_xauusd.zip", env=env)
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
Option B — Load weights saved as SafeTensors into a fresh PPO policy
|
| 80 |
+
|
| 81 |
+
```python
|
| 82 |
+
from safetensors.torch import load_file
|
| 83 |
+
import torch
|
| 84 |
+
from stable_baselines3 import PPO
|
| 85 |
+
from stable_baselines3.common.vec_env import VecNormalize
|
| 86 |
+
import os
|
| 87 |
+
|
| 88 |
+
# Create or reconstruct the same environment used for training
|
| 89 |
+
env = ...
|
| 90 |
+
|
| 91 |
+
# If you have VecNormalize statistics, load them and wrap the env
|
| 92 |
+
if os.path.exists("models/vecnormalize.pkl"):
|
| 93 |
+
vec = VecNormalize.load("models/vecnormalize.pkl", env)
|
| 94 |
+
vec.training = False
|
| 95 |
+
vec.norm_reward = False
|
| 96 |
+
env = vec
|
| 97 |
+
|
| 98 |
+
# Instantiate a PPO model with the same policy architecture
|
| 99 |
+
model = PPO("MlpPolicy", env)
|
| 100 |
+
|
| 101 |
+
# Load SafeTensors state dict and convert values to torch.Tensor if needed
|
| 102 |
+
raw_state = load_file("models/ppo_xauusd.safetensors")
|
| 103 |
+
state_dict = {k: (torch.tensor(v) if not isinstance(v, torch.Tensor) else v) for k, v in raw_state.items()}
|
| 104 |
+
|
| 105 |
+
# Load weights into the policy
|
| 106 |
+
model.policy.load_state_dict(state_dict)
|
| 107 |
+
|
| 108 |
+
# Ensure the model has the same env wrapper
|
| 109 |
+
model.set_env(env)
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
Notes:
|
| 113 |
+
- Option A is preferred when `ppo_xauusd.zip` is available (it contains the entire SB3 model).
|
| 114 |
+
- Option B is useful when only the policy weights were exported as SafeTensors. Ensure the policy architecture and observation/action spaces match the original training setup.
|
| 115 |
+
- Always set `vec.training = False` and `vec.norm_reward = False` when running inference.
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
### For Full Inference
|
| 119 |
+
|
| 120 |
+
To use the model for trading, you'll need to:
|
| 121 |
+
1. Set up the trading environment (`XAUUSDTradingEnv`)
|
| 122 |
+
2. Load VecNormalize stats
|
| 123 |
+
3. Run predictions
|
| 124 |
+
|
| 125 |
+
Note: This is a simulation model. Use with caution in real trading.
|
| 126 |
+
|
| 127 |
+
## Training Configuration
|
| 128 |
+
|
| 129 |
+
- Learning Rate: 0.0003
|
| 130 |
+
- Batch Size: 256
|
| 131 |
+
- Gamma: 0.99
|
| 132 |
+
- GAE Lambda: 0.95
|
| 133 |
+
- Clip Range: 0.2
|
| 134 |
+
- Entropy Coefficient: 0.01
|
| 135 |
+
|
| 136 |
+
## Files
|
| 137 |
+
|
| 138 |
+
- `ppo_xauusd.safetensors`: Model weights in SafeTensors format
|
| 139 |
+
- `vecnormalize.pkl`: VecNormalize statistics for observation normalization
|
| 140 |
+
|
| 141 |
+
## License
|
| 142 |
+
|
| 143 |
+
MIT License
|
| 144 |
+
|
| 145 |
+
## Disclaimer
|
| 146 |
+
|
| 147 |
This model is for educational and research purposes only. Trading involves risk, and past performance does not guarantee future results. Always backtest and validate before using in live trading.
|