Hello,

I'm trying to calculate Van Tharp's SQN as a custom backtest metric.

I don't believe it's the best objective function but it's more a way to learn AFL and AB.

This code doesn't work because I guess I have a problem calculating the standard deviation.

You can see that I calculate it on an array "trades" that I manually created to be sure it's properly populated.

Problem is that "stdrmultiples" variable is always 0.

Do you see any problem in my syntax there that would prevent the proper calculation of the standard deviation on array "trades"?

```
/* Now custom-backtest procedure follows */
if( Status("action") == actionPortfolio )
{
_TRACE("!CLEAR!");
bo = GetBacktesterObject();
bo.Backtest(1); // run default backtest procedure
st = bo.GetPerformanceStats(0); // get stats for all trades
averageRisk = st.GetValue("LosersAvgLoss"); // Average loss = 1R
SumProfitPerRisk = 0;
NumTrades = 0;
// iterate through closed trades first
for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
{
// Avg loss = LosersAvgLoss
RMultiple = trade.GetProfit()/ abs(averageRisk); // Rmultipe per trade
SumProfitPerRisk = SumProfitPerRisk + RMultiple; // Summ of all R multiple
trade.AddCustomMetric("R-Multiple", RMultiple );
NumTrades++;
}
trades = 0;
trades[ 0 ] = 1;
trades[ 1 ] = 11;
trades[ 2 ] = 41;
trades[ 3 ] = 21;
_TRACE("trades" + trades[ 3 ]); // 21
expectancy = SumProfitPerRisk / NumTrades; // Average of sum of R multiples = Expectancy.
bo.AddCustomMetric( "Expectancy (per risk)", expectancy );
stdrmultiples = StDev( trades , 4 , False );
_TRACE("stdrmultiples" + stdrmultiples); // 0
MSQN = ( expectancy / stdrmultiples ) * ( sqrt(4) );
bo.AddCustomMetric( "stddev", MSQN );
bo.ListTrades();
}
```