| New signals service! | |
|
|||||||
| Register in Forex TSD! | |
|
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time). Click here to register and get more information |
|
![]() |
|
|
LinkBack (1) | Thread Tools | Display Modes |
|
||||
|
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. Code:
/*===============================================================================================|\
|| ||
|| 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 < 0 || HourPart > 23 ) { break; }
if( MinutePart < 0 || MinutePart > 59 ) { break; }
for( Index = 0; Index < Length; Index = Index + 1 ) {
Character = StringGetChar( InString, Index );
if( Character < 48 && 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 |
|
||||
|
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. Code:
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:
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. Code:
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:
The next post we will be adding the InitializeTime() function to the Time Module Library. Keit |
|
|||
|
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! Last edited by floatingw; 07-08-2008 at 06:28 PM. |
|
|||
|
Moved your post to this thread.
The other related threads: Indicator which is showing the broker's server time. The post with indicator http://www.forex-tsd.com/54827-post68.html Trading Times Enhancement Extend MQL!! Date format function Need procedure to only trade at certain times - Please help Help to know GMT time on the chart all the times: the post http://www.forex-tsd.com/214117-post116.html how to get the real server time or how to tell if the market is open The other post: http://www.forex-tsd.com/211150-post1101.html |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
LinkBacks (?)
LinkBack to this Thread: http://www.forex-tsd.com/lessons/13132-automatic-time-control.html
|
||||
| Posted By | For | Type | Date | |
| Automatic Time Control - Forex Trading | This thread | Refback | 04-05-2008 05:49 AM | |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need EA to control SL | matrixebiz | Expert Advisors - Metatrader 4 | 9 | 08-01-2007 03:48 PM |
| multi-account control using EA | white_tiger | Expert Advisors - Metatrader 4 | 3 | 11-06-2006 03:52 AM |
| Control Points | inwestorthc | Setup Questions | 1 | 10-18-2006 11:27 AM |
| NewsTracker to control EAs around newstime | phampton | Metatrader 4 | 1 | 08-25-2006 08:27 AM |
| Need help about trade time control | DHALSIM | Metatrader 4 | 1 | 08-06-2006 03:27 AM |