petergluis
Active member
I combined RSI and Stochastic indicators into one indicator with highlighted overbought and oversold states. Enjoy your successful trading.
Ruby:
# Smoothed RSI2
# Pensar
# StochasticFull code is from ThinkorSwim
# Modified by Peter Luis
# Modified on 03/03/2022
declare lower;
input period = 2;
input over_bought = 90;
input over_sold = 10;
input idata = close;
input averageType = AverageType.WILDERS;
input over_bought1 = 80;
input over_sold1 = 20;
input KPeriod =5;
input DPeriod = 3;
input priceH = high;
input priceL = low;
input priceC = close;
input slowing_period = 3;
input averageType1 = AverageType.SIMPLE;
input showBreakoutSignals = {default "No", "On FullK", "On FullD", "On FullK & FullD"};
def NetChgAvg = MovingAverage(averageType, idata - idata[3], period);
def TotChgAvg = MovingAverage(averageType, AbsValue(idata - idata[3]), period);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
plot RSI = 50 * (ChgRatio + 1);
RSI.DefineColor("OverBought", color.red);
RSI.DefineColor("Normal", color.gray);
RSI.DefineColor("OverSold", color.green);
RSI.AssignValueColor(if RSI > over_Bought then RSI.color("OverBought")
else if RSI < over_Sold then RSI.color("OverSold")
else RSI.color("Normal"));
RSI.SetLineWeight(2);
plot OverSold = over_Sold1;
plot OverBought = over_Bought1;
OverSold.SetDefaultColor(color.green);
Oversold.setstyle(curve.short_dash);
Oversold.setlineweight(1);
OverBought.SetDefaultColor(color.red);
OverBought.setstyle(curve.short_dash);
Oversold.setlineweight(1);
AddCloud(if RSI > OverBought then RSI else double.nan, Overbought, color.red,color.red);
AddCloud(if RSI < OverSold then RSI else double.nan, OverSold, color.green,color.green);
def lowest_k = Lowest(priceL, KPeriod);
def c1 = priceC - lowest_k;
def c2 = Highest(priceH, KPeriod) - lowest_k;
def FastK = if c2 != 0 then c1 / c2 * 100 else 0;
plot FullK = MovingAverage(averageType1, FastK, slowing_period);
plot FullD = MovingAverage(averageType1, FullK, DPeriod);
plot OverBought1 = over_bought;
plot OverSold1 = over_sold;
def upK = FullK crosses above OverSold;
def upD = FullD crosses above OverSold;
def downK = FullK crosses below OverBought;
def downD = FullD crosses below OverBought;
plot UpSignal;
plot DownSignal;
switch (showBreakoutSignals) {
case "No":
UpSignal = Double.NaN;
DownSignal = Double.NaN;
case "On FullK":
UpSignal = if upK then OverSold else Double.NaN;
DownSignal = if downK then OverBought else Double.NaN;
case "On FullD":
UpSignal = if upD then OverSold else Double.NaN;
DownSignal = if downD then OverBought else Double.NaN;
case "On FullK & FullD":
UpSignal = if upK or upD then OverSold else Double.NaN;
DownSignal = if downK or downD then OverBought else Double.NaN;
}
UpSignal.setHiding(showBreakoutSignals == showBreakoutSignals."No");
DownSignal.setHiding(showBreakoutSignals == showBreakoutSignals."No");
FullK.SetDefaultColor(GetColor(1));
Fullk.SetLineWeight (2);
FullD.SetDefaultColor(GetColor(0));
FullD.SetLineWeight (2);
OverBought.SetDefaultColor(GetColor(1));
OverSold.SetDefaultColor(GetColor(1));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
Last edited: