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
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.
There are quite a few different ways to do this.
How you approach it will depend on a number of factors:
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.
When you want to check if you are inside the trading hours:
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.
Great stuff thank you for your input as always!
Knave 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.
Ive made some comments to Roeders code to make sure im understanding this properly:
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
So how to set the input for Start time and End Time as String.
Then convert this string to a uint
then use that uint in the seconds calculation you have shown.
So i suppose my question was what is the code to show the string values as a list how do i define that list.
and then how do i convert that string to uint to use in your code.
that might be a bit cleared as to what i was asking.
Managed to solve this one in the end.
Used 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.
Then 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.
Job Done! cheers for the help guys. just opened a new subject for trade value calculations which looks to be my last major issue.
Thanks for the help guys, been a real big learning curve your help and guidance has been really appreciated.