This trading bot was developed for research purposes only. It is not intended to be used as a tool for trading or making financial decisions
This trading bot was built using the Lumibot framework and the Alpaca API. The bot uses a trading strategy based on the Moving Average Convergence Divergence (MACD) indicator to generate and execute buy and sell signals for different stocks.
- MACD-Based Strategy: The bot uses the MACD indicator along with other methods to identify buy and sell opportunities.
- Automated Trading: Trades can be automatically executed using Alpaca as the broker.
- Backtesting: The bot can be backtested on historical data using Yahoo Finance data.
- Python 3.8 or higher
- Pip
- Alpaca API Account (with API key and secret)
- Clone the repository:
git clone https://github.com/ErikTsai/MACD-trading-bot.git`
- Install the required Python packages in your project's virtual environment:
pip install lumibot pandas numpy python-dotenv
-
Create a free Alpaca account and get your API keys
-
Set up your environment variables:
Create a
.env
file in the root directory and add your Alpaca API credentials:
API_KEY = your_alpaca_api_key
API_SECRET = your_alpaca_secret_key
To choose which stock to trade, set the self.symbol
variable to a symbol of your choice.
self.symbol = "SYMBOL" #AAPL OR SPY AS AN EXAMPlE
To run the bot in live trading mode, set the trade
variable in the __main__
block at the end of the code to True
.
if __name__ == "__main__":
trade = True
To backtest the strategy, set the trade
variable to False
and specify the backtesting period:
if __name__ == "__main__":
trade = False
backtesting_start = datetime(2015, 4, 15) #(year, month, day)
backtesting_end = datetime(2023, 4, 15)
- MACD: The MACD is calculated using a 12-day EMA, a 26-day EMA, and a 9-day signal line.
- 200-day EMA: The strategy uses a 200-day EMA to determine the overall market trend. Buy and sell signals are only generated when the market is in an uptrend and downtrend respectively.
- Position Management: The bot will buy the stock if no position is currently held when a buy signal is triggered. It will sell the entire position when a sell signal is triggered.
Here are some of the backtesting results from the trading bot (Trading from April 15, 2015 to April 15, 2023)
The MACD strategy used by the bot performs well when trading stocks that have clear trends. Stocks in strong uptrends or downtrends, such as SPY and AAPL, tend to yield high annual returns. Conversely, GLD, which experienced relatively sideways movement from 2018 to 2023, resulted in lower annual returns.
While WBA has been in a consistent downtrend—a good sign for the MACD strategy, the backtesting resulted in a negative return. This outcome stems from the current bot's handling of bearish signals—where it sells all positions instead of properly shorting the stock. I plan on implementing this functionality in the future, and using the lessons I've learned from this project to build a more effective trading bot.
Feel free to open issues or submit pull requests if you have any suggestions or improvements.
This project is licensed under the MIT License.