# Larry Connors RSI Amibroker AFL code

What is Connors RSI?

Connors RSI is a composite indicator consist of 3 components:

1. Price Momentum: By default, ConnorsRSI uses a standard 3 period Wilder RSI for the 1st component.

2. Duration of Up Trend vs. Down Trend streak days. And applies a default 2 day Wilder RSI to this integer series.Connors research shows that the longer the number of consecutive days up or down, the more likely the security is likely to bounce when it reverts to the mean. Likewise, the *magnitude* of the mean reversion snap-back is correlated with the streak length. The 2nd indicator uses integer values to quantify the number of streak days, and applies a default 2 day Wilder RSI to this integer series.

3. Relative Magnitude of Price Change: This 3rd component uses the PercentRank function over a default value of 100 days (approx. 5 months) to measure price change as a percentage of the previous day’s price, and rank the current value over the lookback period. Large positive returns will have a rank closer to 100, large negative returns will have a percent rank closer to 0.

Connors RSI Formula

ConnorsRSI(3,2,100) = [RSI(Close,3) + RSI(Streak,2) + PercentRank(100)] / 3

Connor’s Research purports this robust indicator is more effective than any of the three components use individually, and in fact the blended effect of the mathematical summation allows the strong value from one indicator to compensate for a slightly weaker value from another, in a way yielding superior results than a voting system between the 3.

```//Connor's RSI (Larry Connors).  Code for function provided by Connors Research

paramLenRSI = Param("RSI Closes Length", 3, 2, 100, 1);
paramLenUD = Param("RSI UpClose Length", 2, 2, 100, 1);
paramLenRank = Param("PerecentRank Length", 100, 10, 200, 1);

function ConnorsRSI(lenRSI, lenUD, lenROC)
{
updownDays = IIf(upDays > 0, upDays, IIf(downDays > 0, -downDays, 0));
crsi = ( PercentRank(ROC(C,1), lenROC) + RSIa(updownDays,lenUD) +RSI(lenRSI))/3;
return crsi;
}

Plot( ConnorsRSI(paramLenRSI,paramLenUD,paramLenRank)
, "ConnorsRSI("+paramLenRSI+","+paramLenUD+","+paramLenRank+")"
, colorBlue, styleLine, 0, 100);```

## Mini Certification Course on Algorithmic Trading

Welcome to the Mini Certification on Algorithmic Trading using Amibroker and OpenAlgo! This comprehensive 4-part series is designed to equip you with the knowledge...

## Pivot Reversal Strategy – Tradingview Pinescript to Amibroker AFL…

Pivot Reversal Strategy is a popular in-built strategy in Tradingview Platform. Here I had attempted to convert the pinescript v5 to Amibroker AFL Code....

## 16 Replies to “Larry Connors RSI Amibroker AFL code”

1. Meena says:

It’s giving Syntax error on line 11 & 13.
Pls help sir.

1. Which Amibroker Version you are using?

1. Meena says:

Sir . it’s 5.3
Had read in TJ that some function ( percentrank or so) is absent in 5.3 and the member posted solution.
But since i am newbie,will be kind of u if u can gimme some simple solution.
thanks

2. Ashish says:

It gives following error.

Line 12, Col: 27; Error 30: Syntex error
Line 14, Col: 18; Error 29: Variable ‘crsi’ used without having initialized

1. You can initialize. Add the following code in the 8th line. However it is not mandatory here
crsi =0;

1. Mahender says:

Hi Rajandran,

I’m using 5.30.4

yes that is optional, crsi=0;
In spite of adding it also, I’ve same issue.

Once I remove “PercentRank(ROC(C,1), lenROC)”, No more issue.
What is going wrong with it?

Thanks
Mahender

1. @Mahendar,

Percentile rank function does works with Amibroker 5.3 and lower versions.

3. Mahir Shah says:

Hi Rajandran,
I have seen your supertrend indicator for metasock, I was wondering if you can/ have coded Connors RSI for metastock. Any help will be highly appreciated.
Regards
Mahesh

4. Chandrakant says:

Dear Mr. Rajandran,

Please guide us how we can add explore code for connorsRSI for stock selection.

Regards,
Chandrakant

5. Dear Rajandran ,
Is there any source from where i can get MCX-SX Currency EOD data in metastock form .

Thanks & Regards
Abhay Mehrotra

6. anil says:

pls correct this afl

SetChartOptions(1,chartShowArrows|chartShowDates|chartWrapTitle);
Title = EncodeColor(colorYellow)+ Name() + ” – ” + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorSkyblue) +
” – ” + Date() +” – “+”
” +EncodeColor(colorYellow) +”Open-“+O+” “+”High-“+H+” “+”Low-“+L+” “+”Close-“+C+” “+ “Volume= “+ WriteVal(V);

//SetBarsRequired(100000,0);
GraphXSpace = 15;
EntrySignal = F1();
ExitSignal = F2();
Color = IIf( EntrySignal, colorBlue, IIf( ExitSignal, colorRed, colorGrey50 ));
Plot( C, “Price”, Color, styleBar | styleThick );

SetChartBkColor(ParamColor(“Outer panel color “,colorLightYellow));
SetChartBkColor(ParamColor(“BackGround Color”, colorDarkGrey));
messageboard =ParamToggle(“Message Board”,”Show|Hide”,1);
target = Optimize(“Target”,Param(“Target (Change To Optimise)”,10,0.05,1000,0.05),0.05,1000,0.05);
AutoTarget = ParamToggle(“Auto Target Mode”, “Yes|No”,1);
starttime=Param(“Market Start Time”,091500,000000,235959,1 );
endtime=Param(“Market End Time”,152500,000000,235959,1 );
BackTestMode = ParamToggle(“Use Back Tester Settings”, “Yes|No”);
Param_Margin = Param(“Margin required (used for backtesting only)”, 15,0.001,100,0.001);
Param_LotSize = Param(“Lot Size (used for backtesting only)”, 50,1,50000,1);
Param_NoOfLots = Param(“No of lots normally traded (used for backtesting only)”,1,1,10000,1);
ConstantLot = ParamToggle(“Trade Constant Lot”, “No|Yes”);

C13=20;
C14=2.1;
C15=12;
Miny = Status(“axisminy”);
Maxy = Status(“axismaxy”);
lvb = Status(“lastvisiblebar”);
fvb = Status(“firstvisiblebar”);
pxwidth = Status(“pxwidth”);
pxheight = Status(“pxheight”);

#include_once “SS_include.afl”

bars = LastValue( IIf(BarsSincebuy endtime AND NOT Relax)==1) Say(“Exit position in “+Name()+” at current market price “+C);
if (SelectedValue(bookshort AND NOT Short AND NOT Relax)==1) Say(“Target achieved in short in “+Name()+” at “+tar);
if (SelectedValue(booklong AND NOT Buy AND NOT Relax)==1) Say(“Target achieved in long in “+Name()+” at “+tar);
}

{
AlertIf( booklong AND NOT Buy AND NOT Relax, “EMAIL”, “Target achieved in long in “+FullName(), 1 );
AlertIf( Short, “EMAIL”, “Short signal generated on “+FullName(), 1 );
AlertIf( bookshort AND NOT Short AND NOT Relax, “EMAIL”, “Target achieved in long in “+FullName(), 1 );
AlertIf( TimeNum() > endtime AND NOT Relax, “EMAIL”, “Exit position in “+FullName(), 1 );
}

AlertIf( booklong AND NOT Buy AND NOT Relax, “”, “Target achieved in long in “+Name(), 4 );
AlertIf( Short, “”, “Short Signal Generated”+Name(), 4 );
AlertIf( bookshort AND NOT Short AND NOT Relax, “”, “Target achieved in short in “+Name(), 4 );
AlertIf( TimeNum() > endtime AND NOT Relax, “”, “Exit position in “+Name(), 4 );
}

if
(messageboard == 1 )
{

GfxSelectFont( “Tahoma”, 13, 100 );

GfxSetBkMode( 1 );

GfxSetTextColor
( colorWhite );

if ( SelectedValue( LastSignal ) == 1 )
{
GfxSelectSolidBrush( colorDarkBlue );
}
else
{
GfxSelectSolidBrush( colorDarkRed );
}

pxHeight = Status( “pxchartheight” ) ;

xx = Status( “pxchartwidth”);

Left = 1100;

width = 310;

x = 5;

x2 = 290;

y = pxHeight;

GfxSelectPen
( colorLightBlue, 1); // border color

GfxRoundRect
( x, y – 90, x2, y , 7, 7 ) ;

GfxTextOut

GfxTextOut
( (“” + WriteIf(Buy, “Go Long At “+C+” – SL ” +sl,””)), 13, y-65);

GfxTextOut
( (“” + WriteIf (Short, “Go Short At “+C+” – SL ” +sl,””)), 13, y-65);

GfxTextOut
( (“” + WriteIf (TimeNum() > endtime AND NOT Relax, “Exit Position At “+C,””)), 13, y-65);

GfxTextOut
( (“” + WriteIf (Long AND NOT Buy, “Long At “+BuyPrice+” – SL ” + sl + “”,””)), 13, y-65);

GfxTextOut
( (“” + WriteIf (shrt AND NOT Short, “Short At “+ShortPrice+” – SL ” + sl + “”,””)), 13, y-65);

GfxTextOut
( (“” + WriteIf (Relax, “Not In Trade – RELAX!!!”,””)), 13, y-65);

GfxTextOut
( (“” + WriteIf (shrt AND NOT Short, “Current P/L: “+(ShortPrice-C)+” Points”,””)), 13, y-45);

GfxTextOut
( (“” + WriteIf (Long AND NOT Buy, “Current P/L: “+(C-BuyPrice)+” Points”,””)), 13, y-45);

GfxTextOut
( (“” + WriteIf (bookshort AND NOT Short AND NOT Relax, “Profit Booked: “+(ShortPrice-tar)+” Points”,””)), 13, y-45);

GfxTextOut
( (“” + WriteIf (booklong AND NOT Buy AND NOT Relax, “Profit Booked: “+(tar-BuyPrice)+” Points”,””)), 13, y-45);

GfxTextOut
( (“” + WriteIf (Long OR Buy OR Shrt OR Short, “Target: “+tar,””)), 13, y-25);

GfxTextOut
( (“” + WriteIf (booklong AND NOT Buy AND NOT Relax, “Target Done: “+tar,””)), 13, y-25);

GfxTextOut
( (“” + WriteIf (bookshort AND NOT Short AND NOT Relax, “Target Done: “+tar,””)), 13, y-25);
}

GfxSetBkMode( 1 );
GfxSelectFont(“Tahoma”, Status(“pxheight”)/C13);
GfxSetTextAlign( 6 );
GfxSetTextColor( ColorRGB (217,217,213));
GfxTextOut( “buy sell”, Status(“pxwidth”)/C14, Status(“pxheight”)/C15 );
GfxSelectFont(“Tahoma”, Status(“pxheight”)/C13*0.5 );
GfxSetTextColor( ColorRGB (103,103,103));
GfxTextOut( “By”, Status(“pxwidth”)/C14, Status(“pxheight”)/C15*2.5 );
GfxSelectFont(“Tahoma”, Status(“pxheight”)/C13*0.5 );
GfxSetTextColor( ColorRGB (103,103,103));
GfxTextOut( “buy sell”, Status(“pxwidth”)/C14, Status(“pxheight”)/C15*4 );
GfxSelectFont(“MS Sans Serif”, 10, 500, False, False, 0);

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-25);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=25);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=35);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-30);

PlotShapes(IIf(TimeNum() > endtime AND Long, shapeHollowStar, shapeNone),colorGold, 0,H, Offset=15);
PlotShapes(IIf(TimeNum() > endtime AND Shrt, shapeHollowStar, shapeNone),colorGold, 0,L, Offset=-15);

PlotShapes(IIf(Sell AND NOT Short, shapeStar, shapeNone),colorRed, 0, H, Offset=15);
PlotShapes(IIf(Cover AND NOT Buy, shapeStar, shapeNone),colorBlue, 0,L, Offset=-15);

PlotShapes(IIf(booklong AND NOT Buy AND NOT Relax, shapeStar, shapeNone),colorRed, 0, H, Offset=15);
PlotShapes(IIf(bookshort AND NOT Short AND NOT Relax, shapeStar, shapeNone),colorBlue, 0,L, Offset=-15);

//Settings for Backtester
if (BackTestMode==True)
{
SetOption(“AllowSameBarExit”, False);
SetOption(“AllowPositionShrinking”, False);
SetOption(“FuturesMode”, True);
SetOption(“InterestRate”,0);
SetOption(“MaxOpenPositions”,1);
RoundLotSize = Param_LotSize;
SetOption(“MinShares”,RoundLotSize);
SetOption(“PriceBoundChecking”,False);
//SetOption(“CommissionMode”,3);
//SetOption(“CommissionAmount”,12.5/RoundLotSize);

SetOption(“AccountMargin”,Param_Margin);
SetOption(“ReverseSignalForcesExit”,True);
SetOption(“UsePrevBarEquityForPosSizing”,True);
ApplyStop(stopTypeTrailing, stopModePoint, TSL, True, True );

if (ConstantLot==True)
{
PositionSize = C*RoundLotSize*Param_NoOfLots;
}
SellPrice = Close;
ShortPrice = Close;
CoverPrice = Close;
}
_SECTION_END();

7. Surez says:

Rajandren Sir, Did u correct Mr.Anil’s request ?
it showing error in #include_once “SS_include.afl”
Do we need SS_include.afl indicator ?
Pls guide me

8. shabber merchant says:

hello
i would like to know the above mentioned connors rsi work with esignal or it has to modified ,if so pls modify it , it will be helpfull.
thanks.

9. senthil says:

hello sir i am using MT4 demo softwar i want mcx commodity live chat for lowest cost pls help me sir

10. Thomas Musselman says:

Have you seen or do you know how to code connorsrsi for Excel?
-T.M. in Scottsdale

11. James Tucker says:

I too would love to have a copy of Connorsrsi for Excel, if anyone has it…
Thank you
JWT