EasyLanguage Includes a Powerful String Manipulation Library
I thought I would share this function. I needed to convert a date string (not a number per se) like “20010115” or “2001/01/15” or “01/15/2001” or “2001-01-15” into a date that TradeStation would understand. The function had to be flexible enough to accept the four different formats listed above.
Most programming languages have functions that operate strictly on strings and so does EasyLanguage. The most popular are:
- Right String (rightStr) – returns N characters from the right side of the string.
- Left String (leftStr) – returns N character starting from the left side of the string
- Mid String (midStr) – returns the middle portion of a string starting at a specific place in the string and advance N characters
- String Length (strLen) – returns the number of characters in the string
- String To Number (strToNum) – converts the string to a numeric representation. If the string has a character, this function will return 0
- In String (inStr) – returns location of a sub string inside a larger string ( a substring can be just one character long)
Unpack the String
If the format is YYYYMMDD format then all you need to do is remove the dashes or slashes (if there are any) and then convert what is left over to a number. But if the format is MM/DD/YYYY format then we are talking about a different animal. So how can you determine if the date string is in this format? First off you need to find out if the month/day/year separator is a slash or a dash. This is how you do this:
whereIsAslash = inStr(dateString,”/”);
whereIsAdash = inStr(dateString,”-“);
If either is a non zero then you know there is a separator. The next thing to do is locate the first “dash or slash” (the search character or string). If it is located within the first four characters of the date string then you know its not a four digit year. But, lets pretend the format is “12/14/2001” so if the first dash/slash is the 3rd character you can extract the month string by doing this:
firstSrchStrLoc = inStr(dateString,srchStr);
So if firstSrchStrLoc = 3 then we want to leftStr the date string and extract the first two characters and store them in mnStr. We then store what’s left of the date string in tempStr by using rightStr:
strLength = strLen(dateString);
tempStr = rightStr(dateString,strLength-firstSrchStrLoc);
Here I pass dateString and the strLength-firstSrchStrLoc – so if the dateString is 10 characters long and the firstSrchStrLoc is 3, then we can create a tempstring by taking [10 -3 = 7 ] characters from right side of the string:
“12/14/2001” becomes “14/2001” – once that is done we can pull the first two characters from the tempStr and store those into the dyStr [day string.] I do this by searching for the “/” and storing its location in srchStrLoc. Once I have that location I can use that information and leftStr to get the value I need. All that is left now is to use the srchStrLoc and the rightStr function.
srchStrLoc = inStr(tempStr,srchStr);
dyStr = leftStr(tempStr,srchStrLoc-1);
yrStr = rightStr(tempStr,strLen(tempStr)-srchStrLoc);
Now convert the strings to numbers and multiply their values accordingly.
DateSTrToYYYMMDD = strToNum(yrStr) X 10000-19000000 + strToNum(mnStr) X 100 + strToNum(dyStr)
To get the date into TS format I have to subtract 19000000 from the year. Remember TS represents the date in YYYMMDD format.
Now what do you do if the date is in the right format but simply includes the dash or slash separators. All you need to do here is loop through the string and copy all non dash or slash characters to a new string and then convert to a number. Here is the loop:
Here I use midStr to step through each character in the string. MidStr requires a string and the starting point and how many characters you want returned from the string. Notice I step through the string with iCnt and only ask for 1 character at a time. If the character is not a dash or slash I concatenate tempStr with the non dash/slash character. At the end of the While loop I simply strToNum the string and subtract 19000000. That’s it! Remember EasyLanguage is basically a full blown programming language with a unique set of functions that relate directly to trading.
Here is the function and testFunc caller.