is the following code fail-proof for the mentioned purpose?
No.
TimeCurrent() is unreliable when used in OnInit(). (OnInit can be run without a connection to a broker after you start your platform for example).
is the following code fail-proof for the mentioned purpose?
The following code would be better.
void OnTimer() { if(SESSION_TIME_LIMIT != -1) { datetime PassedTime = TimeCurrent() - StartTime; if((PassedTime - (PassedTime%3600))/3600 == SESSION_TIME_LIMIT) CLOSE_EA_LIKE_A_GENTLEMAN(); } }
But I still think that using OnTimer() isn't a good idea much in this case.
And there is another problem in the code. Imagine that you start your EA within 3 hours before ending market at Friday. The EA will never close like a gentleman ;-)
No.
TimeCurrent() is unreliable when used in OnInit(). (OnInit can be run without a connection to a broker after you start your platform for example).
Thanks
the solution is to check connection, to make sure terminal is connected to server, then set the timer in OnInit() ?
Also another side question : other event handler functions are not triggered when OnInit is running, right ?
example:
if on the first line in OnInit, I set the millisecond timer ( say to 1 millisecond ), then the timer event function is run with the OnInit ? (probably not, I think)
or when OnInit is running, new tick triggers OnTick handler ?
The following code would be better.
But I still think that using OnTimer() isn't a good idea much in this case.
And there is another problem in the code. Imagine that you start your EA within 3 hours before ending market at Friday. The EA will never close like a gentleman ;-)
but if not using OnTimer, then what ?
I can avoid using a TimeCurrent in Ontimer, but still need the timer event to calculate the passed time.
since i'm not gonna be using OnTick handler. (gonna check for new bar formation on timer event)
Thanks
the solution is to check connection, to make sure terminal is connected to server, then set the timer in OnInit() ?
Also another side question : other event handler functions are not triggered when OnInit is running, right ?
example:
if on the first line in OnInit, I set the millisecond timer ( say to 1 millisecond ), then the timer event function is run with the OnInit ? (probably not, I think)
or when OnInit is running, new tick triggers OnTick handler ?
I would have do it so :
input int Hours = 3; // remove ea after x hours datetime startea = 0; void OnInit() { startea = TimeCurrent(); ///// ..... } void OnTick() { if (startea+(3600*Hours)<TimeCurrent()) { ExpertRemove(); } /// ..... }
I would have do it so :
Alain said the problem is the unreliability of TimeCurrent() inside OnInit() .
and thanks for the much cleaner code
Thanks
the solution is to check connection, to make sure terminal is connected to server, then set the timer in OnInit() ?
Yes.
Or to not use TimeCurrent(), depends of your needs.
Also another side question : other event handler functions are not triggered when OnInit is running, right ?
example:
if on the first line in OnInit, I set the millisecond timer ( say to 1 millisecond ), then the timer event function is run with the OnInit ? (probably not, I think)
or when OnInit is running, new tick triggers OnTick handler ?
First OnTimer call will be after OnInit() end.
But I agree with Petr, unless you have very good reasons it's not a good idea to use OnTimer for that. And if you still want to do it, you have pay attention to each details when implementing it (for example, OnInit() is called on each EA settings changed).
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent and prices are valid.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
is the following code fail-proof for the mentioned purpose?