Download MetaTrader 5
To add comments, please log in or register
Deposit to your account in any convenient way. There are 4 of them here!
mar
1143
mar 2015.06.22 13:31 

Hey coders,

I program an expert advisor and there is one part which should only be executed one time a day after 11:00 am.

I do it this way but there is something missing:

if (TimeHour(iTime(NULL, PERIOD_M1, 0))==11 && TimeHour(iTime(NULL, PERIOD_M1, 1))==10) {

.....
.....
.....

}

 But the problem here is that this code is executed with every tick between 11:00:00 and 11:01:00.

How can I modify it that it is only executed once a day after 11:00 am?

PomeGranate
554
PomeGranate 2015.06.22 13:42  

A solution might be to embrace your code in alike if-condition:

if (Volume<=1){your code} //executed only on first ticks of a bar
mar
1143
mar 2015.06.22 14:07  
Very good idea! There will never be a tick without a volume. Thanks!!
honest_knave
969
honest_knave 2015.06.22 16:04  

The only reliable way to detect a new bar is using time. All other methods have been proven unreliable. If you have a search through the site you'll find the relevant threads.

Use flags to make sure that it only fires once.

Example (uncompiled, untested):

static int lastday;
int        today  =TimeDay(TimeCurrent());
if(today!=lastday && TimeHour(TimeCurrent())>=11) // changed from ==
  {
   // run your code
   lastday=today;
  }
whroeder1
13641
whroeder1 2015.06.22 17:14  
PomeGranate:A solution might be to embrace your code in alike if-condition:
if (Volume<=1){your code} //executed only on first ticks of a bar
Volume[0] is unreliable, you can miss ticks. Bars is unreliable (max bars on chart) Always use time
New bar or first tick.
New bar only
start(){
  static datetime time0; 
  bool isNewBar=time0 != Time[0]; time0 = Time[0];
  :
  if(isNewBar){ ...
datetime time0;
OnInit(){ time0 = Time[0];
start(){
  bool isNewBar=time0 != Time[0]; time0 = Time[0];
  :
  if(isNewBar){ ...
honest_knave:

The only reliable way to detect a new bar is using time. All other methods have been proven unreliable. If you have a search through the site you'll find the relevant threads.

Use flags to make sure that it only fires once.

static int lastday;
int        today  =TimeDay(TimeCurrent());
if(today!=lastday && TimeHour(TimeCurrent())==11)
  {
   // run your code
   lastday=today;
  }
This fails to reset on chart change (deinit/init). Testing for equals 11 means it fails to trigger on Sunday (Market opens 2100/2200 UTC) should your broker's timezone be UTC. Better
#define HR2400 PERIOD_D1 * 60           // 86400 = 24 * 3600
int      TimeOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
                                          return( when % HR2400 );            }
datetime DateOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
                                          return( when - TimeOfDay(when) );   }
datetime Tomorrow( datetime when=0){      if(when == 0)  when = TimeCurrent();
                                          return(DateOfDay(when) + HR2400);   }
//datetime Yesterday(datetime when=0){      if(when == 0)  when = TimeCurrent();
//   int iD1 = iBarShift(NULL, PERIOD_D1, DateOfDay(when) - 1);
//                                       return( iTime(NULL, PERIOD_D1, iD1) ); }

datetime nextTrade;
#define HR1100 39600
OnInit(){
 nextTrade = DateOfDay() + HR1100;
}
:
if(TimeCurrent() >= nextTrade){
  nextTrade = Tomorrow() + HR1100;
  :
}
honest_knave
969
honest_knave 2015.06.22 17:24  
WHRoeder:
Volume[0] is unreliable, you can miss ticks. Bars is unreliable (max bars on chart) Always use time

Is there an echo around here?

 

WHRoeder:
This fails to reset on chart change (deinit/init).

Which may be how the OP wants it. Otherwise a change in chart TF after 11am would result in another order being fired off.

If that's not how the OP wants it, instead of using a static variable simply use a variable with global scope and reset it in OnInit(). 

 

WHRoeder:
Testing for equals 11 means it fails to trigger on Sunday (Market opens 2100/2200 UTC) should your broker's timezone be UTC.

 Valid point. The correction to the code is >=11

mar
1143
mar 2015.06.23 08:16  

Hey guys, thank you all. I appreciate your help!

/
To add comments, please log in or register