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/
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.
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.
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.
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.
Looking at the oscillator index, you can see that the market is astonishingly bullish.
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.
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.
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