Questions on Short trades in Rotational TS

Hello Community,
I wrote a rotational trading system which gives quite good results on french market from January 2000 till now, but I don't really understand how code in line 39 works.
The rotation is made every month on a weekly basis data.

// ***********************************************************************************
//                            Jiel Rotational system                                //   
//                                                                                  //   
//          IMPORTANT : Settle Backtester Settings on Weekly periodicity   ******   //
//                                                                                  //   
// ***********************************************************************************
    
_SECTION_BEGIN("181210 Jiel Rotational system") ;
 
// Market Filter Condition
// ***********************************************************************************
FilterIndex = Foreign("^FCHI", "Close", 1); // French market index CAC40
FilterIndexMA = 0.96* EMA( FilterIndex, 72) ; //Optimize("Level", 0.9 , 0.94, 0.99, 0.005)* EMA( FilterIndex, Optimize("EMA", 65, 20, 100, 2));
inBearMkt = FilterIndex < FilterIndexMA ;

// Settings
// ***********************************************************************************
SetBacktestMode( backtestRotational );
SetOption(	"InitialEquity", 30000	); 
SetOption(	"CommissionMode", 2	); // 2 = mode $ per trade 
SetOption(	"CommissionAmount", 5 ); 

Totalpositions = 9 ; //	Optimize("Totalpositions",4,3,20,1);
SetOption("WorstRankHeld", 10) ; //Optimize("WorstRankHeld",1,1,25,1)); 
SetOption("MaxOpenPositions", Totalpositions ); 
PositionSize = -10 ; // Optimize("PositionSize", -30, -80,-6, 2) / Totalpositions ; 

RotationDay = TimeFrameExpand(1, inMonthly, expandPoint);	 
 
RocPeriod = 22 ; //Optimize("ROC",50,20,180,2); 
ROCDelay = -5 ; //Optimize("ROCDelay",-5,-10,0,1);
Smoothing = MA(Close, 2.4) ; //MA(Close,Optimize("Smoothing",2,1,10,0.2));
Score = Ref(ROC(Smoothing, RocPeriod), ROCDelay);

PositionScore = IIf(Score < 0, 0, Score ); // Long only 	
PositionScore = IIf(inBearMkt, 0, Score ); // not in Bear Mkt  _________ 1) Why does this line run short trades
//                                                                          insteade of only avoiding long trades to enter and staying in Market???
//                                                                       2) Why are short trades entered only when NOT inBearMkt?
//																	    	3) How can I run short trades also when InBearMkt?
PositionScore = IIf(RotationDay ,  PositionScore, scoreNoRotate);
 
_SECTION_END ();
 
// Plottings
// ***********************************************************************************     
SetChartOptions(1,chartShowDates|chartLogarithmic);  
_N(Title = StrFormat("{{NAME}} - (" + FullName() + ") - {{VALUES}} \n{{INTERVAL}} {{DATE}} \nOpen %g \nHigh  %g \nLow   %g \nClose %g (%.2f%%) ", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) ) ;   

Plot( C, "Close", colorDefault, styleCandle, Null, Null, XShift = 0, Zorder = 0, width = 3 );      

//Exploration 
// ***********************************************************************************     
Filter = 1; 
AddColumn(Score ,"Score",1.1);  // 1 decimal digit 
AddColumn(PositionScore ,"PositionScore ",1.1); 
AddColumn(PositionSize ,"Position Size",1.1); 

So I have 3 questions about line 39 code:

  1. Why does this code run short trades instead of only avoiding long trades to enter and staying in Market???
  2. Why are short trades entered only when NOT inBearMkt?
  3. How can I run short trades also when InBearMkt?

I thank you in advance for your help;
Jiel

The information you have provided is not complete until you describe the settings you have in the AA Backtester Settings window.

see this:
image

PositionScore = IIf(inBearMkt, 0, Score );

If Score is -ve, then those symbols are candidates for Short. The Lower the Negative Value, the better it is.

1 Like

@Jiel78

Simply do this...

Score = 10000 + Ref(ROC(Smoothing, RocPeriod), ROCDelay);
PositionScore = IIf(RotationDay AND ! inBearMkt, Score, scoreNoRotate);

So you add high number to ROC. Then it is ranked by highest Score (-> highest ROC) not going negative. And within Iif(...) you check for RotationDay at Bullish Index condition (-> "! inBearMkt" -> "!" stands for "NOT").


// ***********************************************************************************
//                            Jiel Rotational system                                //   
//                                                                                  //   
//          IMPORTANT : Settle Backtester Settings on Weekly periodicity   ******   //
//                                                                                  //   
// ***********************************************************************************
    
_SECTION_BEGIN("181210 Jiel Rotational system") ;
 
// Market Filter Condition
// ***********************************************************************************
FilterIndex = Foreign("^FCHI", "Close", 1); // French market index CAC40
FilterIndexMA = 0.96* EMA( FilterIndex, 72) ; //Optimize("Level", 0.9 , 0.94, 0.99, 0.005)* EMA( FilterIndex, Optimize("EMA", 65, 20, 100, 2));
inBearMkt = FilterIndex < FilterIndexMA ;

// Settings
// ***********************************************************************************
SetBacktestMode( backtestRotational );
SetOption(	"InitialEquity", 30000	); 
SetOption(	"CommissionMode", 2	); // 2 = mode $ per trade 
SetOption(	"CommissionAmount", 5 ); 

