Rotational Back-test problem

I'm trying to do a simple long-only rotational system based on slope of a trend indicator (Coral Trend Indicator). However, there are no trades triggering. Running a back-test with detailed log enabled shows that the scores assigned to the stocks in my list were all zero. I tried back-testing with simple ROC as the score, which is working, though my original code is still not working. The indicator and its slope works fine when plotted and the slope doesn't stay on zero line. I have checked other threads too, but this doesn't seem to match any of them. Code is given below, please let me know if I have made any mistakes.

sm = Param("Smoothing Period", 21, 2, 50, 1);
cd = Param("Constant D", 0.4, 0.1, 1, 0.1);

di = 1 + (sm - 1)/2;
c1 = 2/(di + 1);
c2 = 1 - c1;
c3 = 3 * (cd^2 + cd^3);
c4 = -3 * (cd + 2*cd^2 + cd^3);
c5 = 1 + 3*cd + 3*cd^2 + cd^3;

i1 = i2 = i3 = i4 = i5 = i6 = 0;

for( bar = 1; bar < BarCount; bar++ )
{	
	i1[bar] = c1 * Close[bar] + c2 * i1[bar - 1];
	i2[bar] = c1 * i1[bar] + c2 * i2[bar - 1];
	i3[bar] = c1 * i2[bar] + c2 * i3[bar - 1];
	i4[bar] = c1 * i3[bar] + c2 * i4[bar - 1];
	i5[bar] = c1 * i4[bar] + c2 * i5[bar - 1];
	i6[bar] = c1 * i5[bar] + c2 * i6[bar - 1];
}

cti = (-1*cd^3*i6) + (c3*i5) + (c4*i4) + (c5*i3);

cti_slope = LinRegSlope(cti, 2);

SetBacktestMode( backtestRotational );

posScore = IIf(cti_slope < 0, 0, cti_slope );

RoundLotSize = 1;
PointValue = 1;

scrips = 5;
EachPosPercent = 100 / scrips ; 

PositionSize = -EachPosPercent;

day_week = DayOfWeek() == 5;
count_week = Cum( day_week );

rotation = day_week AND (count_week % 2 == 0 );

SetOption("MinShares", 1);
SetOption("WorstRankHeld", scrips * 2 );
SetOption("MaxOpenPositions", scrips ); 

PositionScore = IIf( rotation, posScore, scoreNoRotate);

You do not need barcount loop

This:

i1 = i2 = i3 = i4 = i5 = i6 = 0;
for( bar = 1; bar < BarCount; bar++ )
{	
	i1[bar] = c1 * Close[bar] + c2 * i1[bar - 1];
	i2[bar] = c1 * i1[bar] + c2 * i2[bar - 1];
	i3[bar] = c1 * i2[bar] + c2 * i3[bar - 1];
	i4[bar] = c1 * i3[bar] + c2 * i4[bar - 1];
	i5[bar] = c1 * i4[bar] + c2 * i5[bar - 1];
	i6[bar] = c1 * i5[bar] + c2 * i6[bar - 1];
}

cti = (-1*cd^3*i6) + (c3*i5) + (c4*i4) + (c5*i3);

is AMA2.

So all you need is this

i1 = AMA2(C, c1, c2);
for( i = 2; i <= 6; i++ )
	VarSet("i"+i, AMA2(VarGet("i"+(i-1)), c1, c2));

cti = (-1*cd^3*i6) + (c3*i5) + (c4*i4) + (c5*i3);

Results in same thing but being shorter and faster.


Use this one then you get results. Make sure Pad&align is enabled in Analysis Settings - General.

/// @link https://forum.amibroker.com/t/rotational-back-test-problem/19499/2
sm = Param("Smoothing Period", 21, 2, 50, 1);
cd = Param("Constant D", 0.4, 0.1, 1, 0.1);

di = 1 + (sm - 1)/2;
c1 = 2/(di + 1);
c2 = 1 - c1;
c3 = 3 * (cd^2 + cd^3);
c4 = -3 * (cd + 2*cd^2 + cd^3);
c5 = 1 + 3*cd + 3*cd^2 + cd^3;

i1 = AMA2(C, c1, c2);
for( i = 2; i <= 6; i++ )
	VarSet("i"+i, AMA2(VarGet("i"+(i-1)), c1, c2));

cti = (-1*cd^3*i6) + (c3*i5) + (c4*i4) + (c5*i3);
cti_slope = LinRegSlope(cti, 2);

scrips = 5;
SetOption("MinShares", 1);
SetOption("WorstRankHeld", scrips * 2 );
SetOption("MaxOpenPositions", scrips ); 
SetPositionSize( 100/scrips, spsPercentOfEquity );
SetBacktestMode( backtestRotational );

posScore = IIf(cti_slope < 0, 0, cti_slope);
//posScore = 10000 + cti_slope;

RoundLotSize = 1;
PointValue = 1;

day_week = DayOfWeek() == 5;
count_week = Cum( day_week );

rotation = day_week AND (count_week % 2 == 0 );
PositionScore = IIf(rotation, posScore, scoreNoRotate);

If you still don't get BT results then cti_slope of your symbols may be lower than zero.
So check cti_slope via explorer.

Filter = 1;
AddColumn(cti_slope, "cti_slope", 1.2);

You might alternatively use score like this

posScore = 10000 + cti_slope;
5 Likes

Thank you so much! It is working now.

Thank you for this elegant solution as well.