Download MetaTrader 5

Trade Time Management query - best userfriendly way to do this?

To add comments, please log in or register
Thehallster
106
Thehallster  

Hi All,

 

had to put in a time restriction function to this EA im building and ive done it but looks and feels quite crudely done.

Ive added some Start Hour and Start Minute/ End Hour/Min etc input variables then used 4 crude logics.

if ( ( _CurrentHour >= EndHour ))

then return false and end code.

 

But this requires 4 input variables and a really *** looking mess of codes and queries as you have to use 4 logics to get a start before Minute and hours and after hours.

 

Has anyone done this before in a neat and tidy manner?

 

I'm thinking could you use something like an input variable of a standard time format "08:30"

then use something like

extern string starttrading = "08:30";

datetime StringToTime(starttrading);

If (CurrentTime() < datetime )
Return (False)


If you get my drift?? at the moment it works with the individual calls but i think it could be a lot cleaner.

 

be grateful for any guidance. 

honest_knave
Moderator
2296
honest_knave  

@Thehallster

There are quite a few different ways to do this.

How you approach it will depend on a number of factors:

  • Do you want your EA to continue doing other things "out of hours" (like manage already opened trades, display certain information etc.) or stop all operations?
  • How easy do you want to make it to enter the settings (usually at the cost of flexibility)?

For the latter, you really have 3 options:

1. Have 4 settings (like you currently have - hours and minutes for start and finish). This gives a lot of flexibility but isn't particularly elegant.

2. Have a dropdown list. Easy for the user, but at the cost of flexibility. What if you want to start at 09:14? You must reach a compromise between the size of your list and the options the user may want.

 

3. Use a string and convert it to a datetime as per your last suggestion. The problem is that you may need some extra code to deal with users entering data in a non-standard way.

Once you have your start and end settings, you need to work with them in your code.

One method is to end up with the number of seconds since midnight. With the enumeration (dropdown list) this is straightforward - it is the value of the enumeration option. With Option 3 you need a bit of manipulation e.g. 

#define DAY 86400

input string str_start_time; // Start Time
datetime start_time;

int OnInit()
  {
   start_time = (datetime)str_start_time; // convert the string to datetime
   start_time = start_time % DAY;         // convert to seconds since midnight
   return(INIT_SUCCEEDED);
  }


When you want to check if you are inside the trading hours:

datetime now = TimeCurrent();
now = now % DAY;
if(now >= start_time && now < end_time)
  {
   // EA can do stuff
  }

It will get more complicated when you want to trade through midnight e.g. your start time is 22:00 and your end time is 06:00

Please bear in mind there are many, many different ways of doing this. For example playing around with time structures. I've just outlined a few options.

HTH 

whroeder1
16278
whroeder1  
It also matters if you want to optimize the value(s). You can't use a string in the optimizer. The enumeration works. So does an int:
#define DAY 86400
#define SECONDS uint
#define HR2400 86400
SECONDS    time(datetime when=0){
      return SECONDS(when == 0 ? TimeCurrent() : when) % HR2400;               }
datetime   date(datetime when=0){
      return datetime( (when == 0 ? TimeCurrent() : when) - time(when) );      }

bool       in_range(SECONDS begin, SECONDS, end, SECONDS when=0){
   if(when == 0) when = time();
   if(begin < end)  return begin <= when && when < end; // 0300-2300
                    return begin <= when || when < end; // 2300-0300
}
////////////////////////////////////////////////////////////////////////////////
input SECONDS start_time; // Start Time
input SECONDS   end_time; // End Time
int OnInit()
  {
   if(start_time >= HR2400) return INIT_PARAMETERS_INCORRECT;
   if(  end_time >= HR2400) return INIT_PARAMETERS_INCORRECT;
   return INIT_SUCCEEDED;
  }

if(in_range(start_time, end_time) )
  {
   // EA can do stuff
  }
Note the use of self-documenting functions, instead of in-line code.

Note the case of over midnight.

Instead of an int, I've also used a double, as in hour start/end. Can be optimized. User just needs to know that 10.5 means 10:30.
Thehallster
106
Thehallster  

G‌reat stuff thank you for your input as always!

K‌nave i Like you way of doing it with the list. I think that makes it a bit more idiot proof. and as I have found thats a big deal for some people lol

I‌ could still perhaps limit the list size just by only allowing it to be modded over the 12hr period etc so limit start from 01:00 - 12:00 then end times from 13:00-23:00

‌My query would be though will this code be able to go mid way through my EA as a Logic called each tick or would it prevent the EA working as a whole as I have a clever MTF EMA stop loss system running that I would like to have running for the whole time regardless if its in or out of hours so it can control non closed trades.

I‌ve made some comments to Roeders code to make sure im understanding this properly:

#define DAY 86400
#define SECONDS uint
#define HR2400 86400 // assuming this equates to the amount of seconds at 24:00 and so the above definition of a Day starts at 1 @ 00:00;01 and ends at 86400 seconds 24:00:00
SECONDS    time(datetime when=0){                                                // This then I assume takes datetime of current time right now example 13:50:00 (36600 seconds since midnight)
      return SECONDS(when == 0 ? TimeCurrent() : when) % HR2400;               } // This returns that calculation of 36600 seconds?
datetime   date(datetime when=0){
      return datetime( (when == 0 ? TimeCurrent() : when) - time(when) );      }

bool       in_range(SECONDS begin, SECONDS, end, SECONDS when=0){
   if(when == 0) when = time();
   if(begin < end)  return begin <= when && when < end; // 0300-2300             //then this one im assuming is just calculating if 36600 is less or more than the start and end times.
                    return begin <= when || when < end; // 2300-0300
}
////////////////////////////////////////////////////////////////////////////////
input SECONDS start_time; // Start Time                                         // Is this where we can set as a string or a double or even as Knave suggested is it done as a Boolean List?
input SECONDS   end_time; // End Time
int OnInit()
  {
   if(start_time >= HR2400) return INIT_PARAMETERS_INCORRECT;
   if(  end_time >= HR2400) return INIT_PARAMETERS_INCORRECT;
   return INIT_SUCCEEDED;
  }

if(in_range(start_time, end_time) )
  {
   // EA can do stuff
  }
whroeder1
16278
whroeder1  
input SECONDS start_time; // Start Time  // Is this where we can set as a string or a double or even as Knave suggested is it done as a Boolean List?

‌I defined what SECONDS is. How can it be a string or a double?

Thehallster
106
Thehallster  
whroeder1:
input SECONDS start_time; // Start Time  // Is this where we can set as a string or a double or even as Knave suggested is it done as a Boolean List?

‌I defined what SECONDS is. How can it be a string or a double?

I‌ think i didnt explain what i meant as Seconds is defined as a unit which is fine what i meant is what does the user put in the input field if we locked it to a drop down list of times eg. 01:00 


So like Knave suggested to use a drop down. could we define the start time and end time as entered as a string and then convert that value into a uint

#define DAY 86400

input string str_start_time; // Start Time
datetime start_time;

int OnInit()
  {
   start_time = (datetime)str_start_time; // convert the string to datetime
   start_time = start_time % DAY;         // convert to seconds since midnight
   return(INIT_SUCCEEDED);
  }

‌So how to set the input for Start time and End Time as String.

T‌hen convert this string to a uint

t‌hen use that uint in the seconds calculation you have shown.

S‌o i suppose my question was what is the code to show the string values as a list how do i define that list.

a‌nd then how do i convert that string to uint to use in your code.

t‌hat might be a bit cleared as to what i was asking.

t‌hanks,

C‌hris H

Thehallster
106
Thehallster  

Hey Guys,

M‌anaged to solve this one in the end.

U‌sed this code as a group of functions to get the values in minutes a bit like how wroeader did it in seconds from Midnight I couldnt quite work out how to get that done in 1 calculation and couldnt get it to work like Knaves suggestion using a string entry.

