Page 1 of 2 12 LastLast
Results 1 to 10 of 13
 2 Attachment(s)    

Thread: Add a time filter

  1. #1
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    7,003

    Default Add a time filter

    Here is how to add a time filter to your expert advisor :

    In the header, add this :

    Code:
    extern string TF="---------------- Time Filter";
    extern int StartHour=8;
    extern int EndHour=21;
    Then this before the conditions to enter in trade :

    Code:
    if (Hour()>=StartHour&&Hour()<EndHour) {
    Here are your conditions...

    Code:
    }
    And finally that to close the orders at EndHour :

    Code:
    if((Hour()==EndHour)){
    OrderClose(OrderTicket(),...

    Code:
    }
    Note that there is a conflict when midnight is included in your time filter. I have not found any solution for the moment. There is certainly a simple one. You will have two versions for your expert advisor. If you have an idea or another way to do a time filter, don't hesitate to post it.

    If midnight is included :

    In the header, add this :

    Code:
    extern string TF="---------------- Time Filter";
    extern int StartHour=21;
    extern int EndHour=8;
    Then this before the conditions to enter in trade :

    Code:
    if ((Hour()>=StartHour&&Hour()<=24)||(Hour()>=0&&Hour()<EndHour)) {
    Here are your conditions...

    Code:
    }
    And finally that to close the orders at EndHour :

    Code:
    if((Hour()==EndHour)){
    OrderClose(OrderTicket(),...

    Code:
    }

  2. #2
    Junior Member
    Join Date
    Jan 2009
    Posts
    21

    Exclamation

    will this time filter close all the trade at the stop hour? if so, that not the one I want. is there one where it will just not put on any more trades after the stop hour but will still manage the other trades put on before the stop hr? what about trade friday true/false option?

  3. #3
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    7,003

    Default

    Quote Originally Posted by hazelj80 View Post
    will this time filter close all the trade at the stop hour? if so, that not the one I want. is there one where it will just not put on any more trades after the stop hour but will still manage the other trades put on before the stop hr? what about trade friday true/false option?
    Hi hazelj80,

    You have to put this before the conditions to open :

    Code:
    if (!(Hour()>=StartHour&&Hour()<=EndHour)||(TradeFriday==false&&DayOfWeek()==5))return(0);
    And this in the extern parameters :

    Code:
    extern int StartHour=0;
    extern int EndHour=24;
    extern bool TradeFriday=true;

  4. #4

    Default

    I do this.

    Extern parameters:

    Code:
    extern int StartHour=0;
    extern int StartMinute=0;
    extern int EndHour=23;
    extern int StartMinute=59;
    Then in the Start() function:

    Code:
    bool operating=false;
    
    if(((Hour()>StartHour)&&(Hour()<EndHour))
     || (Hour()==StartHour&&Minute()>=StartMinute)
     || (Hour()==EndHour&&Minute()<EndMinute))
     operating=true;
    Then in your entry conditions add this extra condition:

    Code:
    &&operating
    This condition would not feature as an exit condition or when modifying your SL's.

    Running a start time of 0:00 and an end time of 23:59 is the equivalent of it running 24 hours.
    Last edited by jezzer1961; 01-27-2009 at 11:56.

  5. #5
    Junior Member
    Join Date
    Feb 2009
    Posts
    5

    Default

    Hello, Funyoo,

    It's important to calculate time, I don't know much about coding, can we calculate like this:
    For example:
    2009.02.27 00:00
    year =2009*365*23*60
    Month=02*31*23*60
    day=27*23*60
    hour=00*60
    minute=00
    Add all above together will have the minute for the above time , any time before it will be less, any time after will be bigger.

    we should not bother about how many days a month, 31 is max, and one day have 23 hours, because it starts with 0

    Do you think it will work in this way? Even it works, I don't know how to have the code to do this?

    Jean
    Last edited by jeandl; 02-28-2009 at 02:33.

  6. #6
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    7,003

    Default

    Quote Originally Posted by jeandl View Post
    Hello, Funyoo,

    It's important to calculate time, I don't know much about coding, can we calculate like this:
    For example:
    2009.02.27 00:00
    year =2009*365*23*60
    Month=02*31*23*60
    day=27*23*60
    hour=00*60
    minute=00

    Add all above together will have the minute for the above time , any time before it will be less, any time after will be bigger.

    we should not bother about how many days a month, 31 is max, and one day have 23 hours, because it starts with 0

    Do you think it will work in this way? Even it works, I don't know how to have the code to do this?

    Jean
    Hello jeandl,

    Code:
    int minute = 0;
    int hour = 0;
    int day = 27;
    int month = 2;
    int year = 2009;
    
    if (((TimeMinute(TimeCurrent()) > minute) && (TimeHour(TimeCurrent()) > hour ) && (TimeDay(TimeCurrent()) > day) && (TimeMonth(TimeCurrent()) >= month) && (TimeYear(TimeCurrent()) >= year)) || ((TimeMonth(TimeCurrent()) > month) && (TimeYear(TimeCurrent()) >= year)) || (TimeYear(TimeCurrent()) > year))
       {
       Print(" Time is bigger ");
       return;
       }
    else
       {
       Print(" Time is less");
       return;
       }
    More informations about date and time functions.

  7. #7
    Junior Member
    Join Date
    Feb 2009
    Posts
    5

    Default

    Thanks Funyoo, I'll try to understand.

    Jean

  8. #8

    Default

    ok funyoo here it is
    Attached Files Attached Files
    • File Type: mq4 1.mq4 (10.4 KB, 502 views)

  9. #9
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    7,003

    Default

    Quote Originally Posted by cimi View Post
    ok funyoo here it is
    Hi cimi,

    It's one of my code with different copyrights, I will not work on it.

  10. #10

    Default

    ok but ı took it from a different person (name on it).ı guess he took from you then..

Similar Threads

  1. Mandor EA time filter
    By EaLabSpain in forum Ideas for expert advisors
    Replies: 0
    Last Post: 04-10-2012, 20:36
  2. Time filter
    By Viv108 in forum MQL programming
    Replies: 19
    Last Post: 05-25-2011, 12:07
  3. Add a time filter
    By sunyc1982 in forum MQL programming
    Replies: 3
    Last Post: 02-27-2011, 18:53
  4. Time filter code
    By himnoor in forum MQL programming
    Replies: 0
    Last Post: 12-21-2010, 02:55
  5. Req. Trading Time Filter
    By Trint in forum Ideas for expert advisors
    Replies: 0
    Last Post: 01-23-2010, 09:19

Tags for this Thread

add, advisor, coding, color, dmi, eas, expert, expert advisor, how to, main, managed, mq4, mql, signals, simple, stop, time, trading

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •