How do I discover
these?
I need a loop to calculate
a custom Standard Deviation for monthly returns that are only
positive.
I was successful and came
up with StDevs that matched calculations in Excel.
I used the following loop
routine (first one below). But my problem came when I copied the routine
and changed some variable names in order to calculate StDevs of the monthly
returns that are negative. Same code, same structure, slightly different
variable names. In the case of the negative return routines, the variable
to hold the number of months that have negative values is Empty and stays empty,
and it is not obvious why it stays empty. I have tried all sorts of
variations, including use of TRACE statements and debugview...but no
joy.
I hope someone with loop
and subscripted variable experience will glance into the details and "discover"
why the first routine works and the second one does not.
Thanks.
Positive routine which
works:
In both of these,
monthly returns are represented by MnGain
Sums of gains and squares
of sums of gains are obvious
NoUp is a count of number
of up months
Everything is the same in
the second routine except for name changes to get negative
returns
The MYSTERY is why the
variable NoDn is Empty. An even deeper mystery is why, when using _TRACE
statements,
the indicator in the
debugview window for NoDn is {NAN} instead of {EMPTY}.
Help!
// Routine to calculate StdDev of
Positive Monthly Gains
k = BarNo;
NoUp[FirstBar-1] = 0;
GainUpSq[FirstBar-1] = 0;
MnGainUpSum[FirstBar-1] = 0;
for (k=FirstBar-1; k < LastBar ; k++
)
{
if (MnGain1[k] > 0)
{
MnGainUpSum[k] = MnGainUpSum[k-1] +
MnGain1[k];
GainUpSq[k] = GainUpSq[k-1] + (MnGain1[k])^2;
NoUp[k] = NoUp[k-1] + 1;
}
else
{
MnGainUpSum[k] = MnGainUpSum[k-1];
GainUpSq[k] = GainUpSq[k-1];
NoUp[k] = NoUp[k-1];
}
}
A = (GainUpSq) / NoUp;
B = (MnGainUpSum / NoUp)^2;
SdUp = sqrt(A - B);
Negative return routine
which has NoDn array Empty (or with {NAN} "value" in
debugview
// Routine to calculate StdDev of
Negative Monthly Gains
k = BarNo;
NumDn[FirstBar-1] = 0;
_TRACE("k =
" + WriteVal(NumDn[102],1.0));
GainDnSq[FirstBar-1] = 0;
MnGainDnSum[FirstBar-1] = 0;
for
(k=FirstBar-1; k < LastBar ; k++ )
{
if
(MnGain1[k] < 0)
{
MnGainDnSum[k] =
MnGainDnSum[k-1] -
MnGain1[k];
GainDnSq[k] =
GainDnSq[k-1] + (MnGain1[k])^2;
NumDn[k] =
NumDn[k-1] +
1;
// _TRACE("k = " +
WriteVal(NumDn[k],1.0));
}
else
{
MnGainDnSum[k] =
MnGainDnSum[k-1];
GainDnSq[k] =
GainDnSq[k-1];
NumDn[k] =
NumDn[k-1];
}
}
AD = (GainDnSq) /
NumDn;
BD = (MnGainDnSum /
NumDn)^2;
SdDn = sqrt(AD -
BD);
__._,_.___
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
__,_._,___
|