ta-lib下载地址:Python Extension Packages for Windows
pip install tushare
pip install ta-lib
talib库有超多现成的办法,不消辛辛苦苦造轮子。上面几篇博客写了MACD、动量、rsi、挪动均线的办法,但用起来仍是不爽。刚好talib都有那些函数。
比力懒,就间接放代码吧
先看10日的挪动均线:
import tushare as ts import pandas as pd import matplotlib.pyplot as plt import numpy as np import talib df=ts.get_k_data(600600) df[MA10_rolling] = pd.rolling_mean(df[close],10) close = [float(x) for x in df[close]] # 挪用talib计算10日挪动均匀线的值 df[MA10_talib] = talib.MA(np.array(close), timeperiod=10) df.tail(12)再来看指数挪动均线和MACD
import matplotlib.pyplot as plt import numpy as np import talib df=ts.get_k_data(600600) close = [float(x) for x in df[close]] # 挪用talib计算指数挪动均匀线的值 df[EMA12] = talib.EMA(np.array(close), timeperiod=6) df[EMA26] = talib.EMA(np.array(close), timeperiod=12) # 挪用talib计算MACD目标 df[MACD],df[MACDsignal],df[MACDhist] = talib.MACD(np.array(close), fastperiod=6, slowperiod=12, signalperiod=9) df.tail(12)最初来看动量和RSI的函数
import tushare as ts import pandas as pd import matplotlib.pyplot as plt import numpy as np import talib df=ts.get_k_data(600600) close = [float(x) for x in df[close]] df[RSI]=talib.RSI(np.array(close), timeperiod=12) #RSI的天数一般是6、12、24 df[MOM]=talib.MOM(np.array(close), timeperiod=5) df.tail(12)
0