EA open at time error

 

Hello


I coded my EA to open orders at time , the issue is the EA doesn't open trades if i leave the EA to work for next days even if i use VPS .

extern string               OpenTime="01:00";//Time to place trades (HH:MM)

if(TimeCurrent()>=StrToTime(OpenTime)&&TimeCurrent()<StrToTime(OpenTime)+Period()){
 double sell= OrderSend(NULL,OP_SELL,LotSize,Bid,50,SL,TP,NULL,MagicNumber,0,clrRed);
      
}
 
  1. Period is in minutes (only on MT4). OpenTime is in seconds. Use PeriodSeconds().
  2. If you fix № 1, you will be trying to open an order per tick.
  3. Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum 2020.07.25
 
William Roeder:
  1. Period is in minutes (only on MT4). OpenTime is in seconds. Use PeriodSeconds().
  2. If you fix № 1, you will be trying to open an order per tick.
  3. Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum 2020.07.25

I tested it but it doesn't work .

the EA doesn't open at time .

    TimeCurrent()>=StrToTime(OpenTime)&&TimeCurrent()<StrToTime(OpenTime)+PeriodSeconds()
 
Oussama Mansour:

I tested it but it doesn't work .

the EA doesn't open at time .

Hello. Please check the EXPERTS journal - is there any errors?

 
Mikhail Zhitnev:

Hello. Please check the EXPERTS journal - is there any errors?

I checked it , there isn't any error .

if I activate the EA during the day  it works without any issue .

but if i leave it to work in night it didn't open at time .

even if I use it in VPS

 
extern string               OpenTime="01:00";//Time to place trades (HH:MM)

datetime time = StrToTime(OpenTime);
datetime current = TimeCurrent();
datetime next = time + PeriodSeconds();

if(TimeHour(current)>=TimeHour(time) && TimeMinute(current)>=TimeMinute(time) && TimeHour(current)<TimeHour(next) && TimeMinute(current)<TimeMinute(next)){
 if(n_open_sells<=0) double sell= OrderSend(NULL,OP_SELL,LotSize,Bid,50,SL,TP,NULL,MagicNumber,0,clrRed);
}
 
Hector Pacheco:
if(TimeHour(current)>=TimeHour(time) && TimeMinute(current)>=TimeMinute(time)

Bogus. For example. If time is 1:15 then the compare is true for [1:15 to 2:00) and [2:15 to 3:00) etc. it fails from [2:00 to 2:15)

 
Oussama Mansour:

I tested it but it doesn't work .

the EA doesn't open at time .

TimeCurrent()>=StrToTime(OpenTime)&&TimeCurrent()<StrToTime(OpenTime)+PeriodSeconds()

Never use StrToTime() or StringToTime() unless the string also includes the date.

If there is no actual date in the string, the date will be taken from the computer's local time, not platform time.

So if the string is "01:00" and the platform date is 7th January, you will expect the time returned to be 1:00 on the 7th January.

However, if your computer local time is behind the platform time, eg 23:00 on 6th January, the time returned will be  1:00 on the 6th January.

This will obviously affect your calculations.

 
Keith Watford:

Never use StrToTime() or StringToTime() unless the string also includes the date.

If there is no actual date in the string, the date will be taken from the computer's local time, not platform time.

So if the string is "01:00" and the platform date is 7th January, you will expect the time returned to be 1:00 on the 7th January.

However, if your computer local time is behind the platform time, eg 23:00 on 6th January, the time returned will be  1:00 on the 6th January.

This will obviously affect your calculations.

I have already used PeriodSecond() but without any result .

so if the time of computer is totally different of broker time , what is the best solution ?

 
Keith Watford:

Never use StrToTime() or StringToTime() unless the string also includes the date.

If there is no actual date in the string, the date will be taken from the computer's local time, not platform time.

So if the string is "01:00" and the platform date is 7th January, you will expect the time returned to be 1:00 on the 7th January.

However, if your computer local time is behind the platform time, eg 23:00 on 6th January, the time returned will be  1:00 on the 6th January.

This will obviously affect your calculations.

can I change my computer time to broker time ? it will work in this wat ?
 
Keith Watford:

Never use StrToTime() or StringToTime() unless the string also includes the date.

Oussama Mansour:

I have already used PeriodSecond() but without any result .

What does that have to do with my post?

Oussama Mansour:

so if the time of computer is totally different of broker time , what is the best solution ?

Don't use StringToTime().

Oussama Mansour:
can I change my computer time to broker time ? it will work in this wat ?

That would be a rather silly way to make it work.