viewtopic.php?f=3&t=474

Here are some examples of date arithmetic in irregular workfiles.

Again, I'll assume that we are working in a daily workfile, although this time I'm assuming some days may not be included in the workfile (typically because of holidays).

I'll start with some simple examples, then tackle a quite difficult example.

First, we can easily calculate the number of days between this observation and the previous observation in the workfile:

Code: Select all

`show @date-@date(-1)`

Next, we can create a dummy variable for the first day of each month included in the workfile

Code: Select all

`show @datefloor(@date, "m")<>@datefloor(@date(-1), "m")`

Note that this version will return an NA for the first observation in the workfile. An alternate approach that always returns a 1 for the first observation in the workfile is:

Code: Select all

`show @day=@minsby(@day,@datefloor(@date(-1), "m")) `

The same approach can be used to create a dummy variable for the last day of each month. Either:

Code: Select all

`show @datefloor(@date, "m")<>@datefloor(@date(+1), "m")`

or

Code: Select all

`show @day=@maxsby(@day,@datefloor(@date(-1), "m"))`

Another calculation that can be useful is the number of observations in the workfile within the month containing this observation:

Code: Select all

`show @sumsby(1, @datefloor(@date, "m"))`

We can also calculate the number of observations from the beginning of the month:

Code: Select all

`show @obsid-@minsby(@obsid, @datefloor(@date,"m"))`

Or the number of observations until the end of the month:

Code: Select all

`show @maxsby(@obsid, @datefloor(@date,"m"))-@obsid`

Now, to deal with a fairly difficult example, let's say that we needed to calculate the number of trading days remaining before the next option expiration date, where options expire on the third Friday of every month which can be a holiday.

The first step is to calculate the weekday number of the first day of the current month:

Code: Select all

`show @datepart(@datefloor(@date,"m"),"w")`

We can use this (in a somewhat cryptic way) to calculate the day number of the first Friday in the month:

Code: Select all

`show (8-@mod(@datepart(@datefloor(@date,"m"),"w")+2,7))`

It is trivial to shift this forwards to the third Friday in the month:

Code: Select all

`show (8-@mod(@datepart(@datefloor(@date,"m"),"w")+2,7))+14`

Now, we can create a dummy variable for the last day included in the workfile on or before the third Friday in the month:

Code: Select all

`series lastday = (@day<=(8-@mod(@datepart(@datefloor(@date,"m"),"w")+2,7))+14 and @day(+1)> (8-@mod(@datepart(@datefloor(@date,"m"),"w")+2,7))+14)`

(Although note that this assumes that there is at least one day in the workfile *after* the third Friday of the month. If not the second condition would also be satisfied if @month<>@month(+1))

Finally, we can calculate the number of observations remaining in the workfile before the next option expiration day:

Code: Select all

`show @maxsby(@obsid,@cumsum(lastday))-@obsid`