An automated trading bot for cryptocurrency futures trading on Binance, leveraging LSTM (Long Short-Term Memory) neural networks for price prediction and automated trading execution.
This bot combines deep learning with automated trading to create a sophisticated cryptocurrency trading system. It uses LSTM neural networks to predict price movements and automatically executes trades on Binance Futures based on these predictions.
-
LSTM Price Prediction
- Uses historical price data to predict future movements
- Implements a sliding window approach for time series analysis
- Normalizes data for better prediction accuracy
- Provides directional signals (up/down) based on prediction confidence
-
Automated Futures Trading
- Real-time order execution on Binance Futures
- Supports both long and short positions
- Automatic position management with stop-loss and take-profit orders
- Handles multiple trading pairs simultaneously
- Implements isolated margin trading with configurable leverage
-
Risk Management
- Configurable position sizes and leverage
- Automatic stop-loss and take-profit placement
- Continuous monitoring of open positions
- Error handling and graceful failure recovery
- Minimum notional value checks for order placement
The LSTMPredictor class implements the price prediction logic:
- Takes historical price data with a configurable lookback period
- Normalizes the data using MinMaxScaler
- Trains an LSTM model with the following architecture:
- LSTM layer (50 units) with return sequences
- Dropout layer (20%)
- LSTM layer (50 units)
- Dropout layer (20%)
- Dense layers for final prediction
- Generates trading signals based on predicted price movements
The BinanceFuturesTrader class handles all trading operations:
- Monitors multiple trading pairs simultaneously
- Places market orders based on LSTM predictions
- Sets leverage and margin type for each trade
- Implements automatic stop-loss and take-profit orders
- Manages open positions and orders
The main loop orchestrates the entire trading process:
- Monitors current positions and their PnL
- Processes existing positions:
- Gets new predictions for open positions
- Closes positions if prediction changes
- Looks for new trading opportunities:
- Analyzes all available trading pairs
- Places new orders based on LSTM predictions
- Implements error handling and retry mechanisms
Create a file named config.py in the bots directory with the following structure:
config = {
"name": "binance",
"key": "your_binance_api_key",
"secret": "your_binance_api_secret"
}name: Always set to "binance"key: Your Binance API keysecret: Your Binance API secret key- Generate these from your Binance account under API Management
- Ensure Futures Trading is enabled for your API key
volume: Amount in USDT for each tradetp_percentage: Take profit target (percentage)sl_percentage: Stop loss limit (percentage)leverage: Trading leverage (1x to 20x)margin_type: ISOLATED or CROSSED margin type
List of cryptocurrency pairs to trade (must be valid Binance Futures trading pairs)
lookback_period: Historical data points used for predictionprediction_threshold: Minimum confidence level for trade execution
- Never commit your
config.pyfile to version control - Keep your API keys secure and never share them
- Use IP restrictions in Binance API settings
- Add
config.pyto your.gitignorefile
The bot can be configured through the TradingConfig class:
volume: Trading volume in USDTtp_percentage: Take profit percentagesl_percentage: Stop loss percentageleverage: Trading leverage (default: 1x)margin_type: Margin type (ISOLATED/CROSSED)
- Python 3.8+
- TensorFlow 2.x
- Binance Futures account with API access
- Required Python packages:
- numpy
- pandas
- keras
- python-binance
- scikit-learn
- Python 3.8-3.11 (TensorFlow is not compatible with Python 3.13)
- pip (Python package installer)
- Clone the repository:
git clone https://github.com/halitince7/ai_trading_bot.git
cd ai_trading_bot- Create and activate a virtual environment:
# Create virtual environment
python -m venv venv
# Activate virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows:
# venv\Scripts\activate- Install dependencies:
pip install -r requirements.txtRun the trading bot:
cd bots
python future_trading.pyThis trading bot is for educational and experimental purposes. Please note:
- Cryptocurrency trading involves substantial risk
- Past performance does not guarantee future results
- Only trade with funds you can afford to lose
- Test thoroughly with small amounts before deploying significant capital
- Add more sophisticated LSTM architectures
- Implement portfolio management
- Add backtesting capabilities
- Include more technical indicators
- Add web interface for monitoring
- Implement dynamic position sizing
- Add more risk management features
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License.