Help a newbie - Day and Time functions

 

Hi,

I'm a newbie in here so i need some help.

I have an EA that trades when the market opens at sundays. The problem with the EA is that it also take trades during the week and i don't want that.

I want it to work like this:

extern int StartDay="sunday"

extern int StartTime="23:00"

extern int StopDay="monday"

extern int StopTime="15:00"

If day="sunday" and market is open(only one trade can be open between sunday 23:00 and monday 15:00, no new trades if our trade is closed because of TP or SL)

{

Start trade(some code i already have)

}

How can i do this in MQL?

 

check Time[0]

whith those functions in help Date & Time functions

 
// SEE IF THIS WORK FOR YOU..... (I'VE NOT TESTED IT....)



int start()
{
  bool sunday_cond = (TimeDayofWeek(TimeCurrent())>=0) && (Hour()>=23);
  bool monday_cond = (TimeDayofWeek(TimeCurrent())<=1) && (Hour()<=15); 
  if(sunday_cond  || monday_cond )
      go_trading();

}
 
abstract_mind:
  bool sunday_cond = (TimeDayofWeek(TimeCurrent())>=0) && (Hour()>=23);
  bool monday_cond = (TimeDayofWeek(TimeCurrent())<=1) && (Hour()<=15); 
  if(sunday_cond  || monday_cond )

I'm not sure this is right. The "sunday_cond" is true every day of the week between 11pm and midnight. Therefore, because the code does either sunday_cond or monday_cond, it will potentially trade after 11pm every day. In addition, "monday_cond" is met on Sundays up to 3pm. It's unlikely, but in some timezones that could also fall within trading hours. More importantly, Hour() <= 15 is true up until 3:59pm, not until 3:00pm.


But the principle is obviously sound in terms of what EagleEye needs to do: check the time, and also the day of week using DayOfWeek() or TimeDayOfWeek().

 
jjc:

I'm not sure this is right. The "sunday_cond" is true every day of the week between 11pm and midnight. Therefore, because the code does either sunday_cond or monday_cond, it will potentially trade after 11pm every day. In addition, "monday_cond" is met on Sundays up to 3pm. It's unlikely, but in some timezones that could also fall within trading hours. More importantly, Hour() <= 15 is true up until 3:59pm, not until 3:00pm.


But the principle is obviously sound in terms of what EagleEye needs to do: check the time, and also the day of week using DayOfWeek() or TimeDayOfWeek().



You are right. This should work:

  bool sunday_cond = (TimeDayofWeek(TimeCurrent())==0) && (Hour()>=23);
  bool monday_cond = (TimeDayofWeek(TimeCurrent())==1) && (Hour()<=15);
  if(sunday_cond  || monday_cond )
go_trading();
 

Thanks guys,


I will try this and let you know if it works.

 

only one trade can be open between sunday 23:00 and monday 15:00

The above code should trade from Sunday 23:00 through Monday 15:59 (Hour()<=15), not 15:00 (Hour()<15)

Also you'll need to have code for opening only one Trade during the allowed period.

 
What type should the sunday_cond and monday_cond be? int?
 

I'm not sure i understand what you mean with this part WHRoeder : Monday 15:59 (Hour()<=15), not 15:00 (Hour()<15)??

Yeah u r right i need that code as well. Hope somebody can provide me with that.

 
EagleEye:

Yeah u r right i need that code as well. Hope somebody can provide me with that.

This is a slightly unusual problem because you don't want to take a new trade if you've already opened one during the current trading window, even if the trade is now closed. Most discussions on this forum are only concerned with checking that there isn't a trade currently open (and that's a topic which comes up a lot).


In essence, you have three options in terms of checking for closed trades:


  1. When opening a new trade, store the current time in a variable. When considering placing a new trade, check that the stored time of the last trade doesn't fall within the current trading window.
  2. As above, but store the last-trade time in MT4's global variables.
  3. Scan the MT4 trade list, looking for closed trades which were opened during the current trading window.


These options are roughly in order of complexity (and also in inverse order of performance, i.e. with 3 being by far the slowest, though any speed difference between them is very unlikely to pose problems in real life).


However, the options are also in order of resilience:


  1. If the last-trade time is stored in a variable (in memory), then the value is lost if MT4 is restarted or if your EA has to be re-loaded for any other reason. In other words, if the EA is re-loaded then it will potentially trade more than once during the same trading window.
  2. If the time is stored in an MT4 global variable, then the record of it should survive re-loads of the EA. However, if trading has to be moved in a hurry to a new computer then the record of the last-trade time will be lost (unless a backup of MT4's gvariables.dat file is available).
  3. Scanning the MT4 trade list is the only option which is properly resilient in terms of disaster-recovery: you can log into the broker account from a copy of MT4 on another computer, start up the EA, and have it guaranteed not to take incorrect extra trades.


 

// Approach_1

  bool sunday_cond = (TimeDayofWeek(TimeCurrent())==0) && (Hour()>=23);
  bool monday_cond = (TimeDayofWeek(TimeCurrent())==1) && (Hour()<=15);
  if(sunday_cond  || monday_cond )
if(OrdersTotal()==0)
go_trading();



// Approach_2

  bool sunday_cond = (TimeDayofWeek(TimeCurrent())==0) && (Hour()>=23);
  bool monday_cond = (TimeDayofWeek(TimeCurrent())==1) && (Hour()<=15);
  if(sunday_cond  || monday_cond )
{
if(number_of_longs()==0) // Count the longs from both "trade context" and history for the period in qestion. There is code for counting open longs/shorts in the forum
go_trading_longs();
if(number_of_shorts()==0)
go_trading_shorts();

}
Reason: