Discussion of article ""New Bar" Event Handler" - page 6

 

Good day and good mood everyone!

I wonder if this code

bool IsNewBar()
  {
   static datetime TimeOld;
   datetime TimeNew[1];
   CopyTime(_Symbol,PERIOD_CURRENT,0,1,TimeNew);
   if(TimeNew[0]!=TimeOld)
     {
      TimeOld=TimeNew[0];
      return(true);
     }
   return(false);
  }

will it work? I found it on the MQL5 website a long time ago and don't remember who I copied it from.

Regards, Vladimir.

 
MrBrooklin #:

Good day and good cheer everyone!

I wonder if a code like this:

will it work? I found it on the MQL5 website a long time ago and don't remember who I copied it from.

Regards, Vladimir.

By the way, I also use this code and it works quite well, but it is a bit wider, it can be used for any TF!

bool isNewBar(string INST,int K, ENUM_TIMEFRAMES timeFrame)
  {
//----
   bool res=false;               // analysis result variable 
   int  i=0;                     // array cell number old_Times[] 
   datetime new_Time[1];         // time of the new bar

   switch(K)
     {
      case 0:  i= 0; break;
      case 1:  i= 1; break;
      case 2:  i= 2; break;
      case 3:  i= 3; break;
      case 4:  i= 4; break;
      case 5:  i= 5; break;
      case 6: i= 6; break;
      case 7: i= 7; break;
      case 8: i= 8; break;
      case 9: i= 9; break;
      case 10: i=10; break;
      case 11:  i=11; break;
      case 12:  i=12; break;
      case 13:  i=13; break;
     }
   // copy the time of the last bar to the cell new_Time[0] 
   int copied=CopyTime(INST,timeFrame,0,1,new_Time);
   
   if(copied>0) // all ok. data copied
      {
      if(old_Times[i]!=new_Time[0])       // if the old bar time is not equal to the new bar time
         {
         if(old_Times[i]!=0) res=true;    // if this is not the first run, then true = new bar
         old_Times[i]=new_Time[0];        // remember the time of the bar 
         }
      }      
//----
   return(res);
  }
 
switch(K)
     {
      case 0:  i= 0; break;
      case 1:  i= 1; break;
      case 2:  i= 2; break;
      case 3:  i= 3; break;
      case 4:  i= 4; break;
      case 5:  i= 5; break;
      case 6: i= 6; break;
      case 7: i= 7; break;
      case 8: i= 8; break;
      case 9: i= 9; break;
      case 10: i=10; break;
      case 11:  i=11; break;
      case 12:  i=12; break;
      case 13:  i=13; break;
     }

Wow.)

 
Dmitry Fedoseev #:

Wow.)

Who's in K? I think I've looked at everything.

array comparison

I was disappointed in the new bar, no coincidence of trades, for some reason I do not understand, there is no print, i.e. no triggering or serieslastbar or just a lag in time, I have a high ping (140), maybe that's the reason.

I have tried many variants of newbar, everywhere was a stutter, tested in 3 terminals on one computer.

I removed newbar and all terminals traded on the server 1in1 100% for several days.

 
Fast235 #:

Who's in K? I think I've looked at everything.

array comparison

I'm disappointed in the new bar, no coincidence of deals, for reasons I do not understand, there is no print, i.e. no triggering or serieslastbar or just a lag in time, I have a high ping (140), maybe that's the reason.

In K parameters.

Because with this function there is no second attempt in case of an error with an order.

 

and the trade is not in OnTick, but by OnChartEvent(spy), and in the fastest possible implementation, only through id, the rest are not transferred

7 symbols, the passage of one symbol on a large number of conditions and counts, in the American session, 0.5-0.7% ryzen 3700x

tried even a new bar through a new bar from the indicator, also diverge deals, 5 normal and then in one, then in the other stutters.

 
I threw it out altogether, anyway the algorithm looks at the last bar, and in this case there is even an advantage, if the trade is cancelled, it will not go anywhere within the limits of spread and "deviation" limitation
 
Mihail Marchukajtes #:

By the way yes I also use this code and it works quite well, only it is a bit wider, you can use it for any TF!

Thanks, Mikhail, I will add it to my library of functions.

Regards, Vladimir.

 
Armen Shahinyan #:

I have no simpler method than this one.
Tracks the opening time of the current bar and compares them at each tick.


Remove the first function from the code. And life will get better.

 

In general, 2 circumstances are interesting:

1. Everyone got banned for some reason.

2. A new bar is always different in time from the previous one. Any time, because it always goes from left to right. Take any time. If it does not coincide with the same previous one, then the bar is new.