[PYTHON] Calculation of technical indicators by TA-Lib and pandas

TA-Lib is a library for calculating typical indicators in technical analysis when collecting and analyzing financial data with Python. As I wrote in Previous, when analyzing daily stock data (= daily opening price, high price, etc.) with pandas, various typical It is more convenient and safe to use the library than to implement everything by yourself to calculate various indicators.

TA-Lib http://ta-lib.org/

Make TA-Lib available

To use this TA-Lib with Python, bindings for Python are provided.

Installation method http://mrjbq7.github.io/ta-lib/install.html

It's easy to install, just download the source tarball and make install. In the link above, --prefix = / usr is used, but considering FHS, --prefix = / usr / local is better.

VERSION=0.4.0
FILENAME=ta-lib-$VERSION-src.tar.gz
curl -L http://prdownloads.sourceforge.net/ta-lib/$FILENAME -O
tar xzvf $FILENAME
cd ta-lib
./configure --prefix=/usr/local
make
sudo make install

Python bindings can be included with easy_install at the time of writing.

easy_install TA-Lib

If eazy_install is successful, import from IPython and make sure you can use the functions starting with ta.

Use TA-Lib

You can see the list of functions and how to use them on the above GitHub site, but to explain briefly

import talib as ta
ta.get_function_groups

Then you can see each function and the group for each function.

As I wrote in Previous, if you scrape the stock price data from Yahoo! Finance, you can freely calculate using TA-Lib. However, if you try to process too much data beyond scraping, it can be a problem. There is a charge, but you can also download time-series data from Yahoo! Finance VIP Club, so you can download the data as needed. You can also buy it.

After preparing the data, let's handle it using IPython.

filename = 'stock_N225.csv' #Nikkei Stock Average Data
df = pd.read_csv(filename,
                 index_col=0, parse_dates=True)
closed = df.asfreq('B')['Adj Close'].dropna() #Extract the adjusted closing price
prices = np.array(stock, dtype='f8') #Keep it in a NumPy array with floating point numbers

#Find the 5-day simple moving average
sma5 = ta.SMA(prices, timeperiod=5)

# RSI (14th)Seeking
rsi14 = ta.RSI(prices, timeperiod=14)

# MACD (Leading 12-day moving average, lagging 26-day moving average, 9-day signal line)Seeking
macd, macdsignal, macdhist = ta.MACD(prices,
                                     fastperiod=12, slowperiod=26, signalperiod=9)

Well, you can calculate all the technical indicators implemented in TA-Lib like this.

Plot

It's hard to imagine if it's just numerical values, so let's try some plotting. With the familiar combination of pandas + matplotlib and talib, you can plot charts in the same way as so-called stock software.

First, let's plot the daily chart, EWMA (index-weighted moving average), and Bollinger Bands from the Nikkei Stock Average for the last 180 days.

ohlc_N225.png

As you can see, the deviation from the 25-day moving average is a decent support, and the standard deviation + 2σ line is up.

Next, let's take a look at DeNA, which has become a hot topic for the second consecutive day due to the partnership with Nintendo. This time it's a plot for the last 90 days.

ohlc_2432.png

Looking at the oscillator index, you can see that the market is astonishingly bullish.

osci_2432.png

In addition, when it comes to charts like this, various stock experts come out and it is difficult, but the author is not a stock expert, but data from the perspective of data analysis by IT and statistics / machine learning. I'm watching it.

If you take an approach from a statistical point of view, it is legitimate to take an approach such as quantifying changes in data and predicting how it will change from past data.

There is a distortion in human cognition. I buy it because I think it will go up, and I sell it because I think it will go down, so I tend to make biased decisions.

Technical analysis is, of course, not a panacea, but it can help eliminate such biases and make mechanical decisions.

Perform cycle color by machine learning

Now, when some stocks rise, we may want to search for and buy other stocks with similar market prices from all stocks on the TSE based on these technical indicators. Searching for the next rising stock while investing in other industries in such a rising market is called circulation color. .. In other words, the stocks that have secured profits are already high enough, so we are looking for the next target, thinking that another stock may rise this time.

When you want to sell the stocks you are currently holding and invest new funds in other stocks like this, it would take a huge amount of time and labor to research the market manually one by one. It can only be said that it is a leisure person to do such a thing every time. Therefore, since computers have been developed and anyone can easily make full use of IT and mathematics, I will try the circulation color by machine learning.

First of all, as teacher data, we will pick up several stocks of industries that have already made profits and generate a data frame. It depends on what kind of viewpoint you create the features here, but this time I will simply pick up the momentum of time series data as an example.

import os
import pandas as pd
import numpy as np
from sklearn import svm

def read_from_csv(stock, filename):
    if os.path.exists(filename):
        return pd.read_csv(filename,
                           index_col=0, parse_dates=True)

def read_txt(filename):
    stocks = pd.read_csv(filename, header=None)
    data = pd.DataFrame([])
    for s in stocks.values:
        stock = str(s[0])
        csvfile = "".join(['ti_', stock, '.csv'])
        df = read_from_csv(stock, csvfile).asfreq('B')[-30:]
        data[stock] = df.ix[:,'mom25']
    print(data.T.tail(10))
    return data.T

