量化工具TA-Lib 使用例子


轉發自:http://30daydo.com/article/196

TA-Lib主要用來計算一些股市中常見的指標。 比如MACD,BOLL,均線等參數。  

#-*-coding=utf-8-*- 
import Tkinter as tk 
from Tkinter import * 
import ttk 
import matplotlib.pyplot as plt 
 
import numpy as np 
import talib as ta 
 
series = np.random.choice([1, -1], size=200) 
close = np.cumsum(series).astype(float) 
 
# 重疊指標 
def overlap_process(event): 
    print(event.widget.get()) 
    overlap = event.widget.get() 
 
    upperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) 
    fig, axes = plt.subplots(2, 1, sharex=True) 
    ax1, ax2 = axes[0], axes[1] 
    axes[0].plot(close, '', markersize=3) 
    axes[0].plot(upperband, '') 
    axes[0].plot(middleband, '') 
    axes[0].plot(lowerband, '') 
    axes[0].set_title(overlap, fontproperties="SimHei") 
 
    if overlap == '布林線': 
        pass 
    elif overlap == '雙指數移動平均線': 
        real = ta.DEMA(close, timeperiod=30) 
        axes[1].plot(real, '') 
    elif overlap == '指數移動平均線 ': 
        real = ta.EMA(close, timeperiod=30) 
        axes[1].plot(real, '') 
    elif overlap == '希爾伯特變換——瞬時趨勢線': 
        real = ta.HT_TRENDLINE(close) 
        axes[1].plot(real, '') 
    elif overlap == '考夫曼自適應移動平均線': 
        real = ta.KAMA(close, timeperiod=30) 
        axes[1].plot(real, '') 
    elif overlap == '移動平均線': 
        real = ta.MA(close, timeperiod=30, matype=0) 
        axes[1].plot(real, '') 
    elif overlap == 'MESA自適應移動平均': 
        mama, fama = ta.MAMA(close, fastlimit=0, slowlimit=0) 
        axes[1].plot(mama, '') 
        axes[1].plot(fama, '') 
    elif overlap == '變周期移動平均線': 
        real = ta.MAVP(close, periods, minperiod=2, maxperiod=30, matype=0) 
        axes[1].plot(real, '') 
    elif overlap == '簡單移動平均線': 
        real = ta.SMA(close, timeperiod=30) 
        axes[1].plot(real, '') 
    elif overlap == '三指數移動平均線(T3)': 
        real = ta.T3(close, timeperiod=5, vfactor=0) 
        axes[1].plot(real, '') 
    elif overlap == '三指數移動平均線': 
        real = ta.TEMA(close, timeperiod=30) 
        axes[1].plot(real, '') 
    elif overlap == '三角形加權法 ': 
        real = ta.TRIMA(close, timeperiod=30) 
        axes[1].plot(real, '') 
    elif overlap == '加權移動平均數': 
        real = ta.WMA(close, timeperiod=30) 
        axes[1].plot(real, '') 
    plt.show() 
 
# 動量指標 
def momentum_process(event): 
    print(event.widget.get()) 
    momentum = event.widget.get() 
 
    upperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) 
    fig, axes = plt.subplots(2, 1, sharex=True) 
    ax1, ax2 = axes[0], axes[1] 
    axes[0].plot(close, '', markersize=3) 
    axes[0].plot(upperband, '') 
    axes[0].plot(middleband, '') 
    axes[0].plot(lowerband, '') 
    axes[0].set_title(momentum, fontproperties="SimHei") 
 
    if momentum == '絕對價格振盪器': 
        real = ta.APO(close, fastperiod=12, slowperiod=26, matype=0) 
        axes[1].plot(real, '') 
    elif momentum == '錢德動量擺動指標': 
        real = ta.CMO(close, timeperiod=14) 
        axes[1].plot(real, '') 
    elif momentum == '移動平均收斂/散度': 
        macd, macdsignal, macdhist = ta.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9) 
        axes[1].plot(macd, '') 
        axes[1].plot(macdsignal, '') 
        axes[1].plot(macdhist, '') 
    elif momentum == '帶可控MA類型的MACD': 
        macd, macdsignal, macdhist = ta.MACDEXT(close, fastperiod=12, fastmatype=0, slowperiod=26, slowmatype=0, signalperiod=9, signalmatype=0) 
        axes[1].plot(macd, '') 
        axes[1].plot(macdsignal, '') 
        axes[1].plot(macdhist, '') 
    elif momentum == '移動平均收斂/散度 固定 12/26': 
        macd, macdsignal, macdhist = ta.MACDFIX(close, signalperiod=9) 
        axes[1].plot(macd, '') 
        axes[1].plot(macdsignal, '') 
        axes[1].plot(macdhist, '') 
    elif momentum == '動量': 
        real = ta.MOM(close, timeperiod=10) 
        axes[1].plot(real, '') 
    elif momentum == '比例價格振盪器': 
        real = ta.PPO(close, fastperiod=12, slowperiod=26, matype=0) 
        axes[1].plot(real, '') 
    elif momentum == '變化率': 
        real = ta.ROC(close, timeperiod=10) 
        axes[1].plot(real, '') 
    elif momentum == '變化率百分比': 
        real = ta.ROCP(close, timeperiod=10) 
        axes[1].plot(real, '') 
    elif momentum == '變化率的比率': 
        real = ta.ROCR(close, timeperiod=10) 
        axes[1].plot(real, '') 
    elif momentum == '變化率的比率100倍': 
        real = ta.ROCR100(close, timeperiod=10) 
        axes[1].plot(real, '') 
    elif momentum == '相對強弱指數': 
        real = ta.RSI(close, timeperiod=14) 
        axes[1].plot(real, '') 
    elif momentum == '隨機相對強弱指標': 
        fastk, fastd = ta.STOCHRSI(close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) 
        axes[1].plot(fastk, '') 
        axes[1].plot(fastd, '') 
    elif momentum == '三重光滑EMA的日變化率': 
        real = ta.TRIX(close, timeperiod=30) 
        axes[1].plot(real, '') 
 
    plt.show() 
 
# 周期指標 
def cycle_process(event): 
    print(event.widget.get()) 
    cycle = event.widget.get() 
 
    upperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) 
    fig, axes = plt.subplots(2, 1, sharex=True) 
    ax1, ax2 = axes[0], axes[1] 
    axes[0].plot(close, '', markersize=3) 
    axes[0].plot(upperband, '') 
    axes[0].plot(middleband, '') 
    axes[0].plot(lowerband, '') 
    axes[0].set_title(cycle, fontproperties="SimHei") 
 
    if cycle == '希爾伯特變換——主要的循環周期': 
        real = ta.HT_DCPERIOD(close) 
        axes[1].plot(real, '') 
    elif cycle == '希爾伯特變換,占主導地位的周期階段': 
        real = ta.HT_DCPHASE(close) 
        axes[1].plot(real, '') 
    elif cycle == '希爾伯特變換——相量組件': 
        inphase, quadrature = ta.HT_PHASOR(close) 
        axes[1].plot(inphase, '') 
        axes[1].plot(quadrature, '') 
    elif cycle == '希爾伯特變換——正弦曲線': 
        sine, leadsine = ta.HT_SINE(close) 
        axes[1].plot(sine, '') 
        axes[1].plot(leadsine, '') 
    elif cycle == '希爾伯特變換——趨勢和周期模式': 
        integer = ta.HT_TRENDMODE(close) 
        axes[1].plot(integer, '') 
 
    plt.show() 
 
 
# 統計功能 
def statistic_process(event): 
    print(event.widget.get()) 
    statistic = event.widget.get() 
 
    upperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) 
    fig, axes = plt.subplots(2, 1, sharex=True) 
    ax1, ax2 = axes[0], axes[1] 
    axes[0].plot(close, '', markersize=3) 
    axes[0].plot(upperband, '') 
    axes[0].plot(middleband, '') 
    axes[0].plot(lowerband, '') 
    axes[0].set_title(statistic, fontproperties="SimHei") 
 
    if statistic == '線性回歸': 
        real = ta.LINEARREG(close, timeperiod=14) 
        axes[1].plot(real, '') 
    elif statistic == '線性回歸角度': 
        real = ta.LINEARREG_ANGLE(close, timeperiod=14) 
        axes[1].plot(real, '') 
    elif statistic == '線性回歸截距': 
        real = ta.LINEARREG_INTERCEPT(close, timeperiod=14) 
        axes[1].plot(real, '') 
    elif statistic == '線性回歸斜率': 
        real = ta.LINEARREG_SLOPE(close, timeperiod=14) 
        axes[1].plot(real, '') 
    elif statistic == '標准差': 
        real = ta.STDDEV(close, timeperiod=5, nbdev=1) 
        axes[1].plot(real, '') 
    elif statistic == '時間序列預測': 
        real = ta.TSF(close, timeperiod=14) 
        axes[1].plot(real, '') 
    elif statistic == '方差': 
        real = ta.VAR(close, timeperiod=5, nbdev=1) 
        axes[1].plot(real, '') 
 
    plt.show() 
 
 
# 數學變換 
def math_transform_process(event): 
    print(event.widget.get()) 
    math_transform = event.widget.get() 
 
    upperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) 
    fig, axes = plt.subplots(2, 1, sharex=True) 
    ax1, ax2 = axes[0], axes[1] 
    axes[0].plot(close, '', markersize=3) 
    axes[0].plot(upperband, '') 
    axes[0].plot(middleband, '') 
    axes[0].plot(lowerband, '') 
    axes[0].set_title(math_transform, fontproperties="SimHei") 
 
 
    if math_transform == '反余弦': 
        real = ta.ACOS(close) 
        axes[1].plot(real, '') 
    elif math_transform == '反正弦': 
        real = ta.ASIN(close) 
        axes[1].plot(real, '') 
    elif math_transform == '反正切': 
        real = ta.ATAN(close) 
        axes[1].plot(real, '') 
    elif math_transform == '向上取整': 
        real = ta.CEIL(close) 
        axes[1].plot(real, '') 
    elif math_transform == '余弦': 
        real = ta.COS(close) 
        axes[1].plot(real, '') 
    elif math_transform == '雙曲余弦': 
        real = ta.COSH(close) 
        axes[1].plot(real, '') 
    elif math_transform == '指數': 
        real = ta.EXP(close) 
        axes[1].plot(real, '') 
    elif math_transform == '向下取整': 
        real = ta.FLOOR(close) 
        axes[1].plot(real, '') 
    elif math_transform == '自然對數': 
        real = ta.LN(close) 
        axes[1].plot(real, '') 
    elif math_transform == '常用對數': 
        real = ta.LOG10(close) 
        axes[1].plot(real, '') 
    elif math_transform == '正弦': 
        real = ta.SIN(close) 
        axes[1].plot(real, '') 
    elif math_transform == '雙曲正弦': 
        real = ta.SINH(close) 
        axes[1].plot(real, '') 
    elif math_transform == '平方根': 
        real = ta.SQRT(close) 
        axes[1].plot(real, '') 
    elif math_transform == '正切': 
        real = ta.TAN(close) 
        axes[1].plot(real, '') 
    elif math_transform == '雙曲正切': 
        real = ta.TANH(close) 
        axes[1].plot(real, '') 
 
    plt.show() 
 
 
# 數學操作 
def math_operator_process(event): 
    print(event.widget.get()) 
    math_operator = event.widget.get() 
 
    upperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) 
    fig, axes = plt.subplots(2, 1, sharex=True) 
    ax1, ax2 = axes[0], axes[1] 
    axes[0].plot(close, '', markersize=3) 
    axes[0].plot(upperband, '') 
    axes[0].plot(middleband, '') 
    axes[0].plot(lowerband, '') 
    axes[0].set_title(math_operator, fontproperties="SimHei") 
 
 
    if math_operator == '指定的期間的最大值': 
        real = ta.MAX(close, timeperiod=30) 
        axes[1].plot(real, '') 
    elif math_operator == '指定的期間的最大值的索引': 
        integer = ta.MAXINDEX(close, timeperiod=30) 
        axes[1].plot(integer, '') 
    elif math_operator == '指定的期間的最小值': 
        real = ta.MIN(close, timeperiod=30) 
        axes[1].plot(real, '') 
    elif math_operator == '指定的期間的最小值的索引': 
        integer = ta.MININDEX(close, timeperiod=30) 
        axes[1].plot(integer, '') 
    elif math_operator == '指定的期間的最小和最大值': 
        min, max = ta.MINMAX(close, timeperiod=30) 
        axes[1].plot(min, '') 
        axes[1].plot(max, '') 
    elif math_operator == '指定的期間的最小和最大值的索引': 
        minidx, maxidx = ta.MINMAXINDEX(close, timeperiod=30) 
        axes[1].plot(minidx, '') 
        axes[1].plot(maxidx, '') 
    elif math_operator == '合計': 
        real = ta.SUM(close, timeperiod=30) 
        axes[1].plot(real, '') 
 
    plt.show() 
 
 
root = tk.Tk() 
 
# 第一行:重疊指標 
rowframe1 = tk.Frame(root) 
rowframe1.pack(side=tk.TOP, ipadx=3, ipady=3) 
tk.Label(rowframe1, text="重疊指標").pack(side=tk.LEFT) 
 
overlap_indicator = tk.StringVar() # 重疊指標 
combobox1 = ttk.Combobox(rowframe1, textvariable=overlap_indicator) 
combobox1['values'] = ['布林線','雙指數移動平均線','指數移動平均線 ','希爾伯特變換——瞬時趨勢線', 
                       '考夫曼自適應移動平均線','移動平均線','MESA自適應移動平均','變周期移動平均線', 
                       '簡單移動平均線','三指數移動平均線(T3)','三指數移動平均線','三角形加權法 ','加權移動平均數'] 
combobox1.current(0) 
combobox1.pack(side=tk.LEFT) 
 
combobox1.bind('<<ComboboxSelected>>', overlap_process) 
 
 
# 第二行:動量指標 
rowframe2 = tk.Frame(root) 
rowframe2.pack(side=tk.TOP, ipadx=3, ipady=3) 
tk.Label(rowframe2, text="動量指標").pack(side=tk.LEFT) 
 
momentum_indicator = tk.StringVar() # 動量指標 
combobox2 = ttk.Combobox(rowframe2, textvariable=momentum_indicator) 
combobox2['values'] = ['絕對價格振盪器','錢德動量擺動指標','移動平均收斂/散度','帶可控MA類型的MACD', 
                       '移動平均收斂/散度 固定 12/26','動量','比例價格振盪器','變化率','變化率百分比', 
                       '變化率的比率','變化率的比率100倍','相對強弱指數','隨機相對強弱指標','三重光滑EMA的日變化率'] 
 
combobox2.current(0) 
combobox2.pack(side=tk.LEFT) 
 
combobox2.bind('<<ComboboxSelected>>', momentum_process) 
 
 
 
# 第三行:周期指標 
rowframe3 = tk.Frame(root) 
rowframe3.pack(side=tk.TOP, ipadx=3, ipady=3) 
tk.Label(rowframe3, text="周期指標").pack(side=tk.LEFT) 
 
cycle_indicator = tk.StringVar() # 周期指標 
combobox3 = ttk.Combobox(rowframe3, textvariable=cycle_indicator) 
combobox3['values'] = ['希爾伯特變換——主要的循環周期','希爾伯特變換——主要的周期階段','希爾伯特變換——相量組件', 
                       '希爾伯特變換——正弦曲線','希爾伯特變換——趨勢和周期模式'] 
 
combobox3.current(0) 
combobox3.pack(side=tk.LEFT) 
 
combobox3.bind('<<ComboboxSelected>>', cycle_process) 
 
 
# 第四行:統計功能 
rowframe4 = tk.Frame(root) 
rowframe4.pack(side=tk.TOP, ipadx=3, ipady=3) 
tk.Label(rowframe4, text="統計功能").pack(side=tk.LEFT) 
 
statistic_indicator = tk.StringVar() # 統計功能 
combobox4 = ttk.Combobox(rowframe4, textvariable=statistic_indicator) 
combobox4['values'] = ['貝塔系數;投資風險與股市風險系數','皮爾遜相關系數','線性回歸','線性回歸角度', 
                       '線性回歸截距','線性回歸斜率','標准差','時間序列預測','方差'] 
 
combobox4.current(0) 
combobox4.pack(side=tk.LEFT) 
 
combobox4.bind('<<ComboboxSelected>>', statistic_process) 
 
 
# 第五行:數學變換 
rowframe5 = tk.Frame(root) 
rowframe5.pack(side=tk.TOP, ipadx=3, ipady=3) 
tk.Label(rowframe5, text="數學變換").pack(side=tk.LEFT) 
 
math_transform = tk.StringVar() # 數學變換 
combobox5 = ttk.Combobox(rowframe5, textvariable=math_transform_process) 
combobox5['values'] = ['反余弦','反正弦','反正切','向上取整','余弦','雙曲余弦','指數','向下取整', 
                       '自然對數','常用對數','正弦','雙曲正弦','平方根','正切','雙曲正切'] 
 
combobox5.current(0) 
combobox5.pack(side=tk.LEFT) 
 
combobox5.bind('<<ComboboxSelected>>', math_transform_process) 
 
 
# 第六行:數學操作 
rowframe6 = tk.Frame(root) 
rowframe6.pack(side=tk.TOP, ipadx=3, ipady=3) 
tk.Label(rowframe6, text="數學操作").pack(side=tk.LEFT) 
 
math_operator = tk.StringVar() # 數學操作 
combobox6 = ttk.Combobox(rowframe6, textvariable=math_operator_process) 
combobox6['values'] = ['指定期間的最大值','指定期間的最大值的索引','指定期間的最小值','指定期間的最小值的索引', 
                       '指定期間的最小和最大值','指定期間的最小和最大值的索引','合計'] 
 
combobox6.current(0) 
combobox6.pack(side=tk.LEFT) 
 
combobox6.bind('<<ComboboxSelected>>', math_operator_process) 
 
 
 
 
root.mainloop()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM