# AFL for Stochastic RSI

I am trying to write AFL for "STOCHASTIC RSI" as given in chartink.com.
But Unable to write the correct one as required.

hi
Did you try StochasticRSI that allready exist for Amibroker?
also in this forum there is at least one Afl.

Yes, I have tried.
But, I am much use to with Stochastic RSI used in chartink.com
That's why I am looking for the AFL

@drana, So what is the formula from chartink?

What have you tried?

What isn't coming the way you think it should?

Show your work, and the reference, and we can probably help.

3 Likes

@snoopy.pa30 i bet he needs plotOHLC() to look like his foto. Let see hes answer first.

1 Like

@drana All you need is Cloud style with clipping region

Here you can find the details:

https://www.amibroker.com/guide/h_indbuilder2.html

1 Like

I have tried this:

``````_SECTION_BEGIN("STOCHRSI");

rise=Max(0,Close-Ref(Close,-1));
fall=Max(0,Ref(Close,-1)-Close);
mmrise=Wilders(rise,20);
mmfall=Wilders(fall,20);
RS=mmrise/mmfall;
mioRSI=100-100/(1+RS);

upper=HHV(High,20);
lower=LLV(Low,20);
oscillator=(Close-lower)/(upper-lower)*100;
lineaK=MA(oscillator, 20);

StochRSI=((mioRSI*lineaK)/100)*1.43;

_SECTION_END();

OverBought=70;

Plot(OverBought,"OverBought",colorRed);

OverSold = 30;

Plot(OverSold,"OverSold",colorBrightGreen);

SetChartOptions(0,0,ChartGrid30 | ChartGrid70 );
r = StochRSI;
Plot( r, "StochRSI", colorBlue,styleLine );
PlotOHLC( r,r,50,r, "", IIf( r > 50, colorRed, colorGreen ), styleCloud | styleClipMinMax, 30, 70 );
``````

But, I am not getting the Values what I required.
StochRSI value for a stock in chartink.com is different from mine.

@drana try this:

``````_SECTION_BEGIN( "Chande/Kroll Stochastic RSI Oscillator" );
periodRSI = Param( "Period Stochastic RSI", 14, 2, 252, 1 );
overSoldLevel = Param( "Oversold Level", 20, 0, 45, 5 );
overBoughtLevel  = Param( "Overbought Level", 80, 55, 100, 5 );
stochLineColor = ParamColor( "Stochastic Line Color", ColorRGB( 255, 0, 255 ) ); // Magenta
overSoldColor = ParamColor( "Oversold color", colorBlue );
overBoughtColor = ParamColor( "overbought color", colorRed );

// As described in the book "The New Technical Trader" - pp. 124:133
// by Tushar S. Chande and Stanley Kroll - 1994 - Wiley
aRSI =  RSI( periodRSI );
llvRSI = LLV( aRSI, periodRSI );
hhvRSI = HHV( aRSI, PeriodRSI );
// original book formula range is 0..1 - here multiplied by 100
sr = 100 * ( ( aRSI - llvRSI ) / Max( hhvRSI - llvRSI, 0.00001 ) ); // Max used to avoid div/0 warning 501

GraphXSpace = 5;
Plot( sr, "Stochastic RSI(" + periodRSI + ")", stochLineColor, styleLine, Null, Null, 0, 2 );
Plot( overSoldLevel, "", overSoldColor, styleDashed, Null, Null, 0, 1 );
Plot( overBoughtLevel, "", overBoughtColor, styleLine | styleDashed, Null, Null, 0, 1 );
PlotOHLC( sr, sr, 50, sr, "", IIf( sr > 50, overBoughtColor, overSoldColor ), styleNoLabel | styleCloud | styleClipMinMax, overSoldLevel, overBoughtLevel );
_SECTION_END();
``````

In any case, keep in mind that the site you cited may use a slightly different formula. I have seen other implementations that apply a second independent period to the RSI to calculate the LLV and HHV arrays.

8 Likes

Thank You for the prompt response.

Can Anyone Provide the exact AFL for the above STOCHASTIC RSI ?

@drana, IMHO this is pretty close to that chart (enlarge/save it to see it properly):

To achieve it, modify the formula I posted above as follow:

``````// Existing code above/including this line - no change
GraphXSpace = 5;

SetChartOptions(1, chartShowDates, chartGrid0 | chartGrid50 | chartGrid100);
_N( Title = StrFormat( "Stochastic RSI(%2.0f) = %2.2f", periodRSI, SelectedValue( sr ) ) );
// _N( Title = StrFormat( "Stochastic RSI(%2.0f) = %2.2f", periodRSI, LastVisibleValue( sr ) ) );
Plot( sr, "", stochLineColor, styleLine | styleThick, Null, Null, 0, 2 );
Plot( overSoldLevel, "", overSoldColor, styleline, Null, Null, 0, 1 );
Plot( overBoughtLevel, "", overBoughtColor, styleLine, Null, Null, 0, 1 );
PlotOHLC( sr, sr, 50, sr, "", IIf( sr > 50, overBoughtColor, overSoldColor ), styleNoLabel | styleCloud | styleClipMinMax, overSoldLevel, overBoughtLevel );
_SECTION_END();
``````

Note also that to get the chart as per your posted picture you have to change the period to 20.

Moreover, please, beware that in your original chart the OHLCV values are the ones corresponding to the date 04-04-18 while the StockRSI value (30.92) is the "last visible value" and is not the one of the "selected" day (where it was 74.76).

This is INCONSISTENT.

In the above code, just to create the sample chart image, I commented out the line that will show the SelectedValue (i.e., the correct value).

When you have compared some of your charts with the modified formula, and you find that it works as expected (at least I hope so..) I suggest you to uncomment that line and delete the next one!

5 Likes

@drana Beppe has provided you with a nice afl. Your problem is that you appear not to know what the StochasticRSI is. You found some free web site that has a chart but you did not ask them what formula they are using.

The "REAL" StochasticRSI was written about by Chande and Kroll in the mid-1990's. Many others have made modifications on their publication. This was also discussed on this forum. If you don't like the original then find out what modifications your free web site has made.

"Exact afl" ? You have not provided the chartlink formula.

As for your chartlink version, what is it if it is not the original? Do you know what the RSI is, do you know how the stochastic is usually calculated? Where is the StochK, and where is the StochD? You see, "exact" depends upon the underlying forumula.

3 Likes