Python stock quantitative trading (6) --- use TA-Lib to calculate technical indicators

Be sure to know what’s wrong every day, and change every day; if you don’t know what’s wrong every day, you will be content with yourself every day; if you don’t change a day, you can’t make progress every day; there are many smart and handsome people in the world, so morality is not modified. Those who do not broaden their karma will only delay their lives for the sake of following the word.

What is TA-Lib

TA-Lib (Technical Analysis Library) is an open source technical analysis library provided by python. Since its release, it has a history of more than 20 years. It contains calculation functions for about 200 technical indicators and K-line pattern recognition functions, such as MACD, RSI , KDJ, momentum indicators, etc.

From the analysis of the previous blog posts, if you don’t understand this library and we analyze stocks directly, you need to remember the calculation formulas of all indicators. It is really unfriendly to people who are not very good at mathematics. But after we have the TA-Lib library, we can easily get some parameters of the indicator drawing, which can greatly save our analysis time.

Therefore, this blog post will explain all the previous technical indicators, calculated through the TA-Lib library. So first of all, we need to import this library in the python file, the specific code is as follows:

import talib

Calculation of SMA indicator

SMA is the moving average explained in the previous blog post, which is the abbreviation of simple moving average. The TA-Lib library directly provides us with talib.SMA() to calculate SMA.

There are two parameters to use the SMA() function:

(1) timeperiod: the time to calculate the moving average. For example, write 5 for a 5-day moving average, write 10 for a 10-day moving average, and so on.

(2) Close: Close price, you can directly import the column of closing price through pandas.

Next, let's use the SMA() function to calculate the 10, 20, and 30-day moving averages. The specific code is as follows:

import pandas as pd
import talib
df = pd.read_excel("牧原股份.xlsx")
df["SMA10"]=talib.SMA(df['close'],timeperiod=10)
df["SMA20"]=talib.SMA(df['close'],timeperiod=20)
df["SMA30"]=talib.SMA(df['close'],timeperiod=30)

But here we need to pay attention that when calculating the moving average, the n days before the moving average must be null. For example, if the 10-day moving average is calculated here, then the first 9 days are worthless. If there is no value, NaN will be filled by default. In order to make the data beautiful when drawing, we need to assign the blank value of the previous N days to the effective value of the first day's moving average, so that there will be a blank curve. The specific code is as follows:

df['SMA10'].fillna(method="bfill",inplace=True)
df['SMA20'].fillna(method="bfill",inplace=True)
df['SMA30'].fillna(method="bfill",inplace=True)

At present, there are actually many different calculation methods for our moving average. For example, when calculating the exponential moving average, the EMA method is used, and when calculating the weighted moving average, the WMA method is used. However, these calculations have a general method MA, which distinguishes whether to calculate EMA, WMA, or SMA through the last parameter matype. E.g:

df['ma10']=talib.MA(df['close'],timeperiod=10,matype=0)
df['ma20']=talib.MA(df['close'],timeperiod=20,matype=1)
df['ma30']=talib.MA(df['close'],timeperiod=30,matype=2)

0 stands for SMA, 1 stands for EMA, 2 stands for WMA, other parameter values, we will introduce when we explain other indicators later, these three are the most commonly used methods and need to be remembered.

The code to draw the moving average is as follows:

import pandas as pd
import talib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111)
df = pd.read_excel("牧原股份.xlsx")
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].apply(lambda x: x.strftime('%Y-%m-%d'))
df["SMA10"]=talib.SMA(df['close'],timeperiod=10)
df["SMA20"]=talib.SMA(df['close'],timeperiod=20)
df["SMA30"]=talib.SMA(df['close'],timeperiod=30)
df['SMA10'].fillna(method="bfill",inplace=True)
df['SMA20'].fillna(method="bfill",inplace=True)
df['SMA30'].fillna(method="bfill",inplace=True)
ax.plot(np.arange(0, len(df)), df['SMA10'])  # 绘制5日均线
ax.plot(np.arange(0, len(df)), df['SMA20'])  # 绘制10日均线
ax.plot(np.arange(0, len(df)), df['SMA30'])  # 绘制30日均线
ax.xaxis.set_major_locator(ticker.MaxNLocator(20))


def format_date(x, pos=None):
    if x < 0 or x > len(df['date']) - 1:
        return ''
    return df['date'][int(x)]


ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()

After running, the display effect is as shown in the figure below:
Insert picture description here

MACD indicator calculation

Since the TA-Lib library directly provides the SMA() method to calculate the moving average, there will definitely be a MACD method that will provide you with the parameters for calculating the MACD. The specific usage method is as follows:

dif, dea, bar = talib.MACD(df['close'].values, fastperiod=12, slowperiod=26, signalperiod=9)

It can be seen that when calculating MACD parameters, we will calculate EMA1 (moving average of closing price on the 12th) and EMA2 (moving average of closing price of 26) corresponding to the above 12, 26. Similarly, the formula span for calculating DEA is also 9, which is substituted into the last value.

However, its internal implementation is actually the same step as our own operation according to the formula, but the TA-Lib library directly omits the steps and gives us the method directly, so that if it is only used for analysis, most developers do not need to understand the principle.

Although it directly provides us with the calculation method MACD(), but we calculate according to the previous formula, there is no null value, and the direct code here will cause a null value. So we need an extra step to replace the null value, the specific code is as follows:

dif[np.isnan(dif)],dea[np.isnan(dea)],bar[np.isnan(bar)]=0,0,0

The complete drawing code is as follows:

import pandas as pd
import talib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111)
df = pd.read_excel("牧原股份.xlsx")
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].apply(lambda x: x.strftime('%Y-%m-%d'))
dif, dea, bar = talib.MACD(df['close'].values, fastperiod=12, slowperiod=26, signalperiod=9)
dif[np.isnan(dif)], dea[np.isnan(dea)], bar[np.isnan(bar)] = 0, 0, 0
ax.plot(np.arange(0, len(df)), dif)
ax.plot(np.arange(0, len(df)), dea)
red_bar = np.where(bar > 0, 2 * bar, 0)
blue_bar = np.where(bar < 0, 2 * bar, 0)
ax.bar(np.arange(0, len(df)), red_bar, color="red")
ax.bar(np.arange(0, len(df)), blue_bar, color="blue")

ax.xaxis.set_major_locator(ticker.MaxNLocator(20))


def format_date(x, pos=None):
    # 由于前面股票数据在 date 这个位置传入的都是int
    # 因此 x=0,1,2,...
    # date_tickers 是所有日期的字符串形式列表
    if x < 0 or x > len(df['date']) - 1:
        return ''
    return df['date'][int(x)]


ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()

After running, the display effect is as follows:
Insert picture description here

Calculation of KDJ indicator

Although the method names for calculating SMA and MACD are the same in the TA-Lib library, it is really not a KDJ function this time. The method of calculating KDJ in TA-Lib library is talib.STOCH function. First, we can calculate the value of K and D through this function, and then calculate the value of J through K and D. The specific calculation method is as follows:

df['K'], df['D'] = talib.STOCH(df['high'].values, df['low'].values, df['close'].values, fastk_period=9, slowk_period=3,
                               slowk_matype=0, slowd_period=3, slowd_matype=0)
df['K'].fillna(0,inplace=True)
df['D'].fillna(0,inplace=True)
df['J']=3*df['K']-2*df['D']

Here fastk_period=9, slowk_period=3, slowd_period=3, although the calculation method is different, the core idea is the same. Similarly, the KD value calculated using the TA-Lib library also has an invalid value and needs to be replaced with 0.

The complete code for drawing KDJ curve is as follows:

import pandas as pd
import talib
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111)
df = pd.read_excel("牧原股份.xlsx")
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].apply(lambda x: x.strftime('%Y-%m-%d'))
df['K'], df['D'] = talib.STOCH(df['high'].values, df['low'].values, df['close'].values, fastk_period=9, slowk_period=3,
                               slowk_matype=0, slowd_period=3, slowd_matype=0)
df['K'].fillna(0,inplace=True)
df['D'].fillna(0,inplace=True)
df['J']=3*df['K']-2*df['D']
ax.plot(df["date"], df["K"], label ="K")
ax.plot(df["date"], df["D"], label ="D")
ax.plot(df["date"], df["J"], label ="J")
plt.legend()
ax.xaxis.set_major_locator(ticker.MaxNLocator(20))
def format_date(x, pos=None):
    # 由于前面股票数据在 date 这个位置传入的都是int
    # 因此 x=0,1,2,...
    # date_tickers 是所有日期的字符串形式列表
    if x < 0 or x > len(df['date']) - 1:
        return ''
    return df['date'][int(x)]


ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()

After running, the display effect is as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/liyuanjinglyj/article/details/113401385