How to make an EA trade during a specified time range only?

Is the following code fail-proof for the mentioned purpose?

datetime RightNow;
int SESSION_TIME_LIMIT = 3; // in hours

OnInit()
{
datetime StartTime = TimeCurrent();
EventSetTimer(15); // timer tick set to 15 seconds

void OnTimer()
{
        RightNow = TimeCurrent();
        if(SESSION_TIME_LIMIT != -1)
        {
                int PassedTime = (int)RightNow - StartTime;
                if((PassedTime - (PassedTime%3600))/3600 == SESSION_TIME_LIMIT) CLOSE_EA_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).

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 :wink:

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 ?

Good point about Friday. actually was gonna auto-disable EA for the first 12 and last 12 hours of trading week. kinda freaky moments.
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)

I would have to do 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(); }
///
.....
}

@bspeight5 said the problem is the unreliability of TimeCurrent() inside OnInit() .

and thanks for the much cleaner code

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 ?

No.

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).

Don’t try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. New tick is received, OnCalculate / OnTick is called. Now TickValue , TimeCurrent and prices are valid.