How to create algo for RSI Trigger Line strategy?

How to create algo for RSI Trigger Line strategy?

Welcome to Episode 11 of Algo Trading Series from MarketSecrets.

In this episode , we are going to learn how to code RSI Trigger Line strategy.

Let’s get into the set-up.

Condition:

The moving average of RSI indicates the average momentum, while RSI indicates current momentum. So we are trying to buy when RSI crosses above its moving average.The short RSI period of 7 is used to generate sufficient signals. The Moving Average period used is 15, arbitrarily the double of RSI period.

Buy: When RSI crosses above its trigger line and RSI>60
Sell:  When RSI goes below trigger line and RSI<40

Reverse is applicable for shorting.

 

Let’s begin with the script set-up now.

Script:

_SECTION_BEGIN( “RSI Trigger Line Strategy” );

 

SetChartOptions( 0, chartShowArrows | chartShowDates );

Plot( C, “Close”, ParamColor( “Color”, colorBlack ), styleNoTitle | ParamStyle( “Style” ) | GetPriceStyle() );

 

SetBarsRequired( 100000, 0 );

SetPositionSize(1, spsShares);

 

per1= Param(“RSI Period”, 7, 5, 100, 1);

per2= Param(“Trigger Line Period”,15, 15, 100, 1);

 

RSI1= RSI(per1);

MA1= MA(RSI1,per2);

 

Buy=Cross(RSI1,MA1) AND RSI1>60;

Sell=Cross(MA1,RSI1) AND RSI1<40;

Buy = ExRem( Buy, Sell );

Sell = ExRem( Sell, Buy );

 

Short = Cross(MA1,RSI1) AND RSI1<40;

Cover = Cross(RSI1,MA1) AND RSI1>60;

Short = ExRem( Short, Cover );

Cover = ExRem( Cover, Short );

 

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  and Moving Average. As dicussed earlier, we are setting this to 7 & 15 respectively.

per1= Param(“RSI Period”, 7, 5, 100, 1);

per2= Param(“Trigger Line Period”,15, 15, 100, 1);

 

Next, we are getting the corresponding values.

RSI1= RSI(per1);

MA1= MA(RSI1,per2);

 

Next line is the buy condition.

Buy=Cross(RSI1,MA1) AND RSI1>60;

Sell=Cross(MA1,RSI1) AND RSI1<40;

 

we’ll go long if

  • When RSI crosses above its trigger line
  • RSI>60

We will Square off the long

  • When RSI goes below trigger line
  • RSI<40

Reverse the conditions for shorting:

Short = Cross(MA1,RSI1) AND RSI1<40;

Cover = Cross(RSI1,MA1) AND RSI1>60;

We will short

  • When RSI goes below trigger line
  • RSI<40

We will Square off short position

  • When RSI crosses above its trigger line
  • RSI>60

 

ExRem is used to remove excess signals which helps us to avoid taking multiple positions at the same time.

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 buy and sell signals using the up and down arrows.

You can also run a backtest on this code, I am running the backtest now on Nifty and BankNifty for 6 months. This is being executed on 15 Min Timeframe and we are taking only long positions. This generates approximately 10K profits on a capital of 1L.

 

You can play around the parameters we have used in this strategy to optimize the returns. 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/Uz89COHMyNI

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!