NewBar

 

Hello Guys! 

I have also a question which is also time related.

So I have the following newbar function:

void OnTick()
  {
newbar(_Symbol,PERIOD_D1);
  }


 bool newbar(string Ssymbol,int _timeframe)
  {
   bool bar = false;
   bool first = false;

   datetime  prev = iTime(Ssymbol,_timeframe,0);
   datetime static now;
   
   if(now == 0)
      first = true;

   if(prev > now)
     {
      if(first == false)
        {
         bar = true;
        }
      now = prev;
     }


   return bar;
  }

  This function works if I used it for any other period except daily. If trying to detect new Daily  candle it is not working every time.  

I use this function on a EA which I load it on 5 Min timeframe. 

Kindly advice!

 
  1. Your function only works for a single symbol/timeframe. You can never use it with more than one.
              detecting a new bar without removing ability to detect tick in multiple timeframe - Easy Trading Strategy - MQL4 programming forum #8 (2021.08)

  2. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

 
William Roeder #:
  1. Your function only works for a single symbol/timeframe. You can never use it with more than one.
              detecting a new bar without removing ability to detect tick in multiple timeframe - Easy Trading Strategy - MQL4 programming forum #8 (2021.08)

  2. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

Thank you! I agree, I can only used for one symbol and one timeframe, still, if I want to detect new daily candle while my EA is loaded on a 5 min timeframe, it is not working every time. But if I load the EA on a daily timeframe, it works. 

 
DannyBass:

Hello Guys! 

I have also a question which is also time related.

So I have the following newbar function:

  This function works if I used it for any other period except daily. If trying to detect new Daily  candle it is not working every time.  

I use this function on a EA which I load it on 5 Min timeframe. 

Kindly advice!

Hello,

Try using this function:

datetime      curbar;
datetime      lastbar;

bool new_bar(string symbol,ENUM_TIMEFRAMES periods)
{
   curbar=(datetime)SeriesInfoInteger(symbol,periods,SERIES_LASTBAR_DATE);

   if(lastbar==0) lastbar=(datetime)SeriesInfoInteger(symbol,periods,SERIES_LASTBAR_DATE);
   if(lastbar!=curbar)
   {
      lastbar=curbar;
      return(true);
   }
   return(false);
}
 
Alexander Voronkov #: Try using this function:

Same problem as OP's code.  "you must handle 4066/4073 errors"

 
Alexander Voronkov #:

Hello,

Try using this function:

Thank you 
 
Can anyone please help me out in this. I have made my ea and it is working fine. But i want to add double lot function for my second trade.

If my first buy trade is in loss then i want my ea to wait for four candles and execute the second trade with double lot if ask price is lower than previous buy. And i want it to continue this way for 7 trades until all touches the tp.

I have no idea how to make it happen . 
 
Sunny DP #: Can anyone please help me …I have no idea how to make it happen . 

Help you with what? You haven't stated a problem, you stated a want.
     How To Ask Questions The Smart Way. (2004)
          Prune pointless queries.

You have only four choices:

  1. Search for it (CodeBase or Market). Do you expect us to do your research for you?

  2. Beg at:

  3. MT4: Learn to code it.
    MT5: Begin learning to code it.

    If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

  4. Or pay (Freelance) someone to code it. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2019)

We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
          No free help (2017)

 
William Roeder #:

Same problem as OP's code.  "you must handle 4066/4073 errors"

Thank you ! 

How about this version? I am only trying to detect new day.

bool NewDay()
  {
   int static prev;
  
   if(prev == 0)
      prev = DayOfWeek();
   if(prev != DayOfWeek())
     {prev = DayOfWeek(); return true;}

   return false;
  }
 
DannyBass #: How about this version? I am only trying to detect new day.

You've eliminated the 4066/4073 errors because you are now reading the current chart.

I simplify thus:
bool NewDay()
  {
   int static curr=EMPTY; int prev=curr; curr=DayOfWeek();
   return curr != prev;
  }
 
William Roeder #:

You've eliminated the 4066/4073 errors because you are now reading the current chart.

I simplify thus:
Thank you! 
Reason: