关于macd买卖指标的问题,我们总结了以下几点,给你解答:
macd买卖指标公式源码
//该模型仅仅用来示范如何根据指标编写简单的模型
//用户需要根据自己交易经验,进行修改后再实际应用!!!
DIFF : EMA(CLOSE,SHORT) - EMA(CLOSE,LONG);
DEA : EMA(DIFF,M);
MACD:2*(DIFF-DEA),COLORSTICK;
MACD<0 AND REF(MACD,1)MACD>0 AND REF(MACD,1)>MACD,SPK; AUTOFILTER; 这个模型问题在于,当红绿柱参差不齐的时候会发生频繁的反手买卖,所以实战上不建议,仅供参考。
原函数模型:
diff : ema(close,short) - ema(close,long);
dea : ema(diff,m);
cross(diff,dea),bpk;//diff上穿dea,做多。
cross(dea,diff),spk;//diff下穿dea,做空。
autofilter;
其中的cross函数是不包含等于的,只有突破之后才有信号,所以信号是在交叉的下一根k线的,这应该就是你说的对不上的原因。所以文华中本身这个模型是没有问题的。
macd买卖指标
def macd_buy_sell(self, df):
# 初始化买卖信号
df['buy_signal'] = 0
df['sell_signal'] = 0
# 当dif向上突破dea时,买入信号
buy_index = df[df['dif'] > df['dea']].index
df.loc[buy_index, 'buy_signal'] = 1
# 当dif向下跌破dea时,卖出信号
sell_index = df[df['dif'] < df['dea']].index
df.loc[sell_index, 'sell_signal'] = 1
return df
# 计算macd
def macd(self, df):
# 计算dif
df['dif'] = df['close'].ewm(span=12).mean() - df['close'].ewm(span=26).mean()
# 计算dea
df['dea'] = df['dif'].ewm(span=9).mean()
# 计算macd
df['macd'] = 2 * (df['dif'] - df['dea'])
# 计算买卖信号
df = self.macd_buy_sell(df)
return df
# 计算kdj
def kdj(self, df):
# 计算RSV
low_list = df['low'].rolling(9).min()
high_list = df['high'].rolling(9).max()
rsv = (df['close'] - low_list) / (high_list - low_list) * 100
df['rsv'] = rsv
# 计算KDJ
df['k'] = df['rsv'].ewm(com=2).mean()
df['d'] = df['k'].ewm(com=2).mean()
df['j'] = 3 * df['k'] - 2 * df['d']
return df
# 计算cci
def cci(self, df):
# 计算TP
df['tp'] = (df['high'] + df['low'] + df['close']) / 3
# 计算MA
df['ma'] = df['tp'].rolling(14).mean()
# 计算MD
df['md'] = abs(df['tp'] - df['ma']).rolling(14).mean()
# 计算CCI
df['cci'] = (df['tp'] - df['ma']) / (0.015 * df['md'])
return df
# 计算rsi
def rsi(self, df):
# 计算涨跌幅
df['change'] = df['close'].diff()
# 计算上涨的幅度
df['up'] = df['change'][df['change'] > 0].fillna(0)
# 计算下跌的幅度
df['down'] = abs(df['change'][df['change'] < 0].fillna(0))
# 计算RS
df['rs'] = df['up'].rolling(14).mean() / df['down'].rolling(14).mean()
# 计算RSI
df['rsi'] = 100 - (100 / (1 + df['rs']))
return df
# 计算wr
def wr(self, df):
# 计算最高价
df['highest'] = df['high'].rolling(14).max()
# 计算最低价
df['lowest'] = df['low'].rolling(14).min()
# 计算WR
df['wr'] = (df['highest'] - df['close']) / (df['highest'] - df['lowest']) * 100
return df
# 计算boll
def boll(self, df):
# 计算MA
df['ma'] = df['close'].rolling(20).mean()
# 计算标准差
df['std'] = df['close'].rolling(20).std()
# 计算上轨线
df['up'] = df['ma'] + 2 * df['std']
# 计算中轨线
df['mid'] = df['ma']
# 计算下轨线
df['down'] = df['ma'] - 2 * df['std']
return df
# 计算obv
def obv(self, df):
# 计算涨跌幅
df['change'] = df['close'].diff()
# 计算OBV
df['obv'] = df['volume']
df.loc[df['change'] > 0, 'obv'] = df['volume']
df.loc[df['change'] < 0, 'obv'] = -df['volume']
df['obv'] = df['obv'].cumsum()
return df
# 计算roc
def roc(self, df):
# 计算ROC
df['roc'] = (df['close'] - df['close'].shift(12)) / df['close'].shift(12) * 100
return df
# 计算arbr
def arbr(self, df):
# 计算AR
df['ar'] = (df['high'] + df['low']) / 2
# 计算BR
df['br'] = (df['high'] - df['low']) / df['close'].shift(1) * 100
# 计算ARBR
df['arbr'] = (df['ar'] - df['ar'].shift(26)) / df['ar'].shift(26) * 100
return df
# 计算dma
def dma(self, df):
# 计算DMA
df['dma'] = df['close'].rolling(12).mean() - df['close'].rolling(26).mean()
return df
# 计算trix
def trix(self, df):
# 计算EMA1
df['ema1'] = df['close'].ewm(span=12).mean()
# 计算EMA2
df['ema2'] = df['ema1'].ewm(span=12).mean()
# 计算EMA3
df['ema3'] = df['ema2'].ewm(span=12).mean()
# 计算TRIX
df['trix'] = (df['ema3'] - df['ema3'].shift(1)) / df['ema3'].shift(1) * 100
return df
# 计算vr
def vr(self, df):
# 计算上涨天数
df['up_days'] = df['close'] - df['close'].shift(1)
df.loc[df['up_days'] > 0, 'up_days'] = 1
df.loc[df['up_days'] <= 0, 'up_days'] = 0
# 计算下跌天数
df['down_days'] = df['close'].shift(1) - df['close']
df.loc[df['down_days'] > 0, 'down_days'] = 1
df.loc[df['down_days'] <= 0, 'down_days'] = 0
# 计算VR
df['vr'] = df['up
macd买卖指标无未来函数
MACD即指数平滑异同平均线,是由快来自的指数移动平均线(EMA)减去慢的指数移动平均线,MACD的意义和双移动平均线基本相同,但阅读起来更方便。当MACD从负数转向正数,是买的信号础领基降。当MACD从正数转向负数,是卖的信号。
工具/原料交易软件德触司单特蒸起镇语方法/步骤1进入个股k线图,如603889,新澳令考照决住散股份
2在指标栏调出macd指标,这里放大处理,方便查看
3dif(白线)上穿0轴为买入信号,如箭头所示
4dif(白线)上争晶话众铁别穿dea(黄线)买入,黄色箭头所示
5dif(白线)下穿0轴为卖出信号,如箭头所示
6dif(白线)下穿dea(黄线)买入,黄色箭头所示