[Plz help] New bar detection function in an indicator doesn't work in backtest visual mode

To add comments, please log in or register
yum1573
40
yum1573  

Hi everyone,

I have a problem as follows.

When I use the function isNewBar_up() to detect a new bar in higher-level chart in a self-made  indicator in real time it works well. The functions comes from here: https://www.mql5.com/en/articles/159

However, when I run some backtest in visual mode(model: every tick), the same isNewBar_up() function in the same indicator doesn't work properly. Every time when it's called, the return is always "false" no matter a new higher-level bar comes or not.

Is it any feature in backtest mode that disturbs this function to work in backtest mode?


Thanks very much


int start()
{
   ...
   //Current time frame = PERIOD_M5
   if(isNewBar_up(PERIOD_M15))
   ...
}


bool isNewBar_up(int TF_up)
{
   static datetime last_time_up = 0;
   datetime lastbar_time_up = (datetime) SeriesInfoInteger(_Symbol, TF_up, SERIES_LASTBAR_DATE);
   if(last_time_up == 0)
   {
      last_time_up = lastbar_time_up;
      return(false);
   }
   if(last_time_up != lastbar_time_up)
   {
      last_time_up = lastbar_time_up;
      return(true);
   }
   else return(false);
}
The "New Bar" Event Handler
The "New Bar" Event Handler
  • www.mql5.com
Authors of indicators and experts have always been interested in writing the compact code in terms of execution time. You can approach to this problem from different angles. From this broad topic in this article we will cover the problem, that is seemingly already have been solved: check for a new bar. This is quite a popular way to limit the...
William Roeder
22407
William Roeder  
yum1573: When I use the function isNewBar_up() to detect a new bar in higher-level chart in a self-made  indicator in real time it works well.
Indicators go through the bars and display things. You shouldn't ever, need isNewBar in an indicator.
AIRAT SAFIN
1421
AIRAT SAFIN  
The posted function has an obvious logical defect
because
1 => MAY BE USED for DIFFERENT time frames
but

2 => REMEMBERS ONLY ONE time value for the last checked time frame
<in the static variable last_time_up>

yum1573
40
yum1573  
William Roeder:
Indicators go through the bars and display things. You shouldn't ever, need isNewBar in an indicator.

Thank you for your reply.

Any replacement for isNewBar? I want to detect the bar of higher level...

yum1573
40
yum1573  
AIRAT SAFIN:
The posted function has an obvious logical defect
because
1 => MAY BE USED for DIFFERENT time frames
but

2 => REMEMBERS ONLY ONE time value for the last checked time frame
<in the static variable last_time_up>

I wrote other codes to detect current time frame then use it in the function.

The code I posted is just for convenience.

Thanks anyway.

   //Current time frame = PERIOD_M5
   if(isNewBar_up(PERIOD_M15))
Documentation on MQL5: Date and Time / TimeCurrent
Documentation on MQL5: Date and Time / TimeCurrent
  • www.mql5.com
Returns the last known server time, time of the last quote receipt for one of the symbols selected in the "Market Watch" window. In the OnTick() handler, this function returns the time of the received handled tick. In other cases (for example, call in handlers OnInit(), OnDeinit(), OnTimer() and so on) this...
William Roeder
22407
William Roeder  
yum1573: Any replacement for isNewBar? I want to detect the bar of higher level...

Yes, your brain. Stop thinking about a new bar, and code your loop. No detection is necessary.
          The XY Problem

yum1573
40
yum1573  
William Roeder:

Yes, your brain. Stop thinking about a new bar, and code your loop. No detection is necessary.
          The XY Problem

I solved it by directly take value from past bars. Thank you for the sharing.

To add comments, please log in or register