Deviation Trend Profile [BigBeluga] For Thinkorswim

chewie76

Well-known member
VIP
VIP Enthusiast
This is an adaptation of the Deviation Trend Profile converted from Tradingview to Thinkorswim.
Original Link: https://www.tradingview.com/script/6Wr1CEuo-Deviation-Trend-Profile-BigBeluga/

A statistical trend analysis tool that combines moving average dynamics with standard deviation zones and trend-specific price distribution.
  • Standard Deviation Zones: Calculates ±1, ±2, ±3 and ±4 levels from the SMA using ATR, forming dynamic envelopes around the mean.

The shaded areas are the 3-4 standard deviation from a moving average (default 50 SMA). Notice in the below picture of /GC on the four-hour timeframe, price was rejected every single time when it hit the ±4 levels.

Color Candles can be turned on/off in settings and align with the Standard Deviation Zones.

1754976054722.png


Code:
# Deviation Trend Profile [BigBeluga] - Thinkorswim Adaptation
# Converted from Pine Script v6
# Box plotting omitted (not supported in Thinkorswim)
# Assembled by Chewie 08/2025

input ColorBar = yes;
input length = 50;
input mult1 = 1;
input mult2 = 2;
input mult3 = 3;
input mult4 = 4;
input atrLength = 200;
input showTrendSignals = yes;
input averageType = AverageType.Simple;

# Theme colors
DefineGlobalColor("UpColor", Color.CYAN);
DefineGlobalColor("DownColor", Color.RED);

# Core calculations
def avg = movingAverage(averageType, close, length);
def atr = Average(TrueRange(high, close[1], low), atrLength);

def stdv1 = avg + atr * mult1;
def stdv2 = avg + atr * mult2;
def stdv3 = avg + atr * mult3;
def stdv4 = avg + atr * mult4;

def stdv_1 = avg - atr * mult1;
def stdv_2 = avg - atr * mult2;
def stdv_3 = avg - atr * mult3;
def stdv_4 = avg - atr * mult4;

# SMA slope
def avgDiff = avg - avg[5];

# Normalize slope for color intensity
def maxSlope = Highest(avgDiff, 500);
def minSlope = Lowest(avgDiff, 500);
def avgColNorm = if maxSlope != minSlope then (avgDiff - minSlope) / (maxSlope - minSlope) else 0;

# Gradient color simulation
def slopeUp = avgColNorm > 0.4;
def slopeDn = avgColNorm < 0.4;


# Trend detection
def trend = CompoundValue(1,
    if avgColNorm crosses above 0.55 then 1
    else if avgColNorm crosses below 0.45 then 0
    else trend[1], 0);

# Plotting deviation lines
plot pStdv1 = stdv1;
plot pStdv2 = stdv2;
plot pStdv3 = stdv3;
plot pStdv4 = stdv4;
plot pStdv_1 = stdv_1;
plot pStdv_2 = stdv_2;
plot pStdv_3 = stdv_3;
plot pStdv_4 = stdv_4;

pStdv4.setlineWeight(2);
pStdv_4.setlineWeight(2);

pStdv1.SetDefaultColor(Color.LIGHT_GRAY);
pStdv2.SetDefaultColor(Color.dark_orange);
pStdv3.SetDefaultColor(Color.red);
pStdv4.SetDefaultColor(Color.magenta);
pStdv_1.SetDefaultColor(Color.LIGHT_GRAY);
pStdv_2.SetDefaultColor(Color.LIGHT_GREEN);
pStdv_3.SetDefaultColor(Color.GREEN);
pStdv_4.SetDefaultColor(Color.CYAN);

AddCloud(pStdv_3, pStdv_4, COLOR.dark_green, COLOR.gray);
AddCloud(pStdv3, pStdv4, COLOR.gray, COLOR.dark_red);

# Plot average with dynamic color
plot pAvg = avg;
#pAvg.AssignValueColor(
#    if slopeUp then GlobalColor("UpColor")
#    else if slopeDn then GlobalColor("DownColor")
#    else Color.GRAY);

PAvg.assignvalueColor(if PAvg < PAvg[3] then color.red else color.green);
pAvg.SetLineWeight(2);

# Trend markers
AddChartBubble(showTrendSignals and trend crosses above 0.4, avg, "UpT", GlobalColor("UpColor"), no);
AddChartBubble(showTrendSignals and trend crosses below 0.4, avg, "DnT", GlobalColor("DownColor"), yes);

