Rotational System going long and short at the same time


I'm trying to make a rotational system that would buy 2 stocks from a watchlist of 20 of the largest stocks and short 2 stocks at the same time. For positionscore for the buys I would use the lowest RSI value and for the shorts, I would use the highest RSI value.

Positionscore looks for the highest values, shorts negative numbers and buys positive numbers. I could sort the RSI values from highest to lowest. For the lowest two values, I could just convert them to really high numbers, and positionscore would result in buys for them. For the highest two values, I could just convert them to negative numbers and positionscore would short them.

Would it be possible to have two longs and two shorts with the same rotational code? Maybe I have to split it up into two rotational codes. I started to write an AFL which won't work as it is. Does anyone have an idea how I might proceed?

//and short two of the stocks with the highest RSI so that you always have two long and two short positions 
_SECTION_BEGIN("Rotational to buy lowest and sell highest RSI"); 
SetChartOptions(1, chartShowArrows | chartShowDates); 
Float = 100000;  
// Options  
SetOption("AllowPositionShrinking", True); 
SetOption("InitialEquity", Float);  
MaxPositions =4;//Optimize("MaxPositions",2,1,20,1);
RSIPeriod =10;//Optimize("RSIPeriod",2,2,25,1);
RSIIndication = RSI(RSIPeriod);
SetOption("MaxOpenPositions", MaxPositions ); 
SetOption("WorstRankHeld", MaxPositions + Number); 
SetPositionSize( 100 / MaxPositions, spsPercentOfEquity );  
SetBacktestMode( backtestRotational ); 
//SetTradeDelays( 0, 0, 0, 0 ); //use settings, set to 0 for close or 1 for open next day
//BuyPrice = Close; //has no effect in rotational mode, use settings
Score=100-RSIIndication;//for the two buys
Score=-RSIIndication;//for the shorts....can I have two longs and two shorts in the same Rotational AFL?
PositionScore = Score ;


Not sure if you can make that work with the Rotational mode. Another approach is to explicitly rank your symbols using the StaticVarGenerateRanks function (see examples, either on the forum or in the user guide). And then either have 2 separate ranking arrays for long and short signals ... or if you're shorting the bottom 'x' and buying the top 'x' use a single array.


@Marcel before creating a solution I think you need to think about the logic of the strategy you are proposing. Your basic premise is going Long on the most "oversold" stocks and going Short the most "overbought" stocks. Reasonable at first glance.

But you do not actually look for stocks that are "overbought" or "oversold". Instead you are willing to accept the relatively most overbought or relatively most oversold in your list.

Consider the scenario where the lowest RSI in your list is 48 and the highest RSI is 52. Your strategy considers those as the most oversold and overbought, but would you consider those RSI values to really suggest that? I'm fairly certain that none of the mean reversion strategies you have looked at would consider selling short a stock just because it's RSI had climbed to 52 (just as an example).

Similarly depending on how correlated your watch list constituents are, they may all approach oversold (or overbought) simultaneously. For example in a market correction, your lowest may have RSI's around 15 and your highest RSI may be around 20. Ok to to go long perhaps on the two at 15 but is your logic really to be selling short the two stocks with RSI around 20?

I don't know but it doesn't seem intuitive to me.

If you are looking at "pairs trading" it might be best to start with a different premise. If the idea is to learn how to properly code an afl that includes some long/short hedging I think that is a great idea.


Yes, I was thinking along the lines of a pair trading system but instead of just looking at two stocks, it would look at a group of stocks or perhaps ETFs. I would want to buy one or more of the stocks with the lowest relative price and then short the same number of the highest price stocks. I wouldn't necessarily use RSI but I would try any indicator that would work with the concept.

I'm not sure that you could make a successful system using this method. If something like that does work, you probably would have heard of it already. The challenge would be to find a way to code it so that you could backtest it. Any thoughts or ideas from anyone would be welcome.


In Howard Bandy's book, Quantitative Trading Systems, there's a code tilted Figure 15.1. You can buy his book or download the code from here:

Howard's code is for a rotational system that will go long, or short on a group of ETFs depending on the ROC. It will go short if the ROC reads negative for any of the ETFs.

The code allows for longs and shorts at the same time, but it's not quite what I had in mind where you would have matching long and shorts like in pair trading. It does prove that it is possible to go long and short at the same time with the same Amibroker rotational code.