posterx Posted October 20, 2010 Report Share Posted October 20, 2010 (edited) Hello, The ADX indicator, Wilders version is missing from MT4 but available with MT5. Below is the MT5 code. Can any MT coding expert please port this code to MT4. Many Thanks //+------------------------------------------------------------------+ //| ADXW.mq5 | //| Copyright 2009, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2009, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property description "Average Directional Movement Index" #property description "by Welles Wilder" #include <MovingAverages.mqh> //--- #property indicator_separate_window #property indicator_buffers 10 #property indicator_plots 3 #property indicator_type1 DRAW_LINE #property indicator_style1 STYLE_SOLID #property indicator_width1 1 #property indicator_color1 LightSeaGreen #property indicator_type2 DRAW_LINE #property indicator_style2 STYLE_DOT #property indicator_width2 1 #property indicator_color2 YellowGreen #property indicator_type3 DRAW_LINE #property indicator_style3 STYLE_DOT #property indicator_width3 1 #property indicator_color3 Wheat #property indicator_label1 "ADX Wilder" #property indicator_label2 "+DI" #property indicator_label3 "-DI" //--- input parameters input int InpPeriodADXW=14; // Period //---- buffers double ExtADXWBuffer[]; double ExtPDIBuffer[]; double ExtNDIBuffer[]; double ExtPDSBuffer[]; double ExtNDSBuffer[]; double ExtPDBuffer[]; double ExtNDBuffer[]; double ExtTRBuffer[]; double ExtATRBuffer[]; double ExtDXBuffer[]; //--- global variable int ExtADXWPeriod; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ void OnInit() { //--- check for input parameters if(InpPeriodADXW>=100 || InpPeriodADXW<=0) { ExtADXWPeriod=14; printf("Incorrect value for input variable InpPeriodADXW=%d. Indicator will use value=%d for calculations.",InpPeriodADXW,ExtADXWPeriod); } else ExtADXWPeriod=InpPeriodADXW; //---- indicator buffers SetIndexBuffer(0,ExtADXWBuffer); SetIndexBuffer(1,ExtPDIBuffer); SetIndexBuffer(2,ExtNDIBuffer); //--- calculation buffers SetIndexBuffer(3,ExtPDBuffer,INDICATOR_CALCULATIONS); SetIndexBuffer(4,ExtNDBuffer,INDICATOR_CALCULATIONS); SetIndexBuffer(5,ExtDXBuffer,INDICATOR_CALCULATIONS); SetIndexBuffer(6,ExtTRBuffer,INDICATOR_CALCULATIONS); SetIndexBuffer(7,ExtATRBuffer,INDICATOR_CALCULATIONS); SetIndexBuffer(8,ExtPDSBuffer,INDICATOR_CALCULATIONS); SetIndexBuffer(9,ExtNDSBuffer,INDICATOR_CALCULATIONS); //--- set draw begin PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtADXWPeriod<<1); PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtADXWPeriod+1); PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtADXWPeriod+1); //--- indicator short name string short_name="ADX Wilder("+string(ExtADXWPeriod)+")"; IndicatorSetString(INDICATOR_SHORTNAME,short_name); //--- change 1-st index label PlotIndexSetString(0,PLOT_LABEL,short_name); //--- indicator digits IndicatorSetInteger(INDICATOR_DIGITS,2); //---- end of initialization function } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ 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 &TickVolume[], const long &Volume[], const int &Spread[]) { //--- checking for bars count if(rates_total<ExtADXWPeriod) return(0); //--- detect start position int start; if(prev_calculated>1) start=prev_calculated-1; else { start=1; for(int i=0;i<ExtADXWPeriod;i++) { ExtADXWBuffer=0; ExtPDIBuffer=0; ExtNDIBuffer=0; ExtPDSBuffer=0; ExtNDSBuffer=0; ExtPDBuffer=0; ExtNDBuffer=0; ExtTRBuffer=0; ExtATRBuffer=0; ExtDXBuffer=0; } } //--- main cycle for(int i=start;i<rates_total;i++) { //--- get some data double Hi =High; double prevHi=High[i-1]; double Lo =Low; double prevLo=Low[i-1]; double prevCl=Close[i-1]; //--- fill main positive and main negative buffers double dTmpP=Hi-prevHi; double dTmpN=prevLo-Lo; if(dTmpP<0.0) dTmpP=0.0; if(dTmpN<0.0) dTmpN=0.0; if(dTmpN==dTmpP) { dTmpN=0.0; dTmpP=0.0; } else { if(dTmpP<dTmpN) dTmpP=0.0; else dTmpN=0.0; } ExtPDBuffer=dTmpP; ExtNDBuffer=dTmpN; //--- define TR double tr=MathMax(MathMax(MathAbs(Hi-Lo),MathAbs(Hi-prevCl)),MathAbs(Lo-prevCl)); //--- write down TR to TR buffer ExtTRBuffer=tr; //--- fill smoothed positive and negative buffers and TR buffer if(i<ExtADXWPeriod) { ExtATRBuffer=0.0; ExtPDIBuffer=0.0; ExtNDIBuffer=0.0; } else { ExtATRBuffer=SmoothedMA(i,ExtADXWPeriod,ExtATRBuffer[i-1],ExtTRBuffer); ExtPDSBuffer=SmoothedMA(i,ExtADXWPeriod,ExtPDSBuffer[i-1],ExtPDBuffer); ExtNDSBuffer=SmoothedMA(i,ExtADXWPeriod,ExtNDSBuffer[i-1],ExtNDBuffer); } //--- calculate PDI and NDI buffers if(ExtATRBuffer!=0.0) { ExtPDIBuffer=100.0*ExtPDSBuffer/ExtATRBuffer; ExtNDIBuffer=100.0*ExtNDSBuffer/ExtATRBuffer; } else { ExtPDIBuffer=0.0; ExtNDIBuffer=0.0; } //--- Calculate DX buffer double dTmp=ExtPDIBuffer+ExtNDIBuffer; if(dTmp!=0.0) dTmp=100.0*MathAbs((ExtPDIBuffer-ExtNDIBuffer)/dTmp); else dTmp=0.0; ExtDXBuffer=dTmp; //--- fill ADXW buffer as smoothed DX buffer ExtADXWBuffer=SmoothedMA(i,ExtADXWPeriod,ExtADXWBuffer[i-1],ExtDXBuffer); } //---- OnCalculate done. Return new prev_calculated. return(rates_total); } //+------------------------------------------------------------------+ Edited October 20, 2010 by posterx Quote Link to comment Share on other sites More sharing options...
osirisja Posted October 20, 2010 Report Share Posted October 20, 2010 Wilders ADX here is the wilders version for MT4 Cheers Andy //+------------------------------------------------------------------+ //| Wilder's DMI.mq4 | //| coded by mladen | //| | //| Directional movement index was developed by Welles Wilder | //+------------------------------------------------------------------+ #property copyright "mladen" #property link "[email protected]" #property indicator_separate_window #property indicator_buffers 5 #property indicator_minimum 0 #property indicator_color1 Lime #property indicator_color2 Tomato #property indicator_color3 DimGray #property indicator_color4 Orange #property indicator_color5 DimGray #property indicator_width3 2 #property indicator_width4 2 #property indicator_style1 STYLE_DOT #property indicator_style2 STYLE_DOT #property indicator_style5 STYLE_DOT // // // // // extern int DMI.Length = 14; extern bool ShowADX = true; extern bool ShowADXR = false; extern int Level = 20; // // // // // double DIp[]; double DIm[]; double ADX[]; double ADXR[]; double ADXLevel[]; double averageDIp[]; double averageDIm[]; double averageTR[]; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // int init() { IndicatorBuffers(8); SetIndexBuffer(0,DIp); SetIndexLabel(0,"DI+"); SetIndexBuffer(1,DIm); SetIndexLabel(1,"DI-"); SetIndexBuffer(2,ADX); SetIndexLabel(2,"ADX"); SetIndexBuffer(3,ADXR); SetIndexLabel(3,"ADXR"); SetIndexBuffer(4,ADXLevel); SetIndexLabel(4,NULL); SetIndexBuffer(5,averageDIp); SetIndexBuffer(6,averageDIm); SetIndexBuffer(7,averageTR); for (int i=0;i<8;i++) SetIndexEmptyValue(i,0.00); // // // // // IndicatorShortName("Wilder\'s DMI ("+DMI.Length+")"); return(0); } int deinit() { return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // int start() { int counted_bars=IndicatorCounted(); int i,limit; if(counted_bars<0) return(-1); if(counted_bars>0) counted_bars--; limit=Bars-counted_bars; limit=MathMin(limit,Bars-2); // // // // // double sf = (DMI.Length-1.0)/DMI.Length; for (i=limit;i>=0;i--) { double currTR = MathMax(High,Close[i+1])-MathMin(Low,Close[i+1]); double DeltaHi = High - High[i+1]; double DeltaLo = Low[i+1] - Low; double plusDM = 0.00; double minusDM = 0.00; if ((DeltaHi > DeltaLo) && (DeltaHi > 0)) plusDM = DeltaHi; if ((DeltaLo > DeltaHi) && (DeltaLo > 0)) minusDM = DeltaLo; // // // // // averageDIp = sf*averageDIp[i+1] + plusDM; averageDIm = sf*averageDIm[i+1] + minusDM; averageTR = sf*averageTR[i+1] + currTR; ADXLevel = Level; // // // // // DIp = 0.00; DIm = 0.00; if (averageTR > 0) { DIp = 100.00 * averageDIp/averageTR; DIm = 100.00 * averageDIm/averageTR; } if(ShowADX) { double DX; if((DIp + DIm)>0) DX = 100*MathAbs(DIp - DIm)/(DIp + DIm); else DX = 0.00; ADX = sf*ADX[i+1] + DX/DMI.Length; if(ShowADXR) ADXR = 0.5*(ADX + ADX[i+DMI.Length]); } } return(0); } domonkos, posterx and metal1713006284 3 Quote Link to comment Share on other sites More sharing options...
posterx Posted October 20, 2010 Author Report Share Posted October 20, 2010 I am getting the following errors when compiling the MT4 code. Can anyone please help. Thanks http://img249.imageshack.us/img249/6641/adxerror.jpg Quote Link to comment Share on other sites More sharing options...
metal1713006284 Posted October 20, 2010 Report Share Posted October 20, 2010 I copied the formula on the notepad and saved as MQ4 file (be carefull to copy just the formula) then I put the saved file in indicator file of metatrader and opened the metatrader (actualy I opened the metatrader twice, since the first time I did not see the indicator on the list). the indicator works no problem. posterx 1 Quote Link to comment Share on other sites More sharing options...
osirisja Posted October 21, 2010 Report Share Posted October 21, 2010 adx wilders 2 if anyone is interested also - here is the wilders dmi in BAR and dot format - it needs the wilders dmi book indicator that I posted earlier. Just displays ADX/DMI in a different way. This indicator should be saved as 'Wilders_DMI_FILTER_sBar_MTF.MQ4' and the previous wilders indi (above) should be named 'Wilder's DMI book.mq4' copy and paste between the markers below Cheers Andy ============== MQ4 after her ======================= #property copyright "" #property link "" #property indicator_separate_window #property indicator_minimum 1.0 #property indicator_buffers 5 #property indicator_color1 Green #property indicator_color2 Red #property indicator_color3 Yellow #property indicator_color4 Lime #property indicator_color5 Orchid // // // // // extern int TimeFrame = 0; extern int ADX_Period = 14; extern int ADX_Level = 20; extern int sBarLevel = 5; extern bool _ADXstyleLine = FALSE; extern bool pADXstyleLine = FALSE; extern bool nADXstyleLine = TRUE; extern string note_sBarLevel = "sBarLevel:1-77(bar position)"; extern string note_ADXstyle = " ADXstyle: Line/Dot"; extern string note_TimeFrames = "M1;5,15,30,60H1;240H4;1440D1;10080W1;43200MN"; // // // // // double buffer1[]; double buffer2[]; double buffer3[]; double buffer4[]; double buffer5[]; // // // // // int init() { SetIndexBuffer(0, buffer4); SetIndexStyle(0, DRAW_ARROW); SetIndexArrow(0, 167); SetIndexEmptyValue(0, EMPTY_VALUE); SetIndexBuffer(1, buffer5); SetIndexStyle(1, DRAW_ARROW); SetIndexArrow(1, 167); SetIndexEmptyValue(1, EMPTY_VALUE); SetIndexBuffer(2, buffer1); if (_ADXstyleLine) SetIndexStyle(2, DRAW_LINE); else { SetIndexStyle(2, DRAW_ARROW); SetIndexArrow(2, 158); SetIndexEmptyValue(2, EMPTY_VALUE); } SetIndexBuffer(3, buffer2); if (pADXstyleLine) SetIndexStyle(3, DRAW_LINE); else { SetIndexStyle(3, DRAW_ARROW); SetIndexArrow(3, 158); SetIndexEmptyValue(3, EMPTY_VALUE); } SetIndexBuffer(4, buffer3); if (nADXstyleLine) SetIndexStyle(4, DRAW_LINE); else { SetIndexStyle(4, DRAW_ARROW); SetIndexArrow(4, 158); SetIndexEmptyValue(4, EMPTY_VALUE); } SetIndexDrawBegin(1, 2); SetIndexDrawBegin(2, 2); SetIndexDrawBegin(3, 2); SetIndexDrawBegin(4, 2); // // // // // TimeFrame = MathMax(TimeFrame, Period()); string timeFrameName= ""; switch (TimeFrame) { case 1: timeFrameName = "M1"; break; case 5: timeFrameName = "M5"; break; case 15: timeFrameName = "M15"; break; case 30: timeFrameName = "M30"; break; case 60: timeFrameName = "H1"; break; case 240: timeFrameName = "H4"; break; case 1440: timeFrameName = "D1"; break; case 10080: timeFrameName = "W1"; break; case 43200: timeFrameName = "MN"; break; default: timeFrameName = "TF0"; } // // // // // IndicatorShortName("Wilde\'s DMI (" + ADX_Period + ") Filter sBar [" + timeFrameName + "]"); SetIndexLabel(0, "posDI [" + TimeFrame + "]Up"); SetIndexLabel(1, "negDI [" + TimeFrame + "]Down"); SetIndexLabel(2, "DMI [" + TimeFrame + "] Level < " + ADX_Level + ""); SetIndexLabel(3, "DMI [" + TimeFrame + "] Up"); SetIndexLabel(4, "DMI [" + TimeFrame + "] Down"); SetIndexDrawBegin(0, 2); return (0); } int deinit() { return (0); } // // // // // int start() { int counted_bars=IndicatorCounted(); int i,limit; if(counted_bars<0) return(-1); if(counted_bars>0) counted_bars--; limit=MathMin(Bars-counted_bars,Bars-1); limit=MathMax(limit,TimeFrame/Period()); // // // // // for(i=limit;i>=0;i--) { int y = iBarShift(NULL,TimeFrame,Time); double cAdx = iCustom(NULL,TimeFrame,"Wilder\'s DMI book",ADX_Period,2,y); double pAdx = iCustom(NULL,TimeFrame,"Wilder\'s DMI book",ADX_Period,2,y+1); double cDmp = iCustom(NULL,TimeFrame,"Wilder\'s DMI book",ADX_Period,0,y); double cDmm = iCustom(NULL,TimeFrame,"Wilder\'s DMI book",ADX_Period,1,y); if (cAdx > pAdx) { buffer2 = sBarLevel; buffer3 = EMPTY_VALUE; } else { buffer2 = EMPTY_VALUE; buffer3 = sBarLevel; } if (cAdx < ADX_Level) buffer1 = sBarLevel + 5; if (cDmp > cDmm) buffer4 = sBarLevel + 2; if (cDmp < cDmm) buffer5 = sBarLevel + 2; } return (0); } ============ to here ================== posterx and metal1713006284 2 Quote Link to comment Share on other sites More sharing options...
vladv Posted October 21, 2010 Report Share Posted October 21, 2010 Thanks for sharing these MT4 versions! Quote Link to comment Share on other sites More sharing options...
a1ra Posted November 3, 2010 Report Share Posted November 3, 2010 Wilder DMI package with multipair wilder DMI http://www.mediafire.com/?x8l1dcmwwtetgcm Keithp 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.