Download MetaTrader 5

help with iADX and lag

To add comments, please log in or register
Publish your application in the Market - 7 million potential clients will see it
Marius Guscius
23
Marius Guscius 2016.03.01 20:59 

Hi guys!

I have encountered problem with the iADX lag with daily chart. Basically I have created ea for daily bars based on adx indicator. 

//INDICATOR CODE BEGIN

 int start()

  {

    int limit;

    int counted_bars = IndicatorCounted();

//---- check for possible errors

    if(counted_bars < 0) 

        return(-1);

//---- last counted bar will be recounted

    if(counted_bars > 0) 

        counted_bars--;

    limit = Bars - counted_bars;

//----

    for(int i = 0; i < limit; i++)

      {

        b4plusdi = iADX(NULL, 0, ADXcrossesPeriod, PRICE_CLOSE, MODE_PLUSDI, i - 1);

        nowplusdi = iADX(NULL, 0, ADXcrossesPeriod, PRICE_CLOSE, MODE_PLUSDI, i);

        b4minusdi = iADX(NULL, 0, ADXcrossesPeriod, PRICE_CLOSE, MODE_MINUSDI, i - 1);

        nowminusdi = iADX(NULL, 0, ADXcrossesPeriod, PRICE_CLOSE, MODE_MINUSDI, i);  

        //----

        if(b4plusdi > b4minusdi && nowplusdi < nowminusdi) {

            ExtMapBuffer1[i] = Low[i] - nShift*Point;

            }

        //----

        if(b4plusdi < b4minusdi && nowplusdi > nowminusdi) {

            ExtMapBuffer2[i] = High[i] + nShift*Point;

            }

      }

//----

    return(0);

  }

//INDICATOR CODE END 

 

And here's my ea: 

//EA CODE BEGIN

 void OnTick()

{

   if (IsTradeAllowed() == true) {

      double beforePositiveDI = iADX(NULL, 0, smoothingPeriod, PRICE_CLOSE, MODE_PLUSDI, 0);

      double nowPositiveDI = iADX(NULL, 0, smoothingPeriod, PRICE_CLOSE, MODE_PLUSDI, 1);

      double beforeNegativeDI = iADX(NULL, 0, smoothingPeriod, PRICE_CLOSE, MODE_MINUSDI, 0);

      double nowNegativeDI = iADX(NULL, 0, smoothingPeriod, PRICE_CLOSE, MODE_MINUSDI, 1);

      if (beforePositiveDI > beforeNegativeDI && nowPositiveDI < nowNegativeDI) {

         buy();    

      } else if (beforePositiveDI < beforeNegativeDI && nowPositiveDI > nowNegativeDI) {

         sell(); 

      }

   }  

}

 //EA CODE END

 

Problem is that on daily chart while using indicator I see buy signal at 00:00 time, example:

2016.01.01 00:00

2016.01.02 00:00 buy

2016.01.03 00:00 

2016.01.04 00:00  

But when using my ea I get buy signal way too late (and I expect it to open buy position @ 2016.01.03 00:00):

2016.01.01 00:00

2016.01.02 00:00

2016.01.03 19:02 buy 

2016.01.04 00:00   

 

What should I do to force my ea to open buy position @ 2016.01.02 00:00? I know I could use iADX shift 2 and 1 instead of 1 and 0, but this lags by whole DAY as in:

2016.01.01 00:00

2016.01.02 00:00

2016.01.03 00:00 

2016.01.04 00:00 buy 

Carl Schreiber
6609
Carl Schreiber 2016.03.01 22:03  

Look at your indicator code:

b4plusdi = iADX(NULL, 0, ADXcrossesPeriod, PRICE_CLOSE, MODE_PLUSDI, i - 1);

in mt4 it means you are using the next future bar (x-1) to decide what to do now - try to understand this!

Marius Guscius
23
Marius Guscius 2016.03.01 22:51  
Carl Schreiber:

Look at your indicator code:

in mt4 it means you are using the next future bar (x-1) to decide what to do now - try to understand this!

Oh snap... Basically that means, that there is no way to calculate same way as in indicator except for delaying until the next bar is created in ea, right?
Carl Schreiber
6609
Carl Schreiber 2016.03.01 22:58  

The indicator is coded in a bad manner - don't copy that!

In mt4 (see Docs!) e.g. Time[0] means the actual bar (which is changing) Time[0] is the open-time.

Time[1] is the previous bar's (open) time and Time[0-1] does not exists acc. to a negative index.

That's how the time/quote series are defined in mt4.

But of course 'future times' are possible like Time[0] + 3*_Period*60 - e.g. to place an object.

To add comments, please log in or register