Use Multi-variate time-series on stock prediction using Kats
Introduction
We have already seen so many time-series models on different problems, including stock price prediction. However, those were often just a single variant with just price as a predictor. In this article, let us focus on multivariate price prediction using various technical indicators.
I am planning to use the kats library from Facebook for this article. If interested, I will write more articles for other time-series related subjects.
Setup
Of course, we will use Python as our standard. Kats is a recent time-series library from Facebook. For other technical indicators, we will use the listed libraries. Kats can be installed using pip; some additional packages are mentioned in requirements.txt
pip install kats
pip install ta==0.5.17
pip install tqdm==4.42.1
Technical indicators
Before creating technical indicators, we need to download data using yahoo finance or any other standard library.
import yfinance as yfdata = yf.download("AAPL", start="2010-01-01", end="2021-08-30",)
Also, save the data to a CSV file for creating technical features. Now it is time to generate technical indicators. We will be generating around 400+ technical indicators.
from data_generator import DataGenerator
from logger import Loggertrade_sticker = 'AAPL'
start_date="2017-01-01"
end_date ="2022-04-30"
logger = Logger('./logs', '_')data_gen = DataGenerator(trade_sticker,start_date,end_date,'./logs','./outputs','original', False, logger)df = data_gen.generate_data()
If we run the above code, we will generate all the technical indicators like the below screenshot.
It contains many related functions to generate the actual indicators from the data downloaded in yahoo finance.
I do not want to go too much into detail about these technical indicators as I wanted to focus primarily on time series.
Running uni-variant time-series
We are going to run a simple uni-variant time-series model using the Kats library. Some checks before running any model in kats.
- Kats expect the data to be in specific names in metadata. The timestamp can be of different format(which needs to be mentioned), and it should be named time.
2. If there are any gaps in the days, some algorithms might not work. So we need to fill the gaps.
stock_df = stock_df.asfreq('D',method='ffill')stock_df.reset_index(inplace=True)
Now we have a proper dataset to run the time-series model. Now the below code will run the prophet time-series model from kats
Once you run the above code, you will see a nice chart of the model prediction.
Running HOLT Winters model.
Holt-Winters Exponential Smoothing is used for forecasting time series data that exhibits both a trend and a seasonal variation. I would recommend reading more about it here.
Ensemble time series model
Multiple models are always better than one. It avoids overfitting/underfitting. The prediction will be more accurate. So here, we will create an ensemble model of all the known time series models.
By the chart and analyzing the numbers, it seems the prediction is not wild and better.
Kats Backtesting
However, we need to perform backtesting and create an error metric. Kats provides different loss functions to test and performs all the backtesting for us. However, the only drawback is, we cannot perform backtesting for ensemble models. We will discuss backtesting in the following article.
Multivariate model forecasting
Now we have learned how kats works, and it is time to take it to the next step. Let's perform multi-variant model forecasting.
In the Technical indicators section in this article, we have created a bunch(430+) of technical indicators.
Here we used the VAR model to forecast multivariate time series data. VAR model is a multivariate extension of the univariate autoregressive (AR) model. It captures the linear interdependencies between multiple variables using a system of equations; for detailed implementation of the VAR model, check here.
Now we can predict any technical indicator using the VAR model.
Some drawbacks of the Multivariate model:
- We will not be able to use the ensemble model because all models do not support multi-variant features.
- We need to be cautious about multi-collinearity and address if it exists.
- The model interpretation might be a little tricky.
Final thoughts
- We have learned about the Kats library and different time-series models in kats.
- Predicted the stock prices of uni-variant and multi-variant models.
- Created ensemble time-series model, which is better than a single model.
- We performed Kats back-testing on a model and calculated errors.
Disclaimer
This article is entirely informative. None of the content presented in this notebook constitutes a recommendation of any particular security. All trading strategies are used at your own risk.
References:
- https://towardsdatascience.com/24x5-stock-trading-agent-to-predict-stock-prices-with-deep-learning-with-deployment-c15570720ae9
- https://github.com/facebookresearch/Kats
Get Code
Please subscribe to my newsletter to get the complete working code for my articles and other updates.