This Python script implements a hybrid trading strategy that uses both deep learning (LSTM) and machine learning (XGBoost) models to predict stock price movements and execute trades automatically. The strategy fetches real-time stock data from the Alpaca API and applies advanced technical indicators such as Bollinger Bands, MACD, and On-Balance Volume (OBV) for feature engineering. It executes trades for multiple stock tickers (e.g., AAPL, GOOGL, TSLA), leveraging the strengths of both LSTM for time-series analysis and XGBoost for non-linear relationships.
- Deep Learning (LSTM): Captures temporal dependencies in stock price data, making it suitable for time-series predictions.
- Machine Learning (XGBoost): Complements LSTM by handling tabular data and non-linear relationships.
- Hybrid Model: Combines LSTM and XGBoost predictions to enhance accuracy.
- Technical Indicators: Includes Bollinger Bands, MACD, OBV, and ATR for better market analysis.
- Real-Time Data: Fetches stock market data in real time using Alpaca API, allowing the script to react dynamically to the latest market conditions.
- Automatic Trade Execution: Uses limit orders for both long and short positions, minimizing slippage and optimizing trade execution.
- Periodic Model Retraining: Retrains the models periodically to ensure the strategy adapts to changing market conditions.
- Python 3.x
- Required Python libraries:
alpaca-trade-api
pandas
numpy
tensorflow
(for LSTM)xgboost
scikit-learn
yfinance
You can install the necessary libraries using the following command:
pip install alpaca-trade-api pandas numpy tensorflow xgboost scikit-learn yfinance
-
Alpaca API Setup:
- Create an account at Alpaca to get your API Key and Secret Key.
- Insert your API credentials into the script:
API_KEY = "your_alpaca_api_key" SECRET_KEY = "your_alpaca_secret_key"
-
Stock Ticker Configuration:
- Add the stock tickers you want to trade in the
tickers
list:
tickers = ['AAPL', 'GOOGL', 'TSLA'] # Add tickers of your choice
- Add the stock tickers you want to trade in the
-
Model Retraining Interval:
- You can adjust how frequently the model retrains itself by modifying the
retrain_interval
parameter (e.g., every 10 minutes):
retrain_interval = 60 * 10 # Retrains the model every 10 minutes
- You can adjust how frequently the model retrains itself by modifying the
-
Run the Script:
- Make sure all dependencies are installed, and the Alpaca API keys are configured correctly.
- Run the script in your terminal or command prompt:
python hybrid_trading_strategy.py
-
Monitoring:
- The script will print the portfolio value at regular intervals. You can monitor the progress and stop the script at any time by using
Ctrl+C
.
- The script will print the portfolio value at regular intervals. You can monitor the progress and stop the script at any time by using
-
add_technical_indicators
:- Adds technical indicators like Bollinger Bands, MACD, OBV, and ATR to the raw stock data, enriching the dataset for both the LSTM and XGBoost models.
-
create_lstm_model
:- Defines the architecture of the LSTM model, including layers and dropout for reducing overfitting.
-
train_hybrid_model
:- Trains both LSTM and XGBoost models using historical stock data and technical indicators as features.
-
get_latest_data
:- Fetches real-time stock data from Alpaca and applies the same technical indicators used during model training.
-
run_hybrid_strategy
:- Combines predictions from the LSTM and XGBoost models and executes buy/sell trades based on the averaged prediction.
-
place_limit_order
:- Executes buy or sell trades with limit orders, optimizing trade execution to reduce slippage.
- API Limits: Alpaca API has rate limits. Ensure your script complies with these limits to avoid throttling.
- Market Conditions: This strategy is designed for real-time, high-frequency trading and may behave differently in low-volume or highly volatile markets.
- Backtesting: It is recommended to backtest your strategy with historical data before deploying it in live markets.
This Python script provides a robust, hybrid trading solution that combines the predictive strengths of LSTM and XGBoost models. It adapts dynamically to changing market conditions by continuously retraining itself, making it a powerful tool for algorithmic trading in real-time stock markets. By leveraging advanced technical indicators and automating trade execution, it offers an effective way to engage in high-frequency trading strategies.