Horizontal ATR

opeyemiajoje

New member
Hi,

Can someone please assist on a TOS code to draw a horizontal line of 3 ATR (average true range) moves above and below the 21EMA
 

Attachments

  • Screenshot 2026-06-20 163127.png
    Screenshot 2026-06-20 163127.png
    12.8 KB · Views: 135
Last edited:
Solution
Hi,

Can someone please assist on a TOS code to draw a horizontal line of 3 ATR (average true range) moves above and below the 21EMA

here you go

draw horizontal ATR lines , x bars back, from last bar, based on prices on last bar.
you can pick how many bars back to start the lines.
i modified mc01439 code.

enable these lines to see continuous lines that follow the ema line.
#plot u1 = upperatr;
#plot l1 = loweratr;


Code:
#ATR_horz_lines

#https://usethinkscript.com/threads/horizontal-atr.22497/
#Horizontal ATR
#opeyemiajoje  7/4

#Can someone please assist on a TOS code to draw a horizontal line of 3 ATR (average true range) moves above and below the 21EMA


def na = double.nan;
def bn = barnumber();

# x is true during a...
Code:
input emaLength     = 21;
input atrLength     = 14;
input atrMult       = 3;
input atrAvgType    = AverageType.WILDERS;

# 21 EMA of close
plot ema21 = ExpAverage(close, emaLength);
EMA21.SetDefaultColor(Color.CYAN);
EMA21.SetLineWeight(2);

# ATR based on Wilder's average of TrueRange
def tr  = TrueRange(high, close, low);
def atr = MovingAverage(atrAvgType, tr, atrLength);

# Levels: 3 ATR above and below the 21 EMA
plot upperATR = ema21 + atrMult * atr;
plot lowerATR = ema21 - atrMult * atr;

upperATR.SetDefaultColor(Color.GREEN);
lowerATR.SetDefaultColor(Color.RED);

upperATR.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowerATR.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

upperATR.SetLineWeight(2);
lowerATR.SetLineWeight(2);
 

Attachments

  • Screenshot 2026-07-04 121307.png
    Screenshot 2026-07-04 121307.png
    84.7 KB · Views: 114
Code:
input emaLength     = 21;
input atrLength     = 14;
input atrMult       = 3;
input atrAvgType    = AverageType.WILDERS;

# 21 EMA of close
plot ema21 = ExpAverage(close, emaLength);
EMA21.SetDefaultColor(Color.CYAN);
EMA21.SetLineWeight(2);

# ATR based on Wilder's average of TrueRange
def tr  = TrueRange(high, close, low);
def atr = MovingAverage(atrAvgType, tr, atrLength);

# Levels: 3 ATR above and below the 21 EMA
plot upperATR = ema21 + atrMult * atr;
plot lowerATR = ema21 - atrMult * atr;

upperATR.SetDefaultColor(Color.GREEN);
lowerATR.SetDefaultColor(Color.RED);

upperATR.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowerATR.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

upperATR.SetLineWeight(2);
lowerATR.SetLineWeight(2);

your code matches most of his words, but not his picture.
he wants horizontal lines
 
Hi,

Can someone please assist on a TOS code to draw a horizontal line of 3 ATR (average true range) moves above and below the 21EMA

here you go

draw horizontal ATR lines , x bars back, from last bar, based on prices on last bar.
you can pick how many bars back to start the lines.
i modified mc01439 code.

enable these lines to see continuous lines that follow the ema line.
#plot u1 = upperatr;
#plot l1 = loweratr;


Code:
#ATR_horz_lines

#https://usethinkscript.com/threads/horizontal-atr.22497/
#Horizontal ATR
#opeyemiajoje  7/4

#Can someone please assist on a TOS code to draw a horizontal line of 3 ATR (average true range) moves above and below the 21EMA


def na = double.nan;
def bn = barnumber();

# x is true during a group of bars, ending at the last bar
input last_bars = 8;
def z = !isnan(close[ (-(last_bars-1)) ]) and isnan(close[-last_bars]);
#addverticalline(z, "-", color.cyan);


input ema_length     = 21;
input atr_length     = 14;
input atr_mult       = 3.0;
input atrAvgType    = AverageType.WILDERS;

plot ema = ExpAverage(close, ema_length);
EMA.SetDefaultColor(Color.CYAN);
EMA.SetLineWeight(2);

def tr  = TrueRange(high, close, low);
def atr = MovingAverage(atrAvgType, tr, atr_length);

def upperATR = ema + atr_mult * atr;
def lowerATR = ema - atr_mult * atr;

#plot u1 = upperatr;
#plot l1 = loweratr;

# define horz lines
def upper;
def lower;
if bn == 1 then {
 upper = na;
 lower = na;
} else if z then {
 upper = getvalue(upperATR, -(last_bars-1));
 lower = getvalue(lowerATR, -(last_bars-1));
} else {
 upper = upper[1];
 lower = lower[1];
}


plot ua = upper;
plot la = lower;
ua.SetDefaultColor(Color.GREEN);
ua.SetStyle(Curve.MEDIUM_DASH);
# ua.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ua.SetLineWeight(2);
la.SetDefaultColor(Color.RED);
la.SetStyle(Curve.MEDIUM_DASH);
#la.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
la.SetLineWeight(2);
#
 

Attachments

  • pic.png
    pic.png
    71.4 KB · Views: 98
Solution
here you go

draw horizontal ATR lines , x bars back, from last bar, based on prices on last bar.
you can pick how many bars back to start the lines.
i modified mc01439 code.

enable these lines to see continuous lines that follow the ema line.
#plot u1 = upperatr;
#plot l1 = loweratr;


Code:
#ATR_horz_lines

#https://usethinkscript.com/threads/horizontal-atr.22497/
#Horizontal ATR
#opeyemiajoje  7/4

#Can someone please assist on a TOS code to draw a horizontal line of 3 ATR (average true range) moves above and below the 21EMA


def na = double.nan;
def bn = barnumber();

# x is true during a group of bars, ending at the last bar
input last_bars = 8;
def z = !isnan(close[ (-(last_bars-1)) ]) and isnan(close[-last_bars]);
#addverticalline(z, "-", color.cyan);


input ema_length     = 21;
input atr_length     = 14;
input atr_mult       = 3.0;
input atrAvgType    = AverageType.WILDERS;

plot ema = ExpAverage(close, ema_length);
EMA.SetDefaultColor(Color.CYAN);
EMA.SetLineWeight(2);

def tr  = TrueRange(high, close, low);
def atr = MovingAverage(atrAvgType, tr, atr_length);

def upperATR = ema + atr_mult * atr;
def lowerATR = ema - atr_mult * atr;

#plot u1 = upperatr;
#plot l1 = loweratr;

# define horz lines
def upper;
def lower;
if bn == 1 then {
 upper = na;
 lower = na;
} else if z then {
 upper = getvalue(upperATR, -(last_bars-1));
 lower = getvalue(lowerATR, -(last_bars-1));
} else {
 upper = upper[1];
 lower = lower[1];
}


plot ua = upper;
plot la = lower;
ua.SetDefaultColor(Color.GREEN);
ua.SetStyle(Curve.MEDIUM_DASH);
# ua.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ua.SetLineWeight(2);
la.SetDefaultColor(Color.RED);
la.SetStyle(Curve.MEDIUM_DASH);
#la.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
la.SetLineWeight(2);
#
Thank you
 

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