Help With TimeCurrent function please

 
Hello... I am new here and need to know where I can read how to use this interface ie, pasting code into the text etc. ? Anyways I'll try to ask the question the best I can without the use of copy and paste functions. I am a newbie coder so please bear with me. What I am trying to do is take an extern date eg; [ extern NoTradeTimeFrom=D'2006. 01. 09 01:00' ] and [ extern NoTradeTimeTo=D'2006. 01.09 07:00' ] and stop ordersend during those time periods but I'm having trouble getting the Time functions correct. I have tried several ways and have been scearching for days looking for similar code but have not found any. Could someone please post a similar code or show me just the part comparing the extern date to the if expression eg; [ if TimeToStr(TimeCurrent()) >= NoTradeTimeFrom && TimeToStr(TimeCurrent()) <= NoTradeTimeTo)    I would really appreciate anyones help with this, apparently I am comparing two different data types in this case.    Thank you very much for you help.  TJ    
 
extern NoTradeTimeFrom = D'2006.01.09 01:00';
extern NoTradeTimeTo = D'2006.01.09 07:00';
The above won't compile. Should be
extern datetime NoTradeTimeFrom = D'2006.01.09 01:00';
extern datetime NoTradeTimeTo = D'2006.01.09 07:00';
Then just compare the values of the same type datetime directly
if (TimeCurrent() >= NoTradeTimeFrom)
    if (TimeCurrent() <= NoTradeTimeTo)
        Print("No trade interval");
Note, that the dates given in NoTradeTime... variables are absolute, i.e. the trade is disabled on 09/01/2006 that might be not actual.

You need to use TimeHour() and Hour() functionality to build a proper filter.
 

As Irtron said, is absolute - once off way to do your job. Can run expert as required and change extern values: see #property show_inputs in .docs

Another way as mentioned is to do filter calculation. Below is code which you can play around with and if you copy 'n paste into editor window can execute to see what's what.

I was a bit over the top here, simply because I read your post and could not at first see how MQL4 could help... it's the datetime concept which is good but requires calculations to bend to your own requirements and it was this which made me do below.

Actually, would be interesting to see other takes on this area. I could be totally over complicating things here and always wanting to learn so maybe other readers will chip in too!

After all, when all's said and done - the EA must attempt to return() before next data tic otherwise Terminal will not run EA... so will loose that datum. So FAST is beautiful and for that reason, others may totally blow my idea out of the running - that's ok 'cause end result is my code gets tighter and faster too. Is win-win result as they say - LOL

Regards

 
#property show_inputs                //allows you to modify extern's before EA executes
 
