import pandas as pd
class macd(object):
'''
名称:MACD指标
参数:short:短移动指数计算跨度取值如12;long:长移动指数计算跨度取值如26;mid:差值移动指数计算跨度取值如9.
说明:输出格式:[macd ,dif,dea]
'''
def __init__(self,short,long,mid):
self._short = short
self._long = long
self._mid = mid
self._datalist = []
def cal_macd(self,data):
self._datalist.append(data)
datalistseries=pd.Series(self._datalist)
shortema = datalistseries.ewm(ignore_na=True, span=self._short, min_periods=0, adjust=False).mean()
longema = datalistseries.ewm(ignore_na=True, span=self._long, min_periods=0, adjust=False).mean()
dif = shortema - longema
dea = dif.ewm(ignore_na=False, span=self._mid, min_periods=0, adjust=False).mean()
macdresult = 2 * (dif - dea)
macdresultlist = list(macdresult)
diflist=list(dif)
dealist=list(dea)
return [macdresultlist[-1],diflist[-1],dealist[-1]]