Skip to main content

Dynamic RSI for Amibroker (AFL)

joeoil over 14 years ago Amibroker (AFL)

  • Rating:
    2 / 5 (Votes 3)
  • Tags:
    oscillator, amibroker

// Dynamic RSI Thresholds — J. Varney

/*
Instead of the traditional fixed RSI = 30 and 70 thresholds to define
oversold and overbought, this AFL uses standard deviation to determine
dynamic overbought and oversold theshold lines.
*/

Screenshots

Indicator / Formula

Copy & Paste Friendly
Period = Param("Period", 10, 2, 30, 1);
myRSI = RSI(Period);
Adev = StDev(myRSI, 3*Period);
Hiline = 50 + 1.8*Adev;
Loline = 50 - 1.8*Adev;
Plot(myRSI,"",colorBlue,8);
Plot(Hiline,"",colorRed,1);
Plot(Loline,"",colorGreen,1);

Title = " " +Name() +
" Dynamic RSI Threshold, t = " + 
WriteVal(period, format=1.0) + " "; 

6 comments

1. ole
over 14 years ago

How do you know that this is an improvment? What are you backtest results?

2. joeoil
over 14 years ago

Re: ole

I have not said this indicator is better or worse than the standard RSI. You have to test if for yourself.
I have tested it on a few stocks and indices and as far as I can see it gives a little better results than standard RSI.

over 14 years ago

Hey Joeoil, thanks for posting this code. I used your Std Dev idea in another way, by adding/subtracting it to a moving average of the RSI and got some pretty good results. I actually made three trades using the signals today.

4. joeoil
over 14 years ago

AlgoTrader

If possible, can I ask you how you write the AFL when you add/subtr StdDev to the moving averages of RSI?
I’m not that good in AFL programming, but I think it could have been interesting to see how your code works out with the stocks/indexes I trade.

It is no problem if you don’t wan’t to share the code.

Regards
Joeoil

6. morgen
4 months ago

Maybe you like this.

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//TRENDADVISOR

pointer[0] = 0;

/* Phase filter */

Cond1 = Close > MA(Close, 50)AND NOT(Close > MA(Close, 200))AND NOT(MA(Close, 50) > MA(Close, 200));
Cond2 = Close > MA(Close, 50)AND Close > MA(Close, 200)AND NOT(MA(Close, 50) > MA(Close, 200));
Cond3 = Close > MA(Close, 50)AND Close > MA(Close, 200)AND MA(Close, 50) > MA(Close, 200);
Cond4 = NOT(Close > MA(Close, 50))AND Close > MA(Close, 200)AND MA(Close, 50) > MA(Close, 200);
Cond5 = NOT(Close > MA(Close, 50))AND NOT(Close > MA(Close, 200))AND MA(Close, 50) > MA(Close, 200);
Cond6 = NOT(Close > MA(Close, 50))AND NOT(Close > MA(Close, 200))AND NOT(MA(Close, 50) > MA(Close, 200));

for (i = 1; i < BarCount; i++)
{

  if (Cond1[i])
    pointer[i] = 1;
  if (Cond2[i])
    pointer[i] = 2;
  if (Cond3[i])
    pointer[i] = 3;
  if (Cond4[i])
    pointer[i] = 4;
  if (Cond5[i])
    pointer[i] = 5;
  if (Cond6[i])
    pointer[i] = 6;

}

/* Plot Graphic */
//GraphXSpace= 15 ;
dynamic_color = IIf(pointer < 4, colorGreen, colorRed);
//Plot(pointer, "TrendAdv2", dynamic_color, styleHistogram | styleThick, Null, Null, 0);
//SetChartBkGradientFill(ParamColor("BgTop", colorWhite), ParamColor("BgBottom", colorLightYellow));

Cond= pointer<4 ;

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//FORMULE

