Automatic Time Control

 

Automatic Cruise Control

I have an Automatic routine that can eleminate all those "what time do I set the Expert at if I am in XXX Time zone? Every time an EA is writen that depends on a time setting to operate, that thread will gets filled with "What Time Setting Do I Use" questions.

I have done a lot of work to narrow down the need to have any time settings questions, as the coder will now be able to code his work with out the need to know what the time zone of the end user or which time that user's brokers terminal is set too. I've created a number of files and functions over the years, that will accomplish this work for you, the last peice is now done. I will need to rewrite the code so that it can work out side of my personal libraries. I will create a seperate Time Library that will operate independly from my other work. It will be able to be called for any MQ4 code. All the code will be given in both MQ4 format and in a usable ex4 library. There are serveral files to rewrite and I will be giving them out one at a time when each are done. This will give me the oppertunity to explain what each function in the library does. When done I will combin it into an easy to use library.

What can be done with this library?

It will be possible for you to code with out knowing what time zone the users location (computor) is, or what time zone the users terminal (broker) is in. All your coding will be done using GMT (or UTC) time. Your code will then be able to adjust itself (settings) to the end user and his broker terminal time.

Now getting the users time is quite easy as we get that from his/her computor clock with the 'TimeLocal()' function. The computer clock must be correct or your EA/Indicater code will not generate the proper outputs. The broker time is also easily aquired from the broker terminal which is naturally 'TimeCurrent()' functions. What is needed then is to have the users terminal be aware of the current GMTime and perform all the needed calculates to determine OffSets to GMT in order to correctly adjust the settings in your code. This may sound quit complicated at first but you will be supprize how simple it really is.

The matter of the clients computer clock being set correctly, is of great importants, as your code will not function correctly if the clients time is wrong. What this Library will do when first used is update the client's computer time automaticly from the US Navy servers in Dever. This will be done only once each time your start the EA/Indi code that calls this time library. There fore an internet connection will be needed for it to work. I have constructed a back up system, as well, for controling time functions for working off linel. But the back up system will only function after you have used the libaray with a live connection at lease one time. This is needed to activate that backup system mode.

I will explain more as I post the different parts to the library.

Keit

 

Impressive job cockeyedcowboy

 

Examples

The code blocks below are only examples of some uses of the StringIsTime() function. These were taken form some past projects. They are not matterial to the Time Module in itself, they are just examples of usage.

The StringIsTime() function can be used independently of any other part of the Time Module Library. As in the first example here we are only checking to see if the user has inputed a viable time as an external input. If it doesnot recognize the string to contain an input of a time string it will not validate the input. Using the function in This fastion should be done in the int() function. The check for DataValidated is then done in the start() function to either run or end the code.

All time inputs are to be in the form of hh:mm as in "13:15". Inputs and any other time that is hard coded should also be in GMT as the rest of the time module will assume this is the case. The use of only one time base, will keep eveyone on the same standard no mater what time they are in or there broker is located.

if( ! StringIsTime( SetFridayClose )) {

Print( "DATA ENTRY ERROR: The Time Entered For Friday Close Is Not ",

"Recongized As A Viable Time Input. Recheck Your Time Entry." );

DataValidated = False;

} // End If, StringIsTime:

[/code]

The next code block test for a time string and if it is a valid time, the remainder of the block would be preformed. In this example we are initializing varibles to contain proper time elements. The InitializeTime(); function shown here, takes the time input and converts it from a string input to a time varible. This function can be used not only to convert a string to a time varible but convert it from GMT to either the correct broker or local time equelivents. It can also be used to adjust for other events, as in OrderExpirs, below. This will be the next function to be added to the time module.

if( StringIsTime( TradeDayEnds ) && StringIsTime( SetStartOfDay ) ) {

TradeQuitingTime = InitializeTime( TradeDayEnds, GMTime );

LocalQuitingTime = InitializeTime( TradeDayEnds, LocalTimeOffSet );

BrokerQuitingTime = InitializeTime( TradeDayEnds, DealerTimeOffSet );

OrderExpires = InitializeTime( TradeDayEnds, DealerTimeOffSet - AdvanceClosing );

} // End If, StringIsTime:

The last example code is a little more complex as it not only verifys the data and converts string time to datetime varibles as in the two examples above. It contains a few branching statements. It first test for StringIsTime(), if its not a time string it branches to the last else statement to exit the code block. Now if the user input was 'OFF' it would as well, branch to the last else statement and turns off this trade session. Just as it would if given a non-valid time string but without triggering any error message. Off used as an input here is not case dependent. (off OFF ofF oFf all will work). There is a second test in the code block to see if the trade session time has already past for the day by checking the trade time agains TimeCurrent(). If the trade time is smaller then TimeCurrent it will turn the session off with a notice.

[code]

if( StringIsTime( TradeAsianSession )) {

Increment( SessionCount );

SrartOfAsianBox = InitializeTime( TradeAsianSession, DataWindowDepth );

OrderTimeAsianSession = InitializeTime( TradeAsianSession, DealerTimeOffSet );

LocalTimeAsianSession = InitializeTime( TradeAsianSession, LocalTimeOffSet );

if( OrderTimeAsianSession > TimeCurrent() ) {

TradeKey[ AsianSession ] = ON;

Increment( NumberOfSessions );

Accumulate( MaxOpenPositions, Two );

if( FirstTradeOfDay == Empty ) { FirstTradeOfDay = OrderTimeAsianSession; }

} else { // this block will turn off the sessions if time has already passed.

TradeKey[ AsianSession ] = OFF;

Print( "NOTICE: The Asian Trade Start Time Has Past, It ",

"Will Not Be Traded Today." );

} // End If, OrderTimeAsianSession:

} else { // heres were the code branches too if OFF is incountered as user input

TradeKey[ AsianSession ] = OFF;

Print( "NOTICE: The Asian Trade Session Has Been Turned OFF." );

} // End If, StringIsTime:

NOTE: The above code was supplied as examples and were not re-writen to comform to MQ4 standards.

The next post we will be adding the InitializeTime() function to the Time Module Library.

Keit

 

Time zones and EA

Hi all!

I am looking for a function to change the timezone in a EA automatically according to the server (trader) with which I connect, there is any? I have a EA that operates at a certain time in a server, but the switch to server like adjust the time difference, there is some way?

TIA!

 

Moved your post to this thread.

The other related threads:

Indicator which is showing the broker's server time. The post with indicator https://www.forex-tsd.com/forum/debates-discussions/116-something-interesting-please-post-here/page5#comment_95467

https://www.mql5.com/en/forum/177362

https://www.mql5.com/en/forum/176409

https://www.mql5.com/en/forum/172885

https://www.mql5.com/en/forum/177332

Help to know GMT time on the chart all the times: the post https://www.mql5.com/en/forum/177102/page8

https://www.mql5.com/en/forum/general

The other post: https://www.mql5.com/en/forum/173060/page74

 

Hello,

Thank you for your responses and for all these links.

 

date and time switch on off

hi,

can you give me sample script or EA to start EA on monday set time and close friday set time.

TQ

 
 

StringIsTime(); function

The first function is an input verification function. It is very important to verifiy all user imputs not just time imputs but all. I have tried out some codes writen by others, were no checks were made of the user inputs. I have had some code peg my CPU at 100% due to over limit inputs. The only way to regain control was to force the computer down.

The need to check inputs becomes more important when the auther gives very little documentation on how a peice of code is to work. In some cases the code is so scriptive that its not easily readable giving the user no indications as to what the inputs are for.

I document all my work at the source. Each EA, Indicator, and script has its own documentation division at the end of the code. I prefer the end instead of the begining as it is out of the way. On the other hand, all my functions, that are located in external files are documented just ahead of the functions code block itself. I also code in a very readable format which aids in understanding. The computer doesnot care about your coding style as long as theres no errors. However the more readable your code is, the easier it will be to understand, change and maintain it at a later date. Some will think this is over kill, maybe it is. I bring this up here so that you will understand my style of coding.

StringIsTime() function:

This first functions in this module does just that, it checks if the user has given an actual time in the form of a string. It can be used to redirect execution within the main code on error or if the user has input the string "OFF" instead of a time sections of the code can be turned off or bypassed with out generating any error messages. In checking the user input one can redirect the flow of the program when errors are encountered or the users intent is to turn off a code section.

