DIFF:=EMA(CLOSE,12) – EMA(CLOSE,26);
DEA:=EMA(DIFF,9);
MACD:2*(DIFF-DEA),NODRAW;
JC:=CROSS(DIFF,DEA);
SC:=CROSSDOWN(DIFF,DEA);
N1:=BARSLAST(JC)+1;
N2:=BARSLAST(SC)+1;
HH:=VALUEWHEN(CROSSDOWN(DIFF,DEA),HHV(H,N1)),NODRAW;//上次MACD红柱期间合约最大值
HH2:=VALUEWHEN(CROSSDOWN(DIFF,DEA),REF(HH,1)),NODRAW;//上上次MACD红柱期间合约最大值
MHD:=VALUEWHEN(CROSSDOWN(DIFF,DEA),HHV(DIFF,N1)),NODRAW;//上次MACD红柱期间DIFF最大值
MHD2:=VALUEWHEN(CROSSDOWN(DIFF,DEA),REF(MHD,1)),NODRAW;//上上次MACD红柱期间DIFF最大值
LL:=VALUEWHEN(CROSS(DIFF,DEA),LLV(L,N2));
LL2:=VALUEWHEN(CROSS(DIFF,DEA),REF(LL,1));
MLD:=VALUEWHEN(CROSS(DIFF,DEA),LLV(DIFF,N2));
MLD2:=VALUEWHEN(CROSS(DIFF,DEA),REF(MLD,1));
A:=CROSSDOWN(MACD,0)&&HH>HH2&&MHD<MHD2;//顶背离
B:=CROSS(MACD,0)&&LL<LL2&&MLD>MLD2;//底背离
A1:=BACKSET(A,HHVBARS(DIFF,N1)+1);
A2:=BACKSET(A,HHVBARS(DIFF,SUMBARS(JC,2))+1);
B1:=BACKSET(B,LLVBARS(DIFF,N2)+1);
//
X:=EMA(CLOSE,12)-EMA(CLOSE,26);
Q:=EMA(X,9);
DRAWTEXT(B,DEA,’B’),FONTSIZE15,ALIGN1,COLORRED;
DRAWTEXT(A,DEA,’S’),FONTSIZE15,ALIGN1,COLORGREEN;
A1X:=BARSLAST(REF(CROSS(X,Q),1));
底背离:=REF(CLOSE,A1X+1)>CLOSE AND X>REF(X,A1X+1) AND CROSS(X,Q);
A2X:=BARSLAST(REF(CROSS(Q,X),1));
顶背离:=REF(CLOSE,A2X+1)<CLOSE AND REF(X,A2X+1)>X AND CROSS(Q,X);
DRAWTEXT(CROSS(B1,0.5),L,’B’),FONTSIZE15,ALIGN1,COLORRED,VALIGN0;
DRAWTEXT(CROSS(A1,0.5),H,’S’),FONTSIZE15,ALIGN1,COLORGREEN;
DRAWTEXT(底背离,L,’B’),FONTSIZE15,ALIGN1,COLORMAGENTA,VALIGN0;
DRAWTEXT(顶背离,H,’S’),FONTSIZE15,ALIGN1,COLORCYAN;