Indicator returns wrong values?

 

Good evening,

I have an issue with my expert advisor. It returns different values than which I see in the chart. I want to have the M5-ATR-Values in my M1-Chart to trade. I used the following code to test the values:

int start() {
   RefreshRates();
   
   double atr = iATR(Symbol(), PERIOD_M5, 9, 0);
   if(Hour() == 10 && atr < 7.5) {
      Alert(atr);
   }
   
   return(0);
}

It gives me the following results (It shouldn't print anything until shortly before 11:00 as in the image below it clearly shows that the ATR is never below 7.5 until that moment):

22:51:37 ATR Test test started
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.2778
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.2778
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.2778
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.3111
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.3111
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.3111
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.3111
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.3111
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.3111
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.3111
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.3556
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.3556
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.4111
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.4111
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.4111
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.4667
22:51:37 2013.08.14 10:00 ATR Test #FDXZ3,M1: Alert: 7.4667
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 6.9222
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 6.9222
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 6.9778
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 6.9778
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.0333
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.0333
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.0333
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.0333
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.0333
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.0333
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.0333
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.0333
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.0444
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.0444
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.1
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.1
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.1556
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.1556
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.1556
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.1556
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.2111
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.2667
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.2667
22:51:37 2013.08.14 10:05 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3778
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.3778
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.4333
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.4333
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.4889
22:51:37 2013.08.14 10:06 ATR Test #FDXZ3,M1: Alert: 7.4889
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.2222
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.2222
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.2222
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.2778
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.2778
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.2778
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.2778
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.2778
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.3333
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.3333
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.4444
22:51:37 2013.08.14 10:25 ATR Test #FDXZ3,M1: Alert: 7.4444
22:51:37 2013.08.14 10:30 ATR Test #FDXZ3,M1: Alert: 7.4333
22:51:37 2013.08.14 10:30 ATR Test #FDXZ3,M1: Alert: 7.4333
22:51:37 2013.08.14 10:30 ATR Test #FDXZ3,M1: Alert: 7.4889
22:51:37 2013.08.14 10:30 ATR Test #FDXZ3,M1: Alert: 7.4889
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.1
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.1111
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.1111
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.1667
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.1667
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.2222
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.2222
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.2778
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3333
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3333
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3333
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3333
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3333
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3333
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3333
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.3889
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.4444
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.4444
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.4444
22:51:37 2013.08.14 10:50 ATR Test #FDXZ3,M1: Alert: 7.4444
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.5889
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.5889
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.5889
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.6
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.6
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.6
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.6
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.6
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.6556
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.6556
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.6556
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.6556
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.6556
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.7111
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.7111
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.7111
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.7667
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.7667
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.7667
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.7667
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.7667
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.7667
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.7667
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.8222
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.8222
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.8778
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.8778
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.9333
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.9889
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 6.9889
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.0444
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.0444
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.0444
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.0444
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.0444
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.0444
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.0444
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.0444
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.0444
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.0444
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.0444
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.1
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.1
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.1556
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.1556
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.2111
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.2111
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.2667
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.2667
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.3222
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.3778
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.3778
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.3778
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.3778
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.3778
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.3778
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.4333
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.4333
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.4889
22:51:37 2013.08.14 10:55 ATR Test #FDXZ3,M1: Alert: 7.4889


Here's the image showing the indicator in an extra chart I opened - I also added some arrows to show you that the settings are correct:


Do you have any idea why it's returning the wrong values?

 
Your chart does not show that it never went below 7.5, it shows that the ATR value at M5 candle closes never went below.
 
I'm calculating values for PERIOD_M5 and in the log the values are below 7.5 whilst in the chart they 'never went below 7.5' as you rightly mentioned.
 
FlashX:
I'm calculating values for PERIOD_M5 and in the log the values are below 7.5 whilst in the chart they 'never went below 7.5' as you rightly mentioned.

Adjust your code so that it only alerts when a M5 candle closes.
 
GumRai:

Adjust your code so that it only alerts when a M5 candle closes.

How can I detect that in a M1 chart?
 
FlashX:

How can I detect that in a M1 chart?


You use iTime

int start()
  {
//----
 static datetime M5NewBarTime;
 
 if(M5NewBarTime != iTime(NULL,PERIOD_M5,0) )
  {
  double atr = iATR(Symbol(), PERIOD_M5, 9, 1);
  if(Hour() == 10 && atr < 7.5) 
     Alert(atr);
  M5NewBarTime = iTime(NULL,PERIOD_M5,0);
  }
   
   
//----
   return(0);
  }

Because it will only alert at the first tick of every new M5 bar, the print at 10:45, for example will be the atr at the close of the 10:40 M5 bar.

 

The ATR calculates its value by the tick, it will hold a range of values during the formation of each bar. That is known as repainting the zero bar.

Reason: