Download MetaTrader 5

Trading at specified time with OnTimer problem

To add comments, please log in or register
AngeloSK
48
AngeloSK  

Hello guys and gals, 

 i have this very inconvenient problem. I want to place pendings orders exactly at 8:59:59 and i dont know how to do it with timer to repeat it every day. In code below every day will be x minutes away from 8:59:59 where x is 9:00 minus time you put EA in chart  

int OnInit()
{
//---


if (Symbol() == "DE.30+" )Print ("Symbol is OK");
else Alert("Wrong symbol, this is not DE.30+");

if (Period() == PERIOD_H1 ) Print("Timeframe is OK");
else Alert("Wrong timeframe, this is not H1");

int h = Hour();
int m = Minute();
int s = Seconds();
  

int timerh = 09 - h;
int timerm = 00 - m;
int timers = 00 - s;
int now = (timerh * 60 * 60 * 1000) + (timerm * 60 * 1000) + (timers * 1000) - 1500 ;

if(EventSetMillisecondTimer(86400000 + now) == false )
{
Print ("Timerh: ", timerh);
  Print ("Timerm: ", timerm);
   Print ("Timers: ", timers);
    Print ("now: ", now);
Print ("Timer error: ", GetLastError());
}  

   return(INIT_SUCCEEDED);
}

Is there any possible way to trade at 9:00 every day with timer except putting EA with 86400000 exactly at 9:00 into the chart ? (you can miss for a second and EA will not work properly)


Thanks in advance.


PS: In code i got minus 1500 milsec which will trade at 8:59:58.500 i know that. 
 

honest_knave
Moderator
2318
honest_knave  

Unfortunately attempting to place orders with 1 second accuracy is never going to work out very well in MT4.

For starters, which time will you use... your computer's time or the server's time?

If the former, what if your computer clock isn't synced with the brokers?

If the latter, what if a tick doesn't come in to update TimeCurrent() when you need it to?

Also, how long does it take your code to process? How long for the order to be received and processed by your broker? These won't be constant or predictable.

If you are going to attempt to code this, I wouldn't run one long timer interval like that. Run a short timer and keep checking. Your code in OnInit might run before Hour/Minute/Seconds have updated.

But, I think you are going to be disappointed. 

Fernando Carreiro
2958
Fernando Carreiro  
AngeloSK: Hello guys and gals, 

 i have this very inconvenient problem. I want to place pendings orders exactly at 8:59:59 and i dont know how to do it with timer to repeat it every day. In code below every day will be x minutes away from 8:59:59 where x is 9:00 minus time you put EA in chart  

Is there any possible way to trade at 9:00 every day with timer except putting EA with 86400000 exactly at 9:00 into the chart ? (you can miss for a second and EA will not work properly)

Thanks in advance.

PS: In code i got minus 1500 milsec which will trade at 8:59:58.500 i know that. 
 

I agree with honest_knave, plus I have the ask - why such precise requirements?

It is not a Market Order, it is a Pending order! It is only going to be triggered later, maybe even never. There is no need to be so precise. As long as it is done before the projected trigger time, you can set it up with reasonable leisure.

If it has to do with its expiry time, then you can easily adjust that after the fact!

AngeloSK
48
AngeloSK  
honest_knave:

Unfortunately attempting to place orders with 1 second accuracy is never going to work out very well in MT4.

For starters, which time will you use... your computer's time or the server's time?

If the former, what if your computer clock isn't synced with the brokers?

If the latter, what if a tick doesn't come in to update TimeCurrent() when you need it to?

Also, how long does it take your code to process? How long for the order to be received and processed by your broker? These won't be constant or predictable.

If you are going to attempt to code this, I wouldn't run one long timer interval like that. Run a short timer and keep checking. Your code in OnInit might run before Hour/Minute/Seconds have updated.

But, I think you are going to be disappointed. 

Well i can place these orders at precise time when i have only now variable without the day in milsec and i will place EA between 8:00-8:58 it works fine as i expected. Im using broker time and it is not problem to sync computer clock with server times as both run on server time. And thats it why i dont want to go around with on tick because i trade Dax Breakout and at 9:00am dax is very volatile and often ignored new tick while it was processing old tick thats why i need timer. time processing is +/- 400ms i have it tested on real acc where i manually start ea every morning.
AngeloSK
48
AngeloSK  
Fernando Carreiro:

I agree with honest_knave, plus I have the ask - why such precise requirements?

It is not a Market Order, it is a Pending order! It is only going to be triggered later, maybe even never. There is no need to be so precise. As long as it is done before the projected trigger time, you can set it up with reasonable leisure.

If it has to do with its expiry time, then you can easily adjust that after the fact!

