Hi,
You are misusing arrays and array indexing. StDev returns an array and
will use the 'periods' last bars for the calculation at each bar. You
are stuffing all your R values at the beginning of the array, but the
most recent value of StDev would be based on values at the end of the
array. Besides, you are really only interested in a single calculation,
not an array of calculations.
Finally, array size is limited by the number of bars under study. If you
had more trades than bars, your approach would fail due to invalid array
indexing.
Try the following instead:
R = sumR = sumR2 = 0;
for (trade = bo.getFirstTrade(); trade; trade = bo.getNextTrade()) {
R = ...;
trades++;
sumR += R;
sumR2 += R^2;
}
if (trades > 0) {
stdDev = sqrt((sumR2 - (sumR^2 / trades)) / trades);
}
The algorithm above is based on the population version of method 2
published here:
http://www.easycalculation.com/statistics/learn-standard-deviation.php
<http://www.easycalculation.com/statistics/learn-standard-deviation.php>
Mike
--- In [email protected], "davemabe2000" <davem...@...> wrote:
>
> I need to get the standard deviation of a custom metric that I'm
> computing by iterating through each trade in the BacktesterObject. The
> problem is that StDev takes an array.
>
> How do I build an array within the loop to compute the StDev of after
> the loop completes? Here's what I'm doing:
>
> if (Status("action") == actionPortfolio) {
> bo = GetBacktesterObject();
> bo.Backtest(1);
> num_trades = 0;
> all_rmultiples = 0;
> for (trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade()) {
> num_trades++;
> // ... getting r_multiple value here
> all_rmultiples[num_trades-1] = r_multiple;
> }
> rmultiple_stddev = StDev(all_rmultiples, num_trades - 1);
> bo.AddCustomMetric("StdDevRs", rmultiple_stddev);
> }
>
>
> StdDevRs is always blank in the backtest report. Any advice?
>