AssignPriceColor(if !ColorBar then Color.CURRENT else if low < pstdv_4 then color.cyan else if high > pstdv4 then color.magenta else if close > pstdv_2 and close < pstdv_1 then color.light_green else if close > pstdv_3 and close < pstdv_2 then color.green else if close > pstdv_4 and close < pstdv_3 then color.dark_green else if low < pstdv_4 then color.cyan else if close < pstdv4 and close > pstdv3 then color.dark_red else if close < pstdv3 and close > pstdv2 then color.red else if close > pstdv1 and close < pstdv2 then color.dark_orange else if close < pstdv2 and close > pavg then color.gray else if close < pavg and close > pstdv_2 then color.gray else Color.dark_gray);

# END CODE


If you would like a WATCHLIST, then use the below code.

Code:
# WATCHLIST    Deviation Trend Profile [BigBeluga] - Thinkorswim Adaptation
# Converted from Pine Script v6
# Box plotting omitted (not supported in Thinkorswim)
# Assembled by Chewie 08/2025

input ColorBar = yes;
input length = 50;
input mult1 = 1;
input mult2 = 2;
input mult3 = 3;
input mult4 = 4;
input atrLength = 200;
input showTrendSignals = yes;
input averageType = AverageType.Simple;

# Theme colors
DefineGlobalColor("UpColor", Color.CYAN);
DefineGlobalColor("DownColor", Color.RED);

# Core calculations
def avg = movingAverage(averageType, close, length);
def atr = Average(TrueRange(high, close[1], low), atrLength);

def stdv1 = avg + atr * mult1;
def stdv2 = avg + atr * mult2;
def stdv3 = avg + atr * mult3;
def stdv4 = avg + atr * mult4;

def stdv_1 = avg - atr * mult1;
def stdv_2 = avg - atr * mult2;
def stdv_3 = avg - atr * mult3;
def stdv_4 = avg - atr * mult4;

# SMA slope
def avgDiff = avg - avg[5];

# Normalize slope for color intensity
def maxSlope = Highest(avgDiff, 500);
def minSlope = Lowest(avgDiff, 500);
def avgColNorm = if maxSlope != minSlope then (avgDiff - minSlope) / (maxSlope - minSlope) else 0;

# Gradient color simulation
def slopeUp = avgColNorm > 0.4;
def slopeDn = avgColNorm < 0.4;


# Trend detection
def trend = CompoundValue(1,
    if avgColNorm crosses above 0.55 then 1
    else if avgColNorm crosses below 0.45 then 0
    else trend[1], 0);

# Plotting deviation lines
def pStdv1 = stdv1;
def pStdv2 = stdv2;
def pStdv3 = stdv3;
def pStdv4 = stdv4;
def pStdv_1 = stdv_1;
def pStdv_2 = stdv_2;
def pStdv_3 = stdv_3;
def pStdv_4 = stdv_4;


# Plot average with dynamic color
plot pAvg = avg;
PAvg.assignvalueColor(if PAvg < PAvg[3] then color.red else color.green);
pAvg.SetLineWeight(3);


AssignBACKGROUNDColor(if !ColorBar then Color.CURRENT else if low < pstdv_4 then color.cyan else if high > pstdv4 then color.magenta else if close > pstdv_2 and close < pstdv_1 then color.light_green else if close > pstdv_3 and close < pstdv_2 then color.green else if close > pstdv_4 and close < pstdv_3 then color.dark_green else if low < pstdv_4 then color.cyan else if close < pstdv4 and close > pstdv3 then color.dark_red else if close < pstdv3 and close > pstdv2 then color.red else if close > pstdv1 and close < pstdv2 then color.dark_orange else if close < pstdv2 and close > pavg then color.gray else if close < pavg and close > pstdv_2 then color.gray else Color.dark_gray);



AddLabel(yes, if low < pstdv_4 then "XBUY" else if high > pstdv4 then "XSELL" else " ", if low < pstdv_4 THEN COLOR.blue ELSE IF high > pstdv4 then color.black else Color.BLACK);
 
Last edited:

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

Nicely Done, Thank You.

Thought I'd make some minor adjustments by stripping out some of the unnecessary options such as candle painting and bubbles to lean the code up.

Added a 5th deviation band with option to toggle 5th band on and off.

Code:
# Deviation Trend Profile [BigBeluga] - Thinkorswim Adaptation
# Overview:
# This indicator plots multi-level deviation bands around a central moving average, using ATR for volatility-based spacing. It's designed for visualizing price deviations in swing trading setups, such as on DVN or other volatile energy stocks. Bands act as dynamic support/resistance zones, with optional clouds for outer extremes.
#
# Best Practices and Input Notes:
# - Ensure charts have adequate historical data (e.g., at least 200 bars for atrLength=200) to avoid NaN errors in ATR calculations; TOS may not plot if bars are insufficient.
# - Test across timeframes: For higher ones like quarterly or monthly, lower atrLength (e.g., to 50) if plots fail due to limited bar count.
# - Customize based on strategy: Shorter inputs increase responsiveness but add noise; longer ones smooth signals but may lag in trends.
# - 'length' (default 50): Period for the central moving average. Shorter (20-30) for quick trends in intraday charts; longer (100+) for smoother signals in daily/weekly, though it may delay entries in volatile assets like DVN.
# - 'atrLength' (default 200): Period for Average True Range, controlling band width sensitivity. Shorter (50-100) for adaptive bands in choppy markets; longer for stable, trend-following envelopes.
# - 'averageType' (default Simple): Type of moving average (Simple, Exponential, etc.). Simple is balanced; Exponential weights recent prices more for faster response.
# - 'mult1-5' (defaults 1-5): Multipliers for band levels. Adjust for tighter/looser channels (e.g., reduce to 0.5-3 for conservative zones; increase for wider in high-vol stocks).
# - 'enableClouds' (default yes): Toggles shading between outer bands (±3 to ±4) for visual emphasis on extremes; disable for cleaner charts or better performance.
# - 'enableFifthBand' (default no): Toggles the fifth deviation band (±mult5); enable for additional outer levels if needed for extreme volatility analysis.
# - Performance Tip: This version is stripped down for speed—avoid layering with heavy studies. Backtest in TOS Strategy Tester before live use, and watch for contraction/expansion in low/high-vol periods.
# - Usage Tip: In swing trading (e.g., DVN), watch price touches on outer bands for reversals; combine with momentum tools like RSI for confirmation.
#===============================================================================================================================================================================
input length = 55;
input atrLength = 233;
input averageType = AverageType.Simple;
input enableClouds = yes;
input enableFifthBand = no;
input mult1 = 1;
input mult2 = 2;
input mult3 = 3;
input mult4 = 4;
input mult5 = 5;

def avg = movingAverage(averageType, close, length);
def atr = Average(TrueRange(high, close[1], low), atrLength);

def stdv1 = avg + atr * mult1;
def stdv2 = avg + atr * mult2;
def stdv3 = avg + atr * mult3;
def stdv4 = avg + atr * mult4;
def stdv5 = avg + atr * mult5;
def stdv_1 = avg - atr * mult1;
def stdv_2 = avg - atr * mult2;
def stdv_3 = avg - atr * mult3;
def stdv_4 = avg - atr * mult4;
def stdv_5 = avg - atr * mult5;

plot pAvg = avg;
pAvg.AssignValueColor(if pAvg < pAvg[3] then Color.RED else Color.GREEN);
pAvg.SetLineWeight(2);

plot pStdv1 = stdv1;
plot pStdv2 = stdv2;
plot pStdv3 = stdv3;
plot pStdv4 = stdv4;
plot pStdv5 = if enableFifthBand then stdv5 else Double.NaN;
plot pStdv_1 = stdv_1;
plot pStdv_2 = stdv_2;
plot pStdv_3 = stdv_3;
plot pStdv_4 = stdv_4;
plot pStdv_5 = if enableFifthBand then stdv_5 else Double.NaN;

pStdv1.SetDefaultColor(Color.LIGHT_GRAY);
pStdv2.SetDefaultColor(Color.DARK_ORANGE);
pStdv3.SetDefaultColor(Color.RED);
pStdv4.SetDefaultColor(Color.MAGENTA);
pStdv4.SetLineWeight(2);
pStdv5.SetDefaultColor(Color.PLUM);
pStdv5.SetLineWeight(2);
pStdv5.SetStyle(Curve.SHORT_DASH);
pStdv_1.SetDefaultColor(Color.LIGHT_GRAY);
pStdv_2.SetDefaultColor(Color.LIGHT_GREEN);
pStdv_3.SetDefaultColor(Color.GREEN);
pStdv_4.SetDefaultColor(Color.CYAN);
pStdv_4.SetLineWeight(2);
pStdv_5.SetDefaultColor(Color.BLUE);
pStdv_5.SetLineWeight(2);
pStdv_5.SetStyle(Curve.SHORT_DASH);