Totalpositions = 9 ; //	Optimize("Totalpositions",4,3,20,1);
SetOption("WorstRankHeld", 10) ; //Optimize("WorstRankHeld",1,1,25,1)); 
SetOption("MaxOpenPositions", Totalpositions ); 
PositionSize = -10 ; // Optimize("PositionSize", -30, -80,-6, 2) / Totalpositions ; 

RotationDay = TimeFrameExpand(1, inMonthly, expandPoint);	 
 
RocPeriod = 22 ; //Optimize("ROC",50,20,180,2); 
ROCDelay = -5 ; //Optimize("ROCDelay",-5,-10,0,1);
Smoothing = MA(Close, 2.4) ; //MA(Close,Optimize("Smoothing",2,1,10,0.2));

Score = 10000 + Ref(ROC(Smoothing, RocPeriod), ROCDelay);
PositionScore = IIf(RotationDay AND ! inBearMkt, Score, scoreNoRotate); 
_SECTION_END ();
 
// Plottings
// ***********************************************************************************     
SetChartOptions(1,chartShowDates|chartLogarithmic);  
_N(Title = StrFormat("{{NAME}} - (" + FullName() + ") - {{VALUES}} \n{{INTERVAL}} {{DATE}} \nOpen %g \nHigh  %g \nLow   %g \nClose %g (%.2f%%) ", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) ) ;   

Plot( C, "Close", colorDefault, styleCandle, Null, Null, XShift = 0, Zorder = 0, width = 3 );      

//Exploration 
// ***********************************************************************************     
Filter = 1; 
AddColumn(Score ,"Score",1.1);  // 1 decimal digit 
AddColumn(PositionScore ,"PositionScore ",1.1); 
AddColumn(PositionSize ,"Position Size",1.1); 

3 Likes
rc = Ref(ROC(Smoothing, RocPeriod), ROCDelay);
Score = IIf( ! inBearMkt, // if not in bear market
             /*then bull market score*/ 10000 + rc, // long score
             /*else bearmarket score */ -10000 + rc ); // short score
PositionScore = IIf(RotationDay, Score, scoreNoRotate); 

So if not in bear market then 10000 + rc will consider highest ROC (and because of 10000 added to rc it will consider Long only)

e.g.
stock A has 10% ROC and Stock B has -5% ROC and Stock C has 5% ROC.
So 10000 + 10 = 10010 is higher than 10000 + (-5) = 9995 and is higher than 10000 + 5 = 10005.
So long ranks are

  1. Stock A
  2. Stock C
  3. Stock B

On the other hand if in bear market then -10000+rc will consider lowest ROC (and because of -10000 added to rc it will consider Short only)

e.g.
stock A has -10% ROC and Stock B has 5% ROC and Stock C has -5% ROC.
So -10000 + (-10) = -10010 is lower than -10000 + 5 = -9995 and is lower than -10000 + (-5) = -10005.
So short ranks

  1. Stock A
  2. Stock C
  3. Stock B

If you want to consider Long trades in Bull market only if roc > 0 and Short trades in bear market only if roc < 0 then you may do

rc = Ref(ROC(Smoothing, RocPeriod), ROCDelay);
Score = IIf(! inBearMkt AND rc > 0, 10000 + rc,  // long score in bull market
		IIf(inBearMkt AND rc < 0, -10000 + rc, // short score in bear market
		0 ));
PositionScore = IIf(RotationDay, Score, scoreNoRotate); 

Since here you add roc condition you may leave out 10000 and -10000

rc = Ref(ROC(Smoothing, RocPeriod), ROCDelay);
Score = IIf(! inBearMkt AND rc > 0, rc, // long score in bull market
		IIf(inBearMkt AND rc < 0, rc, // short score in bear market
		0 ));
PositionScore = IIf(RotationDay, Score, scoreNoRotate); 
7 Likes

As aside it should be noted that scoreNoRotate is not equal to zero. See here

  • the score of zero means no trade (exit the trade if there is already open position on given symbol)
  • the score equal to scoreNoRotate constant means that already open trades should be kept and no new trades entered

e.g.

Score = 1000 + ROC(C, -252);
PositionScore = IIf(RotationDay, Score, scoreNoRotate); 

then it is not the same one as

Score = 1000 + ROC(C, -252);
PositionScore = IIf(RotationDay, Score, 0); 

Same goes for this one

Score = 10000 + Ref(ROC(Smoothing, RocPeriod), ROCDelay);
PositionScore = IIf(RotationDay AND ! inBearMkt, Score, scoreNoRotate);

not being the same as this one

Score = 10000 + Ref(ROC(Smoothing, RocPeriod), ROCDelay);
PositionScore = IIf(RotationDay, IIf( ! inBearMkt, Score, 0), scoreNoRotate);

But this one would be the same one as 1.

Score = 10000 + Ref(ROC(Smoothing, RocPeriod), ROCDelay);
PositionScore = IIf(RotationDay, IIf( ! inBearMkt, Score, scoreNoRotate), scoreNoRotate);
4 Likes

@Jiel78 - Knowledge Base has this covered
http://www.amibroker.com/kb/2016/04/17/long-only-rotational-back-test/

KB should be your first point of reference (after Users Guide of course)

2 Likes

Thank you Fxshrat and others,

Score = 10000 + Ref(ROC(Smoothing, RocPeriod), ROCDelay);
PositionScore = IIf(RotationDay, IIf( ! inBearMkt, Score, 0), scoreNoRotate);

using only long trades with market parameters optimized, gives very nice results :wink:
I'm surprised that short trades don't bring more ...

1 Like