RSI direction bias - JD for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
zHMpJnd.png


This simple indicator gives you a bias on the market that can be used as a filter, an entry indicator for pullbacks,...
added RSX and reverse RSI options as well.

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Duyck
#https://www.tradingview.com/v/H0ysE9Xz/
#// This simple indicator gives you a bias on the market that can be used as a filter, an entry indicator for pullbacks,...
#NotTradingAdvice
#indicator('RSI direction bias - JD')
# Converted and mod by Sam4Cok@Samer800        - 03/2023
declare lower;
input color_bars  = no;         # 'color bars?'
input source  = close;          # 'source'
input AllowRepainting = yes;
input ReverseRSI = yes;
input rsi_type   = {default "Regular RSI", "Future Smoothed RSI", "Jurik's RSX"};  #'rsi type'
input SignalType = {default "Background", "Bias Line", "None"};
input rsiLength   = 14;         # 'rsi length'
input topLevel    = 60;         # ' top level'
input bottomLevel = 40;         # 'bottom level'


def na = Double.NaN;
def rep  = if AllowRepainting then 0 else 1;
#def rep0 = if AllowRepainting then 0 else 1;
def last = IsNaN(close);
def top_level = topLevel;
def bottom_level = bottomLevel;
def src = if AllowRepainting then source else if !last then source[1] else source;
def haclose = if AllowRepainting then ohlc4 else if !last then ohlc4[1] else ohlc4;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def bg = SignalType == SignalType."Background";
def Nan = SignalType == SignalType."None";
def Reg = rsi_type == rsi_type."Regular RSI";
def Jur = rsi_type == rsi_type."Jurik's RSX";
def rsiLevel = (topLevel + bottomLevel) / 2;
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if isNaN(data) then repl else data;
    plot return = ret_val;
}
script rsx {
input src = close;
input rsxLength = 7;
def f8 = 100 * src;
def f10 = nz(f8[1],f8);
def v8 = f8 - f10;
def f18 = 3 / (rsxLength + 2);
def f20 = 1 - f18;
def f28 = f20 * nz(f28[1]) + f18 * v8;
def f30 = f18 * f28 + f20 * nz(f30[1]);
def vC = f28 * 1.5 - f30 * 0.5;
def f38 = f20 * nz(f38[1]) + f18 * vC;
def f40 = f18 * f38 + f20 * nz(f40[1]);
def v10 = f38 * 1.5 - f40 * 0.5;
def f48 = f20 * nz(f48[1]) + f18 * v10;
def f50 = f18 * f48 + f20 * nz(f50[1]);
def v14 = f48 * 1.5 - f50 * 0.5;
def f58 = f20 * nz(f58[1]) + f18 * AbsValue(v8);
def f60 = f18 * f58 + f20 * nz(f60[1]);
def v18 = f58 * 1.5 - f60 * 0.5;
def f68 = f20 * nz(f68[1]) + f18 * v18;
def f70 = f18 * f68 + f20 * nz(f70[1]);
def v1C = f68 * 1.5 - f70 * 0.5;
def f78 = f20 * nz(f78[1]) + f18 * v1C;
def f80 = f18 * f78 + f20 * nz(f80[1]);
def v20 = f78 * 1.5 - f80 * 0.5;
def f88;
def f90_ = if nz(f90_[1]) == 0 then 1 else
           if nz(f88[1]) <= nz(f90_[1]) then nz(f88[1]) + 1 else nz(f90_[1]) + 1;
f88 = if nz(f90_[1]) == 0 and (rsxLength - 1 >= 5) then rsxLength - 1 else 5;
def f0 = if f88 >= f90_ and f8 != f10 then 1 else 0;
def f90 = if f88 == f90_ and f0 == 0 then 0 else f90_;
def v4_ = if f88 < f90 and v20 > 0 then (v14 / v20 + 1) * 50 else 50;
def rsx = if v4_ > 100 then 100 else if v4_ < 0 then 0 else v4_;

plot return = rsx;
}
#reRSI(RSILen, Length) =>
script reRSI {
    input src = close;
    input RSILen = 14;
    input Length = 50;
    def ExpPer = 2 * RSILen - 1;
    def K = 2 / (ExpPer + 1);
    def AUC;
    def ADC;# = 0.0# = 0.0
    AUC = If(src > src[1], K * (src - src[1]) + (1 - K) * nz(AUC[1], 1), (1 - K) * nz(AUC[1], 1));
    ADC = If(src > src[1], (1 - K) * nz(ADC[1], 1), K * (src[1] - src) + (1 - K) * nz(ADC[1], 1));
    def nVal = (RSILen - 1) * (ADC * Length / (100 - Length) - AUC);
    def nRes = If(nVal >= 0, src + nVal, src + nVal * (100 - Length) / Length);
    plot out = nRes;
}