def cloudLower1 = if enableClouds then stdv_3 else Double.NaN;
def cloudUpper1 = if enableClouds then stdv_4 else Double.NaN;
AddCloud(cloudLower1, cloudUpper1, COLOR.DARK_GREEN, COLOR.GRAY);
def cloudLower2 = if enableClouds then stdv3 else Double.NaN;
def cloudUpper2 = if enableClouds then stdv4 else Double.NaN;
AddCloud(cloudLower2, cloudUpper2, COLOR.GRAY, COLOR.DARK_RED);
 
Last edited:
This is an adaptation of the Deviation Trend Profile converted from Tradingview to Thinkorswim.
Original Link: https://www.tradingview.com/script/6Wr1CEuo-Deviation-Trend-Profile-BigBeluga/

A statistical trend analysis tool that combines moving average dynamics with standard deviation zones and trend-specific price distribution.
  • Standard Deviation Zones: Calculates ±1, ±2, ±3 and ±4 levels from the SMA using ATR, forming dynamic envelopes around the mean.

The shaded areas are the 3-4 standard deviation from a moving average (default 50 SMA). Notice in the below picture of /GC on the four-hour timeframe, price was rejected every single time when it hit the ±4 levels.

Color Candles can be turned on/off in settings and align with the Standard Deviation Zones.

View attachment 25449

Code:
# Deviation Trend Profile [BigBeluga] - Thinkorswim Adaptation
# Converted from Pine Script v6
# Box plotting omitted (not supported in Thinkorswim)
# Assembled by Chewie 08/2025

input ColorBar = yes;
input length = 50;
input mult1 = 1;
input mult2 = 2;
input mult3 = 3;
input mult4 = 4;
input atrLength = 200;
input showTrendSignals = yes;
input averageType = AverageType.Simple;

# Theme colors
DefineGlobalColor("UpColor", Color.CYAN);
DefineGlobalColor("DownColor", Color.RED);

# Core calculations
def avg = movingAverage(averageType, close, length);
def atr = Average(TrueRange(high, close[1], low), atrLength);

def stdv1 = avg + atr * mult1;
def stdv2 = avg + atr * mult2;
def stdv3 = avg + atr * mult3;
def stdv4 = avg + atr * mult4;

def stdv_1 = avg - atr * mult1;
def stdv_2 = avg - atr * mult2;
def stdv_3 = avg - atr * mult3;
def stdv_4 = avg - atr * mult4;

# SMA slope
def avgDiff = avg - avg[5];

# Normalize slope for color intensity
def maxSlope = Highest(avgDiff, 500);
def minSlope = Lowest(avgDiff, 500);
def avgColNorm = if maxSlope != minSlope then (avgDiff - minSlope) / (maxSlope - minSlope) else 0;

# Gradient color simulation
def slopeUp = avgColNorm > 0.4;
def slopeDn = avgColNorm < 0.4;


# Trend detection
def trend = CompoundValue(1,
    if avgColNorm crosses above 0.55 then 1
    else if avgColNorm crosses below 0.45 then 0
    else trend[1], 0);

# Plotting deviation lines
plot pStdv1 = stdv1;
plot pStdv2 = stdv2;
plot pStdv3 = stdv3;
plot pStdv4 = stdv4;
plot pStdv_1 = stdv_1;
plot pStdv_2 = stdv_2;
plot pStdv_3 = stdv_3;
plot pStdv_4 = stdv_4;

pStdv4.setlineWeight(2);
pStdv_4.setlineWeight(2);

pStdv1.SetDefaultColor(Color.LIGHT_GRAY);
pStdv2.SetDefaultColor(Color.dark_orange);
pStdv3.SetDefaultColor(Color.red);
pStdv4.SetDefaultColor(Color.magenta);
pStdv_1.SetDefaultColor(Color.LIGHT_GRAY);
pStdv_2.SetDefaultColor(Color.LIGHT_GREEN);
pStdv_3.SetDefaultColor(Color.GREEN);
pStdv_4.SetDefaultColor(Color.CYAN);

AddCloud(pStdv_3, pStdv_4, COLOR.dark_green, COLOR.gray);
AddCloud(pStdv3, pStdv4, COLOR.gray, COLOR.dark_red);

# Plot average with dynamic color
plot pAvg = avg;
#pAvg.AssignValueColor(
#    if slopeUp then GlobalColor("UpColor")
#    else if slopeDn then GlobalColor("DownColor")
#    else Color.GRAY);

