This repository demonstrates how to train a reinforcement learning (RL) agent using Stable-Baselines3 for trading on hourly candlestick data. The project consists of:
-
A custom Gymnasium environment (env/hourly_trading_env.py) that simulates trading with:
- Multi-bar holding (positions can stay open across multiple time steps).
- Stop-Loss and Take-Profit checks at each bar.
- Commission and slippage modeling.
- Partial position closing (to support scaling out of positions).
- Detailed logging of each trade for analysis.
-
A training script (train_rl.py) that:
- Loads real historical data from a CSV file (
historical_data_1h.csv). - Creates and wraps the environment.
- Trains a PPO agent.
- Runs a quick test and prints final results.
- Stores checkpoints, logs and TensorBoard data under
runs/.
- Loads real historical data from a CSV file (
- ATR-based Stop Loss and Take Profit for risk management.
- Partial Position Closing to scale out of trades.
- Simulated Trading with commission and slippage modelling.
- Detailed Trade Logging for analysis.
The model parameters are defined in config.json:
window_size: Number of data points in a sequence.model_name: File name for saving and loading the model.data_file: CSV file containing historical data.initial_balance,trade_risk,min_balance, etc.: Parameters for simulated trading.data_top_n,data_interval,data_start_date,data_end_date,data_output_file: Parameters for downloading market history for the most traded pairs.
The model processes historical market data:
- Calculate Indicators: Computes ATR for each bar.
- Normalization: Scales data for efficient training.
- Sequence Slicing: Creates windows of historical data for input to the agent.
- Uses the PPO algorithm from Stable-Baselines3.
- Simulates trades, updates balances and logs results.
- Detailed Report: Logs individual trades, balances, thresholds, and outcomes.
- Summary Report: Tracks overall performance metrics, such as success rates and total trades.
- Clone the Repository:
git clone https://github.com/yourusername/intelligent-trading-model.git cd intelligent-trading-model - Install Dependencies:
pip install -r requirements.txt
- Prepare the Configuration: Edit
config.jsonto specify your desired parameters. - Prepare Historical Data: Ensure you have a CSV file (
historical_data_1h.csv) with the required columns:open,high,low,close,volume.
python train_rl.py- If
ppo_hourly_model.zipis found, training continues from the saved weights. - Otherwise, a new model is created and trained.
- Check training progress in the console or via TensorBoard:
tensorboard --logdir ./runs/tensorboard/
- Evaluate the model's decisions and performance using the generated reports (
detailed_report_fileandreport_file).
- Trained Model: Saved to
runs/ppo_hourly_model.zipfor future use. - Reports:
- Detailed Report: Logs individual trade metrics, including timestamps, thresholds, positions, and balances.
- Summary Report: Aggregates metrics like total trades, success rates, and final balance.
- Collect hourly historical data for a trading pair (e.g., BTC/USDT) or use
get_data.pyto download top trading pairs from Binance. - Configure the model with
window_size = 168. - Train the model using
train_rl.py. - Analyze trading performance using the detailed and summary reports.
- Automation: End-to-end pipeline from data ingestion to trading simulation.
- Dynamic Learning: Adapts thresholds and parameters for evolving market conditions.
- Comprehensive Reporting: Provides deep insights into trading strategy effectiveness.
This project is licensed under the MIT License. See the LICENSE file for details.
Developed by Ktyby21. Feel free to contact me for suggestions or collaboration opportunities.