The fact that i trade dax breakout means tgat sometimes when it is very volatile and ea is processing old tick and new will come ea will ignore it and i get big slippage. Also at 9:00 these pending dont have to be necessarily pendings but also market order thats why i need this precision. Regardless of a new tick or not i need to place pending order precisely at 8:59:59 
AngeloSK
48
AngeloSK  

Is there possibility to SetEventTimer as now eg. 86400000+now and second and furthermore only 86400000 so i would only null now value when timer first time triggers ? 

honest_knave
Moderator
2318
honest_knave  
AngeloSK:
Well i can place these orders at precise time when i have only now variable without the day in milsec

Sadly, you can't. Not always.. but I guess it depends on your risk tolerance for the times it doesn't work as you want.

You are basing your timer controls on Hour() / Minute() / Seconds(). All these are "last known server time". Not the actual server time. So the last known server time may have been milliseconds ago, or even seconds ago.

Also, your orders are not instantaneous. They need to be transmitted from your EA to the broker, and the broker needs to process them. This takes time which you can't control or reliably estimate to your required level of precision.

Most of these points aren't issues unless you're wanting to-the-second accuracy, which you do.

You must have nerves of steel to have a system running on MT4 that is reliant on to-the-second accuracy!

honest_knave
Moderator
2318
honest_knave  
AngeloSK:

Is there possibility to SetEventTimer as now eg. 86400000+now and second and furthermore only 86400000 so i would only null now value when timer first time triggers ? 

I'm not sure I understand the question. You can use EventSetTimer() and EventSetMillisecondTimer() at any place in your code, not just OnInit. It will then override the previous setting. But you can only have 1 timer running. Does that help? 

AngeloSK
48
AngeloSK  
honest_knave:

Sadly, you can't. Not always.. but I guess it depends on your risk tolerance for the times it doesn't work as you want.

You are basing your timer controls on Hour() / Minute() / Seconds(). All these are "last known server time". Not the actual server time. So the last known server time may have been milliseconds ago, or even seconds ago.

Also, your orders are not instantaneous. They need to be transmitted from your EA to the broker, and the broker needs to process them. This takes time which you can't control or reliably estimate to your required level of precision.

Most of these points aren't issues unless you're wanting to-the-second accuracy, which you do.

You must have nerves of steel to have a system running on MT4 that is reliant on to-the-second accuracy!

Maybe i wrote it badly but i have another EA which have only SetEventMiliTimer(now) and this EA works perfectly (my computer time is precisely the same as the servers time as i work for broker) This EA always set orders around 8:59:59.400  which is great. 

 I just want to solve problem where when i have OnTick at 9:00 to set pendings or if Current price is equals Max/Min of range to set market orders but in this volatile timeframe sometimes it ignored new tick and EA was calculating with  pendings while it should set market order and it displayed me an error. Thats why i started to using timer but i just cant set the timer to repeat itself everyday between 8:59:59 - 9:00:00 (one second difference is not a big deal i just need it to open trade at Open Price of 9:00 hourly candlestick) i know how to make it once (through SetEventMiliTimer(now)) but i have to manually add EA to chart everyday which seems inconvenient. 

AngeloSK
48
AngeloSK  
honest_knave:

I'm not sure I understand the question. You can use EventSetTimer() and EventSetMillisecondTimer() at any place in your code, not just OnInit. It will then override the previous setting. But you can only have 1 timer running. Does that help? 

Okey so will something like this work ? 

 

int now = 0; 

 

 
void OnInit ()
{
 now = 1000;

EventSetMillisecondTimer(86400000 + now);
}

void OnTimer()
{
// execute the code and null now value

now = 0;

}

 So i attached EA at 8:30 so Ea will work every 24 hours and 30 minutes but after first Timer play it will override itself to work only every 24 hours ? (now = 30 minutes in milliseconds)

Fernando Carreiro
2958
Fernando Carreiro  
AngeloSK:

Maybe i wrote it badly but i have another EA which have only SetEventMiliTimer(now) and this EA works perfectly (my computer time is precisely the same as the servers time as i work for broker) This EA always set orders around 8:59:59.400  which is great. 

 I just want to solve problem where when i have OnTick at 9:00 to set pendings or if Current price is equals Max/Min of range to set market orders but in this volatile timeframe sometimes it ignored new tick and EA was calculating with  pendings while it should set market order and it displayed me an error. Thats why i started to using timer but i just cant set the timer to repeat itself everyday between 8:59:59 - 9:00:00 (one second difference is not a big deal i just need it to open trade at Open Price of 9:00 hourly candlestick) i know how to make it once (through SetEventMiliTimer(now)) but i have to manually add EA to chart everyday which seems inconvenient. 

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.

Working with Events - MQL4 Reference
Working with Events - MQL4 Reference
  • docs.mql4.com
Working with Events - MQL4 Reference
123
To add comments, please log in or register