//if wish to have noTrade window during Mon..Fri refer to Date & Time functions - DayOfWeek
//
extern int eiStartNoTradeHH = 13;    //!24hrClock!        //D'01:00'  of  D'2006.01.09 01:00'
extern int eiEndNoTradeHH = 19;      //!24hrClock!        //D'07:00'  of  D'2006.01.09 07:00'
//
//+------------------------------------------------------------------+
//
int start()
{
 
    //Here is my take on the filter Irtron mentioned - for sure many more ways, nothing ever cast in concrete ;-)
    //My idea is to NOT allow trading during 24hour clock times of 13.00 -> 19:00 inclusive
    
    //current 'local' computer time. Use TimeCurrent() if need time of last received data server quote
    //note, by making call once here, can use 'our' copy of the time...
    //
    datetime dtNow = TimeLocal();
 
    //get the number of seconds since day start: Beginning Of Day (BOD)
    //idea is to: convert current time "from 00:00:00" into seconds
    //
    int iNowSecsFromBOD = (TimeHour(dtNow)*60*60 ) + (TimeMinute(dtNow)*60) + TimeSeconds(dtNow);
    
    //once have seconds from 00:00:00 (day start/BOD), we can create 'MQL4 datetime' for "yyyy.mm.dd 00:00:00"
    //idea is: from current time, subtract seconds since day start: giving Beginning Of Day (BOD)
    //
    datetime dtBOD = dtNow - iNowSecsFromBOD;
    
    //finally, get start and end 'MQL4 datetime' for today's noTradeWindow
    //idea is to: add start,end hours worth of seconds to BOD to create two datetime values describing noTradeWindow
    //
    datetime dtStartNoTradeWindow = dtBOD + (eiStartNoTradeHH*60*60);
    datetime dtEndNoTradeWindow = dtBOD + (eiEndNoTradeHH*60*60);
 
    //above calculations seen by test prints to Terminal (control-T), "Experts" tab
    //
    Print( "NoTradeTimeFrom(in \'MQL4 datetime\') = ",dtStartNoTradeWindow," = ",TimeToStr(dtStartNoTradeWindow) );
    Print( "NoTradeTimeTo(in \'MQL4 datetime\') = ",dtEndNoTradeWindow," = ",TimeToStr(dtEndNoTradeWindow) );
 
    /* with extern's set to 13 and 19, expect YES/trading to be allowed as my local hour=12:
        2007.09.11 12:26:27    testBed GBPUSD,Daily: outside noTrade window!
        2007.09.11 12:26:27    testBed GBPUSD,Daily: NoTradeTimeTo(in 'MQL4 datetime') = 1189537200 = 2007.09.11 19:00
        2007.09.11 12:26:27    testBed GBPUSD,Daily: NoTradeTimeFrom(in 'MQL4 datetime') = 1189515600 = 2007.09.11 13:00
        2007.09.11 12:26:27    testBed GBPUSD,Daily inputs: eiStartNoTradeHH=13; eiEndNoTradeHH=19; 
    */
 
    /* with extern's set to 12 and 19, expect NO/trading to be allowed as my local hour=12:
        2007.09.11 12:28:44    testBed GBPUSD,Daily: inside noTrade window!
        2007.09.11 12:28:44    testBed GBPUSD,Daily: NoTradeTimeTo(in 'MQL4 datetime') = 1189537200 = 2007.09.11 19:00
        2007.09.11 12:28:44    testBed GBPUSD,Daily: NoTradeTimeFrom(in 'MQL4 datetime') = 1189512000 = 2007.09.11 12:00
        2007.09.11 12:28:44    testBed GBPUSD,Daily inputs: eiStartNoTradeHH=12; eiEndNoTradeHH=19; 
    */
 
 
    //the actual trade/noTrade filter
    //
    if( dtNow >= dtStartNoTradeWindow && dtNow <= dtEndNoTradeWindow )
    {
        //do not trade...
        //
        Print("inside noTrade window!");
        return(0);    //or do whatever else is required
    }
    
    //if here, then time is outside noTrade window
    //
    Print("outside noTrade window!");
    return(0);        //or do whatever else is required
 
}//start()
 
ukt:

As Irtron said, is absolute - once off way to do your job. Can run expert as required and change extern values: see #property show_inputs in .docs

Another way as mentioned is to do filter calculation. Below is code which you can play around with and if you copy 'n paste into editor window can execute to see what's what.

I was a bit over the top here, simply because I read your post and could not at first see how MQL4 could help... it's the datetime concept which is good but requires calculations to bend to your own requirements and it was this which made me do below.

Actually, would be interesting to see other takes on this area. I could be totally over complicating things here and always wanting to learn so maybe other readers wll chip in too!

After all, when all's said and done - the EA must attempt to return() before next data tic otherwise Terminal will not run EA... so will loose that datum. So FAST is beautiful and for that reason, others may totally blow my idea out of the running - that's ok 'cause end result is my code gets tighter and faster too. Is win-win result as they say - LOL

Regards

 
#property show_inputs                //allows you to modify extern's before EA executes
 