PAvg.assignvalueColor(if PAvg < PAvg[3] then color.red else color.green);
pAvg.SetLineWeight(2);

# Trend markers
AddChartBubble(showTrendSignals and trend crosses above 0.4, avg, "UpT", GlobalColor("UpColor"), no);
AddChartBubble(showTrendSignals and trend crosses below 0.4, avg, "DnT", GlobalColor("DownColor"), yes);

AssignPriceColor(if !ColorBar then Color.CURRENT else if low < pstdv_4 then color.cyan else if high > pstdv4 then color.magenta else if close > pstdv_2 and close < pstdv_1 then color.light_green else if close > pstdv_3 and close < pstdv_2 then color.green else if close > pstdv_4 and close < pstdv_3 then color.dark_green else if low < pstdv_4 then color.cyan else if close < pstdv4 and close > pstdv3 then color.dark_red else if close < pstdv3 and close > pstdv2 then color.red else if close > pstdv1 and close < pstdv2 then color.dark_orange else if close < pstdv2 and close > pavg then color.gray else if close < pavg and close > pstdv_2 then color.gray else Color.dark_gray);

# END CODE


If you would like a WATCHLIST, then use the below code.

Code:
# WATCHLIST    Deviation Trend Profile [BigBeluga] - Thinkorswim Adaptation
# Converted from Pine Script v6
# Box plotting omitted (not supported in Thinkorswim)
# Assembled by Chewie 08/2025

input ColorBar = yes;
input length = 50;
input mult1 = 1;
input mult2 = 2;
input mult3 = 3;
input mult4 = 4;
input atrLength = 200;
input showTrendSignals = yes;
input averageType = AverageType.Simple;

# Theme colors
DefineGlobalColor("UpColor", Color.CYAN);
DefineGlobalColor("DownColor", Color.RED);

# Core calculations
def avg = movingAverage(averageType, close, length);
def atr = Average(TrueRange(high, close[1], low), atrLength);

def stdv1 = avg + atr * mult1;
def stdv2 = avg + atr * mult2;
def stdv3 = avg + atr * mult3;
def stdv4 = avg + atr * mult4;

def stdv_1 = avg - atr * mult1;
def stdv_2 = avg - atr * mult2;
def stdv_3 = avg - atr * mult3;
def stdv_4 = avg - atr * mult4;

# SMA slope
def avgDiff = avg - avg[5];

# Normalize slope for color intensity
def maxSlope = Highest(avgDiff, 500);
def minSlope = Lowest(avgDiff, 500);
def avgColNorm = if maxSlope != minSlope then (avgDiff - minSlope) / (maxSlope - minSlope) else 0;

# Gradient color simulation
def slopeUp = avgColNorm > 0.4;
def slopeDn = avgColNorm < 0.4;


# Trend detection
def trend = CompoundValue(1,
    if avgColNorm crosses above 0.55 then 1
    else if avgColNorm crosses below 0.45 then 0
    else trend[1], 0);

# Plotting deviation lines
def pStdv1 = stdv1;
def pStdv2 = stdv2;
def pStdv3 = stdv3;
def pStdv4 = stdv4;
def pStdv_1 = stdv_1;
def pStdv_2 = stdv_2;
def pStdv_3 = stdv_3;
def pStdv_4 = stdv_4;


# Plot average with dynamic color
plot pAvg = avg;
PAvg.assignvalueColor(if PAvg < PAvg[3] then color.red else color.green);
pAvg.SetLineWeight(3);


AssignBACKGROUNDColor(if !ColorBar then Color.CURRENT else if low < pstdv_4 then color.cyan else if high > pstdv4 then color.magenta else if close > pstdv_2 and close < pstdv_1 then color.light_green else if close > pstdv_3 and close < pstdv_2 then color.green else if close > pstdv_4 and close < pstdv_3 then color.dark_green else if low < pstdv_4 then color.cyan else if close < pstdv4 and close > pstdv3 then color.dark_red else if close < pstdv3 and close > pstdv2 then color.red else if close > pstdv1 and close < pstdv2 then color.dark_orange else if close < pstdv2 and close > pavg then color.gray else if close < pavg and close > pstdv_2 then color.gray else Color.dark_gray);



AddLabel(yes, if low < pstdv_4 then "XBUY" else if high > pstdv4 then "XSELL" else " ", if low < pstdv_4 THEN COLOR.blue ELSE IF high > pstdv4 then color.black else Color.BLACK);
I'm quite surprised that there's not more interest, as this is a rather very useful indicator.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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