PureBytes Links
Trading Reference Links
|
> And what was the first trading day this year? The 4th?
> Let's do it this way:
> Input:
> FirstDat(1000104);
> If Date = FirstDat then begin
How about we use a variant of the same trick I use for finding the
first bar in a new trading week?
if Month(Date) < Month(Date[1]) then begin ...
Works for all years, with no inputs.
> -- Shawn wrote:
> > Just wondering is there a way of just specifying the
> > number of days back the indicator should use (say
> > 360) as one does with moving averages,
>
> Sure...
> You could do it this way:
> If DayCount = NumDays then begin
> UpDays = 0;
> DnDays = 0;
> DayCount = 0;
> End
Sorry, no. That just zeroes everything out every 360 days. So if
you happen to be trading on the 361st day after this indicator
started, you will have no up/down data.
You need a sliding window of up/down data, so you can compute the
total for the most recent 360 days. I don't think there's any way to
do it without keeping 360 days of up/down data around.
The simplest (but not the most efficient) way to do it would be to
have a lookback of 360 bars, and do something like this:
UpDays = 0; DnDays = 0;
for DD = 0 to NDays-1 begin
If C[DD] > C[DD+1] then UpDays = UpDays + 1;
If C[DD] < C[DD+1] then DnDays = DnDays + 1;
...etc
You could get a little fancier and do something like
Up = 0; Dn = 0;
if C > C[1] then Up = 1;
if C < C[1] then Dn = 1;
UpDays = summation(Up,NDays);
DnDays = summation(Dn,NDays);
...but that's not much more efficient. Probably less.
If efficiency is a concern, you can do something like this. I've got
to run so I'm not going to test & verify this, but the idea is to
keep the running 360-day total in a variable and then each day you
update it by adding in the current up/dn day info and subtract out
the up/dn day info from 361 days ago. Over time this will result in
some arithmetic error as the low-precision EL variables accumulate
error, but for the small integral values we're talking about here it
shouldn't be a problem.
Up = 0; Dn = 0;
if C > C[1] then Up = 1;
if C < C[1] then Dn = 1;
{ Build up the 360-day sum for the first 360 days }
if CurrentBar <= NDays then begin
UpDays = UpDays + Up;
DnDays = DnDays + Dn;
end
else begin
{ Update the 360-day sum for days after the first 360 days }
UpDays = UpDays + Up - Up[NDays];
DnDays = DnDays + Dn - Dn[Ndays];
end;
The main drawback of all these approaches is that they require an
NDays-long lookback period. If you have a lot of variables in your
system/indicator, that means you have to store a 360-long history of
all of them, which could get expensive. If that's a problem, you
could store the Up/Dn data in two arrays, and refer back to those
instead of using Up[Ndays] and Dn[Ndays]. I leave that one as an
exercise for the interested student. :-)
Gary
|