An advanced crypto trading bot that uses Reinforcement Learning (PPO) to make trading decisions. The bot features real-time market analysis, risk management, and automatic learning capabilities.
- Reinforcement Learning: Uses Proximal Policy Optimization (PPO) to learn from trading experiences
- Real Market Data: Connects to Binance for real-time market data
- Paper Trading: Test strategies with real market data without risking real money
- Risk Management: Advanced position sizing and risk controls
- Market Analysis: Technical indicators, market state analysis, and correlation tracking
- Performance Tracking: Comprehensive metrics and decision analysis
- Automatic Learning: Continuous improvement through experience
- Node.js 16+ or Bun runtime
- Binance account (for real trading)
- API keys (for real trading)
- Clone the repository:
git clone https://github.com/oyi77/Crypto-RL-Trading-Bot.git
cd crypto-trading-assistant- Install dependencies:
bun install- Create environment file:
cp .env.example .envEdit the .env file with your settings:
# Trading Configuration
SYMBOLS=BTCUSDT,ETHUSDT,BNBUSDT
TIMEFRAME=1h
INITIAL_CAPITAL=10000
# Risk Management
MAX_RISK_PER_TRADE=0.02
MAX_LEVERAGE=10
STOP_LOSS_DISTANCE=0.02
TAKE_PROFIT_DISTANCE=0.04
TRAILING_STOP_DISTANCE=0.01
MAX_OPEN_POSITIONS=5
# Training Configuration
BATCH_SIZE=64
EPOCHS=10
VALIDATION_SPLIT=0.2
MIN_TRADES=100
MIN_WIN_RATE=0.55
MAX_DRAWDOWN=0.2
RETRAIN_INTERVAL=86400000
# Binance API (for real trading)
BINANCE_API_KEY=your_api_key
BINANCE_API_SECRET=your_api_secret- Basic Trading Configuration:
# Trading pairs to monitor and trade
SYMBOLS=BTCUSDT,ETHUSDT,BNBUSDT # Start with 1-3 pairs for testing
# Timeframe for analysis and trading
TIMEFRAME=1h # Options: 1m, 5m, 15m, 1h, 4h, 1d
# Initial capital for paper trading
INITIAL_CAPITAL=10000 # Start with a reasonable amount for testing- Risk Management Settings:
# Maximum risk per trade (as percentage of capital)
MAX_RISK_PER_TRADE=0.02 # 2% risk per trade
# Maximum leverage to use
MAX_LEVERAGE=5 # Start with lower leverage for testing
# Stop loss and take profit distances (as percentage)
STOP_LOSS_DISTANCE=0.02 # 2% stop loss
TAKE_PROFIT_DISTANCE=0.04 # 4% take profit
TRAILING_STOP_DISTANCE=0.01 # 1% trailing stop
# Maximum number of concurrent positions
MAX_OPEN_POSITIONS=3 # Start with fewer positions- Training Parameters:
# Batch size for training
BATCH_SIZE=64 # Number of experiences to train on at once
# Number of training epochs
EPOCHS=10 # Number of times to iterate over the batch
# Validation split for training
VALIDATION_SPLIT=0.2 # 20% of data used for validation
# Minimum trades before retraining
MIN_TRADES=100 # Minimum number of trades to collect before training
# Performance thresholds
MIN_WIN_RATE=0.55 # Minimum win rate to consider strategy successful
MAX_DRAWDOWN=0.2 # Maximum allowed drawdown (20%)
# Retraining interval in milliseconds (24 hours)
RETRAIN_INTERVAL=86400000- RL State Configuration:
# State window size (number of candles to look back)
STATE_WINDOW=50 # How many past candles to consider
# Technical indicators to include in state
INDICATORS=EMA,MACD,RSI,VOLUME # Indicators to use for decision making
# Market conditions to consider
MARKET_CONDITIONS=TREND,VOLATILITY,VOLUME # Market aspects to analyze- Initial Setup:
# 1. Create .env file
cp .env.example .env
# 2. Edit .env with your settings
# Start with conservative values:
SYMBOLS=BTCUSDT
TIMEFRAME=1h
INITIAL_CAPITAL=10000
MAX_RISK_PER_TRADE=0.02
MAX_LEVERAGE=5- Start Paper Trading:
bun run src/scripts/runPaperTrading.ts- Monitor Learning Progress:
- The bot will log training metrics every RETRAIN_INTERVAL
- Check logs/training.log for detailed learning statistics
- Monitor win rate and drawdown in logs/portfolio.log
- Data Collection Phase:
- Bot collects trading experiences
- Minimum MIN_TRADES required before first training
- Experiences stored in memory for training
- Training Phase:
- Occurs every RETRAIN_INTERVAL
- Uses collected experiences to update policy
- Validates performance on separate dataset
- Performance Monitoring:
- Tracks win rate and drawdown
- Adjusts strategy if performance drops
- Logs learning progress and metrics
- For Faster Learning:
# Reduce batch size for more frequent updates
BATCH_SIZE=32
# Increase training frequency
RETRAIN_INTERVAL=43200000 # 12 hours
# Lower minimum trades requirement
MIN_TRADES=50- For More Stable Learning:
# Increase batch size for more stable updates
BATCH_SIZE=128
# Increase epochs for better convergence
EPOCHS=20
# Higher minimum trades for more data
MIN_TRADES=200- For Better Performance:
# Stricter performance requirements
MIN_WIN_RATE=0.6
MAX_DRAWDOWN=0.15
# More frequent validation
VALIDATION_SPLIT=0.3Paper trading uses real market data but simulates trades without using real money. This is perfect for testing strategies.
- Start the paper trading bot:
bun run src/scripts/runPaperTrading.tsThe bot will:
- Connect to Binance for real market data
- Simulate trades using real prices
- Track positions and PnL
- Log performance metrics every 5 minutes
- Provide a comprehensive summary when stopped
For real trading, you'll need Binance API credentials.
- Set up your Binance API keys in
.env:
BINANCE_API_KEY=your_api_key
BINANCE_API_SECRET=your_api_secret- Start the real trading bot:
bun run src/scripts/runRealTrading.tsThe bot logs various information:
-
Portfolio Metrics (every 5 minutes):
- Total value
- Unrealized PnL
- Realized PnL
- Win rate
- Profit factor
- Sharpe ratio
- Maximum drawdown
-
Trading Decisions:
- Entry/exit points
- Position sizes
- Risk assessments
- Market conditions
-
Performance Analysis:
- Decision accuracy
- Market condition performance
- Risk-adjusted returns
Press Ctrl+C to stop the bot gracefully. It will:
- Close all open positions
- Save the current state
- Log a final performance summary
The bot includes several risk management features:
-
Position Sizing:
- Maximum risk per trade
- Dynamic position sizing based on volatility
- Leverage limits
-
Stop Losses:
- Fixed stop losses
- Trailing stops
- Take profit targets
-
Portfolio Limits:
- Maximum open positions
- Correlation-based position limits
- Daily loss limits
The bot uses PPO (Proximal Policy Optimization) to learn from trading experiences:
-
State Representation:
- Price data
- Volume
- Technical indicators
- Market conditions
-
Action Space:
- LONG
- SHORT
- HOLD
-
Reward Function:
- PnL-based rewards
- Risk-adjusted returns
- Time-based penalties
-
Training Process:
- Automatic retraining
- Experience replay
- Policy optimization
Common issues and solutions:
-
API Connection Issues:
- Check your API keys
- Verify network connection
- Ensure correct permissions
-
Performance Issues:
- Reduce number of symbols
- Increase timeframe
- Optimize batch size
-
Memory Issues:
- Clear old logs
- Reduce history length
- Optimize data structures
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Submit a pull request
MIT License - see LICENSE file for details