N:=16;
MM:=4;
MTR:=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/MTR;
MDI: DMM*100/MTR;
ADX: EMA(ABS(MDI-PDI)/(MDI+PDI)*100,MM);
ADXR:EMA(ADX,MM);