Problem in detecting a new Bar

 

Hi,
I tested these codes to detecting the new Bar, but it had problems, im gonna write each problem under its code

Code1:

//+------------------------------------------------------------------+
//|                                                      example.mq4 |
//|                                         
//+------------------------------------------------------------------+

int init()
  {
//----
   return(0);
  }


int deinit()
  {
//----
   return(0);
  }


int start()
  {

   static bool NewBar=true;
//----
   if(IsNewBar())
     {
       NewBar=false;
     }
//----
   if(!NewBar)
     {
      Alert("NewBar");
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

bool IsNewBar()
  {
   static datetime lastbar;
   datetime curbar = (datetime)SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
   if(lastbar != curbar)
     {
      lastbar = curbar;
      return true;
     }
   return false;
  }


//+------------------------------------------------------------------+

Problem:    Each time the EA is added to the chart, it gives a new bar alert, not accurate, 
for EX:  EA added to chart in: 12:07:43 => (NewBar Alert)   next Alert show in 12:12:43,  in timeframe 5M.

While it should be like this:  EA added to chart in: 12:07:43 => waiting until 12:10:00 and then Alert.

Interestingly, when used only for alert, it works accurately,  But when it is used to code and get the position, it acts as above example !!!

And with local time it doesn't show with server time.

Code2:

//+------------------------------------------------------------------+
//|                                                      example.mq4 |
//|                                         
//+------------------------------------------------------------------+

int init()
  {
//----
   return(0);
  }


int deinit()
  {
//----
   return(0);
  }


int start()
  {

   static bool NewBar=true;
//----
   if(isNewBar())
     {
       NewBar=false;
     }
//----
   if(!NewBar)
     {
      Alert("NewBar");
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

bool isNewBar()
  {
   static datetime last_time=0;
   datetime lastbar_time=SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);

   if(last_time==0)
     {
      last_time=lastbar_time;
      return(false);
     }
   if(last_time!=lastbar_time)
     {
      last_time=lastbar_time;
      return(true);
     }
   return(false);
  }

//+------------------------------------------------------------------+

Problem: This code works very well, The only problem is that the next code's doesn't work properly,
for EX, if i say

    if(!NewBar){
//Get Position
}

Position not taken.
And I don't know why

Code3: 

bool IsNewBar()
   {
datetime M5;
   if(M5!=iTime(Symbol(),PERIOD_M5,0)) // new candle on M5
     {
      return(true);
      M5=iTime(Symbol(),PERIOD_M5,0);    // overwrite old with new value
     }
      return(false);
   }

Problem: Like code 1.


Can anyone give me a code that works exactly like what i need?
And not have the above problems?

expectations:

If a new bar is formed (in 5M timeframe), True a few bool's
But exactly!!!!!

  int start(){

bool NewBar=true;
bool getPos=true;

 if(isNewBar()) 
     {
       NewBar=false;
       getPos=false;
     }

}

bool isNewBar(){

// detecting new bar...
}
 
hamit0111:

Hi,
I tested these codes to detecting the new Bar, but it had problems, im gonna write each problem under its code

Code1:

Problem:    Each time the EA is added to the chart, it gives a new bar alert, not accurate, 
for EX:  EA added to chart in: 12:07:43 => (NewBar Alert)   next Alert show in 12:12:43,  in timeframe 5M.

While it should be like this:  EA added to chart in: 12:07:43 => waiting until 12:10:00 and then Alert.

Interestingly, when used only for alert, it works accurately,  But when it is used to code and get the position, it acts as above example !!!

And with local time it doesn't show with server time.

Code2:

Problem: This code works very well, The only problem is that the next code's doesn't work properly,
for EX, if i say

Position not taken.
And I don't know why

Code3: 

Problem: Like code 1.


Can anyone give me a code that works exactly like what i need?
And not have the above problems?

expectations:

If a new bar is formed (in 5M timeframe), True a few bool's
But exactly!!!!!

Hello,


I'm just using this :


bool IsNewBar()
  {
   static datetime lastbar;
   datetime curbar = Time[0];
   if(lastbar!=curbar)
     {
      lastbar=curbar;
      return (true);
     }
   else
     {
      return(false);
     }
  }


And for me it's working fine...

 
remcous:

Hello,


I'm just using this :



And for me it's working fine...

Each time the EA is added to the chart, it gives a new bar alert, not accurate, 
for EX:  EA added to chart in: 12:07:43 => (NewBar Alert)   next Alert show in 12:12:43,  in timeframe 5M.

While it should be like this:  EA added to chart in: 12:07:43 => waiting until 12:10:00 and then Alert.

Interestingly, when used only for alert, it works accurately,  But when it is used to code and get the position, it acts as above example !!!

 
  1.    static bool NewBar=true;
    //----
       if(isNewBar())
         {
           NewBar=false;
         }
        if(!NewBar){
    //Get Position
    }
    The assignment of true only occurs when the code is loaded (static.) So once you set it to false, it never changes.
  2. If isNewBar returns true, why are you setting the variable to false? Don't use negative logic.
  3. Simplify your code.
       bool NewBar=isNewBar();
        if( NewBar){
    //Get Position
    }
  4. datetime M5;
       if(M5!=iTime(Symbol(),PERIOD_M5,0)) // new candle on M5
         {
          return(true);
          M5=iTime(Symbol(),PERIOD_M5,0);    // overwrite old with new value
         }
          return(false);
    M5 has random values; the if will always be true, and you return true.
  5. Why is there a line after the return? It does nothing.
  6. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

 
William Roeder:
  1. The assignment of true only occurs when the code is loaded (static.) So once you set it to false, it never changes.
  2. If isNewBar returns true, why are you setting the variable to false? Don't use negative logic.
  3. Simplify your code.
  4. M5 has random values; the if will always be true, and you return true.
  5. Why is there a line after the return? It does nothing.
  6. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

thank you for comment, but I still have a problem,

Each time the EA is added to the chart, it gives a new bar alert, not accurate, 
for EX:  EA added to chart in: 12:07:43 => (NewBar Alert)   next Alert show in 12:12:43,  in timeframe 5M.

While it should be like this:  EA added to chart in: 12:07:43 => waiting until 12:10:00 and then Alert.

Interestingly, when used only for alert, it works accurately,  But when it is used to code and get the position, it acts as above example !!!

My codes:

int start()
 {

   static bool enter=false;

   if(IsNewBar())
     {
      enter=true;
      Alert("New Bar");
     }


   if(enter)
     {
///get a position or else

  enter=false;
  }

}


bool IsNewBar()
  {
   static datetime lastbar;
   datetime curbar = (datetime)SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
   if(lastbar != curbar)
     {
      lastbar = curbar;
      return true;
     }
   return false;
  }

when i add the EA to chart, I want to wait for new bar, if bar is opened before,
But it doesn't work that way,
Every time I add it, the alert gives a new candle, and 5M later i give new bar alert again, Regardless of the new candle in the chart,
for EX:  if i add EA to chart in: 12:13:45  i give new bar alert and get POS!!! while its not first of the bar,
and i next time i give alert in 12:18:45!!!!

I am confused,

because when used only for alert, it works accurately,  But when it is used to code and get the position, it acts as above example !!!

 
  1. hamit0111: when i add the EA to chart, I want to wait for new bar, if bar is opened before, But it doesn't work that way,
    So code it to do what you want:
    bool IsNewBar()
      {
       static datetime lastbar = 0;
       datetime curbar = (datetime)SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
       if(lastbar == 0){ lastbar = curbar; return false; } // First tick.
       if(lastbar != curbar)
         {
          lastbar = curbar;
          return true;
         }
       return false;
      }
    or simplified:
    bool IsNewBar()
      {
       static datetime lastbar = 0;
       datetime prevBar = lastbar;
       lastbar = (datetime)SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
       return prevBar != lastbar && prevBar !=0;
      }
  2. hamit0111: EX:  if i add EA to chart in: 12:13:45  i give new bar alert and get POS!!! while its not first of the bar,

    and i next time i give alert in 12:18:45!!!!

    Which could be correct. A new bar starts and the old closes only when a new tick is received after the bar start, e.g. 12:10:00 + PeriodSeconds() or 12:15:00. There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific.) requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles
              No candle if open = close ? - MQL4 programming forum

  3. Lastly, if you don't want to return true when you change timeframes, symbols, etc. you use a "resetting static" paradigm:
    OnInit(){ OnInitNewBar(); … }
    ⋮
    datetime lastbar; void OnInitNewBar(){ lastbar = 0; }
    bool IsNewBar()
      {
      // static datetime lastbar = 0;