Forex Factory (https://www.forexfactory.com/forum.php)
-   Platform Tech (https://www.forexfactory.com/forumdisplay.php?f=69)
-   -   Detection of a new bar for an EA in mql4 (https://www.forexfactory.com/showthread.php?t=548770)

dawsonsg Jul 9, 2015 9:15pm | Post# 1

Detection of a new bar for an EA in mql4
 
Hi Guys,

I have been going through this forum and google and am try to find the best method to detect the start of a new bar.

There are lots of different things that people have used in their code. Some look convuluted and some just dont work.

Is there a best way of doing this?

Steev

Akh12851 Jul 10, 2015 1:12am | Post# 2

Hi,

There are 2 (at least) ways of achieving this :

 

  1. You use the previous value returned by "Bars", method for which you will need to store the value of Bars and update this value after each calculations
  2. You define a certain "volume" under which you consider the bar to be a new one (however, even in this method, you should be storing the value of Bars as it will prevent your EA from running multiple computations on the same bar)

I hope it helps a little.

Good day to all,


dawsonsg Jul 10, 2015 2:10am | Post# 3

Thanks Akh,

Is one way better than the other? Is there a code sample you could point me to so I could see how its done?

Steve

Mr_jk Jul 10, 2015 2:18am | Post# 4

hi
Use this function in your code:
bool NewBar()
{
static datetime lastbar;
datetime curbar = Time[0];
if(lastbar!=curbar)
{
lastbar=curbar;
return (true);
}
else
{
return(false);
}
}

honestknave Jul 10, 2015 4:46am | Post# 5

The best way to do it is with time, as Mr_jk has alluded to.
Unfortunately, other methods such as volume or bars has proven unreliable (missing ticks, loading history etc).

Akh12851 Jul 10, 2015 8:28am | Post# 6

hi Use this function in your code: bool NewBar() { static datetime lastbar; datetime curbar = Time[0]; if(lastbar!=curbar) { lastbar=curbar; return (true); } else { return(false); } }
Hi Mr_Jk,

Correct me if i'm wrong, but you declare the lastbar variable in your function (and as a consequence, your test should always return true).
However, since you need to be able to change/store this value, wouldn't it be better to have the declaration in your main code or maybe in your init function ?

Have all a good day,

honestknave Jul 10, 2015 8:33am | Post# 7

{quote} Hi Mr_Jk, Correct me if i'm wrong, but you declare the lastbar variable in your function (and as a consequence, your test should always return true). However, since you need to be able to change/store this value, wouldn't it be better to have the declaration in your main code or maybe in your init function ? Have all a good day,
lastbar has been declared as a static variable. This means that it keeps its value between function calls.

Akh12851 Jul 10, 2015 9:48am | Post# 8

{quote} lastbar has been declared as a static variable. This means that it keeps its value between function calls.
Thanks for correcting me, didn't pay attention to the static part (maybe also because i prefer to use other methods, producing the same result, but well... )

The most important being the result, and Mr_Jk does does have a nicely written solution to the given problem

xilisam Feb 1, 2018 7:33am | Post# 9

hi Use this function in your code: bool NewBar() { static datetime lastbar; datetime curbar = Time[0]; if(lastbar!=curbar) { lastbar=curbar; return (true); } else { return(false); } }
Thank you

Nicholishen Feb 1, 2018 12:03pm | Post# 10

This is the same as Mr JK, however, this version is compatible with MQL4/5 which makes for easier transition to 5 whenever that time comes. eg. You want to test in 5, but trade on 4.

Edit: It's also the only way you want to check for new bars with MTF on MQL4
Inserted Code
bool IsNewBar()
{
   static datetime lastbar;
   datetime curbar = (datetime)SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
   if(lastbar != curbar)
   {
      lastbar = curbar;
      return true;
   }
   return false;
}

TheMaxx Mar 7, 2018 4:44am | Post# 11

This is the same as Mr JK, however, this version is compatible with MQL4/5 which makes for easier transition to 5 whenever that time comes. eg. You want to test in 5, but trade on 4. Edit: It's also the only way you want to check for new bars with MTF on MQL4 bool IsNewBar() { static datetime lastbar; datetime curbar = (datetime)SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE); if(lastbar != curbar) { lastbar = curbar; return true; } return false; }
I've been using this code for an alert, and sometimes it only triggers 5-10 minutes after the bar has formed. What am I doing wrong please?

Nicholishen Mar 7, 2018 9:01am | Post# 12

{quote} I've been using this code for an alert, and sometimes it only triggers 5-10 minutes after the bar has formed. What am I doing wrong please?
Is the symbol in the market watch window?

TheMaxx Mar 7, 2018 10:05am | Post# 13

{quote} Is the symbol in the market watch window?
Yes, I'm running alert indicators on EURJPY and GBPJPY, and they're both showing up in the market watch window.

Nicholishen Mar 7, 2018 10:45am | Post# 14

{quote} Yes, I'm running alert indicators on EURJPY and GBPJPY, and they're both showing up in the market watch window.
How frequent is sometimes? Is it always around the same time of day?

TheMaxx Mar 7, 2018 12:49pm | Post# 15

{quote} How frequent is sometimes? Is it always around the same time of day?
Not really... My indicator should trigger 3-4 times a day per pair, and at random times. I'd say half of my alerts are within a few minutes, half are 5-20 minutes late. I am running it on a VPS which is little overloaded, but runs ok.

What are your thoughts on running this script here to force a refresh?
https://www.forexfactory.com/showthread.php?t=492302

Was thinking of setting the force refresh to every minute to lighten up the resources on my VPS.

Profess Mar 7, 2018 12:54pm | Post# 16

Hi Guys, I have been going through this forum and google and am try to find the best method to detect the start of a new bar. There are lots of different things that people have used in their code. Some look convuluted and some just dont work. Is there a best way of doing this? Steev

can you add code ?

Nicholishen Mar 7, 2018 4:40pm | Post# 17

{quote} Not really... My indicator should trigger 3-4 times a day per pair, and at random times. I'd say half of my alerts are within a few minutes, half are 5-20 minutes late. I am running it on a VPS which is little overloaded, but runs ok. What are your thoughts on running this script here to force a refresh? https://www.forexfactory.com/showthread.php?t=492302 Was thinking of setting the force refresh to every minute to lighten up the resources on my VPS.
What do you mean alerts on a VPS? Are you talking about MQL5.com push notifications to your phone? If so that's your problem. Switch to email.

TheMaxx Mar 8, 2018 8:46am | Post# 18

{quote} What do you mean alerts on a VPS? Are you talking about MQL5.com push notifications to your phone? If so that's your problem. Switch to email.
Thank you.

TheMaxx Mar 8, 2018 1:11pm | Post# 19

{quote} What do you mean alerts on a VPS? Are you talking about MQL5.com push notifications to your phone? If so that's your problem. Switch to email.
I've added an email alert, and it's triggering a few seconds after my push notification?

Voldemar227 Mar 8, 2018 2:01pm | Post# 20

The best method for determining a new bar is presented by me.

It is important to take into account that with the arrival of a new bar a lot of advisers begin to trade, broker servers are under heavy load and get DOS attack trading programs. The broker server refuses and causes requotes and failures of trade.

I recommend to use the time shift and define a new bar not at 00 : 00 but at 00: 05 seconds
Inserted Code
bool NewBar()
  {
   static datetime time=0;
   if(time==0)
     {
      time=Time[0];
      return false;
     }
   if(time!=Time[0])
     {
      time=Time[0];
      return true;
     }
   return false;
  }


© Forex Factory