ATR Trend Bands [Misu] for ThinkOrSwim

samer800

Conversion Expert
VIP
Lifetime
Gtv6gsF.png

Creator Message:
This indicator shows an upper and lower band based on price action and ATR ( Average True Range )
The average true range (ATR) is a market volatility indicator used in technical analysis .

█ Usages:
The purpose of this indicator is to identify changes in trends and price action.
It is mainly used to identify breaking points and trend reversals.
But it can also be used to show resistance or support levels.

█ Features:
Buy & Sell Alerts
Buy & Sell Labels
Color Bars
Show Bands

█ Parameters:
Length: Length is used to calculate ATR.
Atr Multiplier: A factor used to balance the impact of the ATR on the Trend Bands calculation.

Code - Update - added MTF option

CSS:
#/ This source code is subject to the terms of the Mozilla Public License 2.0
#// ©Misu
#https://www.tradingview.com/script/PtnKi3nX-ATR-Trend-Bands-Misu/
#indicator("ATR Trend Bands [Misu]", overlay = true, shorttitle="ATR TB [Misu]")
# Converted by Sam4Cok@Samer800 - 11/2022
# Update - added MTF option, Sam4Cok@Samer800    - 12/2023
#// ] -------- Input  --------------- [
input useChartTimeframe =  {default"Yes", "No"};
input manualTimeframe   = AggregationPeriod.FIFTEEN_MIN;
input Source      = FundamentalType.CLOSE;#, "Source"
input atrLength   = 10;#, "Length Atr"
input atrMultiplier = 2.6;#, "Multiplier Atr"
input ShowLines   = yes;
input ShowCloud   = yes;
input colorbars   = no;#, "Color Bars"
input showBubbles = yes;#, "Show Label

def na = Double.NaN;
def last = isNaN(close);
def srcCTF = Fundamental(FundamentalType = Source);
def srcHTF = Fundamental(FundamentalType = Source, Period = manualTimeframe);
def cHTF = close(Period = manualTimeframe);
def oHTF = open(Period = manualTimeframe);
def hHTF = high(Period = manualTimeframe);
def lHTF = low(Period = manualTimeframe);
#-- MTF
def c; def o; def h; def l;def src;
Switch (useChartTimeframe) {
case "No"  :
    src = srcHTF;
    c = cHTF;
    o = oHTF;
    h = hHTF;
    l = lHTF;
Default :
    src = srcCTF;
    c = close;
    o = open;
    h = high;
    l = low;
}

#getTrendBands(float src, float delta)=>
script getTrendBands {
    input src = close;
    input delta = 14;
    def upperb;
    def lowerb;
    def low;
    def upp;
    if src > upperb[1] {
        upperb = Max(upperb[1], Max(src, src[1]));
        upp    = upp[1];
        low    = upperb - delta;
        lowerb = if low < lowerb[1] or low > lowerb[1] and upperb == upperb[1] then
                 lowerb[1] else low;
    } else {
        if src < lowerb[1] {
            lowerb = Min(lowerb[1], Min(src, src[1]));
            upp    = lowerb + delta;
            low = low[1];
            upperb = if upp > upperb[1] or upp < upperb[1] and lowerb == lowerb[1] then
                upperb[1] else upp;
        } else {
            upperb = upperb[1];
            upp = upp[1];
            low    = low[1];
            lowerb = lowerb[1];
        }
    }
    def midb = (lowerb + upperb) / 2;
    plot midLine = midb;
    plot Upper = upperb;
    plot Lower = lowerb;
}
#// ] -------- Logic ----------------- [
def tr = TrueRange(h, c, l);
def nATR = WildersAverage(tr, atrLength);
def deltaAtr = atrMultiplier * nATR;
def midb   = getTrendBands(src, deltaAtr).midLine;
def upperb = getTrendBands(src, deltaAtr).Upper;
def lowerb = getTrendBands(src, deltaAtr).Lower;

def trendUp     = midb > midb[1];
def trendDown   = midb < midb[1];

def lastState   = if trendUp then 1 else if trendDown then -1 else lastState[1];
def buyCond     = trendUp and lastState[1] == -1 and c>o;
def sellCond    = trendDown and lastState[1] == 1 and c<o;

#// ] -------- Plot Part --------------- [