stoch20up80 =  StochK()>StochD()  AND StochK()>20 OR StochK()>=80 ;
MACDup= MACD()>Signal() ;
MACDup0= MACD()>0 ;
Hist= MACD()-Signal() ;
Histup= Hist>Ref(Hist,-1) ;
macdhistup= MACD()>Signal() AND Hist>Ref(Hist,-1) ;
macdstoch20dn= MACD()<Signal() AND StochK()<StochD() AND StochK()<80 OR StochK()<20 ;
histstochdn= Hist<Ref(Hist,-1) AND StochK()<StochD() AND StochK()<80 OR StochK()<20 ;
histstoch20up= Hist>Ref(Hist,-1) AND StochK()>StochD() AND StochK()>20 ;
CCIup0MA= CCI()>MA(CCI(),7) AND CCI()>0 ;
MA20up= MA(C,20)>=Ref(MA(C,20),-1) ;
PDIupmdi= PDI()>MDI() ;
Uptrend=  MACD()>Signal() AND PDI()>MDI() ;
Uportrend=  MACD()>Signal() OR PDI()>MDI()   ;
Dntrend=  MACD()<Signal() AND PDI()<MDI()   ;
Dnortrend=  MACD()<Signal() OR PDI()<MDI()  ;
cup = C>=Ref(C,-1) ;

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

// Downloaded From https://www.WiseStockTrader.com
// by Joeoil

//RSI() = 14 // per = 14 by default

SetChartOptions(0,chartShowArrows|chartShowDates);

Period = Param("Period", 14, 2, 30, 1);
myRSI = RSI(Period);
marsi = MA(myRSI,7) ;
Adev = StDev(myRSI, 3*Period);
Hiline = 50 + 1.8*Adev;
Loline = 50 - 1.8*Adev;
mid = (Hiline + Loline)/2 ;
//Plot(myRSI,"",colorBlue,8);
Plot(myRSI,"myRSI&DynamicResSupp",colorBlack,styleDots+styleThick );
Plot(maRSI,"MA(RSI,7)",colorRed,styleLine+styleThick );
Plot(Hiline,"Res",colorRed,styleLine+styleThick );
Plot(Loline,"Supp",colorGreen,styleLine+styleThick);
Plot(mid,"Mid",colorBlue,styleLine+styleThick);

myrsiup = myRSI>=Ref(myRSI,-1) ;

/*
MODEL
cloudcol = IIf(bbupcond,colorAqua ,
           IIf(bbdncond,colorPink ,colorYellow)) ;
PlotOHLC( bbt, bbt, bbb, bbb, "", cloudcol , styleNoLabel | styleCloud | styleNoRescale, Null, Null, Null, -1 );
*/

cloudcol = IIf(cond AND (myRSI>=mid AND myRSI>marsi OR myRSI>=Hiline),colorAqua , 
           IIf(myRSI<mid OR !cond, colorPink , colorYellow )) ;
PlotOHLC( Hiline, Hiline, Loline, Loline, "", cloudcol , styleNoLabel | styleCloud | styleNoRescale, Null, Null, Null, -1 );

/*
Title = " " +Name() +
" Dynamic RSI Threshold, t = " + 
WriteVal(period, format=1.0) + " "; 
*/

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//FORMULE

stochup=  StochK()>StochD() ;
stochdn80= StochK()<80 ;
stochtotdn= (StochK()<StochD() AND StochK()<80) ;
MACDup= MACD()>Signal() ;
MACDup0= MACD()>0 ;
Hist= MACD()-Signal() ;
Histup= Hist>Ref(Hist,-1) ;
MACDhistup = MACD()>Signal() AND Hist>Ref(Hist,-1) ;
MACDstoch20dn = MACD()<Signal() AND StochK()<StochD() AND StochK()<80 OR StochK()<20 ;
MFIup30ma = MFI()>MA(MFI(),7) AND MFI()>30 ;
CCIupMA= CCI()>MA(CCI(),7) ;
PDIupmdi= PDI()>MDI() ;
PDIup20= PDI()>20 ;
Uptrend=  (MACD()>Signal() AND PDI()>MDI())   ;
Dntrend=  (MACD()<Signal() AND PDI()<MDI())   ;

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//PARAMTOGGLE