data = read_txt('stocks.txt')

You now have a dataframe with stock code and daily momentum. Let's convert this to a multiple list with time-series momentum for each stock code in advance. This is muddy, so I will omit it.

As for machine learning, we have previously an example of classifying student grades into groups and classifying students for the entire grade based on that. I wrote. Applying this, the support vector machine classifier that learned the above teacher data classifies the past data of all stocks listed on the First Section of the Tokyo Stock Exchange.

from sklearn.cluster import KMeans
from sklearn import svm

kmeans_model = KMeans(n_clusters=100, random_state=100).fit(train_X)
labels = kmeans_model.labels_
clf = svm.SVC(kernel='rbf', C=1) #RBF kernel support Vector machine as classifier
clf.fit(train_X, labels) #Learning
results = clf.predict(test_X) #Classification

Now it is possible to mechanically extract stocks with similar momentum.

Summary

Anyone who makes full use of IT and mathematics will be able to analyze financial data like quants and actually invest and make a profit. Data analysis is meaningless and useless without a purpose, but if you have a clear purpose, for example, to help you invest, I think the calculation will be rewarding. Please note that this article does not encourage readers to invest.

That's all for the analysis of stock data, and I would like to write more about machine learning that appeared in the latter half of this time from the next time onwards.

Recommended Posts

Calculation of technical indicators by TA-Lib and pandas
Analysis of financial data by pandas and its visualization (2)
Analysis of financial data by pandas and its visualization (1)
Calculation of similarity by MinHash
[Scientific / technical calculation by Python] Basic operation of arrays, numpy
[Scientific and technical calculation by Python] Drawing of fractal figures [Sierpinski triangle, Bernsley fern, fractal tree]
Practice of data analysis by Python and pandas (Tokyo COVID-19 data edition)
Scientific / technical calculation by Python] Drawing and visualization of 3D isosurface and its cross section using mayavi
[Scientific / technical calculation by Python] Sum calculation, numerical calculation
Import of japandas with pandas 1.0 and above
[Scientific / technical calculation by Python] Derivation of analytical solutions for quadratic and cubic equations, mathematical formulas, sympy
A little scrutiny of pandas 1.0 and dask
[Scientific / technical calculation by Python] Fitting by nonlinear function, equation of state, scipy
Pandas of the beginner, by the beginner, for the beginner [Python]
[Scientific / technical calculation by Python] Calculation of matrix product by @ operator, python3.5 or later, numpy
[Control engineering] Calculation of transfer functions and state space models by Python
[Scientific / technical calculation by Python] Drawing animation of parabolic motion with locus, matplotlib
[Scientific / technical calculation by Python] Numerical calculation to find the value of derivative (differential)
[Scientific / technical calculation by Python] Analytical solution to find the solution of equation sympy
[Scientific / technical calculation by Python] Drawing of 3D curved surface, surface, wireframe, visualization, matplotlib
[Scientific / technical calculation by Python] Inverse matrix calculation, numpy
[Scientific / technical calculation by Python] Histogram, visualization, matplotlib
[Scientific / technical calculation by Python] Lagrange interpolation, numerical calculation
Calculation of elapsed years and elapsed months including leap years
Low-rank approximation of images by HOSVD and HOOI
Header shifts in read_csv () and read_table () of Pandas
Parallel learning of deep learning by Keras and Kubernetes
Basic operation of Python Pandas Series and Dataframe (1)
[Scientific / technical calculation by Python] Numerical solution of one-dimensional and two-dimensional wave equations by FTCS method (explicit method), hyperbolic partial differential equations
[Scientific / technical calculation by Python] Drawing, visualization, matplotlib of 2D (color) contour lines, etc.
Sort by pandas
Numerical calculation of compressible fluid by finite volume method
Calculation of standard deviation and correlation coefficient in Python
Proposal of multi-speaker voice conversion and voice morphing by RelGAN-VM
[Scientific / technical calculation by Python] Logarithmic graph, visualization, matplotlib
[python] Calculation of months and years of difference in datetime
Calculation of homography matrix by least squares method (DLT method)
[Scientific / technical calculation by Python] Polar graph, visualization, matplotlib
Plot Bitcoin candle charts and technical indicators in Python
Implementation of DB administrator screen by Flask-Admin and Flask-Login
Visualization method of data by explanatory variable and objective variable
[Python] Types of statistical values (features) and calculation methods
[Science / technical calculation by Python] Numerical solution of first-order ordinary differential equations, initial value problem, numerical calculation
[Scientific / technical calculation by Python] Numerical solution of second-order ordinary differential equations, initial value problem, numerical calculation
[Scientific / technical calculation by Python] List of matrices that appear in Hinpan in numerical linear algebra
[Scientific / technical calculation by Python] List of usage of (special) functions used in physics by using scipy
[Scientific / technical calculation by Python] Wave "beat" and group velocity, wave superposition, visualization, high school physics
[Scientific / technical calculation by Python] Numerical solution of one-dimensional harmonic oscillator problem by velocity Verlet method
[Scientific / technical calculation by Python] Numerical solution of eigenvalue problem of matrix by power method, numerical linear algebra