How to plot a horizontal line at the ATR level?

Joshua

Well-known member
VIP
ATR is a distance which requires a starting point and an ending point when applied to upper chart studies.

This, for example, would center ATR around the center of each bar;

Code:
plot x = hl2 + (ATR() * 0.5);
plot y = hl2 - (ATR() * 0.5);
x.setpaintingStrategy(paintingStrategy.HORIZONTAL);
y.setpaintingStrategy(paintingStrategy.HORIZONTAL);

But it depends on where you want to anchor it from, etc.

What, exactly, do you consider to be the ATR level?
 

ronviv1

New member
VIP
I would like to have a horizontal line at the last level of "CLOSE - ATR" instead of the continuous line shown here as a dotted white line.
.
The doted line is the continuous line drawn with the following script:


Here is the image:
2022-01-18-TOS-CHARTS.png
Code:
input ATRLength = 14;
input averagetype = AverageType.WILDERS;
input BasePeriod = AggregationPeriod.DAY;
input showlabel = yes;

Def ATR = (MovingAverage (averagetype, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), ATRLength));

plot line = CLOSE(period = BasePeriod)[1]- ATR;

line.SetDefaultColor(Color.WHITE);
line.SetStyle(Curve.SHORT_DASH);
line.SetLineWeight(1);
 
Last edited:

Svanoy

Active member
@ronviv1
Code:
input ATRLength = 14;
input averagetype = AverageType.WILDERS;
input BasePeriod = AggregationPeriod.thirty_MIN;
input showlabel = yes;

Def ATR = (MovingAverage (averagetype, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), ATRLength));

plot line = CLOSE(period = BasePeriod)[1]- ATR;

line.SetDefaultColor(Color.WHITE);
line.SetStyle(Curve.SHORT_DASH);
line.SetLineWeight(1);

def barNum = if !IsNaN(BarNumber()) then Barnumber() else Double.NaN;
def numBars = HighestAll(barNum);

def FinalBar= fold c = 0 to numbars while !IsNaN(GetValue(close, -c)) do GetValue(BarNumber(), -c);
def FinalATR = fold a = 0 to AbsValue(FinalBar) while !IsNaN(GetValue(close, -a)) do GetValue(line[-1], -a);

plot DayATR = if FinalATR>1 then FinalATR else Double.NaN;
DayATR.SetStyle(Curve.LONG_DASH);
DayATR.AssignValueColor(Color.LIME);
 
Last edited:

ronviv1

New member
VIP
@Svanoy, thank you for your help with code for ATR horizontal line.

Is it possible to create the similar code to plot a horizontal line at 20-Day EMA value?

I tried to modify ATR code but could not succeed. Thank you.
 

ronviv1

New member
VIP
Here is the modified code for 20-Day EMA, but it does plot EMA but will not plot a horizontal line at the final value!


Code:
input price = close;
input length = 20;
input displace = 0;

plot EMA = ExpAverage(price[-displace], length);

plot line = EMA;

EMA.SetDefaultColor(GetColor(1));


def barNum = if !IsNaN(BarNumber()) then Barnumber() else Double.NaN;
def numBars = HighestAll(barNum);

def FinalBar= fold c = 0 to numbars while !IsNaN(GetValue(close, -c)) do GetValue(BarNumber(), -c);
def FinalEMA = fold a = 0 to AbsValue(FinalBar) while !IsNaN(GetValue(close, -a)) do GetValue(line[-1], -a);

plot DayEMA = if FinalEMA>1 then FinalEMA else Double.NaN;
DayEMA.SetDefaultColor(GetColor(1));
 
Last edited:

Svanoy

Active member
@ronviv1
Remove the [-1] in the definition of FinalEMA, that is only needed for ATR.
Should be:
Code:
def FinalEMA = fold a = 0 to AbsValue(FinalBar) while !IsNaN(GetValue(close, -a)) do GetValue(line, -a);
 

Wardo21

