gogo2/readme.md
Dobromir Popov 3871afd4b8 init
2025-03-18 09:23:09 +02:00

183 lines
5.2 KiB
Markdown

# Crypto Trading Bot with Reinforcement Learning
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.
## 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
## 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:
```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
```
### Evaluation Mode
```bash
python main.py --mode eval --episodes 10
```
### Live Trading Mode
```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
```
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
This makes it perfect for testing strategies without financial risk.
## Configuration
Key parameters can be adjusted in `main.py`:
- `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
## Model Architecture
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
## Monitoring
Training progress can be monitored using TensorBoard:
Training progress is logged to TensorBoard:
```bash
tensorboard --logdir=logs
```
This will show:
- Training rewards
- Account balance
- Win rate
- Loss metrics
## Trading Strategy
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
## Safety Features
- Demo mode for safe testing
- Automatic stop-loss
- Position size limits
- Error handling for API calls
- Logging of all actions
## 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
## 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.
## License
[MIT License](LICENSE)
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.