[System trade] I tried playing with Stochastic Oscillator by decomposing with python ♬

By decomposing the Stochastic Oscillator for the purpose of noise removal, we try to read the purchase from fluctuations in a longer cycle. The stock price of this ups and downs is clear to buy and sell without backtesting. 【reference】 ① [Introduction to system trading] I drew stc with python and played with it ♬[Introduction to element decomposition] Arrange time series analysis methods in R and python ♬

What i did

・ Incorporate Decompose ・ What is the result?

・ Incorporate Decompose

The built-in is as follows The Lib to be used is as shown in Reference ②, and the duplicate code in Reference ① is omitted.

...
import matplotlib.pyplot as plt
...
import statsmodels.api as sm
from statsmodels.tsa.seasonal import STL

def...

The data acquisition is as follows, but define series = ... to prepare for Decompose.

stock = '6758.JP' #sony6758 Jal 9201 Sumitomo Mitsui Financial 8316 docomo 9437
start = dt.date(2020,1,1)
end = dt.date(2020,6,5)
df = pd.DataFrame(get_stock(stock, start, end))
series = df['Close']
print(series)

And finally Decompose. Plot the situation on the graph.

cycle, trend = sm.tsa.filters.hpfilter(series, 144)
fig, ax = plt.subplots(3,1)
ax[0].plot(series)
ax[0].set_title('close')
ax[1].plot(trend)
ax[1].set_title('Trend')
ax[2].plot(cycle)
ax[2].set_title('Cycle')
plt.savefig("./stock/close_%K%D_{}_now.png ".format(stock))
plt.pause(1)
plt.close()

The result is shown below, with the Trend data denoised. As a characteristic, the deviation Deviation from Observed Trend is also large when the stock price drops sharply. It can be said that the mullet is large.

df['Close']=trend

If you do this for High and Low, you will get the following graph. High, Low, Close can be seen separately. All_%K%D_6758.JP_now.png Then, with the same code as last time, calculate% K and% D with the data obtained in this way and draw it.

df['%K'] = STOK(df['Close'], df['Low'], df['High'], 14)
df['%D'] = STOD(df['Close'], df['Low'], df['High'], 14)

I got the graph to be very simple as below. With this, it seems that there are no mistakes in buying or selling. ** 6758 Sony; Overbuy risk area, risk area, so refrain from purchasing ** stc_%K%D_6758.JP_now.png

・ What is the result?

Everything has become simple! Obviously, it seems that you can buy and sell without making a mistake. ** 8316 Sumitomo Mitsui Financial; Over-raised area, selling scene soon? ** ** stc_%K%D_8316.JP_now.png ** 9201 Japan Airlines; Risk area of over-raising, selling scene soon? ** ** stc_%K%D_9201.JP_now.png ** 9437 NTT DoCoMo; Risk area of over-lowering, buying scene soon? ** ** stc_%K%D_9437.JP_now.png

Summary

・ I tried to remove noise from Stochastic Oscillator with the decompose function. ・ The graph has changed monotonously, making it easier to buy and sell.

・ Actually, let's apply ・ Let's apply it to short-term trading such as minute bars ・ Let's apply it automatically to all stocks and index the index at the time of buying and selling

bonus

・ Short-term trading

On the contrary, short-term trading using daily ups and downs seems to be possible by using Deviation, so I tried it. When I tried it, it became as follows. This is fine, and it is doubtful that the High, Low, and Close values are really in that hierarchical relationship (it seems that it is not guaranteed because% K and% D exceed the range of 0-100). It's still worth a try, but it's not a guarantee that you're trading from the charts you get. Results of 6758 cycle_stc_%K%D_6758.JP_now.png Results of 9437 cycle_stc_%K%D_9437.JP_now.png

Recommended Posts

[System trade] I tried playing with Stochastic Oscillator by decomposing with python ♬
I tried playing mahjong with Python (single mahjong edition)
I tried fp-growth with python
I tried scraping with Python
I tried gRPC with Python
I tried scraping with python
[Introduction to system trading] I drew a Stochastic Oscillator with python and played with it ♬
I tried a stochastic simulation of a bingo game with Python
I tried web scraping with python.
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
I tried to fix "I tried stochastic simulation of bingo game with Python"
I tried scraping Yahoo News with Python
"System trade starting with Python3" reading memo
I tried sending an email with python.
I tried non-photorealistic rendering with Python + opencv
I tried a functional language with Python
I tried recursion with Python ② (Fibonacci sequence)
I tried playing with the image with Pillow
#I tried something like Vlookup with Python # 2
I tried to communicate with a remote server by Socket communication with Python.
[Zaif] I tried to make it easy to trade virtual currencies with Python
I tried "smoothing" the image with Python + OpenCV
I tried hundreds of millions of SQLite with python
I tried "differentiating" the image with Python + OpenCV
I tried playing a typing game in Python
I tried L-Chika with Raspberry Pi 4 (Python edition)
I tried playing with PartiQL and MongoDB connected
I tried Jacobian and partial differential with python
I tried to get CloudWatch data with Python
I tried using mecab with python2.7, ruby2.3, php7
I tried debugging from Python via System Console
I tried function synthesis and curry with python
I tried to output LLVM IR with Python
I tried "binarizing" the image with Python + OpenCV
I tried running faiss with python, Go, Rust
I tried using the Datetime module by Python
I tried to automate sushi making with python
I tried running Deep Floor Plan with Python 3.6.10.
I tried sending an email with SendGrid + Python
I tried playing with the calculator on tkinter
I tried to easily create a fully automatic attendance system with Selenium + Python
I tried searching for files under the folder with Python by file name
I tried to make the weather forecast on the official line by referring to the weather forecast bot of "Dialogue system made with python".
I tried to implement Minesweeper on terminal with python
I tried using TradeWave (BitCoin system trading in Python)
I tried to get started with blender python script_Part 01
I tried to touch the CSV file with Python
I tried to draw a route map with Python
I tried Python> autopep8
3 things I noticed by analyzing twitter followers with python
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried to solve the soma cube with python
I tried to get started with blender python script_Part 02
I tried to implement an artificial perceptron with python
I tried to automatically generate a password with Python3
Mayungo's Python Learning Episode 1: I tried printing with print
I tried to solve the problem with Python Vol.1
I tried to analyze J League data with Python
I tried to aggregate & compare unit price data by language with Real Gachi by Python
System Trade Beginning with Python 3: Hedge Funds and CTA