The provided Python code implements an automated trading strategy called MLTrader, which employs sentiment analysis of news articles to make trading decisions. It utilizes the Alpaca brokerage for executing trades and Yahoo finance data for backtesting. This document outlines the functionality and workflow of the MLTrader strategy.
Code Overview: The code is divided into several parts:
-
Import Statements:
- Imports necessary modules and libraries including those from the lumibot package, Alpaca API, and finbert_utils for sentiment analysis.
-
API Credentials and Configuration:
- Defines API credentials and base URL for Alpaca paper trading.
-
MLTrader Class:
- Inherits from the Strategy class provided by lumibot.
- Initializes with parameters like symbol and cash_at_risk.
- Implements methods for position sizing, fetching dates, retrieving sentiment from news, and handling trading iterations.
-
Trading Iteration:
- Inside the on_trading_iteration method, it calculates position size, retrieves sentiment, and decides whether to buy, sell, or hold based on sentiment analysis results and available cash.
-
Backtesting:
- Sets start and end dates for backtesting.
- Initializes Alpaca broker and MLTrader strategy with specified parameters.
- Executes backtesting using YahooDataBacktesting class.
Explanation of MLTrader Strategy: The MLTrader strategy aims to automate trading decisions based on sentiment analysis of news related to a specified symbol (default is "SPY" - SPDR S&P 500 ETF Trust). The strategy consists of the following steps:
-
Initialization:
- Sets the trading symbol and cash at risk percentage.
- Establishes connection with the Alpaca API.
-
Position Sizing:
- Calculates the quantity of shares to be traded based on available cash and last price of the symbol.
-
Date Retrieval:
- Fetches current and three days prior dates for fetching news.
-
Sentiment Analysis:
- Retrieves news articles within the specified date range.
- Utilizes sentiment analysis to determine the probability and sentiment (positive/negative) of the news.
-
Trading Decision:
- Decides whether to buy or sell based on the sentiment analysis results and available cash.
- Implements a bracket order with take-profit and stop-loss prices.
-
Backtesting:
- Conducts backtesting of the MLTrader strategy using historical data from Yahoo finance.
Conclusion: The MLTrader strategy utilizes sentiment analysis of news articles to automate trading decisions, aiming to capitalize on sentiment-driven market movements. By integrating with the Alpaca API for execution and Yahoo finance for backtesting, it provides a comprehensive framework for testing and deploying automated trading strategy.