rlohmeyer
Active member
The recent code for ATR plots on a chart also showing the previous days close to show the relationship of the present move to the actual move. The chart shows the code in action on the chart.
Code:
#Basic Definitions
def c_ = close;
def c = if IsNaN(c_[-1]) then c_ else c[1];
#Atr Plots###################
input agg = AggregationPeriod.DAY;
input AtrLngth = 21;
def tr = TrueRange (high(period = agg), close(period = agg), low(period = agg));
def Atr = Average(TrueRange(high, close, low), 21);
def AtrPct = (atr/close)*100;
input ShowAtr = yes;
#ATR Variables
def ATRH = if ShowAtr and !IsNaN(c_[1] + Atr) and IsNaN(c_[1] + Atr[-1]) then c_[1] + Atr else ATRH[1];
plot ATR_High = if IsNaN(c_[1] + Atr[-400]) then ATRH[-400] else Double.NaN;
ATR_High .SetDefaultColor(Color.YELLOW);
ATR_High .SetPaintingStrategy(PaintingStrategy.DASHES);
ATR_High .SetLineWeight(2);
def ATRL = if ShowAtr and !IsNaN(c_[1] - Atr) and IsNaN(c_[1] - Atr[-1]) then c_[1] - Atr else ATRL[1];
plot ATR_Low = if IsNaN(c_[1] - Atr[-400]) then ATRL[-400] else Double.NaN;
ATR_Low .SetDefaultColor(Color.YELLOW);
ATR_Low .SetPaintingStrategy(PaintingStrategy.DASHES);
ATR_Low .SetLineWeight(2);
# --- Previous Close Line (Mirrored from ATR Logic) ---
input pc_agg = AggregationPeriod.DAY; # Set to DAY for 2/12 vs 2/13 logic
def pc_close = close(period = pc_agg);
# This captures the close of 2/12 by looking back 1 bar from the chart's end
def PC_Stored = if !IsNaN(pc_close) and IsNaN(pc_close[-1])
then pc_close[1]
else PC_Stored[1];
# This plots the line 400 bars back and through the expansion area
plot PC_Line = if IsNaN(pc_close[-400])
then PC_Stored[-400]
else Double.NaN;
PC_Line.SetDefaultColor(Color.orange);
PC_Line.SetPaintingStrategy(PaintingStrategy.DASHES);
PC_Line.SetLineWeight(2);
#PC_Line.HideBubble();
################Analysis Labels###################
input ShowLabel = yes;
AddLabel(if ShowLabel then yes else no,"ATR" + AtrLngth + "|$" + Round(Atr,2), Color.YELLOW);
Last edited by a moderator: