作者:川川shilohjr_993 | 来源:互联网 | 2023-10-12 09:51
talib库有超多现成的方法,不用辛辛苦苦造轮子。上面几篇博客写了MACD、动量、rsi、移动均线的方法,但用起来还是不爽。刚好talib都有这些函数。比较懒,就直接放代码吧先看10日的移动均线:im
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']]
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']]
df['EMA12'] = talib.EMA(np.array(close), timeperiod=6)
df['EMA26'] = talib.EMA(np.array(close), timeperiod=12)
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)
df['MOM']=talib.MOM(np.array(close), timeperiod=5)
df.tail(12)