Skip to content

Sumit6258/Reinforcement-Trading-ETH

Repository files navigation

Ethereum Trading Bot - Complete Setup Guide

📚 What is This?

This is a complete Reinforcement Learning AI that learns to trade Ethereum (ETH/USD) automatically. The bot:

  • Learns from historical price data
  • Decides when to buy, sell, or hold
  • Manages stop losses and take profits
  • Gets smarter through trial and error (600,000+ practice trades!)

Perfect for beginners learning AI and algorithmic trading!


📁 Files in This Package

Data Files

  • ETHUSD_Candlestick_1_Hour_2020_2023.csv - Training data (3 years)
  • ETHUSD_Candlestick_1_Hour_2023_2025.csv - Testing data (2 years)
  • generate_eth_data.py - Script that created the synthetic data

Core Code Files

  • indicators.py - Calculates technical indicators (RSI, Moving Averages, etc.)
  • trading_env_eth.py - The trading simulator (the "game" the AI plays)
  • train_agent_eth.py - Trains the AI bot
  • test_agent_eth.py - Tests the trained bot and generates reports

Output Files (created after running)

  • model_ethereum_best.zip - Your trained AI model
  • ethereum_trade_history.csv - Detailed log of every trade
  • ethereum_training_results.png - Performance charts
  • ethereum_test_results.png - Test results charts
  • checkpoints_eth/ - Saved models during training

🚀 Quick Start (3 Steps!)

Step 1: Install Requirements

pip install stable-baselines3 pandas numpy matplotlib gymnasium

Or if you're on Linux/Mac:

pip install stable-baselines3 pandas numpy matplotlib gymnasium --break-system-packages

Step 2: Train Your Bot

python train_agent_eth.py

This takes 2-6 hours! ⏰ Go watch a movie, the AI is learning!

You'll see output like:

Training for 600,000 timesteps...
---------------------------------
| rollout/           |          |
|    ep_len_mean     | 1500     |
|    ep_rew_mean     | 125.3    |
...

Step 3: Test Your Bot

python test_agent_eth.py

This runs in minutes and creates:

  • ethereum_trade_history.csv - All trades with details
  • ethereum_test_results.png - Beautiful charts

That's it! 🎉


📊 Understanding the Results

Good Results Look Like:

✅ Test equity curve going UP
✅ Win rate around 40-60%
✅ Profit factor > 1.0
✅ Max drawdown < 30%
✅ Makes 10-50 trades (not too many, not too few)

Bad Results Look Like:

❌ Test equity curve going DOWN
❌ Win rate < 30% or > 80% (suspicious)
❌ Profit factor < 1.0
❌ Max drawdown > 50%
❌ Too many trades (>100) = overtrading
❌ Too few trades (<5) = not learning

Example Output:

TRADE ANALYSIS
======================================================================

Total trades: 23

Win/Loss breakdown:
  Winning trades:   14 (60.9%)
  Losing trades:     9 (39.1%)

Winning trades:
  Average win: $43.25
  Largest win: $127.50

Losing trades:
  Average loss: $-28.33
  Largest loss: $-51.20

Total P&L: +$350.17
Profit factor: 2.11 (>1 is profitable)

Average time in trade: 18.5 hours (0.8 days)

🎓 How It Works (Beginner Explanation)

The Big Picture

  1. Historical Data → You feed the AI 3 years of Ethereum prices
  2. Learning Phase → AI tries random trades, gets rewards/penalties
  3. Smart Phase → After 600k tries, AI learns what works
  4. Testing → Run on unseen data to see if it really learned

What the AI Sees

Every hour, the AI looks at:

  • RSI (is momentum too high/low?)
  • Moving averages (what's the trend?)
  • Price changes (is it moving fast?)
  • Volume (is there strong interest?)
  • Its current position (am I in a trade?)

What the AI Can Do

  • HOLD - Do nothing this hour
  • CLOSE - Close my current trade
  • OPEN LONG - Buy ETH (bet price goes up)
  • OPEN SHORT - Sell ETH (bet price goes down)
    • Choose stop loss: $20, $50, $100, or $150
    • Choose take profit: $30, $75, $150, or $225

How It Learns

  • ✅ Make profitable trade → Get positive reward → Do more of that
  • ❌ Lose money → Get negative reward → Do less of that
  • After 600,000 tries, it figures out patterns!

⚙️ Customization Guide

Want to Change Trading Parameters?

Edit train_agent_eth.py:

# Stop Loss options (dollars)
SL_OPTS = [20, 50, 100, 150]  # ← Change these!

# Take Profit options (dollars)
TP_OPTS = [30, 75, 150, 225]  # ← Change these!

# Position size (ETH per trade)
position_size_eth=0.1,  # ← 0.1 ETH = ~$300 at $3000/ETH

# Trading costs
spread_pct=0.1,          # ← 0.1% = typical exchange fee
commission_dollars=0.5,  # ← Flat fee per trade

# Training duration
total_timesteps = 600_000  # ← More = better but slower

Want Different Indicators?

Edit indicators.py:

# Add your own indicators!
df["my_indicator"] = ...  # Calculate something

# Add to feature list
feature_cols = [
    "rsi_14",
    "ma_20_slope",
    # ... other features ...
    "my_indicator",  # ← Add yours here!
]

Want to Train on Real Data?

Replace generate_eth_data.py with real data from:

  • Binance API
  • CoinGecko API
  • Yahoo Finance
  • Or any CSV with columns: Gmt time, Open, High, Low, Close, Volume

🐛 Troubleshooting

"ModuleNotFoundError: No module named 'stable_baselines3'"

Solution: Install requirements

pip install stable-baselines3

"KeyError: 'Close'"

Solution: Your CSV has wrong column names. Make sure it has:

  • Gmt time
  • Open
  • High
  • Low
  • Close
  • Volume

"FileNotFoundError: model_ethereum_best.zip"

Solution: You need to train first!

python train_agent_eth.py  # This creates the model
python test_agent_eth.py   # Now you can test

Training is too slow

Solution: Reduce total_timesteps in train_agent_eth.py:

total_timesteps = 100_000  # Instead of 600_000

Less training = faster but potentially worse results.

Bot loses money on test data

Solution: This is normal! Trading is hard. Try:

  1. Train longer (increase timesteps)
  2. Adjust SL/TP options
  3. Add more indicators
  4. Change reward parameters
  5. Use more training data

Remember: Even professional traders have losing strategies!


📈 Advanced Features

View Training Progress in Real-Time

Install TensorBoard:

pip install tensorboard

While training is running, open another terminal:

tensorboard --logdir=./tensorboard_log_eth/

Open browser to: http://localhost:6006

You'll see live graphs of:

  • Reward per episode
  • Episode length
  • Policy loss
  • Value function loss

Resume Training from Checkpoint

If training crashes, resume from last checkpoint:

# In train_agent_eth.py, replace:
model = PPO(policy="MlpPolicy", env=train_vec_env, ...)

# With:
model = PPO.load("checkpoints_eth/ppo_ethereum_500000_steps", env=train_vec_env)
model.learn(total_timesteps=100_000)  # Train for 100k more

Test Different Checkpoints

Want to test a specific checkpoint instead of the best model?

# In test_agent_eth.py:
model = PPO.load("checkpoints_eth/ppo_ethereum_300000_steps", env=vec_test_env)

🎯 Project Structure

ethereum-trading-bot/
│
├── Data/
│   ├── ETHUSD_Candlestick_1_Hour_2020_2023.csv
│   └── ETHUSD_Candlestick_1_Hour_2023_2025.csv
│
├── Core Code/
│   ├── indicators.py              # Feature engineering
│   ├── trading_env_eth.py         # Trading simulator
│   ├── train_agent_eth.py         # Training script
│   └── test_agent_eth.py          # Testing script
│
├── Generated (after training)/
│   ├── model_ethereum_best.zip    # Best model
│   ├── ethereum_trade_history.csv # Trade log
│   ├── ethereum_training_results.png
│   ├── ethereum_test_results.png
│   └── checkpoints_eth/           # Saved models
│       ├── ppo_ethereum_50000_steps.zip
│       ├── ppo_ethereum_100000_steps.zip
│       └── ...
│
└── README.md                      # This file!

🤔 FAQ

Q: Will this make me rich?

A: No. This is a learning project using synthetic data. Real trading involves:

  • Market slippage
  • Liquidity issues
  • News events
  • Changing market conditions
  • Psychological factors

Q: Can I use this with real money?

A: NOT RECOMMENDED. This is for educational purposes only. If you must:

  1. Test extensively on real historical data
  2. Paper trade for months
  3. Start with tiny positions
  4. Never risk more than you can afford to lose

Q: Why use RL instead of simple rules?

A: RL can discover complex patterns humans might miss. But it's also:

  • Harder to interpret ("black box")
  • Requires lots of data
  • Can overfit to training data
  • May fail in unexpected ways

Q: What's a good win rate?

A: 40-60% is excellent if average win > average loss. Even 30% win rate can be profitable with good risk management!

Q: How is this different from Forex trading?

A: Main differences:

  • Crypto is more volatile → Bigger moves, bigger rewards/losses
  • Different spreads → Percentage-based not pip-based
  • 24/7 trading → No market close (though this data is hourly)
  • Higher fees → Exchanges charge more than Forex brokers

Q: Can I trade other cryptocurrencies?

A: Yes! Just:

  1. Get price data for BTC, ADA, SOL, etc.
  2. Adjust position_size_eth for that crypto
  3. Adjust SL/TP values for its volatility
  4. Retrain from scratch

Q: What is PPO?

A: Proximal Policy Optimization - a reinforcement learning algorithm that:

  • Is relatively stable
  • Works well for continuous control
  • Doesn't require huge datasets
  • Popular for trading applications

📚 Learn More

Reinforcement Learning

Trading & Technical Analysis

Python & Data Science


📝 License

This project is for educational purposes. Use at your own risk!


🙏 Acknowledgments

  • Stable Baselines3 team for the amazing RL library
  • OpenAI Gym/Gymnasium for the environment framework
  • The crypto and RL communities for inspiration

💡 Next Steps

  1. ✅ Run the basic training
  2. ✅ Analyze the results
  3. 📊 Try different parameters
  4. 🧪 Add more indicators
  5. 📚 Study winning vs losing trades
  6. 🔄 Iterate and improve!

Good luck and happy learning! 🚀

Remember: The goal is to LEARN about AI and trading, not to get rich quick!

About

Reinforcement Learning AI that learns to trade Ethereum (ETH/USD) automatically

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages