Calculating SQN values

Hello,

I'm trying to calculate the SQN of my list of trade values. I'm stuck at calculating the standard deviation of my array of R-values and I'm not seeing the solution. I was hoping one of the experienced users on this forum could help. Thank you in advance.

Note: the problem has probably to do with how I'm creating the R-value array (=rdist) and after that calculating the standard deviation if it with the total length of the array.

SetCustomBacktestProc( "" );
if ( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();
    bo.Backtest( 1 ); // run default backtest procedure
	SumProfitPerRisk = 0;
	NumTrades = 0;
	rdist=0;
    for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
    {
		dt = DateTime();
		SetForeign( trade.Symbol );
		symstop = StaticVarGet( trade.Symbol + "stopx" );
		stop = Lookup(symstop,trade.EntryDateTime);
		trade.AddCustomMetric( "Stop", stop);
        rval=((trade.getprofit+trade.GetCommission)/trade.Shares)/stop;
		RestorePriceArrays();
		trade.AddCustomMetric( "R-value", rval);
		NumTrades++;
		rdist[NumTrades-1]=rval;
		SumProfitPerRisk = SumProfitPerRisk + rval;
    }
	expectancy = SumProfitPerRisk / NumTrades;
	_TRACEF(NumToStr(numtrades) + NumToStr(rdist));
	sqn=expectancy/StDev(rdist,numtrades);
    bo.AddCustomMetric( "Expectancy", expectancy );
    bo.AddCustomMetric( "SQN", sqn );
    bo.ListTrades();
}

You should not use SetForeign in CBT at all.
You have been shown link to KB article and correction of your former code here already. There is not any SetForeign used there, is there. So I do not understand why you repeat mistake of using it again. Also remove DateTime() array. It has got no place in there.

As for standard deviation in CBT. It has been posted here already:

1 Like

Hello fxshrat,

Solved the problem and adjusted the foreign code. Your comment on the foreign code was initially for the beginning of the code and I failed to see this was also applicable here. I understand I have much to learn and I appreciate your continuous efforts and patience in this forum (though I can see this can be frustrating for you time to time).

Kind regards

Robin

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.