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: