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)

Jagg Mar 8, 2018 3:09pm | Post# 21

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 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; }
... and where you do that in your code (the 5s time shift)?

Voldemar227 Mar 8, 2018 3:19pm | Post# 22

The code that I showed is simple , there is no shift in 5 seconds. .

Voldemar227 Mar 8, 2018 4:05pm | Post# 23

Keep on health !
For MetaTrader 4 and MetaTrader 5 universal stable

Inserted Code
//************************************************************************************************/
//                     Author of the new bar with shift function Voldemar227                     */
//                            https://www.forexfactory.com/voldemar227                           */
//************************************************************************************************/
bool NewBar(string aSymbol="", const ENUM_TIMEFRAMES aPeriod=PERIOD_CURRENT, const int aShiftSeconds=5)
  {
   if(aSymbol=="")
      aSymbol=Symbol();
   static datetime time    = 0;
   datetime        new_bar = (datetime)SeriesInfoInteger(aSymbol,aPeriod,SERIES_LASTBAR_DATE);
   if(time==0)
     {
      time=new_bar+PeriodSeconds(aPeriod);
      return false;
     }
   if((TimeCurrent()-time)>=aShiftSeconds)
     {
      time=new_bar+PeriodSeconds(aPeriod);
      return true;
     }
   return false;
  }
//************************************************************************************************/
//                                                                                               */
//************************************************************************************************/

Nicholishen Mar 8, 2018 7:41pm | Post# 24

Keep on health ! For MetaTrader 4 and MetaTrader 5 universal stable //************************************************************************************************/ // Author of the new bar with shift function Voldemar227 */ // https://www.forexfactory.com/voldemar227 */ //************************************************************************************************/ bool NewBar(const int aShiftSeconds=0) { static datetime time = 0; datetime new_bar = (datetime)SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE); if(time==0)...
Please forgive my criticisms, but this is method is fundamentally flawed due to

  1. Broker maintenance cycles when the market goes down at a scheduled time per day (eg. market closes at 22:00 reopens at 22:05UTC)
  2. Broker has not updated the tick feed, thus a new bar won't form until the next tick of after the time period for new bar (eg. no new tick for a few minutes after close of last period.)

and therefore will falsely signal a new bar when one has not formed.


Voldemar227 Mar 9, 2018 12:22am | Post# 25

{quote} Please forgive my criticisms, but this is method is fundamentally flawed due to Broker maintenance cycles when the market goes down at a scheduled time per day (eg. market closes at 22:00 reopens at 22:05UTC) Broker has not updated the tick feed, thus a new bar won't form until the next tick of after the time period for new bar (eg. no new tick for a few minutes after close of last period.) and therefore will falsely signal a new bar when one has not formed.
The task of the function is to determine a new bar with a shift.

Suggest a better option ?

TheMaxx Mar 9, 2018 2:27am | Post# 26

{quote} Please forgive my criticisms, but this is method is fundamentally flawed due to Broker maintenance cycles when the market goes down at a scheduled time per day (eg. market closes at 22:00 reopens at 22:05UTC) Broker has not updated the tick feed, thus a new bar won't form until the next tick of after the time period for new bar (eg. no new tick for a few minutes after close of last period.) and therefore will falsely signal a new bar when one has not formed.
I need my indicator check to run every thirty minutes on the hour/half hour. Is there a way to schedule this please? I'm still getting the delay whether I use an email alert or a push notification.

mladen Mar 9, 2018 3:07am | Post# 27

You can use a timer

Nicholishen Mar 9, 2018 3:33pm | Post# 28

{quote} The task of the function is to determine a new bar with a shift. Suggest a better option ?
See post 10. Like I mentioned before... you cannot expect a new bar to form exactly at the time of the previous bar's close. Even though the time-stamp on a bar is 00:00:00, it may have not received and tick updates until 00:05:30, which means that bar formed at exactly 00:05:30 with a time-stamp of 00:00:00.

There is a big difference between checking to see if a new bar has formed and checking to see if the most recent bar is the official close price... If you are needing alerts on the most recent bar's close instead of checking for the formation of a new bar then you'd use something like this.
Inserted Code
void OnTimer()
{
//---
   if(IsRecentClosed())
      Alert("CLOSED");
}
bool IsRecentClosed()
{
   static datetime last_closed = 0;
  
   if(last_closed == 0)
   {
      datetime time[];
      if(CopyTime(_Symbol,PERIOD_CURRENT,0,1,time) < 1)
         return false;
      printf("copied time of %s to array",TimeToString(time[0]));
      last_closed = time[0] - 1;
   }
      
   if(TimeCurrent() >= last_closed + PeriodSeconds(PERIOD_CURRENT))
   {
      last_closed+= PeriodSeconds(PERIOD_CURRENT);
      return true;
   }
   return false;
}

FernandoBore Mar 18, 2018 5:40am | Post# 29

If the conditions to take the trade are simple enough, you can use this code (Found it on a forum that I don't remember the name):

Inserted Code
datetime LastActionTime;            //Declare this on the same section you declare inputs
 
void OnTick()
{
if(LastActionTime!=Time[0])       //At the begging the value of that variable is set to nothing, then the condition is true.
  {
    //So it would run your code, I recommend you to set a function that contains all the code for trading to make it easier to structure.
      
       LastActionTime = Time[0]; // Then, you set the variable value to the open time of the current candle, then when you test the if condition it will be the same and the code will not run
      }
                                               //When a new bar comes in, the value stored on the variable would correspond to the open time for the bar 1, and the condition would be true and then the code will run and set the flag to prevent to run again.

saunders Jul 26, 2018 4:23am | Post# 30

I have around 60 EA s running on 60 instances simultaneously. Mostly on M1 and M5 timeframe.. for about a couple of months the EA's have been behaving weirdly. That is they were not closing the positions at the time they should have closed.. I figured out that the EA's really sometimes run sometimes do not run.. Because when you restart the terminal they work properly.. So the only explanation left is that they do not detect the new bar (all my EA's run on new open bar).. This is the code I am using:
*********************************************
// Execute on bar open
if(CheckOncePerBar == true)
{
int BarShift = 1;
if(CurrentTimeStamp != Time[0])
{
CurrentTimeStamp = Time[0];
bool NewBar = true;
}
else NewBar = false;
}
else
{
NewBar = true;
BarShift = 0;
}

// Begin trade block
if(NewBar == true)
{
}
***************************************************

I am going to check the function of Voldemar227 (thank you BTW for the code) and share my experience..

Might there be another fail-proof code which would avoid this problem.

Kind regards..


© Forex Factory