PureBytes Links
Trading Reference Links
|
I posted this to the code-list also. It converts from calendar
days to trading days (ie, market bars to time aligned bars).
> Date: Thu, 22 Apr 1999 17:44:45 -0700
> To: code-list@xxxxxxxxxxxxx
> Subject: CL_EL code to convert calendar days to trading days
I've been experimenting a bit with various systems, and substituting
calendar days in as the input, instead of trading days. For example,
we generally know that if there are no holidays that 28 calendar
days corresponds to 20 trading days, but that if we introduce
holidays, (or extra trading days such as Saturdays) then the
correspondence between trading days and calendar days changes.
And if the number of calendar days we're interested in is not
a multiple of 7, the correspondence requires some extra work to
calculate.
Here's the code for a function that accepts as input, the number
of calendar days, N, and returns the closest number of trading days
that corresponds to N, where in the event that the day would fall
on a weekend, that the next lowest trading day is used.
{
function: calbar(N) returns the number of trading days (ie, bars)
that correspond to the day that is N calendar days ago.
Copright 1999, Gary Funck (gary@xxxxxxxxxxxx)
}
input: n(numericsimple);
var: x(0), y(0), xl(0), yl(0), xh(0), yh(0), yn(0), cnt(0);
xl = 0;
yl = datetojulian(Date[N]);
xh = N;
yh = datetojulian(Date[0]);
yn = yh - N;
x = IntPortion(xl + (xh - xl) * (yn - yl) / (yh - yl));
y = datetojulian(Date[N - x]);
{ print(N, yl, yh, yn, x, y); }
cnt = 0;
while ((y <> yn) and (x <> xl) and (cnt < 10)) begin
{ print(cnt, x, y, xl, yl, xh, yh); }
if y <= yn then begin
xl = x;
yl = y;
end else begin
xh = x;
yh = y;
end;
x = IntPortion(xl + (xh - xl) * (yn - yl) / (yh - yl));
y = datetojulian(Date[N - x]);
cnt = cnt + 1;
end;
calbar = N - x;
Generally the while loop above will terminate within 3 iterations,
and often, will not be executed at all, so this function will
execute quickly. The check for (cnt < 10) is just a safequard
in case bogus values are passed into the function, or there is
something weird in the input dates.
Here's a simple example of how to use this function:
var: nbars(0);
{ trade the approximate Lunar cycle of 29 calendar days }
nbars = calbar(29);
if c[nbars] >= c[nbars + 1] then buy else sell;
--
| Gary Funck, Intrepid Technology, gary@xxxxxxxxxxxx, (650) 964-8135
|