Volatility Trend Bands [UAlgo] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
rP0XFnw.png


Author Message:
The Volatility Trend Bands [UAlgo] is a trend-following indicator that combines the concepts of volatility and trend detection. Built using the Average True Range (ATR) to measure volatility, this indicator dynamically adjusts upper and lower bands around price movements. The bands act as dynamic support and resistance levels, making it easier to identify trend shifts and potential entry and exit points.

CODE:

CSS:
#// Indicator for TOS
#// © UAlgo
#indicator("Volatility Trend Bands [UAlgo]", overlay = true, shorttitle="Volatility Trend Bands [UAlgo]")
# Converted by Sam4Cok@Samer800    - 09/2024

input timeframe = AggregationPeriod.MIN;
input Source    = FundamentalType.CLOSE; # "Price Source"
input atrLength     = 14; # "ATR Length",
input atrMultiplier = 5.0; # "ATR Multiplier"
input ColorBars     = yes ;# "Enable Bar Colors"
input ShowSignals   = yes ;# "Show Signals"

def na = Double.NaN;
def current = GetAggregationPeriod();
def tf = Max(current, timeframe);
def src = Fundamental(FundamentalType = Source, Period = tf);
def tr = TrueRange(high(Period = tf), close(Period = tf), low(Period = tf));
def nATR = WildersAverage(tr, atrLength);
def atrDelta = atrMultiplier * nATR;

script getVolatilityBands {
    input priceSource = close;
    input atrDelta = 1;
    def upperBand; # = 0.0
    def lowerBand; # = 0.0
    def upper = CompoundValue(1, upperBand[1], priceSource);
    def lower = CompoundValue(1, lowerBand[1], priceSource);
    if priceSource > upperBand[1] {
        upperBand = Max(upperBand[1], Max(priceSource, priceSource[1]));
        lowerBand = if (upperBand - atrDelta) < lowerBand[1] or ((upperBand - atrDelta) > lowerBand[1]
                    and upperBand == upperBand[1]) then lowerBand[1] else (upperBand - atrDelta);
    } else if priceSource < lowerBand[1] {
        lowerBand = Min(lowerBand[1], Min(priceSource, priceSource[1]));
        upperBand = if (lowerBand + atrDelta) > (upperBand[1]) or ((lowerBand + atrDelta) < (upperBand[1])
                    and lowerBand == lowerBand[1]) then upperBand[1] else (lowerBand + atrDelta);
    } else {
        upperBand = upper;
        lowerBand = lower;
    }

    plot up = if !isNaN(close) and upperBand then upperBand else Double.NaN;
    plot dn = if !isNaN(close) and lowerBand then lowerBand else Double.NaN;
}

def upperBand = getVolatilityBands(src, atrDelta).up;
def lowerBand = getVolatilityBands(src, atrDelta).dn;
def midBand = (lowerBand + upperBand) / 2;
def isTrendUp   = midBand > midBand[1];
def isTrendDown = midBand < midBand[1];
def lastTrendState = if isTrendUp then 1 else if isTrendDown then -1 else lastTrendState[1];

def buySignal  = isTrendUp and (lastTrendState[1] < 0);
def sellSignal = isTrendDown and (lastTrendState[1] > 0);
def col = if midBand < midBand[1] then -1 else
          if midBand > midBand[1] then 1 else col[1];

#-- PLots

plot upBand = upperBand;  # "Upper Band"
plot dnBand = lowerBand;  # "Lower Band"
plot mdLine = midBand; #
upBand.SetLineWeight(2);
dnBand.SetLineWeight(2);
upBand.AssignValueColor(if col>0 then Color.CYAN else
                        if col<0 then Color.MAGENTA else Color.GRAY);
dnBand.AssignValueColor(if col>0 then Color.CYAN else
                        if col<0 then Color.MAGENTA else Color.GRAY);
mdLine.SetDefaultColor(Color.GRAY);
mdLine.SetStyle(Curve.SHORT_DASH);

#-- Signals
def Offset = atr(14) * 0.5;
AddChartBubble(ShowSignals and buySignal, low - Offset, "B", Color.CYAN, no);
AddChartBubble(ShowSignals and sellSignal, high + Offset, "S", Color.MAGENTA);

#-- Bar Color

AssignPriceColor(if !ColorBars then Color.CURRENT else
                 if col>0 then Color.GREEN else
                 if col<0 then Color.RED else Color.GRAY);

#-- END of 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
401 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