PureBytes Links
Trading Reference Links
|
> How could I write a function, so that I can refer to trading days
> not with the date but with the rank it has in the year, such as the
> first, second, third trading day of the year?
Someone else may know how to do this, but I don't see how you could
compute it. There are too many variables -- market holidays, etc --
to express it in a nice formula.
I can think of two ways to accomplish it, both ugly.
1. Record the date as you go. This lets you know that N bars ago
was the 43rd trading day or whatever, but you can't ask "what was the
Nth trading day of 1999."
if Year(Date) <> Year(Date[1]) then TradeDate = 1;
if Date <> Date[1] then TradeDate = TradeDate + 1;
Then just refer to TradeDate[N] to find the value for N bars ago. If
you're trading a daily chart, that will be the value for N trading
days ago. But I suspect that's not what you're looking for.
2. Store a list of market holidays. Since vanilla TS has no way to
read data from a file, you'd either have to initialize an array with
the holidays in your function, or get a DLL that lets you read files.
Then, given that list of holidays, you could do something like this:
{ Function NthTradingDay }
Inputs: StartYr(Numeric), TrdDay(Numeric);
{ StartYr = 99 for 1999 for pre-build23 TS4 }
Arrays: MktHolidays[50](99999999);
{ Initialize MktHolidays with the Date of all holidays, e.g.: }
if CurrentBar = 1 then begin
MktHolidays[0] = 960101; { New Year }
MktHolidays[1] = 960527; { Memorial day }
MktHolidays[2] = 960704; { Independence day }
{ etc }
end;
{ Count the trading days }
Ndays = 0;
HolidayIdx = 0;
StartDay = DateToJulian(10000*StartYr + 0101);
DayOffset = -1;
while Ndays < TrdDay begin
DayOffset = DayOffset+1;
MyDate = JulianToDate(StartDay + DayOffset);
if DayOfWeek(MyDate) >= 1 and DayOfWeek(MyDate) <= 5 then begin
{ Is this a holiday? Find next holiday >= MyDate }
while MktHolidays[HolidayIdx] < MyDate begin
HolidayIdx = HolidayIdx + 1;
end;
if MyDate <> MktHolidays[HolidayIdx] then Ndays = Ndays + 1;
end;
end;
NthTradingDay = MyDate;
Thus you could call NthTradingDay(99, 6) to get 990111. Note that
you will have to change things around a bit to get this to work with
the Y2K-compliant TS. I think the StartYr value is OK if you use the
2001 = 101 TS year convention, but you'll also have to use 4-digit
years for the MktHolidays array.
Warning, this code is off the top of my head and hasn't been verified
or tested. I offer it as a starting point that should be close to
what you need, but I don't promise it will work in its current form.
Which is not too bad considering what you paid for it. :-)
Gary
|