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.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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