Trying to use stdev in a custom metric - I get no output from stdev but others work

Below is my code for a custom metric. It works except the part where I try to get the standard deviation of my list of trades. This gives me no output at all. I don't understand what I am missing.

I looped though my trades to get all the profits and losses into an array called pnl. Checking with my ami broker output pnl[1] indeed matches the first trade profit or loss output from the backtest. There are 30 trades in the backtest so I hardcoded the 30. At the bottom of the metrics output window I do get the proper output for pnl[1], pnl[5], sumpnl, and totaltrades. I get no output on the custom metric with the variable ssttdev where ssttdev = stdev(pnl, 30); What am I missing? Thank you in advance for any advise.

// Start of Custom Metric  //
SetCustomBacktestProc("");

if( Status("action") == actionPortfolio )
{
	bo = GetBacktesterObject();
	bo.Backtest();
	st=bo.GetPerformanceStats(0);
		expectancy = 
		st.GetValue("WinnersAvgProfit")*st.GetValue("WinnersPercent")/100 +
		st.GetValue("LosersAvgLoss")*st.GetValue("LosersPercent")/100;
		bo.AddCustomMetric( "Expectancy ($) ", expectancy );
	// End of Custom Metric//	
// Start Custom Metric on ALL Trades //
	totaltrades = 0;
	pnl=0;
	sumpnl=0;
	ssttdev=0;
	for (trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
	{
		totaltrades++;
		pnl[totaltrades]=trade.GetProfit();
		sumpnl=sumpnl+trade.GetProfit();
		
	}

	bo.AddCustomMetric( "pnl[1]= ", pnl[1]);
	bo.AddCustomMetric( "pnl[5]= ", pnl[5]);
	ssttdev=StDev(pnl,30);
	bo.AddCustomMetric( "Standard Deviation ", ssttdev );
	bo.AddCustomMetric( "Sum ", sumpnl );
	bo.AddCustomMetric( "total trades ", totaltrades );
	// END Custom Metric on ALL Trades //
}

type or paste code here

Do not use array function in 2nd phase.

To get stddev of trades do like so:

SetCustomBacktestProc("");
if ( Status( "action" ) == actionPortfolio ) 
{
    bo = GetBacktesterObject();
    bo.Backtest(); // run default backtest procedure

    totaltrades = sum_profit = sum_squared_profit = 0; 
    for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() ) 
    {
        profit = trade.GetProfit();
        sum_profit += profit;
        sum_squared_profit += profit^2;
        totaltrades++;
    }

    stddev = sqrt( sum_squared_profit / totaltrades - ( sum_profit / totaltrades ) ^ 2 );
    bo.AddCustomMetric( "Standard Deviation", stddev );
    bo.AddCustomMetric( "Sum ", sum_profit );
    bo.AddCustomMetric( "total trades ", totaltrades );
}
15 Likes

Thank you. I will try it.

Rob

Thank you @fxshrat, this helped me today, also!

2 Likes

Nice way of calculating stdev!!! I thought I'd have to do 2 passes (one for calculating the mean, and another for calculating the sum of squared diffs), but this approach calculates stdev in just one pass. Awesome, thanks for sharing!

@NextLevelForex,

"Mean of squares minus square of means" is not new way of calculating Variance.

E.g. take a look here
https://www.mun.ca/biology/scarr/2250_Mean_&_Variance.html

Or here it is laid out in detail on how it results to that one.

3 Likes