Try changing the line: MnGainDnSum[k] = MnGainDnSum[k-1] - MnGain1[k]; To: MnGainDnSum[k] = MnGainDnSum[k-1] + MnGain1[k];
Note that by subtracting a negative, you are adding rather than getting more negative. If not intended, this would then throw off the calculation for: BD = (MnGainDnSum / NumDn)^2; SdDn = sqrt(AD - BD); Possibly resulting in an erronous attempt to take sqrt of a negative number? Mike --- In [email protected], "Ken Close" <[EMAIL PROTECTED]> wrote: > > 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); >
