Indicator is different in the tester than chart

 

Hello,

I wrote a simple indicator that draws the day's High and Low (useful on timeframes smaller than D1). When I attach the indicator to a chart, it correctly identifies the change of the day at 00:00. The issue is that the indicator, when used in the tester, seems to reset itself twice daily, at 23:00 and at 00:00.


This is the indicator displaying correctly on a live chart.


This is the indicator on the tester.


#property copyright ""
#property link      ""
#property version   ""
#property strict
#property indicator_chart_window
#property indicator_plots   2
#property indicator_buffers 2
//--- plot CurrentHighest
#property indicator_label1  "CurrentHighest"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrLime
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot CurrentLowest
#property indicator_label2  "CurrentLowest"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- indicator buffers
double         CurrentHighestBuffer[];
double         CurrentLowestBuffer[];
datetime thisday;
datetime prevday;


double HH, LL, HHprev, LLprev;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,CurrentHighestBuffer);
   SetIndexBuffer(1,CurrentLowestBuffer);
   IndicatorDigits(Digits);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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 &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

   int counted_bars=IndicatorCounted();
  
   if(counted_bars < 0) 
       return(-1);
   if(counted_bars > 0) 
       counted_bars--;
       
   int limit = Bars - counted_bars;

   for(int i = limit -1 ; i >=0; i--) {
   
      if (isNewDay(i)==true) 
      {        
      HH = High[i];
      LL = Low[i]; 
      }
      else 
      {
      if (High[i] > HH) HH = High[i]; 
      if (Low[i] < LL) LL = Low[i];
      }
      CurrentHighestBuffer[i] = HH;
      CurrentLowestBuffer[i] = LL;
   }
  
   return(rates_total);
  }
  
//+------------------------------------------------------------------+

bool isNewDay(int x)
{
   thisday = TimeDay(Time[x]);        
   if (thisday != prevday)            
   {prevday = thisday; return true; } 
   else return false;                 
}   
//+------------------------------------------------------------------+

The issue exists both when the indicator is tested by itself and when it's tested as a part of an EA.

Any help will be appreciated

 
mix1911:

Hello,

The issue exists both when the indicator is tested by itself and when it's tested as a part of an EA.

Any help will be appreciated

 /*int counted_bars=IndicatorCounted();
  
   if(counted_bars < 0) 
       return(-1);
   if(counted_bars > 0) 
       counted_bars--;
       
   int limit = Bars - counted_bars;*/

   int limit;
   
   if (prev_calculated < 0)
      return(-1);
   if (prev_calculated == 0)
      limit = rates_total - 1;
   else
      limit  = rates_total - prev_calculated + 1;
      
   //for(int i = limit -1 ; i >=0; i--) {
   for(int i = limit; i >= 0; i--) {
 
Naguisa Unada:
Thank you for the prompt reply but unfortunately the change made no difference. The indicator still works well on chart but not on the tester.
 
mix1911:
Thank you for the prompt reply but unfortunately the change made no difference. The indicator still works well on chart but not on the tester.

It's my mistake.

//limit  = rates_total - prev_calculated + 1;
limit  = rates_total - prev_calculated;
 
Naguisa Unada:

It's my mistake.

That did it! Thank you very much for the assist!