// This combines indicators into one timing Signal
//function ParamOptimize( description, default, minv, maxv, step )
//    { return Optimize(description, Param(description,default, minv, maxv, step ), minv, maxv, step ); }
tgl = ParamToggle("Result", "AND logic|Compare");
// switch test calculation and compare the results
if(tgl)
{
myBuy =   cond AND (myRSI>=mid AND myRSI>marsi OR myRSI>=Hiline) ;
myShort = myRSI<mid OR !cond ; 
}
else
{
myBuy   = IIf( pointer<4 AND (myRSI>=mid AND myRSI>marsi OR myRSI>=Hiline)      ,1,0);
myShort = IIf( myRSI<mid OR pointer>=4       ,1,0);
}

Buy = ExRem(myBuy, myShort) ;
Sell = ExRem(myShort, myBuy) ;

StrongBuy =  Buy AND (macdhistup OR uptrend) AND mfiup30ma ;
MediumBuy = Buy AND (macdhistup OR uptrend) ;
WeakBuy = Buy ;

StrongSell = Sell AND (macdstoch20dn OR dntrend) AND !mfiup30ma ;
MediumSell = Sell AND (macdstoch20dn OR dntrend) ;
WeakSell = Sell ;

PlotShapes(IIf(StrongBuy,shapeUpTriangle,shapeNone),colorBlue,0 );
PlotShapes(IIf(MediumBuy,shapeUpArrow,shapeNone),colorBlue,0 );
PlotShapes(IIf(WeakBuy,shapeHollowUpArrow,shapeNone),colorBlue,0 );
PlotShapes(IIf(StrongSell,shapeDownTriangle,shapeNone),colorRed,0 );
PlotShapes(IIf(MediumSell,shapeDownArrow,shapeNone),colorRed,0 );
PlotShapes(IIf(WeakSell,shapeHollowDownArrow,shapeNone),colorRed,0 ) ; 

/*
PlotShapes(IIf(StrongBuy,shapeUpTriangle,shapeNone),colorBlue,0,Low,Offset=-20);
PlotShapes(IIf(MediumBuy,shapeUpArrow,shapeNone),colorBlue,0,Low,Offset=-20);
PlotShapes(IIf(WeakBuy,shapeHollowUpArrow,shapeNone),colorBlue,0,Low,Offset=-20);
PlotShapes(IIf(StrongSell,shapeDownTriangle,shapeNone),colorRed,0,High,Offset=-20);
PlotShapes(IIf(MediumSell,shapeDownArrow,shapeNone),colorRed,0,High,Offset=-20);
PlotShapes(IIf(WeakSell,shapeHollowDownArrow,shapeNone),colorRed,0,High,Offset=-20);
*/

/*
PlotShapes(IIf(StrongBuy,shapeUpTriangle,shapeNone),colorBlue,0,haLow,Offset=-10);
PlotShapes(IIf(MediumBuy,shapeUpArrow,shapeNone),colorBlue,0,haLow,Offset=-10);
PlotShapes(IIf(WeakBuy,shapeHollowUpArrow,shapeNone),colorBlue,0,haLow,Offset=-10);
PlotShapes(IIf(StrongSell,shapeDownTriangle,shapeNone),colorRed,0,haHigh,Offset=-10);
PlotShapes(IIf(MediumSell,shapeDownArrow,shapeNone),colorRed,0,haHigh,Offset=-10);
PlotShapes(IIf(WeakSell,shapeHollowDownArrow,shapeNone),colorRed,0,haHigh,Offset=-10);
*/ 

//PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorBlue,0,haLow,Offset=-10);
//PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,haHigh,Offset=-10);

//PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorBlue,0,trailArray);
//PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,trailArray);

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx






Leave Comment

Please login here to leave a comment.