[PYTHON] Can the Kalman filter be used to predict stock price trends?

Can the Kalman filter be used to predict stock price trends?

About the outline of this article

Implementation of Kalman filter for forecasting stock price trends and their impressions. The conclusion is that it may be available for the short term. It runs on google colab.

What is a Kalman filter

Originally used for controlling robots and rockets, it is used to estimate the state of the system using inaccurate observations. This time, I'm using the simplest one.

Premise

We think that stock price fluctuations follow a certain probability distribution, and we make predictions based on that, so please use it as one of the indicators.

Also, this time we make an unrealistic assumption because we assume that the potential way stock prices fluctuate and how they actually change follows a normal distribution.

Therefore, while it works well for stock prices that are moving steadily to some extent, it seems that it cannot keep up with sudden changes.

Calculation formula (implementation)

It's annoying why the formula is so, so if you want to see it, please refer to [wiki](https://ja.wikipedia.org/wiki/Kalman filter). In terms of implementation, there is no problem if you define the following functions. Takes a sequence as an argument and returns a sequence of the same length.

kalman.py


import numpy as np

def kalman(s,x,P):
    res_x=[]#Estimated position at that time
    res_v=[]#Estimated speed at that time
    for i in range(len(s)):
        Z=np.array([s[i]])
        y=Z-H@x
        S=H@[email protected]+R
        [email protected]@np.linalg.inv(S)
        x=x+K@y
        P=(I-K@H)@P
        #predicion
        x=(F@x)+u
        P=F@[email protected]
        res_x.append(x[0,0])
    return res_x

x=np.array([[0.], [0.]])#The first is the position and the second is the speed
P=np.array([[1000., 0.], [0., 1000.]])#Covariance matrix
u=np.array([[0.], [0.]])#External force but not this time
F=np.array([[1., 1.], [0, 1.]])#Definition of the relationship between position and velocity
H=np.array([[1., 0.]])
R=np.array([[1.]])
I=np.array([[1., 0.], [0., 1.]])

t=[1,2,4,3,7,4,6,5.2]#Sequence to put
print(kalman(t,x,P))

The result is as follows.

res.txt


[0.999000999000999,2.9980029930179533,5.332389258923225,4.500249550392171,7.299929903069252,6.400310235003376, 7.000316132748338, 6.886095984845674]

Since there is no particular regularity in the given sequence this time, it cannot be predicted correctly, but if random numbers are generated according to the probability distribution, it will be estimated with considerable accuracy.

I actually applied it to the stock price

I'm writing this article on August 14th, and I wonder if the stock price will cool down in Corona and hit a double bottom, and it's about to rise. It's a time like that. Assuming that the value obtained by the Kalman filter is the original stock price, the stock price is likely to return to it.

Here are some of the brands that I have applied that are characteristic. One of them is ENEOS Holdings (5020.T). 5020.Tkalman-3.png5020.Tkalman-2.png5020.Tkalman.png

At first glance, I don't know what it is, but the time to start applying it to the filter is different. Around December 2018, the entire stock market plummeted due to the influence of the Trump administration, and around February to March 2020, stock prices plummeted due to the influence of Corona. These events have good outliers, so you can see that the Kalman filter estimates are suddenly off. So, by applying the Kalman filter again after each plunge event, you can see that a relatively good estimate can be made.

Conclusion

If there are no big events, I think it can be used as an index (especially when buying). Personally, I think that it will rise to the middle of the Kalman filter value at a certain point and the stock price, so I also buy other indicators. However, when the stock price changes significantly, it will be bad if you do not shift the initial value to be applied.

add to

I think it can be used in day trading. Rather, the price movement of day trading seems to be closer to the normal distribution, so ... I would appreciate it if you could let me know if you have any opinions.

Also, this Kalman filter can be used by robots as it is.

Recommended Posts

Can the Kalman filter be used to predict stock price trends?
I tried to predict the price of ETF
How to filter foreign keys that can be selected on the Django admin screen
Introduce PyQt 5 to MacOS (Linux can also be used)
How to set variables that can be used throughout the Django app-useful for templates, etc.-
Goroutine (parallel control) that can be used in the field
Goroutine that can be used in the field (errgroup.Group edition)
I tried to expand the database so that it can be used with PES analysis software
I tried to predict the up and down of the closing price of Gurunavi's stock price using TensorFlow (progress)
Which octal literals can be used depends on the programming language
[Note] Let's try to predict the amount of electricity used! (Part 1)
I used Facebook's Prophet to predict the Dow Jones Industrial Average.
A timer (ticker) that can be used in the field (can be used anywhere)
Use PyCaret to predict the price of pre-owned apartments in Tokyo!
Have python check if the string can be converted / converted to int
Kalman filter that you can understand
About the matter that the re.compiled object can be used for the re.match pattern
It seems that cancelall childorders can be used to cancel all parent orders (special orders) with the bitflyer API
QPS control that can be used in the field (Rate Limit) Limits execution to n times per second
[Python3] Code that can be used when you want to change the extension of an image at once