def colorTrend = if midb < midb[1] then -1 else if midb > midb[1] then 1 else colorTrend[1];

plot UpperBand = if last or colorTrend > 0 then na else upperb;#, "Upper Band"
UpperBand.SetDefaultColor(CreateColor(255,82,82));
UpperBand.SetHiding(!ShowLines);
plot LowerBand = if last or colorTrend <= 0  then na else lowerb;#, "Lower Band"
LowerBand.SetDefaultColor(CreateColor(76,175,80));
LowerBand.SetHiding(!ShowLines);

plot UpBand = if last or colorTrend <= 0 then na else upperb;#, "Upper Band"
UpBand.SetDefaultColor(CreateColor(76,175,80));
UpBand.SetHiding(!ShowLines);
plot LoBand = if last or colorTrend > 0  then na else lowerb;#, "Lower Band"
LoBand.SetDefaultColor(CreateColor(255,82,82));
LoBand.SetHiding(!ShowLines);


plot MidBand   = if last then na else midb;#, "Mid Band"
MidBand.AssignValueColor(if colorTrend > 0 then CreateColor(76,175,80) else CreateColor(255,82,82));
MidBand.SetHiding(!ShowLines);

#------ Bubbles
AddChartBubble(showBubbles and buyCond, low, "B", Color.GREEN, no);
AddChartBubble(showBubbles and sellCond, high, "S", Color.RED, yes);

#--- Bar Color
AssignPriceColor(if colorbars then
                 if colorTrend>0 and c>midb then color.GREEN else
                 if colorTrend>0 and c<midb then Color.DARK_GREEN else
                 if colorTrend<0 and c<midb then Color.RED else
                 if colorTrend<0 and c>midb then Color.DARK_RED else Color.GRAY else Color.CURRENT);
#--- coud
AddCloud(if !ShowCloud then na else UpperBand,midb,Color.DARK_RED);
AddCloud(if !ShowCloud then na else midb, LowerBand,Color.DARK_GREEN);

#---- END Code
Code:
 
Last edited:
Hello Sam4Cok @samer800, how could we create an actual scan from scan tab for above coding to get and buy and sell alert, i tried , but could not get the alert aeven though i could see the buy see bulbble in the chart. Thank you

#/ This source code is subject to the terms of the Mozilla Public License 2.0
#// ©Misu
#https://www.tradingview.com/script/PtnKi3nX-ATR-Trend-Bands-Misu/
#indicator("ATR Trend Bands [Misu]", overlay = true, shorttitle="ATR TB [Misu]")
# Converted by Sam4Cok@Samer800 - 11/2022
#// ] -------- Input --------------- [
input src = close;#, "Source"
input len = 10;#, "Length Atr"
input mult = 2.6;#, "Multiplier Atr"
input ShowMidLine = no;
input colorbars = no;#, "Color Bars"
input showBubbles = yes;#, "Show Label

def na = Double.NaN;
def c = close; def o = open;
#getTrendBands(float src, float delta)=>
script getTrendBands {
input src = close;
input delta = 14;
def upperb;
def lowerb;
def low;
def upp;
if src > upperb[1] {
upperb = Max(upperb[1], Max(src, src[1]));
upp = upp[1];
low = upperb - delta;
lowerb = if low < lowerb[1] or low > lowerb[1] and upperb == upperb[1] then
lowerb[1] else low;
} else {
if src < lowerb[1] {
lowerb = Min(lowerb[1], Min(src, src[1]));
upp = lowerb + delta;
low = low[1];
upperb = if upp > upperb[1] or upp < upperb[1] and lowerb == lowerb[1] then
upperb[1] else upp;
} else {
upperb = upperb[1];
upp = upp[1];
low = low[1];
lowerb = lowerb[1];
}
}
def midb = (lowerb + upperb) / 2;
plot midLine = midb;
plot Upper = upperb;
plot Lower = lowerb;
}
#// ] -------- Logic ----------------- [
def deltaAtr = mult * ATR(len);
def midb = getTrendBands(src, deltaAtr).midLine;
def upperb = getTrendBands(src, deltaAtr).Upper;
def lowerb = getTrendBands(src, deltaAtr).Lower;

def trendUp = midb > midb[1];
def trendDown = midb < midb[1];

def lastState;
lastState = if trendUp then 1 else if trendDown then -1 else lastState[1];

def buyCond = trendUp and lastState[1] == -1 and c>o;
def sellCond = trendDown and lastState[1] == 1 and c<o;

#// ] -------- Plot Part --------------- [

def colorTrend = if midb < midb[1] then -1 else if midb > midb[1] then 1 else colorTrend[1];

plot UpperBand = if isNaN(c) then na else upperb;#, "Upper Band"
UpperBand.AssignValueColor(if colorTrend > 0 then CreateColor(76,175,80) else CreateColor(255,82,82));
plot LowerBand = if isNaN(c) then na else lowerb;#, "Lower Band"
LowerBand.AssignValueColor(if colorTrend > 0 then CreateColor(76,175,80) else CreateColor(255,82,82));
plot MidBand = if isNaN(c) then na else midb;#, "Mid Band"
MidBand.AssignValueColor(if colorTrend > 0 then CreateColor(76,175,80) else CreateColor(255,82,82));
MidBand.SetHiding(!ShowMidLine);

#------ Bubbles
AddChartBubble(showBubbles and buyCond, low, "B", Color.GREEN, no);
AddChartBubble(showBubbles and sellCond, high, "S", Color.RED, yes);

#--- Bar Color
AssignPriceColor(if colorbars then
if colorTrend>0 and c>midb then color.GREEN else
if colorTrend>0 and c<midb then Color.DARK_GREEN else
if colorTrend<0 and c<midb then Color.RED else
if colorTrend<0 and c>midb then Color.DARK_RED else Color.GRAY else Color.CURRENT);

#---- END Code
 
Last edited by a moderator:
Gtv6gsF.png

Creator Message:
This indicator shows an upper and lower band based on price action and ATR ( Average True Range )
The average true range (ATR) is a market volatility indicator used in technical analysis .

█ Usages:
The purpose of this indicator is to identify changes in trends and price action.
It is mainly used to identify breaking points and trend reversals.
But it can also be used to show resistance or support levels.

█ Features:


█ Parameters:
Length: Length is used to calculate ATR.
Atr Multiplier: A factor used to balance the impact of the ATR on the Trend Bands calculation.

Code:
CSS:
#/ This source code is subject to the terms of the Mozilla Public License 2.0
#// ©Misu
#https://www.tradingview.com/script/PtnKi3nX-ATR-Trend-Bands-Misu/
#indicator("ATR Trend Bands [Misu]", overlay = true, shorttitle="ATR TB [Misu]")
# Converted by Sam4Cok@Samer800 - 11/2022
#// ] -------- Input  --------------- [
input src         = close;#, "Source"
input len         = 10;#, "Length Atr"
input mult        = 2.6;#, "Multiplier Atr"
input ShowMidLine = no;
input colorbars   = no;#, "Color Bars"
input showBubbles = yes;#, "Show Label

def na = Double.NaN;
def c = close; def o = open;
#getTrendBands(float src, float delta)=>
script getTrendBands {
    input src = close;
    input delta = 14;
    def upperb;
    def lowerb;
    def low;
    def upp;
    if src > upperb[1] {
        upperb = Max(upperb[1], Max(src, src[1]));
        upp    = upp[1];
        low    = upperb - delta;
        lowerb = if low < lowerb[1] or low > lowerb[1] and upperb == upperb[1] then
                 lowerb[1] else low;
    } else {
        if src < lowerb[1] {
            lowerb = Min(lowerb[1], Min(src, src[1]));
            upp    = lowerb + delta;
            low = low[1];
            upperb = if upp > upperb[1] or upp < upperb[1] and lowerb == lowerb[1] then
                upperb[1] else upp;
        } else {
            upperb = upperb[1];
            upp = upp[1];
            low    = low[1];
            lowerb = lowerb[1];
        }
    }
    def midb = (lowerb + upperb) / 2;
    plot midLine = midb;
    plot Upper = upperb;
    plot Lower = lowerb;
}
#// ] -------- Logic ----------------- [
def deltaAtr = mult * ATR(len);
def midb   = getTrendBands(src, deltaAtr).midLine;
def upperb = getTrendBands(src, deltaAtr).Upper;
def lowerb = getTrendBands(src, deltaAtr).Lower;

def trendUp     = midb > midb[1];
def trendDown   = midb < midb[1];

def lastState;
lastState   = if trendUp then 1 else if trendDown then -1 else lastState[1];

def buyCond     = trendUp and lastState[1] == -1 and c>o;
def sellCond    = trendDown and lastState[1] == 1 and c<o;

#// ] -------- Plot Part --------------- [

def colorTrend = if midb < midb[1] then -1 else if midb > midb[1] then 1 else colorTrend[1];

plot UpperBand = if isNaN(c) then na else upperb;#, "Upper Band"
UpperBand.AssignValueColor(if colorTrend > 0 then CreateColor(76,175,80) else CreateColor(255,82,82));
plot LowerBand = if isNaN(c) then na else lowerb;#, "Lower Band"
LowerBand.AssignValueColor(if colorTrend > 0 then CreateColor(76,175,80) else CreateColor(255,82,82));
plot MidBand   = if isNaN(c) then na else midb;#, "Mid Band"
MidBand.AssignValueColor(if colorTrend > 0 then CreateColor(76,175,80) else CreateColor(255,82,82));
MidBand.SetHiding(!ShowMidLine);

#------ Bubbles
AddChartBubble(showBubbles and buyCond, low, "B", Color.GREEN, no);
AddChartBubble(showBubbles and sellCond, high, "S", Color.RED, yes);

#--- Bar Color
AssignPriceColor(if colorbars then
                 if colorTrend>0 and c>midb then color.GREEN else
                 if colorTrend>0 and c<midb then Color.DARK_GREEN else
                 if colorTrend<0 and c<midb then Color.RED else
                 if colorTrend<0 and c>midb then Color.DARK_RED else Color.GRAY else Color.CURRENT);

#---- END Code
hi @samer800 need a favor from you, do you think is possible to make a scan for this awesome indicator. thank you some much!
 
Here is a scan for this excellent study. Please test it out, and let me know if it has value, or if it needs changes. This is a SCAN based on the code that Samer800 converted.


Code:
#/ This source code is subject to the terms of the Mozilla Public License 2.0
#// ©Misu
#https://www.tradingview.com/script/PtnKi3nX-ATR-Trend-Bands-Misu/
#indicator("ATR Trend Bands [Misu]", overlay = true, shorttitle="ATR TB [Misu]")
# Converted by Sam4Cok@Samer800 - 11/2022
#// ] -------- Input --------------- [
input src = close;#, "Source"
input len = 10;#, "Length Atr"
input mult = 2.6;#, "Multiplier Atr"
input ShowMidLine = no;
input colorbars = no;#, "Color Bars"
input showBubbles = yes;#, "Show Label

def na = Double.NaN;
def c = close; def o = open;

def deltaAtr = mult * ATR(len);
#getTrendBands(float src, float delta)=>
#script getTrendBands {
#input src = close;
def delta = deltaAtr;
def upperb;
def lowerb;
def low;
def upp;
if src > upperb[1] {
upperb = Max(upperb[1], Max(src, src[1]));
upp = upp[1];
low = upperb - delta;
lowerb = if low < lowerb[1] or low > lowerb[1] and upperb == upperb[1] then
lowerb[1] else low;
} else {
if src < lowerb[1] {
lowerb = Min(lowerb[1], Min(src, src[1]));
upp = lowerb + delta;
low = low[1];
upperb = if upp > upperb[1] or upp < upperb[1] and lowerb == lowerb[1] then
upperb[1] else upp;
} else {
upperb = upperb[1];
upp = upp[1];
low = low[1];
lowerb = lowerb[1];
}
}
def midb1 = (lowerb + upperb) / 2;
def midLine = midb1;
def Upper = upperb;
def Lower = lowerb;

#// ] -------- Logic ----------------- [

def midb = midLine;


def trendUp = midb > midb[1];
def trendDown = midb < midb[1];

def lastState;
lastState = if trendUp then 1 else if trendDown then -1 else lastState[1];

def buyCond = trendUp and lastState[1] == -1 and c>o;
def sellCond = trendDown and lastState[1] == 1 and c<o;



#------ Bubbles
#AddChartBubble(showBubbles and buyCond, low, "B", Color.WHITE, no);
#AddChartBubble(showBubbles and sellCond, high, "S", Color.WHITE, yes);

plot scan = buycond or sellcond ;
 

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