#// Main

def rsi = RSI(PRICE = src, LENGTH = rsiLength);
def RSX = rsx(src,rsiLength);
def ha_Close = haclose;
def ha_Open = CompoundValue(1, (ha_Open[1] + ha_Close[1]) / 2, ha_Close);
def next_ha_open = (ha_Open + ha_Close) / 2;
def fut_smo_rsi  = RSI(PRICE = next_ha_open, LENGTH = rsiLength);

def rsiSrc = if reg then rsi else if jur then rsx else fut_smo_rsi;
def revRsi = reRSI(rsiSrc, rsiLength, rsiLevel);

def rsi_val = if ReverseRSI then revRsi else rsiSrc;

def bias1;# = 1
if !ReverseRSI {
    if Crosses(rsi_val, top_level, CrossingDirection.ABOVE) {
        bias1 = 1;
    } else
if Crosses(rsi_val, bottom_level, CrossingDirection.BELOW) {
        bias1 = -1;
    } else {
        bias1 = bias1[1];
    }
} else
if Crosses(rsi_val, rsiLevel, CrossingDirection.ABOVE) {
    bias1 = 1;
} else
if Crosses(rsi_val, rsiLevel, CrossingDirection.BELOW) {
    bias1 = -1;
} else {
    bias1 = bias1[1];
}
#// Plots
def bias = if bias1 then bias1 else 1;

def bias_line = if bias == 1 then bottom_level else top_level;
def col  = bias == 1;

plot BiasLine = if !rsi_val then na else bias_line;#, color = bias_color)
BiasLine.AssignValueColor(if col then Color.DARK_GREEN else Color.DARK_RED);

plot RsiLine  = rsi_val;#,   color = bias_color)
RsiLine.AssignValueColor(if col then Color.GREEN else Color.RED);
RsiLine.SetLineWeight(2);

plot TopLine = if bias ==  1 and rsi_val <= top_level then top_level else na;
TopLine.SetDefaultColor(CreateColor(177, 177, 0));

plot BotLine = if bias == -1 and rsi_val >= bottom_level then bottom_level else na;
BotLine.SetDefaultColor(CreateColor(177, 177, 0));

plot Zero = if !rsi_val then na else rsiLevel;
Zero.SetDefaultColor(Color.DARK_GRAY);
Zero.SetStyle(Curve.SHORT_DASH);

AddCloud(RsiLine, BiasLine, Color.DARK_GREEN, Color.DARK_RED);
AddCloud(TopLine, RsiLine, CreateColor(108, 0, 0));
AddCloud(RsiLine, BotLine, CreateColor(0, 61, 0));

def bcol = if bias == 1 then if rsi_val <= top_level then 1 else 2 else
                             if rsi_val >= bottom_level then -1 else -2;

AssignPriceColor(if !color_bars then Color.CURRENT else
                 if bcol == 1 then Color.DARK_GREEN else
                 if bcol == 2 then Color.GREEN else
                 if bcol == -1 then Color.DARK_RED else
                 if bcol == -2 then Color.RED else Color.GRAY);

#/ Alerts

def long_switch  = bias ==  1 and bias[1] == -1;
def short_switch = bias == -1 and bias[1] ==  1;

def State = if long_switch then 1 else if short_switch then -1 else State[1];

plot BiasState = if bg or Nan or !rsi_val then na else if State then 75 else na;
BiasState.SetPaintingStrategy(PaintingStrategy.SQUARES);
BiasState.AssignValueColor(if col then Color.GREEN else Color.RED);

def stateBg = if !bg or Nan then na else State;
AddCloud(if !rsi_val then na else if stateBg > 0 then pos else neg, if stateBg > 0 then neg else pos, CreateColor(0, 61, 0), CreateColor(108, 0, 0));


#--- END 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
484 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