IsNewBarOnTimeframe() Initial Problem detected

 
Hi everyone. Why does the function IsNewbarOnTimeframe return true when the program is just loaded (at the middle of the current bar)? Why not wait until the current bar (first bar) ends before returning true just like the subsequent bars? How can one code that the function should be false until the first bar ends? See the the function below. For instance, it returns true when one recompiles the program.Thank you.
bool IsNewBarOnTimeframe(int timeframe)
  {  
   static datetime TickTime = TimeCurrent();
   datetime BarOpenTime = iTime(Symbol(),timeframe,0);
   if (TickTime != BarOpenTime)
     {
      TickTime = BarOpenTime;
      return true;
     }
   else 
     {
      return false;
     }
  }
 
macpee:
Hi everyone. Why does the function IsNewbarOnTimeframe return true when the program is just loaded (at the middle of the current bar)? Why not wait until the current bar (first bar) ends before returning true just like the subsequent bars? How can one code that the function should be false until the first bar ends? See the the function below. For instance, it returns true when one recompiles the program.Thank you.
  static datetime TickTime = 0;
  if(TickTime==0)
    TickTime=iTime(Symbol(),timeframe,0);

You should also make sure that the time-frame is fully updated.

 
Keith Watford #:

You should also make sure that the time-frame is fully updated.

Thanks. I guess you mean the following:

bool IsNewBarOnTimeframe(int timeframe)
  {  
   static datetime TickTime = 0;
   datetime BarOpenTime = iTime(Symbol(),timeframe,0);
   if(TickTime==0)
     {
      TickTime=iTime(Symbol(),timeframe,0);
      return true;
     }
   else 
     {
      return false;
     }
  }

Or rather

bool IsNewBarOnTimeframe(int timeframe)
  {  
   static datetime TickTime = 0;
   if(TickTime==0)
     {
      TickTime=iTime(Symbol(),timeframe,0);
      return true;
     }
   else 
     {
      return false;
     }
  }
 
Keith Watford #:

You should also make sure that the time-frame is fully updated.

I am trying this on M1 chart but it is not printing anything.

void OnTick()
  {
   if (IsNewBarOnTimeframe(Period()) == true)
     {
      Print ("This is a new bar");
     }
  }    

bool IsNewBarOnTimeframe(int timeframe)
  {  
   static datetime TickTime = 0;
   if(TickTime==0)
     {
      TickTime=iTime(Symbol(),timeframe,0);
      return true;
     }
   else 
     {
      return false;
     }
  }
 
macpee #:

I am trying this on M1 chart but it is not printing anything.

This one still print for first (incomplete) bar and do not print for subsequent bars. That is the opposite of expectation.
 
macpee #:

I am trying this on M1 chart but it is not printing anything.

Go through your code 1 line at a time and think about what it is doing.

 
Keith Watford #:

Go through your code 1 line at a time and think about what it is doing.

This...

void OnTick()
  {
   if (IsNewBarOnTimeframe(Period()) == true)
     {
      Print ("This is a new bar");
     }
  }    

bool IsNewBarOnTimeframe(int timeframe)
  {  
   static datetime TickTime = 0;
   datetime BarOpenTime = iTime(Symbol(),timeframe,0);
   if (TickTime == 0)
     {
      return false;
     }
   else
     {
      TickTime = BarOpenTime;
      return true;
     }
  }
 
Keith Watford #:

Go through your code 1 line at a time and think about what it is doing.

I think this one is doing fine

bool IsNewBarOnTimeframe(int timeframe)
  {  
   static datetime TickTime = 0;
   datetime BarOpenTime = iTime(Symbol(),timeframe,0);
   if (TickTime != BarOpenTime)
     {
      if (TickTime != 0)
        {
         TickTime = BarOpenTime;
         return true;
        }
      else
        {
         TickTime = BarOpenTime;
         return false;
        }
     }
   else 
     {
      return false;
     }
  }
 
macpee #:

I think this one is doing fine

But it returns true when chart is changed

 
macpee #:

But it returns true when chart is changed

Put the function in Global and initialize it in OnInit first.. maybe it works

Reason: