RSI Adaptive zones (AdaptiveRSI) For ThinkOrSwim

JP782

Active member
The author states:
This script introduces a unified mathematical framework that auto-scales oversold/overbought and support/resistance zones for any period length. It also adds true RSI candles for spotting intrabar signals.

Built on the Logit RSI foundation, this indicator converts RSI into a statistically normalized space, allowing all RSI lengths to share the same mathematical footing.
PFmWWIj.png


Here is the original Tradingview code:
https://www.tradingview.com/v/CMBt48lk/

For the new ThinkOrSwim code, you must scroll down to the next post
 
Last edited by a moderator:

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

Code:
declare lower;
################################
# Original script by Cwparker23 #

################################

#-----------------
#- DISCLAIMER
#-----------------
#- I am not a certified financial advisor. The content of this page/site and tools are for informational purposes only and does not constitute financial or legal advice. Under no circumstances will the author be responsible for errors or use of this tool and site. User assumes all risks.

# =========================
# Inputs
# =========================
input rsiLength = 14;
input price = close;

# Sigma levels (from video)
input zOB_Top = 2.14;
input zOB_Bot = 1.73;
input zRes_Top = 1.00;
input zRes_Bot = 0.66;
input zSup_Top = -0.66;
input zSup_Bot = -1.00;
input zOS_Top = -1.73;
input zOS_Bot = -2.14;

# =========================
# RSI
# =========================
def r = RSI(price = price, length = rsiLength);
plot RSIline = r;
RSIline.SetLineWeight(2);
RSIline.SetDefaultColor(Color.WHITE);

# =========================
# Helper: sigma -> RSI via tanh (manual)
# =========================
script SigmaToRSI {
input z = 0.0;
input len = 14;

def x = z / Sqrt(len - 1);
def e2x = Exp(2 * x);
def tanh = (e2x - 1) / (e2x + 1);

plot out = 50 + 50 * tanh;
}

# =========================
# Zones
# =========================
plot OB_Top = SigmaToRSI(zOB_Top, rsiLength);
plot OB_Bot = SigmaToRSI(zOB_Bot, rsiLength);

plot Res_Top = SigmaToRSI(zRes_Top, rsiLength);
plot Res_Bot = SigmaToRSI(zRes_Bot, rsiLength);

plot Sup_Top = SigmaToRSI(zSup_Top, rsiLength);
plot Sup_Bot = SigmaToRSI(zSup_Bot, rsiLength);

plot OS_Top = SigmaToRSI(zOS_Top, rsiLength);
plot OS_Bot = SigmaToRSI(zOS_Bot, rsiLength);

plot MidLine = 50;

# =========================
# Styling
# =========================
OB_Top.SetDefaultColor(Color.DARK_GREEN);
OB_Bot.SetDefaultColor(Color.DARK_GREEN);
OS_Top.SetDefaultColor(Color.DARK_RED);
OS_Bot.SetDefaultColor(Color.DARK_RED);

Res_Top.SetDefaultColor(Color.GRAY);
Res_Bot.SetDefaultColor(Color.GRAY);
Sup_Top.SetDefaultColor(Color.GRAY);
Sup_Bot.SetDefaultColor(Color.GRAY);

MidLine.SetDefaultColor(Color.YELLOW);
MidLine.SetLineWeight(2);

# =========================
# Clouds
# =========================
AddCloud(OB_Top, OB_Bot, Color.GREEN, Color.GREEN);
AddCloud(OS_Top, OS_Bot, Color.RED, Color.RED);

AddCloud(Res_Top, Res_Bot, Color.LIGHT_GRAY, Color.LIGHT_GRAY);
AddCloud(Sup_Top, Sup_Bot, Color.LIGHT_GRAY, Color.LIGHT_GRAY);

AddLabel(yes,
"Logit RSI Adaptive Zones | RSI(" + round(RSIline,2) + ")",
Color.GRAY
);


def h = reference RSI(price = high, length = rsiLength) ;
def l = reference RSI(price = low, length = rsiLength);
def o = reference RSI(price = open, length = rsiLength) ;
def c = reference RSI(price = close, length = rsiLength) ;



def isBull = close > open;
def isBear = close <= open;

# Bear candles
AddChart(
high = if isBear then h else Double.NaN,
low = if isBear then l else Double.NaN,
open = if isBear then o else Double.NaN,
close = if isBear then c else Double.NaN,
type = ChartType.CANDLE,
growColor = Color.RED,
fallColor = Color.GREEN,
neutralColor = Color.CURRENT
);

# Bull candles
AddChart(
high = if isBull then h else Double.NaN,
low = if isBull then l else Double.NaN,
open = if isBull then c else Double.NaN,
close = if isBull then o else Double.NaN,
type = ChartType.CANDLE,
growColor = Color.GREEN,
fallColor = Color.RED,
neutralColor = Color.CURRENT
);
 
Last edited by a moderator:
This one would be much closer to the original

Code:
# AdaptiveRSI for Thinkorswim
# Original Concept by AdaptiveRSI (Pine Script)
# Converted to ThinkScript by ShinJ
# License: CC BY-NC-SA 4.0

declare lower;

# ─── Inputs ─────────────────────────────────────────────
input length = 14;
input plotStyle = {default Candle, Bar, Line};
input smoothingType = {default "None", "SMA + Bollinger Bands", "SMA"};
input maLength = 21;
input bbStdDev = 2.0;

# Visibility Inputs
input showSupportResistance = yes;
input showOverboughtOversold = yes;

# ─── Constants & Math Helpers ───────────────────────────
def SF = 1.0 / length;
def power_exponent = 0.5;
def half_range = 50.0;
def eps = 0.0000000001;

# ─── RSI Components ─────────────────────────────────────
# Standard RSI uses Wilder's Average
def middle = WildersAverage(close, length);

# Hybrid Smoothing: Uses Previous Middle but Current Price Data
def middle_O = (1.0 - SF) * middle[1] + SF * open;
def middle_H = (1.0 - SF) * middle[1] + SF * high;
def middle_L = (1.0 - SF) * middle[1] + SF * low;

# Volatility Calculation
def CC_vol = WildersAverage(AbsValue(close - close[1]), length);
def CC_vol_O = (1.0 - SF) * CC_vol[1] + SF * AbsValue(open - close[1]);
def CC_vol_H = (1.0 - SF) * CC_vol[1] + SF * AbsValue(high - close[1]);
def CC_vol_L = (1.0 - SF) * CC_vol[1] + SF * AbsValue(low - close[1]);

# ─── RSI OHLC Calculation ───────────────────────────────
def den_C = Max(CC_vol, eps) * (length - 1.0);
def den_O = Max(CC_vol_O, eps) * (length - 1.0);
def den_H = Max(CC_vol_H, eps) * (length - 1.0);
def den_L = Max(CC_vol_L, eps) * (length - 1.0);

def RSI_C = half_range + half_range * ((close - middle) / den_C);
def RSI_O = half_range + half_range * ((open - middle_O) / den_O);
def RSI_H = half_range + half_range * ((high - middle_H) / den_H);
def RSI_L = half_range + half_range * ((low - middle_L) / den_L);

# ─── Adaptive Zones Thresholds ──────────────────────────
def inv_sqrt_lenm1 = 1.0 / Power(length - 1.0, power_exponent);
def body_threshold = Sqrt((5.0 - Sqrt(17.0)) / 2.0);
def tail_threshold = Sqrt((5.0 + Sqrt(17.0)) / 2.0);
def breakout_threshold = 1.0;
def reversal_threshold = Sqrt(3.0);

script Tanh {
    input x = 0;
    def ex = Exp(2.0 * x);
    plot val = (ex - 1.0) / (ex + 1.0);
}

def Z_ins  = half_range * Tanh(body_threshold * inv_sqrt_lenm1);
def Z_out  = half_range * Tanh(breakout_threshold * inv_sqrt_lenm1);
def OO_ins = half_range * Tanh(reversal_threshold * inv_sqrt_lenm1);
def OO_out = half_range * Tanh(tail_threshold * inv_sqrt_lenm1);

# ─── Smoothing Logic ────────────────────────────────────
def xc = Min(Max(RSI_C, eps), 100 - eps);
def logit_RSI_C = Log(xc / (100 - xc));
def source_RSI = logit_RSI_C;
def LMA = if smoothingType == smoothingType.None then source_RSI else Average(source_RSI, maLength);
def pop_correction = Sqrt((maLength - 1) / maLength);
def LRSI_StDev = StDev(source_RSI, maLength) * pop_correction;
def smoothedRSI = 100 * (Exp(LMA) / (1 + Exp(LMA)));
def upperBB_logit = LMA + bbStdDev * LRSI_StDev;
def lowerBB_logit = LMA - bbStdDev * LRSI_StDev;
def upperBB = 100 * (Exp(upperBB_logit) / (1 + Exp(upperBB_logit)));
def lowerBB = 100 * (Exp(lowerBB_logit) / (1 + Exp(lowerBB_logit)));

# ─── Plotting: Standard Plots ───────────────────────────
plot MidLine = 50;
MidLine.SetStyle(Curve.SHORT_DASH);
MidLine.SetDefaultColor(Color.DARK_GRAY);

plot BBUpper = if smoothingType == smoothingType."SMA + Bollinger Bands" then upperBB else Double.NaN;
BBUpper.SetDefaultColor(Color.ORANGE);
plot BBLower = if smoothingType == smoothingType."SMA + Bollinger Bands" then lowerBB else Double.NaN;
BBLower.SetDefaultColor(Color.ORANGE);
plot MASmooth = if smoothingType != smoothingType.None then smoothedRSI else Double.NaN;
MASmooth.SetDefaultColor(Color.YELLOW);

# ─── Plotting: Candles/Bars using AddChart ──────────────
# Define logic for Up vs Down candles
def isGreen = RSI_C >= RSI_O;

# Determine if we should plot candles/bars
def useCandle = plotStyle == plotStyle.Candle;
def useBar = plotStyle == plotStyle.Bar;

# 1. Candle Mode (Green)
AddChart(high = if useCandle and isGreen then RSI_H else Double.NaN,
         low = if useCandle and isGreen then RSI_L else Double.NaN,
         open = if useCandle and isGreen then RSI_O else Double.NaN,
         close = if useCandle and isGreen then RSI_C else Double.NaN,
         type = ChartType.CANDLE,
         growColor = Color.GREEN);

# 2. Candle Mode (Red)
AddChart(high = if useCandle and !isGreen then RSI_H else Double.NaN,
         low = if useCandle and !isGreen then RSI_L else Double.NaN,
         open = if useCandle and !isGreen then RSI_O else Double.NaN,
         close = if useCandle and !isGreen then RSI_C else Double.NaN,
         type = ChartType.CANDLE,
         growColor = Color.RED);

# 3. Bar Mode (Green)
AddChart(high = if useBar and isGreen then RSI_H else Double.NaN,
         low = if useBar and isGreen then RSI_L else Double.NaN,
         open = if useBar and isGreen then RSI_O else Double.NaN,
         close = if useBar and isGreen then RSI_C else Double.NaN,
         type = ChartType.BAR,
         growColor = Color.GREEN);

# 4. Bar Mode (Red)
AddChart(high = if useBar and !isGreen then RSI_H else Double.NaN,
         low = if useBar and !isGreen then RSI_L else Double.NaN,
         open = if useBar and !isGreen then RSI_O else Double.NaN,
         close = if useBar and !isGreen then RSI_C else Double.NaN,
         type = ChartType.BAR,
         growColor = Color.RED);

# 5. Line Mode
plot RSILine = if plotStyle == plotStyle.Line then RSI_C else Double.NaN;
RSILine.SetLineWeight(2);
RSILine.AssignValueColor(if RSI_C > (50 + OO_ins) then Color.RED else if RSI_C < (50 - OO_ins) then Color.GREEN else Color.GRAY);

# ─── Clouds / Zones (Background) ────────────────────────
def OB_Top = 50 + OO_out;
def OB_Bot = 50 + OO_ins;
AddCloud(if showOverboughtOversold then OB_Top else Double.NaN, OB_Bot, CreateColor(50, 166, 69), CreateColor(50, 166, 69));

def OS_Top = 50 - OO_ins;
def OS_Bot = 50 - OO_out;
AddCloud(if showOverboughtOversold then OS_Top else Double.NaN, OS_Bot, CreateColor(217, 35, 35), CreateColor(217, 35, 35));

def Res_Top = 50 + Z_out;
def Res_Bot = 50 + Z_ins;
AddCloud(if showSupportResistance then Res_Top else Double.NaN, Res_Bot, Color.GRAY, Color.GRAY);

def Sup_Top = 50 - Z_ins;
def Sup_Bot = 50 - Z_out;
AddCloud(if showSupportResistance then Sup_Top else Double.NaN, Sup_Bot, Color.GRAY, Color.GRAY);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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