• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 1:12am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 1:12am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

3 Bar Fractal with mid-bar and bar on each side 11 replies

MQL4 Script - open on new bar question 22 replies

MQL4 Language Most Recent Version is it updated beyond the tutorial on the mql4 websi 6 replies

Pin bar, pin bar, pin bar 0 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
Tags: Detection of a new bar for an EA in mql4
Cancel

Detection of a new bar for an EA in mql4

  • Last Post
  •  
  • Page 1 2
  • Page 1 2
  •  
  • Post #1
  • Quote
  • First Post: Jul 9, 2015 9:15pm Jul 9, 2015 9:15pm
  •  dawsonsg
  • | Joined Oct 2008 | Status: Member | 127 Posts
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
  • Post #2
  • Quote
  • Jul 10, 2015 1:12am Jul 10, 2015 1:12am
  •  Akh12851
  • | Joined Jan 2013 | Status: Member | 83 Posts
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,

 
 
  • Post #3
  • Quote
  • Jul 10, 2015 2:10am Jul 10, 2015 2:10am
  •  dawsonsg
  • | Joined Oct 2008 | Status: Member | 127 Posts
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
 
 
  • Post #4
  • Quote
  • Jul 10, 2015 2:18am Jul 10, 2015 2:18am
  •  Mr_jk
  • | Joined Apr 2012 | Status: Member | 19 Posts
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);
}
}
 
 
  • Post #5
  • Quote
  • Jul 10, 2015 4:46am Jul 10, 2015 4:46am
  •  honestknave
  • Joined Nov 2014 | Status: Member | 1,300 Posts
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).
 
 
  • Post #6
  • Quote
  • Jul 10, 2015 8:28am Jul 10, 2015 8:28am
  •  Akh12851
  • | Joined Jan 2013 | Status: Member | 83 Posts
Quoting Mr_jk
Disliked
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); } }
Ignored
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,
 
 
  • Post #7
  • Quote
  • Jul 10, 2015 8:33am Jul 10, 2015 8:33am
  •  honestknave
  • Joined Nov 2014 | Status: Member | 1,300 Posts
Quoting Akh12851
Disliked
{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,
Ignored
lastbar has been declared as a static variable. This means that it keeps its value between function calls.
 
 
  • Post #8
  • Quote
  • Jul 10, 2015 9:48am Jul 10, 2015 9:48am
  •  Akh12851
  • | Joined Jan 2013 | Status: Member | 83 Posts
Quoting honestknave
Disliked
{quote} lastbar has been declared as a static variable. This means that it keeps its value between function calls.
Ignored
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
 
 
  • Post #9
  • Quote
  • Feb 1, 2018 7:33am Feb 1, 2018 7:33am
  •  xilisam
  • | Joined Mar 2017 | Status: Currently Trading | 33 Posts
Quoting Mr_jk
Disliked
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); } }
Ignored
Thank you
Be Water My Friend
 
 
  • Post #10
  • Quote
  • Feb 1, 2018 12:03pm Feb 1, 2018 12:03pm
  •  Nicholishen
  • Joined Jul 2005 | Status: zzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzz | 1,289 Posts
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;
}
 
2
  • Post #11
  • Quote
  • Mar 7, 2018 4:44am Mar 7, 2018 4:44am
  •  TheMaxx
  • Joined Jul 2009 | Status: Trade. Review. Improve | 1,089 Posts
Quoting Nicholishen
Disliked
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; }
Ignored
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?
 
 
  • Post #12
  • Quote
  • Mar 7, 2018 9:01am Mar 7, 2018 9:01am
  •  Nicholishen
  • Joined Jul 2005 | Status: zzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzz | 1,289 Posts
Quoting TheMaxx
Disliked
{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?
Ignored
Is the symbol in the market watch window?
 
 
  • Post #13
  • Quote
  • Mar 7, 2018 10:05am Mar 7, 2018 10:05am
  •  TheMaxx
  • Joined Jul 2009 | Status: Trade. Review. Improve | 1,089 Posts
Quoting Nicholishen
Disliked
{quote} Is the symbol in the market watch window?
Ignored
Yes, I'm running alert indicators on EURJPY and GBPJPY, and they're both showing up in the market watch window.
 
 
  • Post #14
  • Quote
  • Mar 7, 2018 10:45am Mar 7, 2018 10:45am
  •  Nicholishen
  • Joined Jul 2005 | Status: zzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzz | 1,289 Posts
Quoting TheMaxx
Disliked
{quote} Yes, I'm running alert indicators on EURJPY and GBPJPY, and they're both showing up in the market watch window.
Ignored
How frequent is sometimes? Is it always around the same time of day?
 
 
  • Post #15
  • Quote
  • Mar 7, 2018 12:49pm Mar 7, 2018 12:49pm
  •  TheMaxx
  • Joined Jul 2009 | Status: Trade. Review. Improve | 1,089 Posts
Quoting Nicholishen
Disliked
{quote} How frequent is sometimes? Is it always around the same time of day?
Ignored
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.
 
 
  • Post #16
  • Quote
  • Mar 7, 2018 12:54pm Mar 7, 2018 12:54pm
  •  Profess
  • | Membership Revoked | Joined Mar 2018 | 224 Posts
Quoting dawsonsg
Disliked
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
Ignored

can you add code ?
[email protected] om
 
 
  • Post #17
  • Quote
  • Mar 7, 2018 4:40pm Mar 7, 2018 4:40pm
  •  Nicholishen
  • Joined Jul 2005 | Status: zzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzz | 1,289 Posts
Quoting TheMaxx
Disliked
{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.
Ignored
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.
 
1
  • Post #18
  • Quote
  • Mar 8, 2018 8:46am Mar 8, 2018 8:46am
  •  TheMaxx
  • Joined Jul 2009 | Status: Trade. Review. Improve | 1,089 Posts
Quoting Nicholishen
Disliked
{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.
Ignored
Thank you.
 
 
  • Post #19
  • Quote
  • Mar 8, 2018 1:11pm Mar 8, 2018 1:11pm
  •  TheMaxx
  • Joined Jul 2009 | Status: Trade. Review. Improve | 1,089 Posts
Quoting Nicholishen
Disliked
{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.
Ignored
I've added an email alert, and it's triggering a few seconds after my push notification?
 
 
  • Post #20
  • Quote
  • Mar 8, 2018 2:01pm Mar 8, 2018 2:01pm
  •  Voldemar227
  • | Commercial Member | Joined Mar 2016 | 152 Posts
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;
  }
Trader and programmer! ! !
 
 
  • Platform Tech
  • /
  • Detection of a new bar for an EA in mql4
  • Reply to Thread
    • Page 1 2
    • Page 1 2
0 traders viewing now
  • More
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023