Mean Reversion Study Code
To make the testing a little easier, I standardized the optimized inputs as follows:
InputVar2= 5-20, step 5 (4 values)
InputVar3= 25-40, step 5 (4 values)
inputVar4= 0 to 1, step 1 (2 values) [off or on]
inputVar5= 0 to 1, step 1 (2 values) [off or on]
64 total tests
For all the entries, I have the following “exit 7 bars after entry” code:
if InputVar5=1 then begin
If marketposition=1 and barssinceentry>7 then sell next bar at market;
If marketposition=-1 and barssinceentry>7 then buytocover next bar at market;
End;
Note this code is not included in each of the strategies detailed below, but it should be added to the bottom of each
strategy.
I chose an exit after 7 bars thinking that mean reversion trades should be relatively short in duration
(approximately 1.5 weeks for daily bars). We’ll see later how that assumption turned out (spoiler alert: not good!).
Strategy #1 – Simple Short Term RSI
General Idea:
When the 2 bar RSI goes below a certain value (oversold area), it is a good time to buy. Vice versa for shorts.
Entry:
If the 2 bar RSI crosses below InputVar2 then buy the next bar.
If the 2 bar RSI crosses above 100-InputVar2 then sellshort the next bar.
Exit:
If 2 bar RSI closes above InputVar3 then exit long.
If 2 bar RSI closes below 100-InputVar3 then exit short.
Tradestation Code:
If RSI(close,2) crosses below InputVar2 then begin
buy next bar at market;
end;
If RSI(close,2) crosses above 100-InputVar2 then begin
sellshort next bar at market;
end;
If Inputvar4=1 then begin
If marketposition=1 and RSI(close,2) crosses above InputVar3 then sell next bar at market;
If marketposition=-1 and RSI(close,2) crosses below 100-InputVar3 then buytocover next bar at market;
end;
Strategy #2 – Simple Connors RSI
General Idea:
When the 2 bar Connors RSI goes below a certain value (oversold area), it is a good time to buy. Vice versa for
shorts.
Entry:
If the Connors RSI crosses above InputVar2 then buy the next bar.
If the Connors RSI crosses below 100-InputVar2 then sellshort the next bar.
Exit:
If 2 bar Connors RSI closes above InputVar3 then exit long.
If 2 bar Connors RSI closes below 100-InputVar3 then exit short.
Tradestation Code:
If ConnorsRSI(3,2,100) crosses below InputVar2 then begin
buy next bar at market;
end;
If ConnorsRSI(3,2,100) crosses above 100-InputVar2 then begin
sellshort next bar at market;
end;
If Inputvar4=1 then begin
If marketposition=1 and ConnorsRSI(3,2,100) crosses above InputVar3 then sell next bar at market;
If marketposition=-1 and ConnorsRSI(3,2,100) crosses below 100-InputVar3 then buytocover next bar at market;
end;
Strategy #3 – Bollinger Band “Stretch”
General Idea:
When the gets near the lower Bollinger Band, it is a good time to buy. Vice versa for shorts.
Entry:
First calculate the Upper and Lower (Down) Bollinger Band. Then calculate where the current close relative to the
Bollinger Band window (=0 if close if at lower band, =1 if at upper band).
If close moves below the 10% point of Bollinger Band range, go long.
If close moves above the 90% point of Bollinger Band range, go short.
Exit:
If close moves above 0.4 of BB range then exit long.
If close moves below 0.6 of BB range then exit short.
Tradestation Code:
InputVar33=.0666667*InputVar3-.666667;
UpBB=BollingerBand(close,InputVar2,InputVar33);
DnBB=BollingerBand(close,InputVar2,-InputVar33);
if UpBB-DnBB<>0 then PercentB=(close-DnBB)/(UpBB-DnBB);
If PercentB<.1 and PercentB[1]<.1 then begin
buy next bar at market;
end;
If PercentB>.9 and PercentB[1]>.9 then begin
sellshort next bar at market;
end;
If Inputvar4=1 then begin
If marketposition=1 and PercentB>.4 then sell next bar at market;
If marketposition=-1 and PercentB<.6 then buytocover next bar at market;
end;
Strategy #4– Moving Average “Stretch”
General Idea:
When the close moves far below an InputVar2 length moving average, it is a good time to buy. Vice versa for
shorts.
Entry:
If the difference between the close and an InputVar2 length moving average is greater than a specified amount
(meaning the close is below the moving average by a large amount), go long.
If the difference between the close and an InputVar2 length moving average is less than a specified amount
(meaning the close is above the moving average by a large amount), go short.
Exit:
If close moves above the InputVar2 length moving average then exit long.
If close moves below the InputVar2 length moving average then exit short.
Tradestation Code:
InputVar33=.5*(.004667*InputVar3-.08667);
If -close + average(close,InputVar2) > InputVar33* average(close,InputVar2) then begin
buy next bar at market;
end;
If close -average(close,InputVar2) > InputVar33* average(close,InputVar2) then begin
sellshort next bar at market;
end;
If Inputvar4=1 then begin
If marketposition=1 and close>average(close,InputVar2) then sell next bar at market;
If marketposition=-1 and close<average(close,InputVar2) then buytocover next bar at market;
end;
Strategy #5 – Percent Decrease
General Idea:
When the close has fallen by a certain percentage, it is a good time to buy. Vice versa for shorts.
Entry:
If the close divided by the close InputVar22 bars ago is less than InputVar33, buy next bar at market.
If the close divided by the close InputVar22 bars ago is greater than 1-InputVar33, sellshort next bar at market.
Exit:
If close divided close InputVar22 bars ago crosses above 1 then exit long.
If close divided close InputVar22 bars ago crosses below 1 then exit short.
Tradestation Code:
InputVar22=.466667*InputVar2+.666667;
InputVar33=.0066667*InputVar3+.68333;
If (close/(close[InputVar22]+.00001)) < InputVar33 then begin
buy next bar at market;
end;
If (close/(close[InputVar22]+.00001)) > 1-InputVar33 then begin
sellshort next bar at market;
end;
If Inputvar4=1 then begin
If marketposition=1 and (close/(close[InputVar22]+.00001))>1 then sell next bar at market;
If marketposition=-1 and (close/(close[InputVar22]+.00001))<1 then buytocover next bar at market;
end;
Strategy #6 – Bars Up/ Bars Down
General Idea:
After a certain number of consecutive down bars, it is a good time to buy. Vice versa for shorts.
Entry:
If the price closes down for 3, 4, 5, or 6 bars in a row (depending on setting) go long at the next bar.
If the price closes up for 3, 4, 5, or 6 bars in a row (depending on setting) go short at the next bar.
Exit:
Once the consecutive down bar streak ends, then exit long.
Once the consecutive up bar streak ends, then exit short.
Tradestation Code:
CanGoLong2=False;
CanGoShort2=False;
If InputVar2=5 and close<close[1] and close[1]<close[2] then CanGoLong2=True;
If InputVar2=5 and close>close[1] and close[1]>close[2] then CanGoShort2=True;
If InputVar2=10 and close<close[1] and close[1]<close[2] and close[2]<close[3] then CanGoLong2=True;
If InputVar2=10 and close>close[1] and close[1]>close[2] and close[2]>close[3] then CanGoShort2=True;
If InputVar2=15 and close<close[1] and close[1]<close[2] and close[2]<close[3] and close[3]<close[4] then
CanGoLong2=True;
If InputVar2=15 and close>close[1] and close[1]>close[2] and close[2]>close[3] and close[3]>close[4] then
CanGoShort2=True;
If InputVar2=20 and close<close[1] and close[1]<close[2] and close[2]<close[3] and close[3]<close[4] and close[4]<close[5]
then CanGoLong2=True;
If InputVar2=20 and close>close[1] and close[1]>close[2] and close[2]>close[3] and close[3]>close[4] and close[4]>close[5]
then CanGoShort2=True;
If CanGoLong2=True then begin
buy next bar at market;
end;
If CanGoShort2=True then begin
sellshort next bar at market;
end;
If Inputvar4=1 then begin
If marketposition=1 and CanGoLong2=False then sell next bar at market;
If marketposition=-1 and CanGoShort2=False then buytocover next bar at market;
end;
Strategy #7 – Reverse Breakout
General Idea:
When a downside breakout occurs, it is a good time to buy. Vice versa for shorts.
Entry:
If the close is the lowest close of the last InputVar2 bars, then go long next bar at market.
If the close is the highest close of the last InputVar2 bars, then go short next bar at market.
Exit:
If the close is the highest close of the last InputVar3 bars, then exit long next bar at market.
If the close is the lowest close of the last InputVar3 bars, then exit short next bar at market.
Tradestation Code:
If close=lowest(close,InputVar2) then begin
buy next bar at market;
end;
If close=highest(close,InputVar2) then begin
sellshort next bar at market;
end;
If Inputvar4=1 then begin
If marketposition=1 and close=highest(close,InputVar3) then sell next bar at market;
If marketposition=-1 and close=lowest(close,InputVar3) then buytocover next bar at market;
end;
Strategy #8 – Closing Range
General Idea:
When the close is in the lower part of the recent range, it is a good time to buy. Vice versa for shorts.
Entry:
Calculate where the close falls relative to the range of the last InputVar2 bars. The result is crange.
If crange is less than InputVar33 then buy next bar at market;
If crange is greater than 1- InputVar33 then short next bar at market;
Exit:
If crange is greater than InputVar33 then exit long next bar at market;
If crange is less than 1- InputVar33 then exit short next bar at market;
Tradestation Code:
Inputvar33=.01*InputVar3-.15;
if (highest(high,InputVar2)-lowest(low,InputVar2))<>0 then crange=(close-
lowest(low,InputVar2))/(highest(high,InputVar2)-lowest(low,InputVar2));
If crange<InputVar33 then begin
buy next bar at market;
end;
If crange>1-InputVar33 then begin
sellshort next bar at market;
end;
If Inputvar4=1 then begin
If marketposition=1 and crange>InputVar33 then sell next bar at market;
If marketposition=-1 and crange<1-InputVar33 then buytocover next bar at market;
end;
Strategy #9 – Linear Regression
General Idea:
When the close is below the linear regression value over the recent past bars, it is a good time to buy. Vice versa
for shorts.
Entry:
If close is below the Linear Regression value over the past InputVar2 bars, go long.
If close is above the Linear Regression value over the past InputVar2 bars, go short.
Exit:
If close is above the Linear Regression value over the past InputVar3 bars, then exit long.
If close is below the Linear Regression value over the past InputVar3 bars, then exit short.
Tradestation Code:
If close <LinearRegValue(close,InputVar2,0) then begin
buy next bar at market;
end;
If close >LinearRegValue(close,InputVar2,0) then begin
sellshort next bar at market;
end;
If Inputvar4=1 then begin
If marketposition=1 and close >LinearRegValue(close,InputVar3,0) then sell next bar at market;
If marketposition=-1 and close <LinearRegValue(close,InputVar3,0) then buytocover next bar at market;
end;
Strategy #10 – Simple Momentum
General Idea:
When the recent momentum is down, it is a good time to buy. Vice versa for shorts.
Entry:
If close is less than the close InputVar2 bars ago, then go long next bar at market;
If close is greater than the close InputVar2 bars ago, then go short next bar at market;
Exit:
If close is greater than the close InputVar3 bars ago, then exit long.
If close is less than the close InputVar3 bars ago, then exit short.
Tradestation Code:
If close <close[InputVar2] then begin
buy next bar at market;
end;
If close >close[InputVar2] then begin
sellshort next bar at market;
end;
If Inputvar4=1 then begin
If marketposition=1 and close >close[InputVar3] then sell next bar at market;
If marketposition=-1 and close <close[InputVar3] then buytocover next bar at market;
end;