Combined RSI and Stochastic Indicator For ThinkOrSwim

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:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

I combined RSI and Color-coded Stochastic Indicators into one indicator. Enjoy your trading.
Ruby:
# Jan 2022 the mucking about with code from mashume
# TD Ameritrade IP Company, Inc. (c) 2008-2021
# Smoothed RSI2
# Pensar
# Modified by Peter Luis
# Modified on March 4, 2022


declare lower;

input over_bought = 80;
input over_sold = 20;
input KPeriod = 5;
input DPeriod = 3;
input priceH = high;
input priceL = low;
input priceC = close;
input slowing_period = 3;
input averageType = AverageType.SIMPLE;
input showBreakoutSignals = {default "No", "On FullK", "On FullD", "On FullK & FullD"};
input period = 2;
input over_bought1 = 90;
input over_sold1 = 10;
input idata = close;
input averageType1= AverageType.WILDERS;

def NetChgAvg = MovingAverage(averageType1, idata - idata[3], period);
def TotChgAvg = MovingAverage(averageType1, 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"));
plot OverSold = over_Sold;
plot OverBought = over_Bought;
     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(averageType, FastK, slowing_period);
plot FullD = MovingAverage(averageType, FullK, DPeriod);
addCloud (FullK, Fulld, color.GREEN, color.red);
plot OverBought1 = over_bought;
plot OverSold1 = over_sold;

def upK = FullK < 50 and FullK crosses above FullD;
def downK = FullK crosses below FullD;

def upD = FullD crosses above OverSold;
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;
}


plot FiftyMark = 50;
FiftyMark.setDefaultColor(color.light_green);


UpSignal.setHiding(showBreakoutSignals == showBreakoutSignals."No");
DownSignal.setHiding(showBreakoutSignals == showBreakoutSignals."No");

FullK.SetDefaultColor(GetColor(5));
FullD.SetDefaultColor(GetColor(0));
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:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
310 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top