How to create algo for RSI Crossover System?
Welcome to Episode 9 of Algo Trading Series from MarketSecrets.
In this episode , we are going to learn how to code RSI Crossover System.
Let’s get into the set-up.
Condition:
The Trading Idea is to Buy when short-term momentum crosses above long-term momentum and vice versa.
RSI is a oscillator which depicts momentum in prices. We try using two RSI of different periods to judge the short-term momentum and long-term momentum. So trade entries will be based on RSI indicator.
However, by construction, RSI gives a lot of whipsaws while it oscillates up and down. So trade exits are done using RSI levels to avoid too frequent re-entries.
So when it comes to trade condition,
Buy: When short-term RSI crosses above long-term RSI
Sell: When RSI goes below 40 level
Let’s begin with the script set-up now.
Script:
_SECTION_BEGIN( “RSI Crossover System” );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, “Close”, ParamColor( “Color”, colorBlack ), styleNoTitle | ParamStyle( “Style” ) | GetPriceStyle() );
SetBarsRequired( 100000, 0 );
SetPositionSize(1, spsShares);
per1= Param(“RSI Fast”, 18, 5, 100, 1);
per2= Param(“RSI Slow”, 52, 15, 100, 1);
RSI1= RSI(per1);
RSI2= RSI(per2);
Buy=Sell=Short=Cover=Null;
flag=0; sflag=0;
for(i=0; i<BarCount; i++) { if(RSI2[i]>RSI1[i] AND RSI2[i-1]<RSI1[i-1] AND flag==0 AND sflag==0) { Buy[i]=1; flag=1; } if(40>RSI1[i] AND 40<RSI1[i-1] AND flag)
{ Sell[i]=1; flag=0; }
if(RSI2[i]<RSI1[i] AND RSI2[i-1]>RSI1[i-1] AND flag==0 AND sflag==0)
{ Short[i]=1; sflag=1; }
if(60<RSI1[i] AND 60>RSI1[i-1] AND sflag)
{ Cover[i]=1; sflag=0; }
}
Title = NumToStr( DateTime(), formatDateTime ) + ” O ” + O + ” H ” + H + ” L ” + L + ” C ” + C ;
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorGreen, 0, L, Offset = -40 );
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorLime, 0, L, Offset = -50 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorWhite, 0, L, Offset = -45 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, Offset = -45 );
PlotShapes( IIf( Short, shapeSquare, shapeNone ), colorRed, 0, H, Offset = 40 );
PlotShapes( IIf( Short, shapeSquare, shapeNone ), colorOrange, 0, H, Offset = 50 );
PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorWhite, 0, H, Offset = -45 );
PlotShapes( IIf( Cover, shapeUpArrow, shapeNone ), colorBlue, 0, L, Offset = -45 );
_SECTION_END();
First of the script is self explanatory – these are parameters used for charting and backtesting , which we have already discussed in detail in the previous episodes. So I’m skipping this part.
Now comes the actual part of the script.
First, we are setting the parameters for RSI Indicator. We are setting up 2 RSIs – one for longterm momentum and another for short term momentum.
per1= Param(“RSI Fast”, 18, 5, 100, 1);
per2= Param(“RSI Slow”, 52, 15, 100, 1);
Then we are fetching the values of RSI for our calculations.
RSI1= RSI(per1);
RSI2= RSI(per2);
So when it comes to trade condition,
We will go long When short-term RSI crosses above long-term RSI
And we will square off the long position: When RSI goes below 40 level
Reverse is applicable for shorting.
We will short, When short-term RSI crosses below long-term RSI
And we will square off the short position: When RSI goes above 60 level
Rest of the lines are self explanatory – these are used for plotting the charts , which we have already discussed in detail in the previous episodes. So I’m skipping this part as well.
So if you look at the charts, you can spot the signals,
You can also run a backtest on this code. In this case, we are running the backtest on NIFTY. I’m setting the timeframe as 15 Minutes and taking only long trades.
And when it comes to report, this strategy is producing the annual returns of around 85%.
Do give this a try and let us know in case of any issues.
For more details and examples, checkout the video: https://youtu.be/ID9KUO17Gq0
Related
amibroker tutorial videos Free Afl for Amibroker how to backtest amibroker afl? how to code amibroker afl for RSI Crossover System? How to create algo for RSI Crossover System? how to create algo script for zerodha? how to do algo trading in india? how to get free afl for amibroker how to install amibroker how to learn algo trading? how to start with algo trading in india? how to use afl codes how to use amibroker how to write amibroker afl scripts
2 Responses
Sir
I have some query related to this strategy.
1) Which time frame we should choose for lesser whipsaws?
2) Set up is RSI (18) crossed above RSI (52) but when to enter? above high of crossover candle?
3)Target?
Regards
Rajeev
Hi Sir, PFB the response.
1. Higher the better sir.
2. After the crossover, we can enter on the opening of immediate next candle
3. Fixed Target based on strategy or we can ride it till reverse crossover happens