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.
Size:
Color:
Thanks for your contribution.
Size:
Color:
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.
Size:
Color:
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.
Size:
Color:
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!
Size:
Color:
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.
Size:
Color:
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.
Size:
Color:
QUOTE:
p.s. Eugene, didn't see the Market Manager tool.
Because it's not released yet.
Size:
Color:
Re: 4/27/1994 - What occurred?
Size:
Color:
President Nixon's funeral.
Size:
Color:
Hi Eugene,
I see in the component library that some features are limited to the US Market (ex. DateOfNextTradingDay or GetRemainingTradingDays).
Is it possible to have a version linked with the Market Manager ?
Moreover, is it possible to have a new feature like GetExchange but linked to Market Manager ?
And if you are agree, I'm not against to have a feature to get starting hour and end hour of the trading date ;-). The end hour is especially important for trader which "simulate" Market on close in their real time strategies.
Size:
Color:
Alexandre, you're the first to ever ask for extending this class. Sorry but it would be a considerable effort in coding and testing to justify its limited appliance to the real world tasks. Don't forget that it would be just a single-user feature without a collection of Market Manager presets for various international markets and a verifiable history of holidays and special hours for each of them. Who's in charge of this job? (rhetorical question)
"GetExchange linked to Market Manager" doesn't make sense. While it worked, it polled a Google web service and returned its output for a symbol in any U.S. or international market alike. That's all to it. Sadly enough, it's broken as a side effect of Google Finance's redesign. Now we have to look for replacements.
As for the start/end hour of the trading date, this is a built-in feature. Please check out the QuickRef (MarketInfo Class), namely OpenTimeNative and CloseTimeNative properties. (Just let's not discuss it here in this topic.)
Size:
Color:
Thank you for your answer. In my mind, the administration of the configuration of the market manager tool was under the user responsibility. I cann’t imagine another way as there are too many markets all round the world :-).
Thanks to you, I see in the QuickRef (MarketInfo Class) that Ican retrieve everything stored in the tool. With this, I will be able to simulate the same function than DateOfNextTradingDay, GetRemainingTradingDays or GetExchange.
Thanks a lot for your help !
Size:
Color:
That's the least I can do. And the power of open source. ;)
Size:
Color: