Standardized SuperTrend Oscillator for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
k3EvO60.png


Author Message:
The Standardized SuperTrend Oscillator (SSO) is a versatile tool that transforms the SuperTrend indicator into an oscillator, offering both trend-following and mean reversion capabilities. It provides deeper insights into trends by standardizing the SuperTrend with respect to its upper and lower bounds, allowing traders to identify potential reversals and contrarian signals.
More Details : https://www.tradingview.com/v/c9V7Ev22/

CODE:

CSS:
#// https://www.tradingview.com/v/c9V7Ev22/
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
#// © EliCobra
#indicator("Standardized SuperTrend Oscillator", "{?} -? Super Osc.", false)
# Converted by Sam4Cok@Samer800    - 11/2023
declare lower;

input useHeikinAshiSource = no;  # "Heikin-Ashi Source"
input OscillatorLength = 10;     # "Length"
input OscillatorFactor = 5.0;    # "Factor"
input SuperTrendLength = 10;     # "Length"
input SuperTrendFactor = 3.0;    # "Factor"
input OscillatorStyle  = {default "Candles", "Line"};  # "Oscillator Style"
input showSuperTrend = yes;      # "SuperTrend Of Osc."
input showReversionTracer = yes; # "Reversion Tracer"
input ReversionSignal  = {default "None", "OB" ,"OS", "Both"};      # "Reversion  Signals"
input ContrarianSignal = {default "None", "Buy" ,"Sell", "Both"};   # "Contrarian Signals"

def na = Double.NaN;
def last = isNaN(close);
def line = OscillatorStyle==OscillatorStyle."Line";
def sell = ContrarianSignal==ContrarianSignal."Sell" or ContrarianSignal==ContrarianSignal."Both";
def buy  = ContrarianSignal==ContrarianSignal."Buy" or ContrarianSignal==ContrarianSignal."Both";
def overB = ReversionSignal==ReversionSignal."OB" or ReversionSignal==ReversionSignal."Both";
def overS = ReversionSignal==ReversionSignal."OS" or ReversionSignal==ReversionSignal."Both";
#-- Color
DefineGlobalColor("csoUp", CreateColor(21,130,255));
DefineGlobalColor("csoDn", CreateColor(255,72,0));
DefineGlobalColor("lineUp",CreateColor(34,171,148));
DefineGlobalColor("lineDn",CreateColor(242,54,69));
DefineGlobalColor("colup", CreateColor(34,171,148));
DefineGlobalColor("coldn", CreateColor(255,218,185));
DefineGlobalColor("colvt", CreateColor(218,165,32));

def o1 = open;
def h1 = high;
def l1 = low;
def c1 = close;

#method ha(bar b, simple bool p = true) =>
Script ha {
input bo = open;
input bh = high;
input bl = low;
input bc = close;
input base = yes;
    def xc = (bo + bh + bl + bc) / 4;
    def xo = if !xo[1] then (bo + bc) / 2 else (xo[1] + xc[1]) / 2;
    def xh = max(bh, max(xo, xc));
    def xl = min(bl, min(xo, xc));
    plot hac = if base then xc else bc;
    plot hao = if base then xo else bo;
    plot hah = if base then xh else bh;
    plot hal = if base then xl else bl;
}
#method atr(bar b, simple int len) =>
script nATR {
    input h = high;
    input c = close;
    input l = low;
    input len = 10;
    def tr = if !h[1] then h - l else TrueRange(h, c, l);
    def nATR = if len == 1 then tr else WildersAverage(tr, len);
    plot out = nATR;
}
#method st(bar b, simple float factor, simple int len) =>
script st {
    input hl = hl2;
    input c = close;
    input len = 10;
    input factor = 3;
    input nATR = close;
    def bar = AbsValue(BarNumber()) == 1;
    def st;def up; def dn;
    def up1 = hl + (factor * nATR);
    def dn1 = hl - (factor * nATR);
    def upPre = if bar then up1 else up[1];
    def dnPre = if bar then dn1 else dn[1];
    def prevST = If bar then up1 else st[1];
        up  = if (up1 < upPre) or (c[1] > upPre) then up1 else upPre;
        dn  = if (dn1 > dnPre) or (c[1] < dnPre) then dn1 else dnPre;
    def dir = if !nATR[1] then 1 else
              if prevST == upPre then if c > up then -1 else  1 else
                                      if c < dn then  1 else -1;
    st  = if dir == -1 then dn else up;
    plot super = st;
    plot trend = dir;
    plot top = up;
    plot bot = dn;
}
#method sso(bar b, supertrend st, simple int len) =>
script sso {
    input st = close;
    input std = 0;
    input bo = open;
    input bh = high;
    input bl = low;
    input bc = close;
    def o   = (bo - st) / std;
    def h   = (bh - st) / std;
    def l   = (bl - st) / std;
    def c   = (bc - st) / std;
    plot op = o;
    plot hi = h;
    plot lo = l;
    plot cl = c;
}
def bo = ha(o1, h1, l1, c1, useHeikinAshiSource).hao;
def bh = ha(o1, h1, l1, c1, useHeikinAshiSource).hah;
def bl = ha(o1, h1, l1, c1, useHeikinAshiSource).hal;
def bc = ha(o1, h1, l1, c1, useHeikinAshiSource).hac;
def hl = (bh + bl) / 2;

