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

Luandre Ezra
263
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?

//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...
Keith Watford
Moderator
28996
Keith Watford  
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
Luandre Ezra
263
Luandre Ezra  
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.

Keith Watford
Moderator
28996
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.
Luandre Ezra
263
Luandre Ezra  
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?

Keith Watford
Moderator
28996
Keith Watford  
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.

Luandre Ezra
263
Luandre Ezra  
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

Keith Watford
Moderator
28996
Keith Watford  
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.