Nadaraya-Watson Regression Liquidity Sweeps for ThinkOrSwim

chewie76

Well-known member
VIP
VIP Enthusiast
The Nadaraya-Watson Regression Liquidity Sweeps indicator is a good trend trading indicator. It also shows horizontal levels from previous highs or lows.

This is an upper chart conversion.

The original code and full description can be found here. https://www.tradingview.com/script/IdfeKkUv-Nadaraya-Watson-Regression-Liquidity-Sweeps-AlgoAlpha/

The picture is a 1 minute chart of SPX.

1781373780758.png


CODE:
Code:
# Nadaraya-Watson Regression Liquidity Sweeps
# Upper chart elements only — conversion from https://www.tradingview.com/script/IdfeKkUv-Nadaraya-Watson-Regression-Liquidity-Sweeps-AlgoAlpha/
# Converted by Chewie 6/13/2026

declare upper;

# ─── INPUTS ───────────────────────────────────────────────────────────────────
input alerts          = yes;
input nw_bandwidth    = 30;
input nw_lookback     = 140;
input norm_len        = 100;
input osc_smooth_len  = 21;
input signal_len      = 14;
input atr_len         = 14;
input vol_smooth_len  = 21;
input use_tip         = yes;
input show_nw_candles = yes;
input show_bar_colors = yes;
input show_momentum   = yes;
input show_rebound    = yes;

# ─── NADARAYA-WATSON KERNEL ───────────────────────────────────────────────────
def h2 = 2.0 * nw_bandwidth * nw_bandwidth;

def nw_num = fold i = 0 to nw_lookback
             with s = 0.0
             do s + GetValue(close, i) * Exp(-(i * i) / h2);

def nw_den = fold j = 0 to nw_lookback
             with d = 0.0
             do d + Exp(-(j * j) / h2);

def nw_val   = if nw_den != 0 then nw_num / nw_den else close;
def nw_slope = nw_val - nw_val[1];

# ─── OSCILLATOR ───────────────────────────────────────────────────────────────
# TOS StandardDeviation() works on a plot series; wrap nw_slope in a plot first,
# then reference it in the def.
plot _slope_src = nw_slope;
_slope_src.Hide();

def slope_std = StandardDeviation(_slope_src, norm_len);
def osc_raw   = if slope_std != 0 then nw_slope / slope_std else 0;
def osc       = Average(osc_raw, osc_smooth_len);
def sig       = ExpAverage(osc, signal_len);

# ─── VOLATILITY ───────────────────────────────────────────────────────────────
def raw_atr    = TrueRange(high, close, low);
def smoothed_atr = ExpAverage(raw_atr, atr_len);
def volatility   = Average(smoothed_atr, vol_smooth_len);

# ─── CROSS CONDITIONS ─────────────────────────────────────────────────────────
def above_sig = osc > sig;
def con1 = above_sig[1] and !above_sig;    # crossunder — weakening bull
def con2 = !above_sig[1] and above_sig;   # crossover  — weakening bear

def draw_upper = osc > 0 and con1;
def draw_lower = osc < 0 and con2;

def is_bull = osc >= 0;

# ─── SWING-POINT TRACKING ─────────────────────────────────────────────────────
def above_tip;
if above_sig and !above_sig[1] {
    above_tip = high;
} else if above_sig and high > above_tip[1] {
    above_tip = high;
} else {
    above_tip = above_tip[1];
}

def below_tip;
if !above_sig and above_sig[1] {
    below_tip = low;
} else if !above_sig and low < below_tip[1] {
    below_tip = low;
} else {
    below_tip = below_tip[1];
}

# ─── LIQUIDITY LEVEL STATE ────────────────────────────────────────────────────
def active_type;
def active_price;

if draw_upper {
    active_type  = 1;
    active_price = if use_tip then above_tip else high;
} else if draw_lower {
    active_type  = -1;
    active_price = if use_tip then below_tip else low;
} else if active_type[1] == 1 and close > active_price[1] {
    active_type  = 0;
    active_price = active_price[1];
} else if active_type[1] == -1 and close < active_price[1] {
    active_type  = 0;
    active_price = active_price[1];
} else {
    active_type  = active_type[1];
    active_price = active_price[1];
}

