The following repo is based on the final project of the course "Algorithmic Trading" taught at Hult International Business School by professor Michael Rolleigh. The goal is to backtest a trading algorithm that receives the output from a machine learning model as a signal to perform the strategy. Please find the final results in Algorithmic Trading in Python.pdf
The corresponding tasks are divided into the following notebooks:
Contains the data science workflow. This script was used as a research environment with the objective of extracting the signal from fundamental and alternative data. The signal extraction was done by ultimately developing a machine learning model that had as target returns in a particular stock (in this case Redhat [RHT] stocks). The Github repo GetOldTweets was used to web scrape all the mentions that Redhat had in the past 4 years (2014 - 2018). a total of 56,400 tweets were collected and cleaned. Sentiment analysis was undertaken by implementing VaderSentimentAnalyzer. After computing the sentiment score for each tweet, they were aggregated by adding the number of positive, negative and neutral tweets. Finally, these aggregated tweets were inner joined by timestamp with the fundamental data frame. These two data frames consisted of the following:- Diluted EPS
- EBIT Margin
- EBITDA Margin Revenue % Growth
- EBITDA % Growth
- EBIT % Growth
- NetIncome % Growth
- Net Debt to Equity
- Alternative Data:
- Favourites
- retweets
- Compound
- Positive
- Neutral
- Negative
The baseline model (hugely overfit decision tree) was used for the backtesting in order to exemplify the dangers of overfitting in trading.
Backtesting consists of creating a trading strategy and going back in time simulating 'what would have happened' if the strategy was implemented in such a time frame. The time frame selected in this script ranged from 2014-01-02 to 2018-12-30. Finally, `bt` was used as the backtesting library for this project. 3 different strategies were implemented:1. Baseline Strategy
- If the predicted return is greater than 2% buy all the stocks with the available funds that we have (e.g. $1,000,000 in stocks).
- If the predicted return is greater than 1% buy half the stocks with the available funds that we have (e.g. $500,000 in stocks).
- If the predicted return is less than -2% sell all the stocks with the available funds that we have (e.g. $1,000,000 in stocks).
- If the predicted return is greater than -1% sell half the stocks with the available funds that we have (e.g. $500,000 in stocks).
- The limit in the change of position was implemented due to the fact that is not plausible to assume that we will be able to buy or sell 100% of our stocks in just 1 day.
- The 1 Day Lag was incorporated due to the fact that the library `bt` used the closing price in its backtesting.