- ago
WL6.x had a bunch of DateTime functions such as:
- DateRules.IsLastTradingDayOfQuarter()
- Calculate.GetRemainingTradingDays()
- OptionExpiryDate
etc. etc.

Are there WL7 equivalents? I see that there's BarHistory/ Members/ DateTimes but it seems to have a different purpose.
0
1,009
15 Replies

Reply

Bookmark

Sort
- ago
#1
1 - There is no IsLastTradingDayOfQuarter in WL7, right. But TradingDaysRemaining supports Quarterly scale. See below on how to find it.
0
- ago
#2
2 - We ported a stable of such handy extension methods to WL7 so both GetRemainingTradingDays and IsOptionExpiry do exist. Being extension method of DateTime (a C# class) they are not documented in the QR though. You can find more equivalents by invoking IntelliSense popup:

0
- ago
#3
Here is a convenient selection of DateTime extensions which are self-descriptive:
CODE:
public static DateTime PreviousTradingDay(this DateTime dt, MarketDetails md) public static DateTime FirstDayOfMonth(this DateTime dt) public static bool IsOptionExpiry(this DateTime date) public static bool IsWeekend(this DateTime dt) public static bool IsHoliday(this DateTime dt, BarHistory bars) public static int CalendarDaysBetweenDates(this string fromDate, string toDate) public static int CalendarDaysBetweenDates(this int fromDate, int toDate) public static int CalendarDaysBetweenDates(this DateTime fromDate, DateTime toDate) //next trading date / day from a bar / date public static DateTime GetNextTradingDate(this BarHistory bars, int bar) public static DateTime GetNextTradingDate(this BarHistory bars, DateTime dt1) public static int GetNextTradingDay(this int bar) public static DateTime AddTradingDays(this BarHistory bars, int bar, int days) public static DateTime SubTradingDays(this BarHistory bars, int bar, int days) public static int TradingDaysRemaining(this BarHistory bars, int bar, HistoryScales interval) public static int GetRemainingTradingDays(this DateTime DateFrom) public static int TradingDaysElapsed(this BarHistory bars, int bar, TradingDayChoices interval) public static int TradingDaysBetweenDates(this BarHistory bars, DateTime dt1, DateTime dt2) public static bool TomorrowIsLastTradingDayOfWeek(this BarHistory bars, int bar) public static bool IsATradingDay(this DateTime dt, BarHistory bars) //returns 24-hour time as an integer. Example: 4pm = 1600 public static int GetTime(this DateTime dt) //use the Bars overload if you need GetTime for non-synchronized Bars public static int GetTime(this BarHistory bars, int bar)


Sample usage

The "this" indicates the variable type on which a method operates e.g.

CODE:
//public static DateTime FirstDayOfMonth(this DateTime dt) var dt = bars.DateTimes[idx].Date.FirstDayOfMonth();
0
- ago
#4
Phew, thanks!! Will check them out, may take a while.

Is there a way to view the list of Holidays (for US markets) in the debug window?
0
- ago
#5
Sure, you will find it in the QuickRef > MarketDetails > HolidayDates

The sample code is universal so to zero in on the U.S. market, change it like this:

CODE:
//_holidays = bars.Market.HolidayDates; _holidays = MarketManager.UsaStocks.HolidayDates;
0
- ago
#6
👍
0
- ago
#7
Couldn't find OptionExpiryDate. Kindly consider adding it, and the related NextOptionExpiryDate, just as they were in WL6, it made finding these dates so much easier.
Also, can you please make the debug window separate as that would make viewing the output concurrent with running a code so much easier instead of having to keep switching tabs.
Thanks.
0
- ago
#8
QUOTE:
Couldn't find OptionExpiryDate.

Please find its equivalent listed in Post #3 above.
0
Cone8
 ( 24.99% )
- ago
#9
Well get NextOptionExpiryDate in the next build.

.. although I noticed that the WL6.9 doc said that it would return the following market day if the the Friday expiry is a holiday. That's wrong. It should be (and will be) the Thursday before the Friday holiday.
1
- ago
#10
Good to hear NextOptionExpiryDate will be reappearing!

I created this code to plot Year-To-Date change:
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript3 {    public class YTDchg : TimeSeries    {       private BarHistory bars;       private TimeSeries ts;       public YTDchg(BarHistory bars, TimeSeries ts, string description) : base(bars.DateTimes, true)       {          this.bars = bars;          this.ts = ts;          int PYbar = -1;          for (int i = ts.FirstValidIndex + 1; i < ts.Count; i++)          {             if (bars.TradingDaysRemaining(i-1, HistoryScales.Quarterly) == 0                && bars.DateTimes[i-1].Month == 12)                PYbar = i-1;             if (PYbar < 0)                base[i] = 0d;             else                base[i] = 100 * (ts[i] /ts[PYbar] - 1);          }       }       public static YTDchg Series(BarHistory bars, TimeSeries ts)       {          string description = "YearToDateChange(" + ts.Description + "), %";          if (bars.Cache.ContainsKey(description))             return (YTDchg)bars.Cache[description];          YTDchg ytd = new YTDchg(bars, ts, description);          bars.Cache[description] = ytd;          return ytd;       }    }        //============================================================    public class Change : UserStrategyBase {           //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) {          string Symbol = bars.Symbol;          TimeSeries cls = bars.Close;          // YTD change          TimeSeries YTD = YTDchg.Series(bars, cls);          //PlotTimeSeries(YTD, YTD.Description, "pYTD", Color.Red, PlotStyles.Line, false); // doesn't give description          PlotTimeSeries(YTD, "YearToDateChange(" + cls.Description + "), %", "pYTD", Color.Red, PlotStyles.Line, false);       } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { } //declare private variables below } }


Issues:
- When I use the <Series>.Description while plotting it doesn't plot the name; what am I doing wrong?
- How do I adjust the Pane size?
- How do I plot this *above* the Price pane?
0
- ago
#11
Firstly, WL7 has a built in YearlyReturns property already. And since Post #10 is not related to "WL6 Date/Time functions" directly let's start a new discussion if necessary.
0
- ago
#12
Heads-up for Build 7:

1. New additions
2. Breaking changes to the syntax of functions in Post #3
3. Robert has documented them the QuickRef
0
- ago
#13
Thanks for the heads up.

Hope you'll consider adding:
- Year-to-Date Change (%)
- Quarter-to-Date Change (%)
0
- ago
#14
QUOTE:
- Year-to-Date Change (%)

It's already been part of WL7 as TimeSeries.ReturnYTD.
1
- ago
#15
YTD change plots ok but doesn't give any description.
Test code:
CODE:
TimeSeries YTD = bars.Close.ReturnYTD(); PlotTimeSeries(YTD, YTD.Description, "pYTD", Color.Red, PlotStyles.Line, false); // gives no description
0

Reply

Bookmark

Sort