def nATR = nATR(bh, bc, bl, OscillatorLength);
def t1   = st(hl, bc, OscillatorLength, OscillatorFactor, nATR).super;
def d1   = st(hl, bc, OscillatorLength, OscillatorFactor, nATR).trend;
def up   = st(hl, bc, OscillatorLength, OscillatorFactor, nATR).top;
def dn   = st(hl, bc, OscillatorLength, OscillatorFactor, nATR).bot;

def dif = (up - dn);
def diff = if dif then dif else 2 * nATR * OscillatorFactor;
def std  = ExpAverage(diff, OscillatorLength) / 100;

def sso1 = sso(t1, std, bo, bh, bl, bc).op;
def ssh1 = sso(t1, std, bo, bh, bl, bc).hi;
def ssl1 = sso(t1, std, bo, bh, bl, bc).lo;
def ssc1 = sso(t1, std, bo, bh, bl, bc).cl;

def ssc = ha(sso1, ssh1, ssl1, ssc1).hac;
def sso = ha(sso1, ssh1, ssl1, ssc1).hao;
def ssh = ha(sso1, ssh1, ssl1, ssc1).hah;
def ssl = ha(sso1, ssh1, ssl1, ssc1).hal;

def ssATR = nATR(ssh, ssc, ssl, SuperTrendLength);
def sshl  = (ssh + ssl) / 2;
def t2    = st(sshl, ssc, SuperTrendLength, SuperTrendFactor, ssATR).super;
def d2    = st(sshl, ssc, SuperTrendLength, SuperTrendFactor, ssATR).trend;
def rvt   = RSI(Price = -d1, Length = OscillatorLength) * 2 - 100;
def ssoc  = if !line then (sso + ssc) / 2 else na;
def cso = if ssc > rvt and ssc > 0 then  1 else
          if ssc < rvt and ssc < 0 then -1 else 0;

plot oLine   = if Line then ssc else na;              # 'SSO'
plot BearST  = if showSuperTrend and d2  > 0 then t2 else na;   # 'Bear ST'
plot BullST  = if showSuperTrend and d2  < 0 then t2 else na;   # 'Bull ST'
plot UpperRT = if showReversionTracer and rvt > 0 then rvt else na;  # 'Upper RT'
plot LowerRT = if showReversionTracer and rvt < 0 then rvt else na;  # 'Lower RT'
plot m = if last then na else 0;

oLine.AssignValueColor(if cso > 0 then GlobalColor("LineUp") else
                       if cso < 0 then GlobalColor("LineDn") else Color.WHITE);
UpperRT.SetDefaultColor(GlobalColor("colvt"));
LowerRT.SetDefaultColor(GlobalColor("colvt"));
BullST.SetDefaultColor(GlobalColor("csoUp"));
BearST.SetDefaultColor(GlobalColor("csoDn"));
m.SetDefaultColor(Color.GRAY);
m.SetStyle(Curve.SHORT_DASH);
# Plot the new Chart
def colup = !line and ssc > sso;
def coldn = !line and ssc < sso;

AddChart(high = if colup then ssh else na, low = ssl, open = ssc,  close = sso,
         type = ChartType.CANDLE, growcolor =  GlobalColor("colup"));
AddChart(high = if coldn then ssh else na, low = ssl, open = sso,  close = ssc,
         type = ChartType.CANDLE, growcolor =  GlobalColor("coldn"));

AddCloud(BearST, ssoc, Color.DARK_RED);
AddCloud(ssoc, BullST, Color.DARK_GREEN);

AddCloud(if oLine > m then oLine else na, oLine - 5 * OscillatorFactor, Color.DARK_GREEN);
AddCloud(if oLine < m then oLine + 5 * OscillatorFactor else na, oLine, Color.DARK_RED);

#-- Signals

def scon = sell and sign(d2 - d2[1]) == 1;
def bcon = buy and sign(d2 - d2[1]) ==-1;
def ucon = overS and (ssc Crosses Above if(rvt < 0, rvt, na));
def dcon = overB and (ssc Crosses Below if(rvt > 0, rvt, na));

plot SellSig = if scon then t2 else na;
plot BuySig = if bcon then t2  else na;
plot obSig = if dcon then rvt else na;
plot osSig = if ucon then rvt else na;
SellSig.SetPaintingStrategy(PaintingStrategy.SQUARES);
BuySig.SetPaintingStrategy(PaintingStrategy.SQUARES);
SellSig.SetDefaultColor(GlobalColor("csoDn"));
BuySig.SetDefaultColor(GlobalColor("csoUp"));

obSig.SetPaintingStrategy(PaintingStrategy.POINTS);
osSig.SetPaintingStrategy(PaintingStrategy.POINTS);
obSig.SetDefaultColor(Color.MAGENTA);
osSig.SetDefaultColor(Color.CYAN);

#--- 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
487 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