detecting a new bar without removing ability to detect tick in multiple timeframe

 

Hi I was created a thread about static datetime in here: https://www.mql5.com/en/forum/362972 and successfully working on it. The code from the previous thread detect and make the EA to works only once a bar since I put it on the first of the strategy. Now, I want to use the newbar variable in the previous thread to works with the tick.

In my EA I use multiple timeframe to do the strategy. I want to detect the new bar in the higher timeframe and do something in the lower timeframe but I still want to have the higher timeframe to works on the tick. The problem is when I use the code on the higher timeframe, the ability to detect tick changes is gone. I want the higher timeframe to have the ability to detect new bar but also doesn't want to remove the ability to detect tick, is there a way to code that way?

//Detect a new bar   
   static datetime preTime=0,curTime;
   bool newBar=false;
   curTime=Time[0];
   if(preTime!=curTime)
     {
      preTime=curTime;
      newBar=true;
     }
Dealing with static datetime function
Dealing with static datetime function
  • 2021.02.17
  • www.mql5.com
Hi I tried to working on the code that is detecting new bar function...
 
Luandre Ezra:

Hi I was created a thread about static datetime in here: https://www.mql5.com/en/forum/362972 and successfully working on it. The code from the previous thread detect and make the EA to works only once a bar since I put it on the first of the strategy. Now, I want to use the newbar variable in the previous thread to works with the tick.

In my EA I use multiple timeframe to do the strategy. I want to detect the new bar in the higher timeframe and do something in the lower timeframe but I still want to have the higher timeframe to works on the tick. The problem is when I use the code on the higher timeframe, the ability to detect tick changes is gone. I want the higher timeframe to have the ability to detect new bar but also doesn't want to remove the ability to detect tick, is there a way to code that way?

I don't understand what your problem is.

You have the bool newBar so the stuff that you want to do only if a new bar is done when the bool is true. Stuff that you want done every tick ignores the bool.

if(newBar)
 {
  //New bar code
 }

//Every tick code
 
Keith Watford:

I don't understand what your problem is.

You have the bool newBar so the stuff that you want to do only if a new bar is done when the bool is true. Stuff that you want done every tick ignores the bool.

Sorry got a little bit confuse also. My code is consist of M5, M15, M30, H1 and H4. when my chart is M5 then the higher timeframe is M30, if my chart is M15 then my higher timeframe is H1. What I want to do is detecting newbar on the higher timeframe.

The EA works on lower timeframe as entry position and higher timeframe as a code trigger. Since my chart is M5 and I want to get the new bar on the M30 so it can perform open position based on the M5 timeframe. Since my chart is M5,  the new bar code will working on the M5 timeframe and not the M30. A workaround of this problem is to reversing the code using M30 chart and instead of higher timeframe I use the lower timeframe. But there's a problem in here, since my multi timeframe consist of 4 to 6 times factor from the current timeframe, M15 doesn't have a lower timeframe if I use the 4 to 6 times factors. it can only be 5 factors which is 3 minutes.

The only solution that I can think of is to make the new bar code working on the M30 timeframe from M5 chart or creating new timeframe script for the 3 minutes timeframe?

PS: I don't know if creating new timeframe is possible in mql4 and it if possible I don't have the ability to do so.

 
It is simple, just check for a new bar on the other time-frame using iTime. Obviously name the bool variable something different than the bool for the chart time-frame.
 
Keith Watford:
It is simple, just check for a new bar on the other time-frame using iTime. Obviously name the bool variable something different than the bool for the chart time-frame.

Recoding to use iTime and here is my code:

   static datetime preTimeHTF=0,curTimeHTF;
   bool newBarHTF=false;
   curTimeHTF= iTime(Symbol(),periodHTF,0);
   if(preTimeHTF!=curTimeHTF)
      {
       preTimeHTF=curTimeHTF;
       newBarHTF=true;
      }

on your reply you gave me a link to GetTickCount64 function, I believe it doesn't exist on mql4 though. I never encounter GetTickCount before and I don't really know what it can do, can you tell me more about GetTickCount64 function has any to do with my problem?

 
Luandre Ezra:

Recoding to use iTime and here is my code:

on your reply you gave me a link to GetTickCount64 function, I believe it doesn't exist on mql4 though. I never encounter GetTickCount before and I don't really know what it can do, can you tell me more about GetTickCount64 function has any to do with my problem?

I have never linked to GetTickCount64.

 
Keith Watford:
It is simple, just check for a new bar on the other time-frame using iTime. Obviously name the bool variable something different than the bool for the chart time-frame.

Oh really? My screen showed that this is a link. Ok then. Thank you

 
Luandre Ezra:

Oh really? My screen showed that this is a link. Ok then. Thank you

Yes, unfortunately, it is a "feature" of the forum that creates auto links. I guess that it searches for key words.

It is really annoying and often creates unrelated links such as in this case.

 
   curTime=Time[0];

The code you posted only works for the current timeframe. For multiple timeframes, you need multiple static variables and check multiple charts.

Not compiled, not tested, just typed.

enum Standard_TF{ ST_M1, ST_M5, ST_M15, ST_M30,  ST_H1,  ST_H4, ST_D1,
                  ST_W1, ST_MN1, ST_CURRENT};
                                          #define ST_COUNT (ST_MN1 + 1)
                                          #define ST_FIRST ST_M1
static const ENUM_TIMEFRAMES  period[]={     PERIOD_M1,  PERIOD_M5,  PERIOD_M15,
         PERIOD_M30, PERIOD_H1,  PERIOD_H4,  PERIOD_D1,  PERIOD_W1,  PERIOD_MN1};
ENUM_TIMEFRAMES   as_period(Standard_TF tf){
        if(tf == ST_CURRENT)            return ENUM_TIMEFRAMES(_Period);
   return period[tf];
}
Standard_TF     as_tf(ENUM_TIMEFRAMES period){
        if(period == PERIOD_CURRENT)    period = ENUM_TIMEFRAMES(_Period);
        Standard_TF tf = ST_FIRST; while(period[tf] < period) ++tf;
   return tf;
}
is_new_bar(Standard_Tf tf){ return is_new_bar( as_period(tf) ); }
is_new_bar(ENUM_TIMEFRAMES period){ 
   Standard_TF tf = as_tf(period);
   static datetime timeCur[ST_COUNT] = {0};
          datetime timePre = timeCur[tf]; timeCur[tf] = iTime(_Symbol, period, 0);
   return timeCur != timePre;
}

Not compiled, not tested, just typed.