plot UpperLiqLevel = if active_type == 1 then active_price else Double.NaN;
UpperLiqLevel.SetDefaultColor(Color.RED);
UpperLiqLevel.SetStyle(Curve.FIRM);
UpperLiqLevel.SetLineWeight(2);

plot LowerLiqLevel = if active_type == -1 then active_price else Double.NaN;
LowerLiqLevel.SetDefaultColor(Color.CYAN);
LowerLiqLevel.SetStyle(Curve.FIRM);
LowerLiqLevel.SetLineWeight(2);

# ─── NADARAYA-WATSON BAND ─────────────────────────────────────────────────────
def nw_open_val  = if is_bull then nw_val - volatility else nw_val + volatility;
def nw_close_val = nw_val;
def nw_high_val  = if !is_bull then high   else nw_val;
def nw_low_val   = if  is_bull then low    else nw_val;

plot NW_BandTop = if show_nw_candles then nw_high_val  else Double.NaN;
plot NW_BandBot = if show_nw_candles then nw_low_val   else Double.NaN;
plot NW_BandOp  = if show_nw_candles then nw_open_val  else Double.NaN;
plot NW_BandCl  = if show_nw_candles then nw_close_val else Double.NaN;

NW_BandTop.Hide();
NW_BandBot.Hide();

NW_BandOp.AssignValueColor(if is_bull then Color.CYAN else Color.RED);
NW_BandOp.SetStyle(Curve.FIRM);
NW_BandOp.SetLineWeight(2);

NW_BandCl.AssignValueColor(if is_bull then Color.CYAN else Color.RED);
NW_BandCl.SetStyle(Curve.FIRM);
NW_BandCl.SetLineWeight(2);

# AddCloud requires Color enum constants — no variables, no CreateColor()
AddCloud(NW_BandOp, NW_BandCl,  Color.RED, Color.CYAN);

# ─── BAR RECOLORING ───────────────────────────────────────────────────────────
AssignPriceColor(
    if !show_bar_colors then Color.CURRENT
    else if is_bull     then Color.CYAN
    else                     Color.RED
);

# ─── MOMENTUM WEAKENING SIGNALS ───────────────────────────────────────────────
plot WeakBull = if show_momentum and osc > 0 and con1 then low  else Double.NaN;
WeakBull.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
WeakBull.SetDefaultColor(Color.CYAN);
WeakBull.SetLineWeight(3);

plot WeakBear = if show_momentum and osc < 0 and con2 then high else Double.NaN;
WeakBear.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
WeakBear.SetDefaultColor(Color.RED);
WeakBear.SetLineWeight(3);

# ─── REBOUND SIGNALS ──────────────────────────────────────────────────────────
def bull_reb = osc > 0 and close[1] < nw_val and close > nw_val;
def bear_reb = osc < 0 and close[1] > nw_val and close < nw_val;

plot BullRebound = if show_rebound and bull_reb
                   then nw_val - volatility * 1.5
                   else Double.NaN;
BullRebound.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BullRebound.SetDefaultColor(Color.CYAN);
BullRebound.SetLineWeight(2);

plot BearRebound = if show_rebound and bear_reb
                   then nw_val + volatility * 1.5
                   else Double.NaN;
BearRebound.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
BearRebound.SetDefaultColor(Color.RED);
BearRebound.SetLineWeight(2);

# ─── ALERTS ───────────────────────────────────────────────────────────────────
Alert(alerts and osc > 0 and con1,                        "Weakening Bullish Momentum", Alert.BAR, Sound.Chimes);
Alert(alerts and osc < 0 and con2,                        "Weakening Bearish Momentum", Alert.BAR, Sound.Chimes);
Alert(alerts and con2,                                    "Bullish Signal Cross",       Alert.BAR, Sound.Bell);
Alert(alerts and con1,                                    "Bearish Signal Cross",       Alert.BAR, Sound.Bell);
Alert(alerts and draw_upper,                              "New Upper Liquidity Level",  Alert.BAR, Sound.Ring);
Alert(alerts and draw_lower,                              "New Lower Liquidity Level",  Alert.BAR, Sound.Ring);
Alert(alerts and active_type[1] == 1  and close > active_price[1], "Upper Level Swept", Alert.BAR, Sound.Ding);
Alert(alerts and active_type[1] == -1 and close < active_price[1], "Lower Level Swept", Alert.BAR, Sound.Ding);
 

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
1278 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