def MACDEXT(df, n_fast, n_slow, n_signal, matype = 0):
macd, macdsignal, macdhist = talib.MACDEXT(df['close'].values, fastperiod=n_fast, fastmatype=matype, slowperiod=n_slow, slowmatype=matype, signalperiod=n_signal, signalmatype=matype)
MACD = pd.Series(macd, index = df.index, name = 'MACD' + str(n_fast) + '_' + str(n_slow) + '_' + str(n_signal))
MACDsig = pd.Series(macdsignal, index = df.index, name = 'MACDsig' + str(n_fast) + '_' + str(n_slow) + '_' + str(n_signal))
MACDhist = pd.Series(macdhist, index = df.index, name = 'MACDhist' + str(n_fast) + '_' + str(n_slow) + '_' + str(n_signal))
return pd.concat([MACD, MACDsig, MACDhist], join='outer', axis=1)
#Mass Index
python类MACDEXT的实例源码
def MACD(security_list, fastperiod=12, slowperiod=26, signalperiod=9):
# ????????????
if isinstance(security_list, str):
security_list = [security_list]
# ?? MACD
security_data = history(slowperiod * 2, '1d', 'close',
security_list, df=False, skip_paused=True)
macd_DIF = {}
macd_DEA = {}
macd_HIST = {}
for stock in security_list:
macd_DIF[stock], macd_DEA[stock], macd = talib.MACDEXT(
security_data[stock], fastperiod=fastperiod, fastmatype=1, slowperiod=slowperiod, slowmatype=1, signalperiod=signalperiod, signalmatype=1)
macd_HIST[stock] = macd * 2
return macd_DIF, macd_DEA, macd_HIST
# MA
def MACD_CN(close, fastperiod=12, slowperiod=26, signalperiod=9) :
macdDIFF, macdDEA, macd = tl.MACDEXT(close, fastperiod=fastperiod, fastmatype=1, slowperiod=slowperiod, slowmatype=1, signalperiod=signalperiod, signalmatype=1)
macd = macd * 2
return macdDIFF, macdDEA, macd
def add_MACDEXT(self, fastperiod=12, fastmatype=0,
slowperiod=26, slowmatype=0,
signalperiod=9, signalmatype=0,
types=['line', 'line', 'histogram'],
colors=['primary', 'tertiary', 'fill'],
**kwargs):
"""Moving Average Convergence Divergence with Controllable MA Type.
Note that the first argument of types and colors refers to MACD,
the second argument refers to MACD signal line and the third argument
refers to MACD histogram.
"""
if not self.has_close:
raise Exception()
utils.kwargs_check(kwargs, VALID_TA_KWARGS)
if 'kind' in kwargs:
kwargs['type'] = kwargs['kind']
if 'kinds' in kwargs:
types = kwargs['type']
if 'type' in kwargs:
types = [kwargs['type']] * 3
if 'color' in kwargs:
colors = [kwargs['color']] * 3
name = 'MACDEXT({},{},{})'.format(str(fastperiod),
str(slowperiod),
str(signalperiod))
macd = name
smacd = name + '[Sign]'
hmacd = name + '[Hist]'
self.sec[macd] = dict(type=types[0], color=colors[0])
self.sec[smacd] = dict(type=types[1], color=colors[1], on=macd)
self.sec[hmacd] = dict(type=types[2], color=colors[2], on=macd)
(self.ind[macd],
self.ind[smacd],
self.ind[hmacd]) = talib.MACDEXT(self.df[self.cl].values,
fastperiod, fastmatype,
slowperiod, slowmatype,
signalperiod, signalmatype)