Someone asked me to share
the code I got running to calculate non-continuous standard deviations.
You may recall that my application was to find the StDev of any month in which
the total gain for the month was positive.
I made an extract of the
code for your use and education, although I must warn that while it calculates
the numbers correctly for the symbols and ranges I have tested it on, it
exhibits some behavior that I am unable to explain. I am nervous about code with
unexplained results, even if the calculations I am interested in are produced
accurately. I will describe this problem in a
moment.
The code, copied below,
will produce the correct calculations for daily symbols and will determine the
StDev for days where the one day ROC is positive and the same for when it is
negative. I have left all of the intermediate calculation columns visible
in the AA window for your inspection. You can obviously delete these or hide
them and only display the data you want to see. You can obviously change
the non-continuous variable from the one day ROC.
The interesting and
unexplained happening requires a look into the code. For those interested,
if you look under the loop section for the negative ROC calculation, you will
see a variable line like this:
NumDn[FirstBar -1] =
0;
This (obviously)
initializes the array NumDn. However, NumDn is not used anywhere in any
subsequent line of calculation. But!! If I delete this line or
comment it out, then the calculation for GainDnSq (initialized in the very next
line) disappears and its array becomes {Empty}. Reinstate the
NumDn variable and the calculation for GainDnSq completes as it should. I
have not discovered the answer to this puzzle and those of you who see it, let
me know if you figure it out. It may have to do with cached values and
variables in memory.
Anyway, here is the
code: It is a standalone code and should run (watch out for line breaks in
your email window). Do a symtax check before trying to run
it.
Ken
// NonContinuousStDev.afl
KSC
// Purpose: to calculate the
StDev of non-continuous data such as all of the
// positive daily ROC
values
// Establish a testing
period
StartDate =
ParamDate("StartDate","12/31/2007");
EndDate =
ParamDate("EndDate","06/30/2008");
BIR =
IIf(Status("BarInRange") > 0, 1, 0);
FirstBar =
LastValue(ValueWhen(Status("FirstBarInRange") >
0, Cum(1)));
LastBar =
LastValue(ValueWhen(Status("LastBarInRange") >
0, Cum(1)));
AllDates =
DateNum();
Per1 = BarsSince(AllDates == StartDate)+1;
// Calculate the
non-continuous data
Gain =
ROC(C,1);
UpDays =
Sum(IIf(Gain > 0, 1, 0),Per1);
DnDays =
Sum(IIf(Gain < 0, 1, 0),Per1);
PCUp = UpDays / (UpDays +
DnDays);
// Routine to calculate
StdDev of Positive Monthly Gains
GainUpSq[FirstBar-1] =
0;
GainUpSum[FirstBar-1] =
0;
for (k=FirstBar-1; k < LastBar ; k++
)
{
if
(Gain[k] > 0)
{
GainUpSum[k] = GainUpSum[k-1] +
Gain[k];
GainUpSq[k] = GainUpSq[k-1] +
(Gain[k])^2;
}
else
{
GainUpSum[k] = GainUpSum[k-1];
GainUpSq[k] = GainUpSq[k-1];
}
}
A = (GainUpSq) /
UpDays;
B = (GainUpSum /
UpDays)^2;
SdUp = sqrt(A -
B);
// Routine to calculate
StdDev of Negative Monthly Gains
// k =
BarNo;
NumDn[FirstBar-1] = 0;
GainDnSq[FirstBar-1] = 0;
GainDnSum[FirstBar-1] = 0;
for
(k=FirstBar-1; k < LastBar ; k++
)
{
if (Gain[k] <
0)
{
GainDnSum[k] = GainDnSum[k-1] -
Gain[k];
GainDnSq[k] = GainDnSq[k-1] + (Gain[k])^2;
}
else
{
GainDnSum[k] = GainDnSum[k-1];
GainDnSq[k] = GainDnSq[k-1];
}
}
AD = Nz((GainDnSq) / DnDays);
BD =
Nz((GainDnSum / DnDays))^2;
SdDn =
sqrt(Nz(AD -
BD));
PercentUpDays =
100 * (UpDays /
(UpDays + DnDays));
Filter =
Status("lastBarinRange");
Filter =
1;
Cd = colorDefault;
AddColumn(C,"Price",1.2,Cd,Cd,80);
AddColumn(Gain,"Gain",1.2,Cd,Cd,80);
AddColumn(Per1,"TotalDays",1.0,Cd,Cd,90);
AddColumn(UpDays,"UpDays",1.0,Cd,Cd,90);
AddColumn(100 *
PCUp,"PCentUpDays",1.2);
AddColumn(GainUpSum,"GainUpSum",1.2,Cd,Cd,110);
AddColumn(GainUpSq,"GainUpSq",1.2,Cd,Cd,110);
AddColumn(A,"A",1.2,Cd,Cd,110);
AddColumn(B,"B",1.2,Cd,Cd,110);
AddColumn(SdUp,"Stndev Up
Gains",1.2,Cd,Cd,120);
AddColumn(DnDays,"DnDays",1.0,Cd,Cd,90);
AddColumn(GainDnSum,"GainDnSum",1.2,Cd,Cd,110);
AddColumn(GainDnSq,"GainDnSq",1.2,Cd,Cd,110);
AddColumn(AD,"AD",1.2,Cd,Cd,110);
AddColumn(BD,"BD",1.2,Cd,Cd,110);
AddColumn(SdDn,"Stndev
Down Gains",1.2,Cd,Cd,120);
__._,_.___
Please note that this group is for discussion between users only.
To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
For other support material please check also:
http://www.amibroker.com/support.html
__,_._,___
|