N:=21;
M:=6;
M1:=20;
TR1:=EMA(MAX(MAX(HIGH-LOW,Abs(HIGH-REF(CLOSE,1))),ABS(REF(CLOSE,1)-LOW)),N);
HD :=HIGH-REF(HIGH,1);
LD :=REF(LOW,1)-LOW;
DMP:=EMA(IF(HD>0 AND HD>LD,HD,0),N);
DMM:=EMA(IF(LD>0 AND LD>HD,LD,0),N);
pdi:= DMP*100/TR1;
mdi:= DMM*100/TR1;
ADX: EMA((PDI-MDI)/(MDI+PDI)*100,M)*2;
ADXR:EMA(ADX,M);
ADMA:EMA(ADX,M1);
ADX1:ADX,COLORRED;
ADXR1:ADXR,COLORGREEN;
A1:=BArslAST(REF(crOSS(ADX,ADXR),1));
B1:=REF(C,A1+1)>C AND REF(ADXR,A1+1)
DRAWTEXT(B1>0,ADX-0.1,'底背离'),COLORFF00FF;
C1:=BARSLAST(REF(CROSS(ADXR,ADX),1));
D1:=REF(C,C1+1)
DRAWTEXT(D1> 0,ADX+0.1,'顶背离');