New member
VIP
@ronviv1
Code:
input ATRLength = 14;
input averagetype = AverageType.WILDERS;
input BasePeriod = AggregationPeriod.thirty_MIN;
input showlabel = yes;

Def ATR = (MovingAverage (averagetype, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), ATRLength));

plot line = CLOSE(period = BasePeriod)[1]- ATR;

line.SetDefaultColor(Color.WHITE);
line.SetStyle(Curve.SHORT_DASH);
line.SetLineWeight(1);

def barNum = if !IsNaN(BarNumber()) then Barnumber() else Double.NaN;
def numBars = HighestAll(barNum);

def FinalBar= fold c = 0 to numbars while !IsNaN(GetValue(close, -c)) do GetValue(BarNumber(), -c);
def FinalATR = fold a = 0 to AbsValue(FinalBar) while !IsNaN(GetValue(close, -a)) do GetValue(line[-1], -a);

plot DayATR = if FinalATR>1 then FinalATR else Double.NaN;
DayATR.SetStyle(Curve.LONG_DASH);
DayATR.AssignValueColor(Color.LIME);
@ronviv1
Code:
input ATRLength = 14;
input averagetype = AverageType.WILDERS;
input BasePeriod = AggregationPeriod.thirty_MIN;
input showlabel = yes;

Def ATR = (MovingAverage (averagetype, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), ATRLength));

plot line = CLOSE(period = BasePeriod)[1]- ATR;

line.SetDefaultColor(Color.WHITE);
line.SetStyle(Curve.SHORT_DASH);
line.SetLineWeight(1);

def barNum = if !IsNaN(BarNumber()) then Barnumber() else Double.NaN;
def numBars = HighestAll(barNum);

def FinalBar= fold c = 0 to numbars while !IsNaN(GetValue(close, -c)) do GetValue(BarNumber(), -c);
def FinalATR = fold a = 0 to AbsValue(FinalBar) while !IsNaN(GetValue(close, -a)) do GetValue(line[-1], -a);

plot DayATR = if FinalATR>1 then FinalATR else Double.NaN;
DayATR.SetStyle(Curve.LONG_DASH);
DayATR.AssignValueColor(Color.LIME);
Thank you so much for this. I see the horizontal line to the downside, but is it possible to add an additional horizontal line for the upper boundary of ATR?
 

Svanoy

Active member
@Wardo21 I assume this is what you're wanting.
9HMuqtg.png

Code:
input ATRLength = 14;
input averagetype = AverageType.WILDERS;
input BasePeriod = AggregationPeriod.thirty_MIN;
input showlabel = yes;

Def ATR = (MovingAverage (averagetype, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), ATRLength));

plot line = CLOSE(period = BasePeriod)[1]- ATR;
line.SetDefaultColor(Color.WHITE);
line.SetStyle(Curve.SHORT_DASH);
line.SetLineWeight(1);

plot line1 = CLOSE(period = BasePeriod)[1]+ ATR;
line1.SetDefaultColor(Color.WHITE);
line1.SetStyle(Curve.SHORT_DASH);
line1.SetLineWeight(1);

def barNum = if !IsNaN(BarNumber()) then Barnumber() else Double.NaN;
def numBars = HighestAll(barNum);

def FinalBar= fold c = 0 to numbars while !IsNaN(GetValue(close, -c)) do GetValue(BarNumber(), -c);
def FinalATRDn = fold a = 0 to AbsValue(FinalBar) while !IsNaN(GetValue(close, -a)) do GetValue(line[-1], -a);
def FinalATRUp = fold b = 0 to AbsValue(FinalBar) while !IsNaN(GetValue(close, -b)) do GetValue(line1[-1], -b);

plot DayATRDn = if FinalATRDn>1 then FinalATRDn else Double.NaN;
DayATRDn.SetStyle(Curve.LONG_DASH);
DayATRDn.AssignValueColor(Color.LIME);

plot DayATRUp = if FinalATRUp>1 then FinalATRUp else Double.NaN;
DayATRUp.SetStyle(Curve.LONG_DASH);
DayATRUp.AssignValueColor(Color.LIME);
 

