Algorithmic Trading Python

LSTM Long Classification Strategy for Algorithmic Trading

First, we will load the python packages.
We will use the yahoo finance package to import the historical data. We will import Microsoft dataset for backtesting.
Next, we will visualize the time series of Adj Close. Apparently this is a non-stationary time series. 
Next, we compute the returns
We define the target by checking return in the next day. If it  is positive we will take the long position. If we don’t have any stock, we will buy some stocks.  if the return in the next day is negative, we will take no position. If we are holding some stocks, we will sell away all the stocks and hold no position.
Next, we will try to scale the data for LSTM. However, since the time series is non-stationary, we decide to do a rolling scaling of two months. We extract the highest and lowest stock price in the 2 months of trading days, then we scale the data. This is a controversial approach. Let us know if you agree to our scaling approach. 
After that, we count  the number of days taking long positions and no positions.  It is almost 50% each. 
Next, we define a sliding window to form the X-y for LSTM training. The inputs are Open, High, Low,  and Adj Close. If you want, you can also include Volume. We did not include the Volume for simplicity. 
Next, we use 10 days of trading data to predict the target. 
We will split the time series into training and testing. We arbitrary take 70% of the data for training, and 30% of the data for testing.
Next we define the LSTM model. In this tutorial, you can  use a simple single layer of LSTM. The  output is a dense layer with sigmoid activation. This will output a probability from 0 to 1. 
You can also try stacked LSTM or Bidirectional LSTM. We added it here for your reference. Generally we don’t have too much advantages of using the stacked or bidirectional LSTM models. 
For LSTM training, we need to reduce the learning rate. So we have chosen a small learning rate. We can choose binary cross entropy as loss function. I think you can also choose mse.  
We have chosen a batch size of 32 for training and epochs of 300.
We plot the loss as a function of epochs. We can see the LSTM is learning as the loss decrease with epoch.
The accuracy is about 52% which is quite poor. 
Since the output is 0 to 1, we define the prediction as 1 or long  if the probability is larger than 0.5 else 0 or no position.
We compare the prediction and testing label, and confirm the poor accuracy.
Next we plot the confusion matrix and the classification report. 
Next we will do some trade analytics. We compute the signal as the model prediction and compute the strategy 
We compute the trades. 0 means no trade. 1 is buy the stock and -1 is sell the stock .We can see that there are not many trades using this strategy. 
We show the strategy returns histogram and look at the buy and sells on the stock data. It seems that most of the trades were concentrated on the early period from 2012 to 2016 in the training data. But there are hardly any trades on the testing data. The model practically just  This could be due to overfitting or could be due to the non-stationary time series that are hard to scale. 
We  compare the strategy returns and the bnh returns. Since the model hold the stock after 2017, the strategy returns are reminiscent of the returns but with a lower profit.
We compute the list of metrics for the strategy using pyfolio. We got a sharpe ratio of 0.55 and maximum drawdown of -57% which are very bad.
If you are satisfied with the strategy, you can save the model and load the mode for paper  trading which we do a video on this in the future. 

The code can be downloaded below

Recommended trading platforms


Recommended courses on Algorithmic Trading

1. Basic Algorithmic Trading with Python

2. Machine Learning for Algorithmic Trading

Other training courses from Tertiary Courses

June 11, 2022