The ML Trading Bot is a machine learning-driven algorithmic trading system built with Lumibot and Alpaca's API. It leverages FinBERT sentiment analysis to make data-driven trading decisions based on financial news sentiment. The bot is designed to backtest trading strategies on historical SPY (S&P 500 ETF) data and can be extended to real-time trading. This project enables flexible backtesting, allowing users to define custom time ranges instead of being restricted to a fixed period. Additionally, it optimizes memory management and execution speed, ensuring smooth performance even on resource-limited systems.
The original ML Trading Bot faced critical performance issues when running large backtests:
- The bot attempted to process an entire multi-year dataset at once, resulting in an out-of-memory crash at 82.35% progress.
- Excessive API calls overloaded Alpaca, increasing latency and API request limits.
- The original version only ran a fixed 4-year backtest (2020-2023), preventing users from customizing backtests.
This optimized version implements key improvements to enhance performance and usability:
- Processes data in user-defined time chunks, reducing RAM usage.
- Clears unnecessary trades and orders to free up memory.
- Caches financial news data, significantly reducing redundant API calls.
- Users can now select a specific start and end date, instead of being locked into a default period.
- Allows for shorter backtests, making faster strategy testing possible.
Follow these steps to set up, run backtests, and deploy the Auto ML Trading Bot.
git clone https://github.com/yourusername/Auto-ML-Trading-Bot.git
cd Auto-ML-Trading-Bot
Ensure you have Python 3.8+ installed, then run:
pip install -r requirements.txt
Create a .env file in the root directory and add your Alpaca API credentials:
ALPACA_API_KEY=your_api_key
ALPACA_API_SECRET=your_api_secret
OR manually edit tradingbot.py:
API_KEY = 'your_api_key'
API_SECRET = 'your_api_secret'Run the bot with customizable backtesting options:
python tradingbot.py
- Select Option 1 to use the default backtest period (2020-2023).
- Select Option 2 to enter custom start and end dates.
After the backtest completes, 2 reports will be generated into a logs folder.
- Open it in a browser to analyze returns, drawdowns, Sharpe ratio, and more.
To enable live trading, modify the script:
# Uncomment these lines in `tradingbot.py` to enable live trading
# trader = Trader()
# trader.add_strategy(strategy)
# trader.run_all()
This project is for educational purposes only and does not constitute financial advice. Trading involves risk, and you should not trade with real money unless you fully understand the risks.
This project utilizes several key technologies to facilitate machine learning-driven algorithmic trading, backtesting, and real-time execution.
- Python 3.8+ → The core language used for developing the ML trading bot.
- Lumibot → Algorithmic trading framework for live trading & backtesting
- Alpaca Trade API → Brokerage API for stock trading & historical data
- YahooDataBacktesting → Fetches historical price data for backtesting
- FinBERT → Sentiment analysis of financial news
- Requests → Handles API requests (Alpaca & news data)
- Datetime → Manages time-sensitive trading operations
- NumPy → Performs numerical calculations (Moving Averages, ATR)
- QuantStats → Generates detailed trading performance reports
- dotenv → Securely loads API credentials from
.envfile
- Git & GitHub → Version control and open-source collaboration
- VS Code → Python development environment
- Jupyter Notebook → Used for testing sentiment analysis & strategy visualization (optional)
This project was inspired by and built upon the work of Nick Nochnack's MLTradingBot.
The goal of this project was to extend the functionality of the original MLTradingBot to improve memory efficiency, optimize API usage, and enhance backtesting customization, addressing limitations encountered on my own system.