Wardo21

New member
VIP
@Wardo21 I assume this is what you're wanting.
9HMuqtg.png

Code:
input ATRLength = 14;
input averagetype = AverageType.WILDERS;
input BasePeriod = AggregationPeriod.thirty_MIN;
input showlabel = yes;

Def ATR = (MovingAverage (averagetype, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), ATRLength));

plot line = CLOSE(period = BasePeriod)[1]- ATR;
line.SetDefaultColor(Color.WHITE);
line.SetStyle(Curve.SHORT_DASH);
line.SetLineWeight(1);

plot line1 = CLOSE(period = BasePeriod)[1]+ ATR;
line1.SetDefaultColor(Color.WHITE);
line1.SetStyle(Curve.SHORT_DASH);
line1.SetLineWeight(1);

def barNum = if !IsNaN(BarNumber()) then Barnumber() else Double.NaN;
def numBars = HighestAll(barNum);

def FinalBar= fold c = 0 to numbars while !IsNaN(GetValue(close, -c)) do GetValue(BarNumber(), -c);
def FinalATRDn = fold a = 0 to AbsValue(FinalBar) while !IsNaN(GetValue(close, -a)) do GetValue(line[-1], -a);
def FinalATRUp = fold b = 0 to AbsValue(FinalBar) while !IsNaN(GetValue(close, -b)) do GetValue(line1[-1], -b);

plot DayATRDn = if FinalATRDn>1 then FinalATRDn else Double.NaN;
DayATRDn.SetStyle(Curve.LONG_DASH);
DayATRDn.AssignValueColor(Color.LIME);

plot DayATRUp = if FinalATRUp>1 then FinalATRUp else Double.NaN;
DayATRUp.SetStyle(Curve.LONG_DASH);
DayATRUp.AssignValueColor(Color.LIME);
You are an amazingly intelligent, beautiful human being! Thank you!
 

Wardo21

New member
VIP
You are an amazingly intelligent, beautiful human being! Thank you!
@Wardo21 I assume this is what you're wanting.
9HMuqtg.png

Code:
input ATRLength = 14;
input averagetype = AverageType.WILDERS;
input BasePeriod = AggregationPeriod.thirty_MIN;
input showlabel = yes;

Def ATR = (MovingAverage (averagetype, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), ATRLength));

plot line = CLOSE(period = BasePeriod)[1]- ATR;
line.SetDefaultColor(Color.WHITE);
line.SetStyle(Curve.SHORT_DASH);
line.SetLineWeight(1);

plot line1 = CLOSE(period = BasePeriod)[1]+ ATR;
line1.SetDefaultColor(Color.WHITE);
line1.SetStyle(Curve.SHORT_DASH);
line1.SetLineWeight(1);

def barNum = if !IsNaN(BarNumber()) then Barnumber() else Double.NaN;
def numBars = HighestAll(barNum);

def FinalBar= fold c = 0 to numbars while !IsNaN(GetValue(close, -c)) do GetValue(BarNumber(), -c);
def FinalATRDn = fold a = 0 to AbsValue(FinalBar) while !IsNaN(GetValue(close, -a)) do GetValue(line[-1], -a);
def FinalATRUp = fold b = 0 to AbsValue(FinalBar) while !IsNaN(GetValue(close, -b)) do GetValue(line1[-1], -b);

plot DayATRDn = if FinalATRDn>1 then FinalATRDn else Double.NaN;
DayATRDn.SetStyle(Curve.LONG_DASH);
DayATRDn.AssignValueColor(Color.LIME);

plot DayATRUp = if FinalATRUp>1 then FinalATRUp else Double.NaN;
DayATRUp.SetStyle(Curve.LONG_DASH);
DayATRUp.AssignValueColor(Color.LIME);
I've been struggling to create a scan that will identify when price is greater than or equal to the yellow DayATRUP line. Would you be able to assist me with this?
 

Similar threads

Top