Help! adding trading time as a function

 

Hello there?

This will be simple for someone but I can't seem to get my head round it. I wish to add an extern bool "UseTimer" to an EA so that it will only trade between the times set by the user but I am unsure on how to write the function and indeed the extern setting options can anyone help

 
I've setup the inputs and time-limitations this way :

  • use a 24 digit, binary-ish string input, representing one trading day, (24 hours of a trading day)
  • fill a bool array, containing the hourly trade-permission (bool Trade_Permission[24])
  • check permission right before sending order. (hour_of_day: 0 to 23)
  • you can use 5 inputs, for each trading day of a week.
input string ActiveHours = "001111101111111111111000"; // zeros disable trading
bool HourlyPermission[24];


OnInit():
// first check ActiveHours for invalid cases (wrong length, non-1 and non-0 characters etc...)
// fill in HourlyPermission[0] to HourlyPermission[23] according to ActiveHours string....

Ontick():
// bla bla bla
// convert current time to struct, and get value of hour_of_day
if(HourlyPermission[MyStruct.hour_of_day])
{
        // trade like a mad man :)
}
else
{
        // don't trade, probabaly just trail-stop or other maintenance procedures 
}
 

thank you ill give it a try

 
Code2219 or probably 2319:
I've setup the inputs and time-limitations this way :

  • use a 24 digit, binary-ish string input, representing one trading day, (24 hours of a trading day)
  • fill a bool array, containing the hourly trade-permission (bool Trade_Permission[24])
  • check permission right before sending order. (hour_of_day: 0 to 23)
  • you can use 5 inputs, for each trading day of a week.

sorry i've never used time as a trigger before could you give me an example in the code of say allowing the user to set to trade between 0800 && 2100, Thanks

 
  1. input string ActiveHours = "001111101111111111111000"; // zeros disable trading
    This is user friendly but can't be optimized. If you make it a uint, then it can be.  min=1, max=224-1, 0xFFFFFF or 16777215. No need for any array or struct.
    input uint ActiveHours = 0xFFFFFF; // zeros disable trading
    :
    bool isActive = bool(ActiveHours & (1 << Hour() ));

  2. The alternative is just use two ints, startHour and endHour. This can be optimized much easier and is likely more robust.
    beg < end ? beg <= tod && tod < end : end <= tod && tod < beg;

  3. Don't double post!
              General rules and best pratices of the Forum. - General - MQL5 programming forum
 

I've submitted this to the codebase but it hasn't been published yet. Maybe it'll work for your use case.

Put the files here :

Include\Schedule\Schedule.mqh
Scripts\Tests\Schedule\ScheduleTests.mq4

Include the Schedule file in your project and wire it up to the user inputs. The schedule object will have a method for checking whether a time is within the active schedule or not. The tests scripts should show the intended functionality.

#include <Schedule\Schedule.mqh>

// setting up values to define the schedule

ENUM_DAY_OF_WEEK StartDay=1;//Start Day

ENUM_DAY_OF_WEEK EndDay=5;//End Day

string   StartTime="12:30";//Start Time

string   EndTime="14:30";//End Time


// Creating the schedule from above settings

Schedule s(StartDay,StartTime,EndDay,EndTime);


// Displaying the schedule as a string in English

Print("Schedule : ",s.ToString());


// Creating a datetime to check, this would normally come from ticks or bars instead.

datetime ted0 = StrToTime(StringConcatenate("2018.06.10 ", (string)(s.TimeEnd.Hour), ":", (string)(s.TimeEnd.Minute)));


// Checking whether the given datetime falls within the schedules active time.

Print(s.IsActive(ted0));



I don't think I tested it where the beginning and ending day of the week are the same value. Let me know if you use it and it works.

Files:
Reason: