DateTimeFunctions_US for Community testing
Author: Sammy_G
Creation Date: 10/28/2010 3:29 PM
profile picture

Sammy_G

#1
Precise calculation of dates and periods is important for proper backtesting. Towards that goal, here is a list of useful Date-related functions, either translated from WL4 (Date Study VI by Fundtimer) or from WL.NET (coded by Cone and/or Eugene). The WL4 functions are those that I had personally used and found useful in my trading; hopefully, you will find them useful, too. From WL.NET, there are only two: DaysBetweenDates & TradingDaysBetweenDates. DaysBetweenDates has been renamed to CalendarDaysBetweenDates in order to avoid any ambiguity about what its actually calculating (Calendar or Trading days), and a third method has been added that takes DateTime inputs. TradingDaysBetweenDates was made a bit more user-friendly in that the DateTime inputs clarify the 'from' and 'to' dates. Also, I strongly believe all date-related functions should be included together rather than scattered over various places. By locating them here inside a function named "DateTimeFunctions", it will be intuitive for users to find them here. For good measure, I also threw in a new function: WasFullTradingDay; this determines if any bar had a shortened session.

I decided to add the suffix _US to the name as its quite likely that not all the holidays practiced in the US are observed worldwide, and vice versa. Nevertheless, even if you live outside the US but trade US securities, you may find these useful.

Limitations: Some of the functions rely on the Bars.MarketInfo property, which is obtained from the Markets.xml file. Currently, it has information on Holidays from the period Jan 2004 to Dec 2020, and on shortened sessions from 2004 to 2009. This means the calculation of Trading days will be accurate within this time span as that needs Holiday dates, a MarketInfo.Holidays property. Similarly, WasFullTradingDay function will only be usable for the 2004-2009 time period. Hopefully, some day the Markets.xml file would be easily updatable, or Fidelity will update it periodically and we won't have to worry about it. Of course, a user can update the file manually, too. Note that the calculation of Calendar days is not affected by this file.
==================================================

Dear Cone and Eugene,
There are some other date-related functions scattered elsewhere inside WealthScript e.g. Days To/From Option Expiry Date, located inside WealthLab.Rules; I can include them here, perhaps modifying them to use DateTime instead of 'bar', if you wish. This is not essential, however, as I have given examples of how the same results can be obtained using the included functions.

Dear Community,
While I have tested all the functions and they work fine and give accurate results, there is only so much one individual can do. So I would like you to test the code in various settings and post some feedback, positive or negative (e.g. errors or wrong calculations). Multiple usage examples are provided for illustration. You can create many, many more using your imagination.

Here's the code:
CODE:
Please log in to see this code.
profile picture

Eugene

#2
Thanks for your contribution.
profile picture

Sammy_G

#3
The Markets.xml file lists the upcoming Monday, 1/3/11, as a holiday. Users should note while it may be a holiday in some countries, it is *not* a holiday in the US. Since I mainly trade the US markets, I took the liberty of deleting that entry from my markets.xml file; other users may do whatever they consider appropriate.
profile picture

Eugene

#4
Good point. Our upcoming Market Manager tool, once released, will let redefine holiday schedule(s) for different markets (including but not limited to).

p.s. Happy New Year.
profile picture

Sammy_G

#5
That market Manager tool sounds interesting; can't wait. Wonder if it will let us maintain a markets.xml file for more than one market, just in case its needed.

p.s. Happy New Year (and Happy New Decade) to you, too!
profile picture

Eugene

#6
Yes. From the ground up, the MM tool was designed with international (Developer) users in mind as an aid to work with the global markets, their various start/end times and time zones.
profile picture

Sammy_G

#7
Looks like the Markets.xml file was updated with the recent v6.1 update. Now the Holidays go back to Jan 1990. One thing, though - it appears an unscheduled holiday is missing from the list: 4/27/1994. Pending this fix to the Markets.xml file sometime in the future, users may add this manually; I already did. With this correction, the code above should be accurate for the time period Jan 1990 - Dec 2020.

p.s. Eugene, didn't see the Market Manager tool.
profile picture

Eugene

#8
QUOTE:
p.s. Eugene, didn't see the Market Manager tool.

Because it's not released yet.
profile picture

Cone

#9
Re: 4/27/1994 - What occurred?
profile picture

Sammy_G

#10
President Nixon's funeral.