Math Operator Functions数学运算

  TA-Lib提供了向量(数组)的加减乘除、在某个周期内求和、最大最小值及其索引等计算函数,注意与Numpy和Pandas数学运算函数的联系与区别,TA-Lib的向量计算功能类似于pandas的moving window(移动窗口),得到的是一个新的序列(不是某个值),具体如下表所示。

ADD : Vector Arithmetic Add 向量加法运算 : ta.ADD(high, low)

SUB : Vector Arithmetic Substraction 向量减法运算:ta.SUB(high, low)

MULT : Vector Arithmetic Mult 向量乘法运算:ta.MULT(high, low)

DIV : Vector Arithmetic Div 向量除法运算:ta.DIV(high, low)

SUM : Summation 周期内求和:ta.SUM(close, timeperiod=30)

MAX : Highest value over a specified period 周期内最大值:ta.MAX(close, timeperiod=30)

MAXINDEX: Index of highest value over a specified period 周期内最大值的索引:ta.MAXINDEX(close, timeperiod=30)

MIN : Lowest value over a specified period 周期内最小值:ta.MIN(close, timeperiod=30)

MININDEX : Index of lowest value over a specified period 周期内最小值的索引:ta.MININDEX(close, timeperiod=30)

MINMAX : Lowest and highest values over a specified period 周期内最小值和最大值:min_, max_ = ta.MINMAX(close, timeperiod=30)

MINMAXINDEX : Index of lowest and highest values over a specified period 周期内最小值和最大值索引:minidx, miaidx = ta.MINMAXINDEX(close, timeperiod=30)

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import talib as ta
import tushare as ts

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

def get_data(code, start='2015-01-01'):
    df = ts.get_k_data(code, start)
    df.index = pd.to_datetime(df.date)
    df = df.sort_index()
    return df

df = get_data('sh')[['open', 'close', 'high', 'low']]
df['add'] = ta.ADD(df.high, df.low)
df['sub'] = ta.SUB(df.high, df.low)
df['mult'] = ta.MULT(df.high, df.low)
df['div'] = ta.DIV(df.high, df.low)
df['sum'] = ta.SUM(df.close, timeperiod=30)
df['min'], df['max'] = ta.MINMAX(df.close, timeperiod=30)
df['minidx'], df['maxidx'] = ta.MINMAXINDEX(df.close, timeperiod=30)
df.tail()

df[['close', 'add', 'sub', 'mult', 'div', 'sum', 'min', 'max']
].plot(figsize=(20,18), subplots=True, layout=(4,2))
plt.subplots_adjust(wspace=0, hspace=0.2)

上证指数走势及数学运算

原文地址:https://www.cnblogs.com/wintalau/p/11618010.html