//if wish to have noTrade window during Mon..Fri refer to Date & Time functions - DayOfWeek
//
extern int eiStartNoTradeHH = 13;    //!24hrClock!        //D'01:00'  of  D'2006.01.09 01:00'
extern int eiEndNoTradeHH = 19;      //!24hrClock!        //D'07:00'  of  D'2006.01.09 07:00'
//
//+------------------------------------------------------------------+
//
int start()
{
 
    //Here is my take on the filter Irtron mentioned - for sure many more ways, nothing ever cast in concrete ;-)
    //My idea is to NOT allow trading during 24hour clock times of 13.00 -> 19:00 inclusive
    
    //current 'local' computer time. Use TimeCurrent() if need time of last received data server quote
    //note, by making call once here, can use 'our' copy of the time...
    //
    datetime dtNow = TimeLocal();
 
    //get the number of seconds since day start: Beginning Of Day (BOD)
    //idea is to: convert current time "from 00:00:00" into seconds
    //
    int iNowSecsFromBOD = (TimeHour(dtNow)*60*60 ) + (TimeMinute(dtNow)*60) + TimeSeconds(dtNow);
    
    //once have seconds from 00:00:00 (day start/BOD), we can create 'MQL4 datetime' for "yyyy.mm.dd 00:00:00"
    //idea is: from current time, subtract seconds since day start: giving Beginning Of Day (BOD)
    //
    datetime dtBOD = dtNow - iNowSecsFromBOD;
    
    //finally, get start and end 'MQL4 datetime' for today's noTradeWindow
    //idea is to: add start,end hours worth of seconds to BOD to create two datetime values describing noTradeWindow
    //
    datetime dtStartNoTradeWindow = dtBOD + (eiStartNoTradeHH*60*60);
    datetime dtEndNoTradeWindow = dtBOD + (eiEndNoTradeHH*60*60);
 
    //above calculations seen by test prints to Terminal (control-T), "Experts" tab
    //
    Print( "NoTradeTimeFrom(in \'MQL4 datetime\') = ",dtStartNoTradeWindow," = ",TimeToStr(dtStartNoTradeWindow) );
    Print( "NoTradeTimeTo(in \'MQL4 datetime\') = ",dtEndNoTradeWindow," = ",TimeToStr(dtEndNoTradeWindow) );
 
    /* with extern's set to 13 and 19, expect YES/trading to be allowed as my local hour=12:
        2007.09.11 12:26:27    testBed GBPUSD,Daily: outside noTrade window!
        2007.09.11 12:26:27    testBed GBPUSD,Daily: NoTradeTimeTo(in 'MQL4 datetime') = 1189537200 = 2007.09.11 19:00
        2007.09.11 12:26:27    testBed GBPUSD,Daily: NoTradeTimeFrom(in 'MQL4 datetime') = 1189515600 = 2007.09.11 13:00
        2007.09.11 12:26:27    testBed GBPUSD,Daily inputs: eiStartNoTradeHH=13; eiEndNoTradeHH=19; 
    */
 
    /* with extern's set to 12 and 19, expect NO/trading to be allowed as my local hour=12:
        2007.09.11 12:28:44    testBed GBPUSD,Daily: inside noTrade window!
        2007.09.11 12:28:44    testBed GBPUSD,Daily: NoTradeTimeTo(in 'MQL4 datetime') = 1189537200 = 2007.09.11 19:00
        2007.09.11 12:28:44    testBed GBPUSD,Daily: NoTradeTimeFrom(in 'MQL4 datetime') = 1189512000 = 2007.09.11 12:00
        2007.09.11 12:28:44    testBed GBPUSD,Daily inputs: eiStartNoTradeHH=12; eiEndNoTradeHH=19; 
    */
 
 
    //the actual trade/noTrade filter
    //
    if( dtNow >= dtStartNoTradeWindow && dtNow <= dtEndNoTradeWindow )
    {
        //do not trade...
        //
        Print("inside noTrade window!");
        return(0);    //or do whatever else is required
    }
    
    //if here, then time is outside noTrade window
    //
    Print("outside noTrade window!");
    return(0);        //or do whatever else is required
 
}//start()
 
Wow... extremely helpful thanks!   One question though...  if I use the 24hr clock extern you show above, then I assume the year, month and day will not be part of the data the ea will use for NoTradeTimes correct?  So If I want to have the year, month, day, hour and minutes in the search date then I would have to use D'yyyy.mm.dd hh:mm' as the extern field.  Is this correct?  Thanks again,  Tj
 
TraderJoe:
Wow... extremely helpful thanks! One question though... if I use the 24hr clock extern you show above, then I assume the year, month and day will not be part of the data the ea will use for NoTradeTimes correct? So If I want to have the year, month, day, hour and minutes in the search date then I would have to use D'yyyy. mm.dd hh:mm' as the extern field. Is this correct? Thanks again, Tj


Good...

year,month and day are the date which EA finds self running and executing code and will be part of the data EA uses - but expressed in 32bit/int size value (as docs state): "Datetime as number of seconds elapsed since midnight (00:00:00), January 1, 1970"

I may be off course here, but essentially types int, datetime are the same as both stored as 32bit integers.

Not understand what mean "...in the search date...". The EAs start() is called everytime new server price datum received by Client Terminal and at this time EA is comparing now time (a datetime held in seconds form) with a just calculated datetime (again, held in seconds form) to see if inside or outside the no trade period as expressed by a from,to hour on the current day/nowtime

I ask this as you originally expressed desire to inhibit OrderSend(), during specified hours of a day. If you are saying that you want to supply EA with complete datetime so that in the future should it find itself running when this extern datetime occurs - it will inhibit orders, then of course above code will not work. I ask this because you said

"..and stop ordersend during those time periods.."

but Irtron did mention that any complete datetime given to EA via extern functionality is essentially cast in concrete or absolute and unchanging. This is why I offered code which would work on any day it found self running.

Ok... I ramble because not get what you mean now, so please elaborate precisely what it is you want to achieve. Surely, language is a pain best of times and none more so at these times - lol

Perhaps it is that you stuck on this D'yyyy.kjdskkjdsakjld' thingy? is seen as 32bit binary constant number - nothing more. I use constant to mean unchanging/absolute and since this value has within it the yyyy.mm.dd which also includes hh:mm:ss - oops, there I go again. better pass over to you now...!

Another thought: remember that by giving extern D'...' value the EA would only ever take notice of the notTradePeriod - once only and that's IF it is running on the date and hour and minute etc, hardcoded into the D'...' value supplied at EA runtime.

 
ukt:
TraderJoe:
Wow... extremely helpful thanks! One question though... if I use the 24hr clock extern you show above, then I assume the year, month and day will not be part of the data the ea will use for NoTradeTimes correct? So If I want to have the year, month, day, hour and minutes in the search date then I would have to use D'yyyy. mm.dd hh:mm' as the extern field. Is this correct? Thanks again, Tj


Good...

year,month and day are the date which EA finds self running and executing code and will be part of the data EA uses - but expressed in 32bit/int size value (as docs state): "Datetime as number of seconds elapsed since midnight (00:00:00), January 1, 1970"

I may be off course here, but essentially types int, datetime are the same as both stored as 32bit integers.

Not understand what mean "...in the search date...". The EAs start() is called everytime new server price datum received by Client Terminal and at this time EA is comparing now time (a datetime held in seconds form) with a just calculated datetime (again, held in seconds form) to see if inside or outside the no trade period as expressed by a from,to hour on the current day/nowtime

I ask this as you originally expressed desire to inhibit OrderSend(), during specified hours of a day. If you are saying that you want to supply EA with complete datetime so that in the future should it find itself running when this extern datetime occurs - it will inhibit orders, then of course above code will not work. I ask this because you said

"..and stop ordersend during those time periods.."

but Irtron did mention that any complete datetime given to EA via extern functionality is essentially cast in concrete or absolute and unchanging. This is why I offered code which would work on any day it found self running.

Ok... I ramble because not get what you mean now, so please elaborate precisely what it is you want to achieve. Surely, language is a pain best of times and none more so at these times - lol

Perhaps it is that you stuck on this D'yyyy.kjdskkjdsakjld' thingy? is seen as 32bit binary constant number - nothing more. I use constant to mean unchanging/absolute and since this value has within it the yyyy.mm.dd which also includes hh:mm:ss - oops, there I go again. better pass over to you now...!

Another thought: remember that by giving extern D'...' value the EA would only ever take notice of the notTradePeriod - once only and that's IF it is running on the date and hour and minute etc, hardcoded into the D'...' value supplied at EA runtime.

 

My purpose is to halt trading during certain event hours without regard to dates although many times the halt will occur only during the same Date/day sometimes the halt will need to begin on one Date/day and continue into the next Date/day, for instance. Major event is set to happen (all gmt) on Tuesday night Jan 31 2006 at 18:15. Because of this I want to halt trading not only through the end of Monday but many times I may need to continue the halt through some, much or most of Tuesday which is a different Date than Monday. Do you understand what I'm trying to explain? How can this be accomplished? Thank you ! Tj

 

Thank you for full detail - I 'get it' now :)
perhaps...
Since using gmt then TimeLocal() will do. In the if(...) <= edtEndNoTrade will allow NO trading up to and including the end time. To restrict NO trading to just up to change <= to <

Bear in mind that whatever start,end time you specifiy may not actually be 'seen' at exact time by EA because entry into start() only happens when new data tic received from server. In the evenings for instance this can be many mins... meaning that although you want NO trading to start from say 22:30, Client Terminal may not get new data tic until say 22:35 at which time start() will be called - guess no biggie really as EA not able to run so not matter does it, like even if not get new tic for any number of mins after start of NO trading time... Anyway, there it is!

Cheers

//String value of date/time format as:
//                                    "yyyy.mm.dd hh:mi:ss"
extern datetime edtStartNoTrade =    D'2007.09.13 09:06:00';
extern datetime edtEndNoTrade   =    D'2007.09.13 09:09:00';
 
int start()
{ 
    if( TimeLocal() >= edtStartNoTrade && TimeLocal() <= edtEndNoTrade )
    {
        //if here, then time is INSIDE NO TRADE window
        return(0);
    }
 
    //if here, then time is OUTSIDE NO TRADE window
    //so do OrderSend() if system says so...
    //...
 
    return(0);
 
}//start()
 
Ok great, I'll give that a shot.  Thanks for all your help! Tj
 
Works except once inside the no trade window, we're staying there in an endless loop :(     Any ideas how to break out?   Thanks.   Tj 
Reason: