MT4 displaying indicator wrong unless I re-compile or re-start MT4

 

Hello everyone,

Happy New year to you all and all the best for 2014!!!

I have been working on the code for the high or low level of the market when the Moving Averages cross up or down, but it keeps recalculating the code for the previous bar when watching live. But when I refresh the chart by closing MT4 down and re-opening, or re-compiling the indicator it calculates the data historically correctly.

Please could you have a look for me. Place the code on a 1 minute chart, just for speed of signal, and you'll see that it looks right, untill it calculates a cross live and you will see what I mean.

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   int limit = Bars-counted_bars;
   for (int i=limit; i >= 0; i--) 
   {
      // Binary Signal:
      static double Level;
      {
      if((iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,i+1)>iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,i+1)) && (iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,i+2)<iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,i+2)))
      Level =High[i+1];
      if((iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,i+1)<iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,i+1)) && (iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,i+2)>iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,i+2)))
      Level =Low[i+1];
      else 
      Level=Level;
      }
      ExtMapBuffer1[i] = Level;
      }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

Below are the 2 charts.

The first chart is when I opened MT4 and let it generate the signal from the code live and you will see the indicator is on the low of the bar when it crossed, but it should only be equal the low of when the MA's crossed on the following bar.

The second chart is after recompiling the indicator and you'll see that it has now correctly displayed the indicator on bar "B".

Has anyone got any ideas on why it displays the indicator perfectly on historical data but pushes the indicator back a bar when it calculates the indicator live?

Thanks very much for your help...I am a bit baffled on this one.

Cheers,

Tim.

 
Timbo618:

Hello everyone,

Happy New year to you all and all the best for 2014!!!

I have been working on the code for the high or low level of the market when the Moving Averages cross up or down, but it keeps recalculating the code for the previous bar when watching live. But when I refresh the chart by closing MT4 down and re-opening, or re-compiling the indicator it calculates the data historically correctly.

Please could you have a look for me. Place the code on a 1 minute chart, just for speed of signal, and you'll see that it looks right, untill it calculates a cross live and you will see what I mean.

If you want anyone to do that you need to provide code that will compile and run . . . not just part of the code.
 

Timbo6182014.01.10 14:40



... but it keeps recalculating the code for the previous bar when watching live.

change

int limit = Bars-counted_bars;

to

int limit = Bars-counted_bars-1;
 
   int limit = Bars-counted_bars;
   for (int i=limit; i >= 0; i--) 
   {
      // Binary Signal:
      static double Level;
      {
      if((iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,i+1)>iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,i+1)) 
      && (iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,i+2)<iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,i+2)))
      Level =High[i+1];
      if((iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,i+1)<iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,i+1)) 
      && (iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,i+2)>iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,i+2)))
      Level =Low[i+1];
      else 
      Level=Level;
      }
      ExtMapBuffer1[i] = Level;
  1. Valid bars are Bars - counted_bars - 1. Your first bar is bogus.
  2. Your look back 1 bars (High[i+1]) and 4 bars (ima(4)) so your look back is 4. Don't look at non-existent bars.
    int    counted_bars=IndicatorCounted();
    if(counted_bars < 4) counted_bars = 4; // Look back
    for (int i=Bars - 1 - counted_bars; i >= 0; i--) 
  3. "else Level=level" Except for the first run (or recompiled) the loop only runs on i=0 so level is bogus. You must save level in a buffer, so you can get it the next time.
    //wong static double Level;
     double Level = ExtMapBuffer1[i+1]; // Use previous level value