Trading at specified time with OnTimer problem - page 2

 
AngeloSK:

this EA works perfectly (my computer time is precisely the same as the servers time as i work for broker) 

Just to clarify - your code isn't using your computer's time if you are using the functions Hour(), Minute() or Seconds(). These all work from server time.

If you want to use your computer's time you need to use TimeHour(), TimeMinute(), TimeSeconds() in conjunction with TimeLocal() or TimeGMT().

I'm still not sure what you need. If your operations are all based on time, don't use OnTick(). Once the EA has set the orders at 08:59:59, just repurpose OnTimer for your new tasks by putting EventSetMillisecondTimer() inside your OnTimer(). 

To reiterate an earlier point, please be very careful using Hour, Minute and Second in OnInit. If you restart your terminal and the EA is already applied on a chart that code can run before you have connected to the server.

 
Fernando Carreiro:

The Timer cannot be set for a particular time of the day. It can only be set for an interval of time, say every minute,  every second, every 100ms, etc.

With the OnTimer event handler, you would then have to "count" the time intervals to work out what time it is, or use the Local time to calculate what time it is, or approximate it based on "count" and the Trade server time.

PS! The Functions are EventSetTimer and EventSetMillisecondTimer. Don't just invent function names. Look them up in the documentation.

Okay i got it and is there something what i can use to set orders at particular time of the day regardless of tick ? (as timer does) 

 

ps: im sorry i was on mobile and havent had time to look at precise names.  

 
honest_knave:

Just to clarify - your code isn't using your computer's time if you are using the functions Hour(), Minute() or Seconds(). These all work from server time.

If you want to use your computer's time you need to use TimeHour(), TimeMinute(), TimeSeconds() in conjunction with TimeLocal() or TimeGMT().

I'm still not sure what you need. If your operations are all based on time, don't use OnTick(). Once the EA has set the orders at 08:59:59, just repurpose OnTimer for your new tasks by putting EventSetMillisecondTimer() inside your OnTimer(). 

To reiterate an earlier point, please be very careful using Hour, Minute and Second in OnInit. If you restart your terminal and the EA is already applied on a chart that code can run before you have connected to the server.

But isnt there limitation of 1 timer per EA ? (meaning also 1 EventSetTimer)
 
AngeloSK:

Okay i got it and is there something what i can use to set orders at particular time of the day regardless of tick ? (as timer does) 

Use OnTimer().

int OnInit()
  {
  
   EventSetMillisecondTimer(250); // check 4 times a second (or whatever)
   return(INIT_SUCCEEDED);
  }
void OnTimer()
  {
   int hr =Hour(),
       min=Minute(),
       sec=Seconds();
   if(hr==8 && min==59 && sec==59)
     {
      // do something
     }
   else if(hr==9 && min==0 && sec==0)
     {
      // do something else
     }
  }

The OnTimer() check should only take a handful of μs, plus whatever your actual code takes. You are still at the mercy of ensuring a tick comes through at the right time (to update the Hour / Minute / Seconds) and how long it takes to place and process the order.

Please bear in mind this is all a fudge... it really isn't advisable to rely on MT4 to place orders with <1 second accuracy. 

 

I got a new idea which could solve my problem, correct me please if im wrong.

 

bool r = false;

void OnTick()
{
 int h = TimeHour();
 int m = TimeMinute();

 if (( h>= 8 && h<=9) && (m >= 59 && m <=00) && r = false)
 {
  EventSetMillisecondTimer(86400000) //day in milliseconds

  r = true;

 }

}


void OnTimer()
{
// execute orders


}


Will it make OnTimer everyday ? 

 
By the way thank you all who try to help me understand the problem and fix it. You are all very helpful. 
 
honest_knave: Use OnTimer(). The OnTimer() code should only take a handful of μs.

Unfortunately, I cannot agree with your code because the functions Hour(), Minute() and Seconds() return the last known server time and if no ticks have come in, then that time will be "stuck" in the past, even thou Time events have continued.

The OP will have to use local time as well in order to synchronize correctly (or estimate the time, by adding an "interval count" to the last know server time).

@AngeloSK: I still believe you are going about this with the wrong mindset. Why not just place pending orders leisurely well before the time, and as the time approaches adjust the opening prices with the OrderModify() function. That way even if there are delays and not exact synchronization, you will still have orders in place within the expected opening price, even if slightly off. It is not like you are trading for a single point change and profits are you?

 
Fernando Carreiro:

Unfortunately, I cannot agree with your code because the functions Hour(), Minute() and Seconds() return the last known server time and if no ticks have come in, then that time will be "stuck" in the past, even thou Time events have continued.

I did say that underneath:

honest_knave:

Use OnTimer().

int OnInit()
  {
  
   EventSetMillisecondTimer(250); // check 4 times a second (or whatever)
   return(INIT_SUCCEEDED);
  }
void OnTimer()
  {
   int hr =Hour(),
       min=Minute(),
       sec=Seconds();
   if(hr==8 && min==59 && sec==59)
     {
      // do something
     }
   else if(hr==9 && min==0 && sec==0)
     {
      // do something else
     }
  }

The OnTimer() check should only take a handful of μs, plus whatever your actual code takes. You are still at the mercy of ensuring a tick comes through at the right time (to update the Hour / Minute / Seconds) and how long it takes to place and process the order.

Please bear in mind this is all a fudge... it really isn't advisable to rely on MT4 to place orders with <1 second accuracy. 

 
honest_knave: I did say that underneath:
Sorry, did not see that!
 
Fernando Carreiro:
Sorry, did not see that!
No problem, I did edit the reply so you possibly caught it pre-edit.
Reason: