Supertrended RSI [AlgoAlpha] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
OqUK4rM.png

Author Message:

How It Works:
At the core of this indicator is the combination of the Relative Strength Index (RSI) and the Supertrend framework, it does so by applying the SuperTrend on the RSI. The RSI settings can be adjusted for length and smoothing, with the option to select the data source. The Supertrend calculation takes into account a specified trend factor and the Average True Range (ATR) over a given period to determine trend direction.

Visual elements include plotting the RSI, its moving average, and the Supertrend line, with customizable colors for clarity. Overbought and oversold conditions are highlighted, and trend changes are filled with distinct colors.

CODE:

CSS:
# https://www.tradingview.com/v/tjP35RG5/
#// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © AlgoAlpha
#indicator("Supertrended RSI [AlgoAlpha]", overlay = false, timeframe = "")
# Converted and mod by Sam4Cok@Samer800    - 02/2024
declare lower;
input colorBars = yes;
input signalType = {Default "Crosses Supertrend/RSI", "Crosses Overbought/Oversold", "Don't Show"};
input timeframe = {Default "Chart", "Custom"};
input customTimeframe = AggregationPeriod.FIFTEEN_MIN;
input rsiSource = FundamentalType.CLOSE;  # "RSI Source"
input rsiLength = 14;                 # "RSI Length"
input overbought = 70;
input oversold = 30;
input SmoothRsi = no;                 # "Smooth RSI?"
input smoothingLength = 21;           # "RSI Smoothing Length"
input showMovingAverage = yes;        # "Show RSI MA?"
input movingAverageType = {"SMA", default "HMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"}; # "MA Type"
input movingAverageLength = 14;       # "MA Length"
input trendFactor = 0.80;             # "Factor"
input atrLength = 10;                 # "ATR Length"

def na = Double.NaN;
def last = isNaN(close);
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def dont = signalType==signalType."Don't Show";
def cross = signalType==signalType."Crosses Supertrend/RSI" and !dont;
def obos  = signalType==signalType."Crosses Overbought/Oversold" and !dont;

def src;def vol;
Switch (timeframe) {
Case "Custom" :
    src = Fundamental(FundamentalType = rsiSource, Period = customTimeframe);
    vol = Volume(Period = customTimeframe);
Default :
    src = Fundamental(FundamentalType = rsiSource);
    vol = volume;
}
#-- Color
DefineGlobalColor("Up", CreateColor(0, 255, 187)); # "Up Color"
DefineGlobalColor("Dn", CreateColor(255, 17, 0));  # "Up Color"
DefineGlobalColor("bg", CreateColor(51, 51, 51)); # "Up Color"

#vwma(source, length)
script VWMA {
input src = close;
input len = 14;
input vol = volume;
    def nom = Average(src * vol, len);
    def den = Average(vol, len);
    def VWMA = nom / den;
    Plot result = VWMA;
}
#// Function to calculate Supertrend
script Supertrend {
    input factor = 0.8;
    input atrPeriod = 10;
    input source = close;
    def highestHigh = Highest(source, atrPeriod);
    def lowestLow = Lowest(source, atrPeriod);
    def trueRange = if isNaN(highestHigh[1]) then highestHigh - lowestLow else TrueRange(highestHigh, source, lowestLow);
    def nATR = WildersAverage(trueRange, atrPeriod);
    def upper = source + factor * nATR;
    def lower = source - factor * nATR;
    def lowerBand;
    def upperBand;
    def prevLowerBand = if lowerBand[1] then lowerBand[1] else lower;
    def prevUpperBand = if upperBand[1] then upperBand[1] else upper;
    lowerBand = if lower > prevLowerBand or source[1] < prevLowerBand then lower else prevLowerBand;
    upperBand = if upper < prevUpperBand or source[1] > prevUpperBand then upper else prevUpperBand;
    def trendDirection;
    def supertrendValue;
    def prevSupertrend = if isNaN(supertrendValue[1]) then upper else supertrendValue[1];
    if nATR[1]==0 {
        trendDirection = 1;
    } else if prevSupertrend == prevUpperBand {
        trendDirection = if source > upperBand then -1 else 1;
    } else {
        trendDirection = if source < lowerBand then 1 else -1;
    }
    supertrendValue = if trendDirection == -1 then lowerBand else upperBand;
    plot ST = if supertrendValue then supertrendValue else Double.NaN;
    plot dir = if trendDirection then trendDirection else Double.NaN;
}
#// Calculating RSI
def nRSI = RSI(Price = src, Length = rsiLength);
def rsiValue = if SmoothRsi then HullMovingAvg(nRSI, smoothingLength) else nRSI;

def rsiMovingAverage;
switch (movingAverageType) {
case "SMA" :
    rsiMovingAverage = Average(rsiValue, movingAverageLength);
case "EMA" :
    rsiMovingAverage = ExpAverage(rsiValue, movingAverageLength);
case "SMMA (RMA)" :
    rsiMovingAverage = WildersAverage(rsiValue, movingAverageLength);
case "WMA" :
    rsiMovingAverage = WMA(rsiValue, movingAverageLength);
case "VWMA" :
    rsiMovingAverage = VWMA(rsiValue, movingAverageLength, vol);
default :
    rsiMovingAverage = HullMovingAvg(rsiValue, movingAverageLength);
}
#// Calculating Supertrend based on RSI values
def rsiST = Supertrend(trendFactor, atrLength, rsiValue).ST;
def Dir = Supertrend(trendFactor, atrLength, rsiValue).DIR;

#/ Plotting
plot supertrend = rsiST;     # "Supertrend"
plot rsiMa = if showMovingAverage then rsiMovingAverage else na;
plot overboughtLine = if last then na else overbought;
plot oversoldLine = if last then na else oversold;
plot midLine = if last then na else (overboughtLine + oversoldLine) / 2;

supertrend.AssignValueColor(if Dir == -1 then GlobalColor("Up") else GlobalColor("Dn"));
rsiMa.SetDefaultColor(Color.GRAY);
midLine.SetStyle(Curve.SHORT_DASH);
overboughtLine.SetLineWeight(2);
oversoldLine.SetLineWeight(2);
midLine.SetDefaultColor(Color.DARK_GRAY);
overboughtLine.AssignValueColor(if rsiValue>=overbought then GlobalColor("Dn") else
                                if rsiValue<=midLine then Color.DARK_GRAY else CreateColor(rsiValue * 2.55, rsiValue/3, 0));
oversoldLine.AssignValueColor(if rsiValue<=oversold then GlobalColor("Up") else
                              if rsiValue>=midLine then Color.DARK_GRAY else CreateColor(0, rsiValue * 2.55, rsiValue*1.5));

AssignPriceColor(if !colorBars then Color.CURRENT else
                 if rsiValue>=overbought then GlobalColor("Up") else
                 if rsiValue<=oversold then GlobalColor("Dn") else CreateColor(255 - rsiValue * 2.55,rsiValue * 2.55, rsiValue*2));
#// Filling
AddCloud(overboughtLine, oversoldLine, GlobalColor("bg")); # "Overbought/Oversold Fill"
AddCloud(if Dir == 1 then supertrend else na, rsiValue, Color.DARK_RED); #  "Trend Fill"
AddCloud(if Dir == 1 then na else rsiValue, supertrend, Color.DARK_GREEN); # "Trend Fill"
AddCloud(if Dir == 1 then supertrend else na, (supertrend+rsiValue)/2, Color.LIGHT_RED); # title="Trend Fill")
AddCloud(if Dir == 1 then na else (supertrend+rsiValue)/2, supertrend, Color.LIGHT_GREEN); # title="Trend Fill")

#// Char plotting for crossover and crossunder

def bgcrossDn = cross and (rsiST > overbought) and (rsiST > rsiValue);
def bgcrossUp = cross and (rsiST < oversold) and (rsiST < rsiValue);
def bgUp = obos and (rsiST > overbought) and Dir==-1;
def bgDn = obos and (rsiST < oversold) and Dir== 1;

def SigDnS1 = cross and (rsiST > rsiValue) and rsiST > overbought;
def sigUpS1 = cross and (rsiST < rsiValue) and rsiST < oversold;
def SigDnS2 = obos and (rsiST < oversold) and rsiST < rsiST[1] and Dir==1;
def sigUpS2 = obos and (rsiST > overbought) and rsiST > rsiST[1] and Dir==-1;
def sigUp1 = (sigUpS1 and !sigUpS1[1]) or (sigUpS2 and !sigUpS2[1]);
def sigDn1 = (SigDnS1 and !sigDnS1[1]) or (SigDnS2 and !sigDnS2[1]);

plot SigDn = if sigDn1 then 85 else na; # "Crossover Down"
plot sigUp = if sigUp1 then 85 else na; # "Crossunder Up"
SigDn.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
SigUp.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
SigDn.SetDefaultColor(Color.RED);
sigUp.SetDefaultColor(Color.GREEN);

AddCloud(if bgcrossDn or bgcrossDn[-1] then pos else na, neg, Color.DARK_RED);
AddCloud(if bgcrossUp or bgcrossUp[-1] then pos else na, neg, Color.DARK_GREEN);

AddCloud(if bgDn or bgDn[-1] then pos else na, neg, Color.DARK_RED);
AddCloud(if bgUp or bgUp[-1] then pos else na, neg, Color.DARK_GREEN);

#-- END of CODE
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
330 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