new__training
This commit is contained in:
278
readme.md
278
readme.md
@ -1,182 +1,178 @@
|
||||
# Crypto Trading Bot with Reinforcement Learning
|
||||
# Clean Trading System
|
||||
|
||||
An automated cryptocurrency trading bot that uses Deep Q-Learning (DQN) to trade ETH/USDT on the MEXC exchange. The bot features a sophisticated neural network architecture with LSTM layers and attention mechanisms for better pattern recognition.
|
||||
A modular, scalable cryptocurrency trading system with CNN and RL components for multi-timeframe analysis.
|
||||
|
||||
## 🚨 CRITICAL: REAL MARKET DATA ONLY
|
||||
|
||||
**This system uses EXCLUSIVELY real market data from cryptocurrency exchanges. NO synthetic, generated, or simulated data is allowed for training, testing, or inference.**
|
||||
|
||||
See [REAL_MARKET_DATA_POLICY.md](REAL_MARKET_DATA_POLICY.md) for complete guidelines.
|
||||
|
||||
## Features
|
||||
|
||||
- Deep Q-Learning with experience replay
|
||||
- LSTM layers for sequential data processing
|
||||
- Multi-head attention mechanism
|
||||
- Dueling DQN architecture
|
||||
- Real-time trading capabilities
|
||||
- TensorBoard integration for monitoring
|
||||
- Comprehensive technical indicators
|
||||
- Demo and live trading modes
|
||||
- Automatic model checkpointing
|
||||
- **Multi-timeframe Analysis**: 1s, 1m, 5m, 1h, 4h, 1d scalping focus
|
||||
- **CNN Pattern Recognition**: Real market pattern detection with temporal attention
|
||||
- **RL Trading Agent**: Reinforcement learning with real historical backtesting
|
||||
- **Real-time Data**: Live market data from Binance API
|
||||
- **Web Dashboard**: Real-time monitoring and visualization
|
||||
- **Modular Architecture**: Clean separation of concerns
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Python 3.8+
|
||||
- MEXC Exchange API credentials
|
||||
- GPU recommended but not required
|
||||
|
||||
## Installation
|
||||
|
||||
1. Clone the repository:
|
||||
|
||||
```bash
|
||||
git clone https://github.com/yourusername/crypto-trading-bot.git
|
||||
cd crypto-trading-bot
|
||||
```
|
||||
2. Create a virtual environment:
|
||||
|
||||
```bash
|
||||
python -m venv venv
|
||||
source venv/bin/activate # On Windows: venv\Scripts\activate
|
||||
```
|
||||
3. Install dependencies:
|
||||
## Quick Start
|
||||
|
||||
### 1. Install Dependencies
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
|
||||
4. Create a `.env` file in the project root with your MEXC API credentials:
|
||||
|
||||
```bash
|
||||
MEXC_API_KEY=your_api_key
|
||||
MEXC_API_SECRET=your_api_secret
|
||||
```
|
||||
## Usage
|
||||
|
||||
The bot can be run in three modes:
|
||||
|
||||
### Training Mode
|
||||
|
||||
```bash
|
||||
python main.py --mode train --episodes 1000
|
||||
### 2. Configure Settings
|
||||
Edit `config.yaml` to set your preferences:
|
||||
```yaml
|
||||
symbols: ["ETH/USDT", "BTC/USDT"]
|
||||
timeframes: ["1s", "1m", "5m", "1h", "4h"]
|
||||
training:
|
||||
use_only_real_data: true # CRITICAL: Never change this
|
||||
```
|
||||
|
||||
### Evaluation Mode
|
||||
|
||||
### 3. Train CNN Model (Real Data Only)
|
||||
```bash
|
||||
python main.py --mode eval --episodes 10
|
||||
python main_clean.py --mode cnn --symbol ETH/USDT
|
||||
```
|
||||
|
||||
### Live Trading Mode
|
||||
|
||||
### 4. Train RL Agent (Real Data Only)
|
||||
```bash
|
||||
# Demo mode (simulated trading with real market data)
|
||||
python main.py --mode live --demo
|
||||
|
||||
# Real trading (actual trades on MEXC)
|
||||
python main.py --mode live
|
||||
python main_clean.py --mode rl --symbol ETH/USDT
|
||||
```
|
||||
|
||||
Demo mode simulates trading using real-time market data but does not execute actual trades. It still:
|
||||
- Logs all trading decisions and performance metrics
|
||||
- Updates the model based on market data (if in training mode)
|
||||
- Displays real-time analytics and position information
|
||||
- Calculates theoretical profits/losses
|
||||
- Saves performance data to TensorBoard
|
||||
### 5. Launch Web Dashboard
|
||||
```bash
|
||||
python main_clean.py --mode web --port 8050
|
||||
```
|
||||
|
||||
This makes it perfect for testing strategies without financial risk.
|
||||
## Architecture
|
||||
|
||||
## Configuration
|
||||
```
|
||||
gogo2/
|
||||
├── core/ # Core system components
|
||||
│ ├── config.py # Configuration management
|
||||
│ ├── data_provider.py # Real market data fetching
|
||||
│ └── orchestrator.py # Decision coordination
|
||||
├── models/ # AI models (real data only)
|
||||
│ ├── cnn/ # CNN pattern recognition
|
||||
│ └── rl/ # RL trading agent
|
||||
├── training/ # Training pipelines
|
||||
│ ├── cnn_trainer.py # CNN training with real data
|
||||
│ └── rl_trainer.py # RL training with real data
|
||||
├── web/ # Web dashboard
|
||||
└── main_clean.py # Unified entry point
|
||||
```
|
||||
|
||||
Key parameters can be adjusted in `main.py`:
|
||||
## Data Sources
|
||||
|
||||
- `INITIAL_BALANCE`: Starting balance for training/demo
|
||||
- `MAX_LEVERAGE`: Maximum leverage for trades
|
||||
- `STOP_LOSS_PERCENT`: Stop loss percentage
|
||||
- `TAKE_PROFIT_PERCENT`: Take profit percentage
|
||||
- `BATCH_SIZE`: Training batch size
|
||||
- `LEARNING_RATE`: Model learning rate
|
||||
- `STATE_SIZE`: Size of the state representation
|
||||
### ✅ Approved Sources
|
||||
- Binance API (real-time and historical)
|
||||
- Cached real market data
|
||||
- TimescaleDB with real data
|
||||
|
||||
## Model Architecture
|
||||
### ❌ Prohibited Sources
|
||||
- Synthetic data generation
|
||||
- Random data simulation
|
||||
- Mock market conditions
|
||||
|
||||
The DQN model includes:
|
||||
- Input layer with technical indicators
|
||||
- LSTM layers for temporal pattern recognition
|
||||
- Multi-head attention mechanism
|
||||
- Dueling architecture for better Q-value estimation
|
||||
- Batch normalization for stable training
|
||||
## Training Modes
|
||||
|
||||
### CNN Training
|
||||
```bash
|
||||
# Train on real ETH/USDT data
|
||||
python main_clean.py --mode cnn --symbol ETH/USDT
|
||||
|
||||
# Quick test with real data
|
||||
python test_cnn_only.py
|
||||
```
|
||||
|
||||
### RL Training
|
||||
```bash
|
||||
# Train RL agent with real data
|
||||
python main_clean.py --mode rl --symbol ETH/USDT
|
||||
|
||||
# Real-time RL training
|
||||
python train_rl_with_realtime.py --episodes 10
|
||||
```
|
||||
|
||||
## Performance
|
||||
|
||||
- **Memory Usage**: <2GB per model
|
||||
- **Training Speed**: ~20 seconds for 50 epochs
|
||||
- **Real Data Processing**: 1000+ candles per timeframe
|
||||
- **Feature Count**: Dynamically detected from real data (typically 48)
|
||||
|
||||
## Monitoring
|
||||
|
||||
Training progress can be monitored using TensorBoard:
|
||||
|
||||
|
||||
Training progress is logged to TensorBoard:
|
||||
|
||||
```bash
|
||||
tensorboard --logdir=logs
|
||||
All operations log their data sources:
|
||||
```
|
||||
INFO - Generating 10000 training cases for ETH/USDT from REAL market data
|
||||
INFO - Loaded 1000 real candles for ETH/USDT 1s
|
||||
INFO - Building network with 48 features from real market data
|
||||
```
|
||||
|
||||
This will show:
|
||||
- Training rewards
|
||||
- Account balance
|
||||
- Win rate
|
||||
- Loss metrics
|
||||
## Testing
|
||||
|
||||
## Trading Strategy
|
||||
```bash
|
||||
# Test data provider with real data
|
||||
python -m pytest tests/test_data_provider.py
|
||||
|
||||
The bot makes decisions based on:
|
||||
- Price action
|
||||
- Technical indicators (RSI, MACD, Bollinger Bands, etc.)
|
||||
- Historical patterns through LSTM
|
||||
- Risk management with stop-loss and take-profit
|
||||
# Test CNN with real data
|
||||
python test_cnn_only.py
|
||||
|
||||
# Test full system
|
||||
python main_clean.py --mode test
|
||||
```
|
||||
|
||||
## Web Dashboard
|
||||
|
||||
Access at `http://localhost:8050` for:
|
||||
- Real-time price charts
|
||||
- Model predictions
|
||||
- Trading performance
|
||||
- System metrics
|
||||
|
||||
## Configuration
|
||||
|
||||
Key settings in `config.yaml`:
|
||||
```yaml
|
||||
data:
|
||||
provider: "binance" # Real exchange API
|
||||
cache_enabled: true # Cache real data
|
||||
real_time_enabled: true # Live data feed
|
||||
|
||||
training:
|
||||
use_only_real_data: true # NEVER change this
|
||||
batch_size: 32
|
||||
epochs: 100
|
||||
|
||||
trading:
|
||||
max_position_size: 0.1
|
||||
trading_fee: 0.0002
|
||||
```
|
||||
|
||||
## Safety Features
|
||||
|
||||
- Demo mode for safe testing
|
||||
- Automatic stop-loss
|
||||
- Position size limits
|
||||
- Error handling for API calls
|
||||
- Logging of all actions
|
||||
- **Data Validation**: Ensures all data comes from real sources
|
||||
- **Cache Verification**: Validates cached data authenticity
|
||||
- **Training Monitoring**: Logs all data sources
|
||||
- **Emergency Stops**: Halts training if synthetic data detected
|
||||
|
||||
## Directory Structure
|
||||
├── main.py # Main bot implementation
|
||||
├── requirements.txt # Project dependencies
|
||||
├── .env # API credentials
|
||||
├── models/ # Saved model checkpoints
|
||||
├── runs/ # TensorBoard logs
|
||||
└── trading_bot.log # Activity logs
|
||||
## Contributing
|
||||
|
||||
|
||||
## Warning
|
||||
|
||||
Cryptocurrency trading carries significant risks. This bot is for educational purposes and should not be used with real money without thorough testing and understanding of the risks involved.
|
||||
When contributing:
|
||||
1. **NEVER** introduce synthetic data generation
|
||||
2. Always use real market data for testing
|
||||
3. Log data sources clearly
|
||||
4. Follow the real data policy strictly
|
||||
|
||||
## License
|
||||
|
||||
[MIT License](LICENSE)
|
||||
This project is for educational and research purposes. Use real market data responsibly.
|
||||
|
||||
The main changes I made:
|
||||
Fixed code block formatting by adding proper language identifiers
|
||||
Added missing closing code blocks
|
||||
Properly formatted directory structure
|
||||
Added complete sections that were cut off in the original
|
||||
Ensured consistent formatting throughout the document
|
||||
Added proper bash syntax highlighting for command examples
|
||||
The README.md now provides a complete guide for setting up and using the trading bot, with clear sections for installation, usage, configuration, and safety considerations.
|
||||
---
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Edits/improvements
|
||||
|
||||
Fixes the shape mismatch by ensuring the state vector is exactly STATE_SIZE elements
|
||||
Adds robust error handling in the model's forward pass to handle mismatched inputs
|
||||
Adds a transformer encoder for more sophisticated pattern recognition
|
||||
Provides an expand_model method to increase model capacity while preserving learned weights
|
||||
Adds detailed logging about model size and shape mismatches
|
||||
The model now has:
|
||||
Configurable hidden layer sizes
|
||||
Transformer layers for complex pattern recognition
|
||||
LSTM layers for temporal patterns
|
||||
Attention mechanisms for focusing on important features
|
||||
Dueling architecture for better Q-value estimation
|
||||
With hidden_size=256, this model has about 1-2 million parameters. By increasing hidden_size to 512 or 1024, you can easily scale to 5-20 million parameters. For even larger models (billions of parameters), you would need to implement a more distributed architecture with multiple GPUs, which would require significant changes to the training loop.
|
||||
**⚠️ REMEMBER: This system's integrity depends on using only real market data. No exceptions.**
|
||||
|
Reference in New Issue
Block a user