2026-06-29 16:23:10 +02:00
# xLSTM PPO Reinforcement Learning Trading Bot for MetaTrader 5 (XAUUSD)
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
An AI-powered trading bot that combines **Extended LSTM (xLSTM)** and **Proximal Policy Optimization (PPO)** to learn profitable trading strategies on XAUUSD.
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
Unlike traditional indicator-based Expert Advisors, this project learns directly from historical market data using reinforcement learning while incorporating Smart Money Concepts (SMC), technical indicators, market structure, and adaptive risk management.
2026-06-19 17:01:42 +02:00
2026-06-07 17:00:15 +02:00
---
## Features
2026-06-19 16:59:27 +02:00
### Reinforcement Learning
2026-06-29 16:23:10 +02:00
- xLSTM policy network
- Proximal Policy Optimization (PPO)
- Sequence-based learning
- Reinforcement learning trading environment
- Reward shaping
- GPU/CPU training support
---
## Smart Money Concepts (SMC)
The bot automatically detects:
- Order Blocks (OB)
- Mitigated Order Blocks
- Breaker Blocks
2026-06-29 16:58:58 +02:00
- Mitigation Blocks (MB)
2026-06-29 16:23:10 +02:00
- Rejection Blocks (RB)
- Fair Value Gaps (FVG)
- Inverse Fair Value Gaps (IFVG)
- FVG Retracements
- Equal Highs (EQH)
- Equal Lows (EQL)
- Market Structure Shifts (MSS)
- Swing Highs
- Swing Lows
- Previous Day High (PDH)
- Previous Day Low (PDL)
- Asia High
- Asia Low
- Trend Lines
- Liquidity Sweeps
2026-07-02 19:52:24 +02:00
- BoS
- Trends
- CHoCHs
2026-06-29 16:23:10 +02:00
---
## Technical Indicators
Included features include:
2026-06-19 16:59:27 +02:00
- EMA 7
- EMA 21
2026-06-29 16:23:10 +02:00
- EMA Difference
- EMA Slopes
2026-06-19 16:59:27 +02:00
- ADX
- +DI
- -DI
2026-06-29 16:23:10 +02:00
- Stochastic Oscillator
2026-06-19 16:59:27 +02:00
- VWAP
2026-06-29 16:23:10 +02:00
- VWAP Upper Band
- VWAP Lower Band
2026-06-19 16:59:27 +02:00
- Volume Moving Average
2026-06-29 16:23:10 +02:00
- Session Detection
2026-06-19 16:59:27 +02:00
- Indecision Candles
2026-06-29 16:23:10 +02:00
---
2026-06-19 16:59:27 +02:00
2026-06-29 16:23:10 +02:00
## Adaptive Trade Management
2026-06-19 16:59:27 +02:00
2026-06-29 16:23:10 +02:00
The bot automatically manages:
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
- Adaptive Stop Loss
- Adaptive Take Profit
- Partial Profit Taking
- Break-even Movement
- Dynamic Position Sizing
- Risk-based Lot Calculation
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
---
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
## Machine Learning Features
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
The model learns from more than 50 engineered features including:
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
- Smart Money Concepts
- Trend
- Momentum
- Liquidity
- Session information
- Price distances
- Volume
- Volatility
- Historical sequences
2026-06-07 17:00:15 +02:00
---
2026-06-29 16:23:10 +02:00
## Trading Environment
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
The custom Gymnasium environment supports:
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
- Buy
- Sell
- Hold
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
Rewards consider:
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
- Profit
- Drawdown
- Risk
- Trade quality
- Spread
- Commission
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
---
2026-06-23 20:34:42 +02:00
2026-06-29 16:23:10 +02:00
## Project Structure
```
.
├── bot.py # Main application
├── download/ # Historical CSV data
├── models/ # Saved models
├── logs/ # Training logs
├── scaler.pkl # Feature scaler
└── README.md
```
---
2026-06-23 20:40:48 +02:00
2026-06-29 16:23:10 +02:00
## Installation
2026-06-23 20:40:48 +02:00
2026-06-29 16:23:10 +02:00
Clone the repository
2026-06-23 20:40:48 +02:00
2026-06-29 16:23:10 +02:00
```bash
git clone https://github.com/pressure679/LSTM-PPO-Reinforcement-Learning-Trading-Bot-for-MetaTrader-5-XAUUSD-
cd LSTM-PPO-Reinforcement-Learning-Trading-Bot-for-MetaTrader-5-XAUUSD-
```
2026-06-23 20:40:48 +02:00
2026-06-29 16:23:10 +02:00
Install dependencies
2026-06-23 20:40:48 +02:00
2026-06-29 16:23:10 +02:00
```bash
pip install -r requirements.txt
```
2026-06-23 20:34:42 +02:00
2026-06-07 17:00:15 +02:00
---
2026-06-19 16:59:27 +02:00
## Requirements
2026-06-07 17:00:15 +02:00
2026-06-19 16:59:27 +02:00
- Python 3.11+
- MetaTrader 5
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
Python packages:
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
```
torch
stable-baselines3
sb3-contrib
gymnasium
MetaTrader5
numpy
pandas
scikit-learn
ta
xlstm
```
---
## Historical Data
Place historical data inside the `download/` folder.
Example:
```
download/
└── xauusd-m5-bid-2023-01-01-2026-06-01.csv
2026-06-07 17:00:15 +02:00
```
2026-06-29 16:23:10 +02:00
The bot automatically calculates all technical indicators and Smart Money Concept features before training.
2026-06-07 17:00:15 +02:00
---
2026-06-29 16:23:10 +02:00
## Training
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
Train the reinforcement learning model:
2026-06-07 17:00:15 +02:00
2026-06-19 16:59:27 +02:00
```bash
2026-06-27 16:59:15 +02:00
python bot.py --train
2026-06-19 16:59:27 +02:00
```
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
Training automatically:
- Loads historical data
- Calculates technical indicators
- Builds SMC features
- Normalizes inputs
- Creates the trading environment
- Trains the PPO agent
- Saves the trained model
---
## Live Trading
Run the live trading bot:
2026-06-07 17:00:15 +02:00
2026-06-19 16:59:27 +02:00
```bash
2026-06-29 16:23:10 +02:00
python bot.py --test
2026-06-19 16:59:27 +02:00
```
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
The bot will:
- Connect to MetaTrader 5
- Read live candles
- Generate feature vectors
- Predict actions
- Execute trades
- Manage open positions automatically
---
## Feature List
Current feature set includes:
2026-06-07 17:00:15 +02:00
```
2026-06-29 16:23:10 +02:00
k
k_smooth
adx
+di
-di
EMA721_DIFF
EMA7_Slope
EMA21_Slope
indecision
bullish_ob
bearish_ob
bullish_fvg
bearish_fvg
bullish_ifvg
bearish_ifvg
eqh
eql
bullish_mb
bearish_mb
bullish_rb
bearish_rb
bullish_bb
bearish_bb
bullish_mss
bearish_mss
bullish_ob_mitigation
bearish_ob_mitigation
bullish_fvg_retracement
bearish_fvg_retracement
bullish
bearish
above_vwap
below_vwap
pdh
pdl
asia_high
asia_low
pdh_distance
pdl_distance
asia_high_distance
asia_low_distance
vwap
vwap_upper
vwap_lower
bullish_ifvg_retracement
bearish_ifvg_retracement
swing_high
swing_low
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
support_trend_line
resistance_trend_line
seq_len
session
```
---
## Risk Management
Supports:
- Fixed percentage risk
- Dynamic lot sizing
- Adaptive stop losses
- Adaptive take profits
- Partial exits
- Break-even protection
- Maximum open positions
---
## Training Statistics
The bot reports:
- Win Rate
- Profit Factor
- Sharpe Ratio
- Sortino Ratio
- Weekly Return
- Maximum Drawdown
- Mean Win
- Mean Loss
- Average R
- Trade Count
---
## Roadmap
- [ ] Multi-symbol training
- [ ] Multi-timeframe observations
- [ ] Prioritized Experience Replay
- [ ] Hyperparameter optimization
- [ ] Walk-forward validation
- [ ] ONNX export
- [ ] Transformer/xLSTM hybrid policy
- [ ] Trade visualization dashboard
- [ ] Pattern similarity search
- [ ] Explainable AI trade analysis
2026-06-22 20:04:50 +02:00
2026-06-07 17:00:15 +02:00
---
## Disclaimer
2026-06-29 16:23:10 +02:00
This project is intended for educational and research purposes only.
Trading financial markets involves substantial risk. Past performance does not guarantee future results.
Always test on a demo account before trading with real funds.
---
## License
2026-06-07 17:00:15 +02:00
2026-06-29 16:23:10 +02:00
This project is licensed under the MIT License.