T‌hen after this as i want the EA to still operate its closing functions and modification functions after trading hours I then just made a simple logic If Less than StartTime or Greater than Endtime Return False. Then display out of trading hours and delete all pending orders.

J‌ob Done! cheers for the help guys. just opened a new subject for trade value calculations which looks to be my last major issue.

T‌hanks for the help guys, been a real big learning curve your help and guidance has been really appreciated.

                //  Converts string in the format "yyyy.mm.dd hh:mi" to datetime type (the amount of seconds that have passed since 1 Jan., 1970).//      //  Sample//  //        datetime var1;//        var1=StrToTime("2003.8.12 17:35");//        var1=StrToTime("17:35");      // returns the current date with the given time//        var1=StrToTime("2003.8.12");  // returns the date with the midnight time of "00:00"
                                                                                _StrToTimeStart = StrToTime(  StartTime );
                                                                                string msg3707;
                                                                                msg3707 = StringConcatenate(msg3707,"Start Time","\n");
                                                                                msg3707 = StringConcatenate(msg3707,"Value of _StrToTimeStart:",TimeToStr(_StrToTimeStart),"\n");
                                                                                chartcomment6969 = StringConcatenate(chartcomment6969,msg3707, "\n" );
                                                                                //  Converts string in the format "yyyy.mm.dd hh:mi" to datetime type (the amount of seconds that have passed since 1 Jan., 1970).//      //  Sample//  //        datetime var1;//        var1=StrToTime("2003.8.12 17:35");//        var1=StrToTime("17:35");      // returns the current date with the given time//        var1=StrToTime("2003.8.12");  // returns the date with the midnight time of "00:00"
                                                                                _StrToTimeEnd = StrToTime(  EndTime );
                                                                                string msg3899;
                                                                                msg3899 = StringConcatenate(msg3899,"End Time","\n");
                                                                                msg3899 = StringConcatenate(msg3899,"Value of _StrToTimeEnd:",TimeToStr(_StrToTimeEnd),"\n");
                                                                                chartcomment6969 = StringConcatenate(chartcomment6969,msg3899, "\n" );
                                                                                //  Returns the hour for the specified time.//  Sample//  int h=TimeHour(TimeCurrent());
                                                                                _HourValue = TimeHour(  TimeCurrent() );
                                                                                //  Returns the minute for the specified time. //  Sample//  int m=TimeMinute(TimeCurrent());
                                                                                _MinuteValue = TimeMinute(  TimeCurrent() );
                                                                                //  Element type: Variable Element name:Variable1 22:20
//  
//  add your notes here ...
                                                                                CurrentTimeinMinutes=_HourValue  * 60  +  _MinuteValue;
                                                                                //  Returns the minute for the specified time. //  Sample//  int m=TimeMinute(TimeCurrent());
                                                                                _StartTimeMinuteValue = TimeMinute(  _StrToTimeStart );
                                                                                //  Returns the hour for the specified time.//  Sample//  int h=TimeHour(TimeCurrent());
                                                                                _StartTimeHourValue = TimeHour(  _StrToTimeStart );
                                                                                //  Element type: Variable Element name:Variable2 22:33
//  
//  add your notes here ...
                                                                                StartTimeinMinutes=_StartTimeHourValue  * 60  +   _StartTimeMinuteValue;
                                                                                //  Returns the minute for the specified time. //  Sample//  int m=TimeMinute(TimeCurrent());
                                                                                _EndTimeMinuteValue = TimeMinute(  _StrToTimeEnd );
                                                                                //  Returns the hour for the specified time.//  Sample//  int h=TimeHour(TimeCurrent());
                                                                                _EndTimeHourValue = TimeHour(  _StrToTimeEnd );
                                                                                //  Element type: Variable Element name:Variable3 22:38
//  
//  add your notes here ...
                                                                                EndTimeinMinutes=_EndTimeHourValue * 60  +  _EndTimeMinuteValue;
                                                                                return( 0 );
                                                                        } // end elements\user\functions\BigBrokingAutoTraderV8\TimeSettings.fe
                                                                        //  add notes here ...


To add comments, please log in or register