//------------------------------------------------------------------ #property copyright "mladen" #property link "www.tradcode.com" //------------------------------------------------------------------ #property indicator_separate_window #property indicator_buffers 2 #property indicator_plots 1 #property indicator_label1 "Chande DMI" #property indicator_type1 DRAW_COLOR_LINE #property indicator_color1 clrLimeGreen,clrOrange,clrGray #property indicator_width1 2 #property indicator_minimum 0 #property indicator_maximum 100 // // // // // enum enAvgType { avgSma, // Simple moving average avgEma, // Exponential moving average avgSmma, // Smoothed MA avgLwma // Linear weighted MA }; enum enPrices { pr_close, // Close pr_open, // Open pr_high, // High pr_low, // Low pr_median, // Median pr_typical, // Typical pr_weighted, // Weighted pr_average, // Average }; input enPrices Price = pr_close; // Price to use input int StdDevPeriod = 5; // Period of Standard Deviation input int MAStdDevPeriod = 10; // Period of smoothing input enAvgType MAStdDevMode = 0; // Mode of smoothing MA input int DMIPeriod = 15; // Dynamic Momentum Index Period input int DmiLowerLimit = 10; // Dynamic Periods Lower Bound input int DmiUpperLimit = 50; // Dynamic Periods Upper Bound input double LevelUp = 70; // Lower level input double LevelDown = 30; // Upper level double dmi[]; double colorBuffer[]; //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // int OnInit() { SetIndexBuffer(0,dmi,INDICATOR_DATA); SetIndexBuffer(1,colorBuffer,INDICATOR_COLOR_INDEX); // // // // // IndicatorSetInteger(INDICATOR_LEVELS,2); IndicatorSetDouble(INDICATOR_LEVELVALUE,0,LevelUp); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,LevelDown); IndicatorSetString(INDICATOR_SHORTNAME,"Chandes DMI ("+DoubleToString(DMIPeriod,1)+")"); return(0); } //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // int totalBars; double trend[]; double avgma[]; double prics[]; double wrkBuffer[][13]; int OnCalculate(const int rates_total, const int prev_calculated, const datetime& time[], const double& open[], const double& high[], const double& low[], const double& close[], const long& tick_volume[], const long& volume[], const int& spread[]) { totalBars = rates_total; if (ArraySize(trend)!=rates_total) { ArrayResize(trend,rates_total); ArrayResize(avgma,rates_total); ArrayResize(prics,rates_total); ArrayResize(wrkBuffer,rates_total); } // // // // // for (int i=(int)MathMax(prev_calculated-1,1); i LevelUp ) trend[i] = 1; if (dmi[i] < LevelDown) trend[i] = -1; if (trend[i] == 1) colorBuffer[i]=0; if (trend[i] ==-1) colorBuffer[i]=1; if (trend[i] == 0) colorBuffer[i]=2; } return(rates_total); } //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // double getPrice(enPrices price, const double& open[], const double& close[], const double& high[], const double& low[], int i, int bars) { switch (price) { case pr_close: return(close[i]); case pr_open: return(open[i]); case pr_high: return(high[i]); case pr_low: return(low[i]); case pr_median: return((high[i]+low[i])/2.0); case pr_typical: return((high[i]+low[i]+close[i])/3.0); case pr_weighted: return((high[i]+low[i]+close[i]+close[i])/4.0); case pr_average: return((high[i]+low[i]+close[i]+open[i])/4.0); } return(0); } //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // double iDeviationPlus(double& array[], double& ma[], int period, int i) { double sum = 0; for(int k=0; k=0; k++) sum += (array[i-k]-ma[i]) *(array[i-k]-ma[i]); return(MathSqrt(sum/period)); } //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // double workRsi[][3]; #define _price 0 #define _change 1 #define _changa 2 // // // // double iRsi(double price, double period, int bars, int i, int forInstance=0) { if (ArrayRange(workRsi,0)!=bars) ArrayResize(workRsi,bars); int z = forInstance*3; double alpha = 1.0 /(double)period; // // // // // workRsi[i][_price+z] = price; if (i=0; k++) sum += MathAbs(workRsi[i-k][_price+z]-workRsi[i-k-1][_price+z]); workRsi[i][_change+z] = (workRsi[i][_price+z]-workRsi[0][_price+z])/MathMax(k,1); workRsi[i][_changa+z] = sum/MathMax(k,1); } else { double change = workRsi[i][_price+z]-workRsi[i-1][_price+z]; workRsi[i][_change+z] = workRsi[i-1][_change+z] + alpha*( change - workRsi[i-1][_change+z]); workRsi[i][_changa+z] = workRsi[i-1][_changa+z] + alpha*(MathAbs(change) - workRsi[i-1][_changa+z]); } if (workRsi[i][_changa+z] != 0) return(50.0*(workRsi[i][_change+z]/workRsi[i][_changa+z]+1)); else return(0); } //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // double iCustomMa(int mode, double price, double length, int r, int instanceNo=0) { switch (mode) { case avgSma : return(iSma(price,(int)length,r,instanceNo)); case avgEma : return(iEma(price,length,r,instanceNo)); case avgSmma : return(iSmma(price,(int)length,r,instanceNo)); case avgLwma : return(iLwma(price,(int)length,r,instanceNo)); default : return(price); } } //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // double workSma[][4]; double iSma(double price, int period, int r, int instanceNo=0) { if (ArrayRange(workSma,0)!= totalBars) ArrayResize(workSma,totalBars); instanceNo *= 2; // // // // // int k; workSma[r][instanceNo] = price; workSma[r][instanceNo+1] = 0; for(k=0; k=0; k++) workSma[r][instanceNo+1] += workSma[r-k][instanceNo]; workSma[r][instanceNo+1] /= (double)k; return(workSma[r][instanceNo+1]); } // // // // // double workEma[][1]; double iEma(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workEma,0)!= totalBars) ArrayResize(workEma,totalBars); // // // // // double alpha = 2.0 / (1.0+period); workEma[r][instanceNo] = workEma[r-1][instanceNo]+alpha*(price-workEma[r-1][instanceNo]); return(workEma[r][instanceNo]); } // // // // // double workSmma[][1]; double iSmma(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workSmma,0)!= totalBars) ArrayResize(workSmma,totalBars); // // // // // if (r=0; k++) { double weight = period-k; sumw += weight; sum += weight*workLwma[r-k][instanceNo]; } return(sum/sumw); }