Stochastic RSI of Smoothed Price [Loxx] for ThinkOrSwim

samer800

Conversion Expert
VIP
Lifetime
POpLFAC.png

Author Message:
https://www.tradingview.com/v/5DXPC0vr/

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#https://www.tradingview.com/v/5DXPC0vr/
#// © loxx
#indicator("Stochastic RSI of Smoothed Price [Loxx]"
# Converted and mod by Sam4Cok@Samer800    - 04/2023
declare lower;
# stoch(source, high, low, length) =>
script stoch {
    input src = close;
    input h = high;
    input l = low;
    input len = 0;
    def stoch = 100 * (src - Lowest(l, len)) / (Highest(h, len) - Lowest(l, len));
    plot return = stoch;
}
#variant(type, src, len) =>
script variant {
    input type = "SMA";
    input src = close;
    input len = 14;
    def v = if IsNaN(volume) then 1 else volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    def sig =
    if type == "SMA" then Average(src, len) else
    if type == "EMA" then ExpAverage(src, len) else
    if type == "WMA" then WMA(src, len) else
    if type == "RMA" then WildersAverage(src, len) else
    if type == "VWMA" then VWMA else sig[1];
    plot out = sig;
}
input colorBars = yes;    # "Color bars?"
input showSigs = yes;     # "Show Signals?"
input StocPeriod = 32;    # "Stochastic Period"
input StocSlowing = 8;    # "Stochastic Slowing Period"
input Source = close;     # "Source"
input PriceSmoothing = 8; # "Price Smoothing Period"
input maType = {"EMA", "WMA", "RMA", default "SMA", "VWMA"}; # "RSI Smoothing Type"
input RsiPeriod = 32;     # "RSI Period"
input rsiType = {"RSX", default "Regular", "Slow", "Rapid", "Harris", "Cuttler", "Ehlers Smoothed"}; # "RSI type"
input MinMaxPeriod =  25; # "Levels Period"
input LevelUp = 80;       # "Upper %"
input LevelDown = 20;     # "Lower %"
input signalType = {"Fixed Levels", default "Floating Levels", "Fixed Middle", "Floating Middle"}; # "Signal type"


def na = Double.NaN;
def last = isNaN(close);
def SM02 = signalType == signalType."Fixed Levels";
def SM03 = signalType == signalType."Floating Levels";
def SM05 = signalType == signalType."Fixed Middle";
def SM06 = signalType == signalType."Floating Middle";

script nz {
    input data  = close;
    def ret_val = if !IsNaN(data) then data else 0;
    plot return = ret_val;
}
#// @function method for returning 1 of 7 different RSI calculation outputs.
#rsx(float src, int len)=>
script rsx {
    input src = close;
    input len = 14;
    def src_out = 100 * src;
    def mom0 = src_out - src_out[1];
    def moa0 = AbsValue(mom0);
    def Kg = 3 / (len + 2.0);
    def Hg = 1 - Kg;
    def f28;
    def f30;
    f28 = Kg * mom0 + Hg * nz(f28[1]);
    f30 = Hg * nz(f30[1]) + Kg * f28;
    def mom1 = f28 * 1.5 - f30 * 0.5;
    def f38;
    def f40;
    f38 = Hg * nz(f38[1]) + Kg * mom1;
    f40 = Kg * f38 + Hg * nz(f40[1]);
    def mom2 = f38 * 1.5 - f40 * 0.5;
    def f48;
    def f50;
    f48 = Hg * nz(f48[1]) + Kg * mom2;
    f50 = Kg * f48 + Hg * nz(f50[1]);
    def mom_out = f48 * 1.5 - f50 * 0.5;
    def f58;
    def f60;
    f58 = Hg * nz(f58[1]) + Kg * moa0;
    f60 = Kg * f58 + Hg * nz(f60[1]);
    def moa1 = f58 * 1.5 - f60 * 0.5;
    def f68;
    def f70;
    f68 = Hg * nz(f68[1]) + Kg * moa1;
    f70 = Kg * f68 + Hg * nz(f70[1]);
    def moa2 = f68 * 1.5 - f70 * 0.5;
    def f78;
    def f80;
    f78 = Hg * nz(f78[1]) + Kg * moa2;
    f80 = Kg * f78 + Hg * nz(f80[1]);
    def moa_out = f78 * 1.5 - f80 * 0.5;
    def rsiout = Max(Min((mom_out / moa_out + 1.0) * 50.0, 100.00), 0.00);
    plot out = rsiout;
}
#export rsiVariety(string rsiMode = "rsi_rsi", float src, int per)=>
script rsiVariety {
    input rsiMode = "Regular";
    input src     = close;
    input per     = 14;
    def _change;# = 0.
    def _changa;# = 0.
#    //Regular RSI, same as ta.rsi(src, per)
    def alpha = 1.0 / Max(per, 1);
    def change = src - src[1];
    _change = CompoundValue(1, _change[1] + alpha * (change - _change[1]), alpha * change - change[1]);
    _changa = CompoundValue(1, _changa[1] + alpha * (AbsValue(change) - _changa[1]), alpha * AbsValue(change) - change[1]);
    def Regular = if _changa != 0 then 50.0 * (_change / _changa + 1) else 50;
#        // Slow RSI
    def up;
    def dn;
    def _rsival;
    up = fold k = 0  to per with p do
                  if (src[k] - GetValue(src, k + 1)) > 0 then p + (src[k] - GetValue(src, k + 1)) else p;
    dn = fold k1 = 0 to per with p1 do
                  if (src[k1] - GetValue(src, k1 + 1)) > 0 then p1 else p1 - (src[k1] - GetValue(src, k1 + 1));
    if (up + dn == 0) {
        _rsival = CompoundValue(1, _rsival[1] + (1 / Max(per, 1)) * (50 - _rsival[1]), Regular);
    } else {
        _rsival = CompoundValue(1, _rsival[1] + (1 / Max(per, 1)) * (100 * up / (up + dn) - _rsival[1]), Regular) ;
    }
    def Slow = _rsival;
#        // Rapid RSI, same as Cuttlers RSI
    def Rapid = if (up + dn == 0) then 50 else 100 * up / (up + dn);
#        // Harris RSI
    def avgUp;
    def avgDn;
    def up1;
    def dn1;
    up1 = fold k2 = 0 to per with p2 do
                    if (src[k2] - GetValue(src, k2 + 1)) > 0 then p2 + 1 else p2;
    dn1 = fold k3 = 0 to per with p3 do
                    if (src[k3] - GetValue(src, k3 + 1)) > 0 then p3 else p3 + 1;
    if (up1 != 0) {
        avgUp = up / up1;
    } else {
        avgUp = up;
    }
    if (dn1 != 0) {
        avgDn = dn / dn1;
    } else {
        avgDn = dn;
    }
    def rs;# = 1
    rs = if avgDn != 0 then avgUp / avgDn else if nz(rs[1]) == 0 then 1 else rs[1];
    def Harris = 100 - 100 / (1.0 + rs);
#        // Cuttler RSI     
    def Cuttler = if dn > 0 then 100 - 100 / (1 + up / dn) else 50;
#        //Ehlers' Smoothed RSI
    def bar = AbsValue(CompoundValue(1, BarNumber(), 0));
    def up2;
    def dn2;# = 0.
    def _prices = if (bar > 2) then (src + 2 * src[1] + src[2]) / 4.0 else src;
    up2 = fold k4 = 0 to per - 1 with p4 do
          if ((_prices[k4]) - (GetValue(_prices, k4 + 1))) > 0 then p4 + ((_prices[k4]) - (GetValue(_prices, k4 + 1))) else p4;
    dn2 = fold k5 = 0 to per - 1 with p5 do
          if ((_prices[k5]) - (GetValue(_prices, k5 + 1))) > 0 then p5 else p5 - ((_prices[k5]) - (GetValue(_prices, k5 + 1)));
    def Ehlers = 50 * (up2 - dn2) / (up2 + dn2) + 50;
    #        // RSX RSI     
    def RSX = rsx(src, per);
    def nRSI = if rsiMode == "Regular" then Regular else
           if rsiMode == "Slow" then Slow else
           if rsiMode == "Rapid" then Rapid else
           if rsiMode == "Harris" then Harris else
           if rsiMode == "Cuttler" then Cuttler else
           if rsiMode == "Ehlers Smoothed" then Ehlers else
           if rsiMode == "RSX" then RSX else Regular;
    plot out = nRSI;
}
def prices = variant(matype, Source, PriceSmoothing);
def rsi = rsiVariety(rsiType, prices, RsiPeriod);
def stoch = Average(stoch(rsi, rsi, rsi, StocPeriod), StocSlowing);

def lvlup;# = LevelUp
def lvldn;# = LevelDown
def lvlmid;# = 50.

def fmin = Lowest(stoch,  MinMaxPeriod);
def fmax = Highest(stoch,  MinMaxPeriod);
def rng = fmax - fmin;
if SM03 or SM06 {
    lvlup = fmin + LevelUp * rng / 100.0;
    lvldn = fmin + LevelDown * rng / 100.0;
    lvlmid = (lvlup + lvldn) * 0.5;
} else {
    lvlup  = if (IsNaN(lvlup[1])  or lvlup[1]==0) then LevelUp else lvlup[1];
    lvldn  = if (IsNaN(lvldn[1])  or lvldn[1]==0) then LevelDown else lvldn[1];
    lvlmid = if (IsNaN(lvlmid[1]) or lvlmid[1]==0) then 50 else lvlmid[1];
}
def state;

if SM02 or SM03 {
    state =  if stoch > lvlup then 1 else
             if stoch < lvldn then -1 else 0;
} else
if SM05 or SM06 {
    state = if stoch > lvlmid then 1 else
            if stoch < lvlmid then -1 else 0;
} else {
    state = state[1];
}
def goLong =  state == 1 and state[1] != 1;
def goShort = state == -1 and state[1] != -1;

plot long = if showSigs and goLong then 15 else -5;       # "Long"
plot short = if showSigs and goShort then 85 else 105;    # "Short"

long.SetDefaultColor(Color.CYAN);
short.SetDefaultColor(Color.MAGENTA);

plot Middle = if last then na else lvlmid;            # "Middle"
plot UpperLevel = if last then na else lvlup;         # "Upper level"
plot LowerLevel = if last then na else lvldn;         # "Lower level"
plot Stochastic = stoch;         # "Stochastic"

Middle.SetDefaultColor(Color.GRAY);
UpperLevel.SetDefaultColor(Color.DARK_GREEN);
LowerLevel.SetDefaultColor(Color.DARK_RED);
Stochastic.AssignValueColor(if state==1 then Color.GREEN else
                            if state==-1 then Color.RED else Color.GRAY);
Stochastic.SetLineWeight(2);

AssignPriceColor(if !colorBars then Color.CURRENT else
                 if state==1 then Color.GREEN else
                 if state==-1 then Color.RED else Color.GRAY);


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