/*===============================================================================================|\

|| ||

|| String Is Time Function Procedure v1.10p Date 10.30.2007 KTL ||

|| ||

|| Returns: True if the string contains a viable time value or False if it doesnot. Will ||

|| return False if the imput is 'OFF' but will not generate any error messages. ||

|| ||

|| purpose: To Check if string input contains a viable time inputed by the user. The format ||

|| of the string must be in the form of [ 00:00 ] / [ HH:MM ]. If any other form ||

|| of time input is given the function return will be 'False'. ||

|| ||

|| Parameters: InString -- Is the string to test for, as time. ||

|| ErrorOutPut -- This is to turn on or off error reporting (on by default) This ||

|| way you can use this function as a warning of a possible bad input or in ||

|| an IF() statement to control program execution on bad user input string. ||

|| ||

|| Note: It is assumed at first the input is False till proven otherwise. The first test ||

|| is on the length of the string if its not 3 or 5 Characters long then return ||

|| is FALSE. If the length is 3 characters it tests to see if the input is 'OFF'. ||

|| Case is not relavent. This is a means to turn off a certain time function with ||

|| a 'off' user input. If the string length is equal to five then a check is made ||

|| to see if all the characters are numbers or the colon. If anything else it will ||

|| trigger an error message. A check is made to see if the center character is a ||

|| colon. The final two test are to see if the hour part of the string in between ||

|| 0 and 23, and the last test is on the minute part to see if its between 0 and ||

|| 59 if these are not true, it will trigger an error. On the occurance of the ||

|| first error the routine will exist without completing all its check points. ||

|| ||

|| Version 1.02 Was improved. Changes were made to cover all posible wrong time ||

|| inputs, the older version let some errors get by. ||

|| ||

|| Version 1.10 This version also has the ability to input the String 'OFF' to ||

|| stop or cancell an opperation with the use of an IF() statement in your code. ||

|| Off will not generate an error message. Its intended to use as a swithch to ||

|| turn off a time function in your code with an extern user input. ||

|| ||

|| version 1.10p is a public release which should work on any MQ4 code b213 and ||

|| up. Other versions require the use of the Lore Script Engine to run. ||

|| ||

|| Example: Command line StringIsTime( string [of time], bool [error reporting] ); ||

|| ||

|| StringIsTime( "12:00", False ); Function returns true, error reporting has ||

|| been turned off. ||

|| ||

|| StringIsTime( CheckTime ); If varaible 'CheckTime' equals a real time input ||

|| the fruction reterns true. If 'CheckTime' is not a viable time input it ||

|| will return false, and will report the error to your expert journal log. ||

|| If 'CheckTime' = OFF (in any case), than you will be able to turn off ||

|| different parts of your code with the use of external user input parameters. ||

|| ||

\|-----------------------------------------------------------------------------------------------*/

bool

StringIsTime( string InString, bool ErrorOutPut = True ) {

int Index = 0;

int Character = 0;

int Length = 0;

int HourPart = 0;

int MinutePart = 0;

bool Seccessful = False;

Length = StringLen( InString );

switch ( Length ) {

case 3:

ErrorOutPut = False;

for( Index = 0; Index < Length; Index = Index + 1 ) {

Character = StringGetChar( InString, Index );

if( Index == 0 &&

( Character != 79 && Character != 111 ))

ErrorOutPut = True;

if(( Index == 1 || Index == 2 ) &&

( Character != 70 && Character != 102 ))

ErrorOutPut = True;

} // End For Loop, Index:

break;

case 5:

HourPart = StrToInteger( StringSubstr( InString, 0, 2 ));

MinutePart = StrToInteger( StringSubstr( InString, 3, 2 ));

if( HourPart 23 ) { break; }

if( MinutePart 59 ) { break; }

for( Index = 0; Index < Length; Index = Index + 1 ) {

Character = StringGetChar( InString, Index );

if( Character 58 )

break;

if( Index == 2 && Character != 58 )

break;

} // End For Loop, Index:

Seccessful = True;

break;

} // End Switch Case, Length:

if( ! Seccessful && ErrorOutPut )

Print( " WARNING: Time Input [ ", InString, " ] does ",

"not check out to be a string containing a time variable." );

return( Seccessful );

} // End Function Procedure, StringIsTime:

The code block above is in the Include file attached. It is easier to create an include file first and then transfer the code to a compiled library.

I will give some examples of the functions usage in the next post.

keit

Reason: