ATR without taking in 1st candle the previous day's close

surreta

New member
Hi I am trying to draw the range of each candle to quickly see the risk unit plotted. For that I added as an indicator the ATR of a single candle that is the range of that candle that serves me, but when plotting the first candle takes for the calculation of the previous day's close, so the value is very large and subsequent values is difficult to visualize. Do you know how to do so that the first candle only calculates it as first bar close - first bar open? I tried to solve it and I put a coding of another query but it does not work:

Code:
input Reset_Time = 0930;
input Length = 1;
input Consecutive_Closes_Above = 1;
input Consecutive_Closes_Below = 1;

def EOD = SecondsTillTime(1800) == 0;
def GB = close > open;
def RB = close < open;
def Doji = close == open;

def Reset = SecondsFromTime(Reset_Time) == 0;
def ResetBN = if Reset then BarNumber() else ResetBN[1];
def Highest_Lowest_TimeFrame = BarNumber() >= ResetBN + Length;

def fH = if GetValue(Reset, Length - 1) == 1 then Highest(high, Length) else fH[1];
def fL = if GetValue(Reset, Length - 1) == 1 then Lowest(low, Length) else fL[1];

def sH = if Reset then fH[-Length + 1] else Double.NaN;
def sL = if Reset then fL[-Length + 1] else Double.NaN;

def Hf = if EOD then Double.NaN else if !IsNaN(sH) then sH else Hf[1];
def Lf = if EOD then Double.NaN else if !IsNaN(sL) then sL else Lf[1];

def BOC = if Reset then 0 else if !IsNaN(BOC[1]) and BOC[1] == Consecutive_Closes_Above then Double.NaN else if RB or EOD or !Highest_Lowest_TimeFrame then 0 else if close > Hf and !Doji and !RB then BOC[1] + 1 else BOC[1];

def BDC = if Reset then 0 else if !IsNaN(BOC[1]) and BDC[1] == Consecutive_Closes_Below then Double.NaN else if GB or EOD or !Highest_Lowest_TimeFrame then 0 else if close < Lf and !Doji and !GB then BDC[1] + 1 else BDC[1];
      
input averageType = AverageType.simple;
plot zeroline = 0 ;
ZeroLine.SetDefaultColor(GetColor(0));
plot ATR = MovingAverage(averageType, TrueRange(high, open, low), length);
ATR.SetDefaultColor(GetColor(8));


input AtrBands = yes;

plot hATR = Highest(ATR,75);

hATR.SetDefaultColor(GetColor(7));
plot lATR = Lowest(ATR,50);
lATR.SetDefaultColor(GetColor(6));

Here I paste the picture of how that first wrong atr looks like
iOz0e7V.png
 
Last edited by a moderator:
Solution
Hi I am trying to draw the range of each candle to quickly see the risk unit plotted. For that I added as an indicator the ATR of a single candle that is the range of that candle that serves me, but when plotting the first candle takes for the calculation of the previous day's close, so the value is very large and subsequent values is difficult to visualize. Do you know how to do so that the first candle only calculates it as first bar close - first bar open? I tried to solve it and I put a coding of another query but it does not work:

input Reset_Time = 0930;
input Length = 1;
input Consecutive_Closes_Above = 1;
input Consecutive_Closes_Below = 1;

def EOD = SecondsTillTime(1800) == 0;
def GB = close > open;
def RB = close < open...
Hi I am trying to draw the range of each candle to quickly see the risk unit plotted. For that I added as an indicator the ATR of a single candle that is the range of that candle that serves me, but when plotting the first candle takes for the calculation of the previous day's close, so the value is very large and subsequent values is difficult to visualize. Do you know how to do so that the first candle only calculates it as first bar close - first bar open? I tried to solve it and I put a coding of another query but it does not work:

input Reset_Time = 0930;
input Length = 1;
input Consecutive_Closes_Above = 1;
input Consecutive_Closes_Below = 1;

def EOD = SecondsTillTime(1800) == 0;
def GB = close > open;
def RB = close < open;
def Doji = close == open;

def Reset = SecondsFromTime(Reset_Time) == 0;
def ResetBN = if Reset then BarNumber() else ResetBN[1];
def Highest_Lowest_TimeFrame = BarNumber() >= ResetBN + Length;

def fH = if GetValue(Reset, Length - 1) == 1 then Highest(high, Length) else fH[1];
def fL = if GetValue(Reset, Length - 1) == 1 then Lowest(low, Length) else fL[1];

def sH = if Reset then fH[-Length + 1] else Double.NaN;
def sL = if Reset then fL[-Length + 1] else Double.NaN;

def Hf = if EOD then Double.NaN else if !IsNaN(sH) then sH else Hf[1];
def Lf = if EOD then Double.NaN else if !IsNaN(sL) then sL else Lf[1];

def BOC = if Reset then 0 else if !IsNaN(BOC[1]) and BOC[1] == Consecutive_Closes_Above then Double.NaN else if RB or EOD or !Highest_Lowest_TimeFrame then 0 else if close > Hf and !Doji and !RB then BOC[1] + 1 else BOC[1];

def BDC = if Reset then 0 else if !IsNaN(BOC[1]) and BDC[1] == Consecutive_Closes_Below then Double.NaN else if GB or EOD or !Highest_Lowest_TimeFrame then 0 else if close < Lf and !Doji and !GB then BDC[1] + 1 else BDC[1];

input averageType = AverageType.simple;
plot zeroline = 0 ;
ZeroLine.SetDefaultColor(GetColor(0));
plot ATR = MovingAverage(averageType, TrueRange(high, open, low), length);
ATR.SetDefaultColor(GetColor(8));


input AtrBands = yes;

plot hATR = Highest(ATR,75);

hATR.SetDefaultColor(GetColor(7));
plot lATR = Lowest(ATR,50);
lATR.SetDefaultColor(GetColor(6));

Here I paste the picture of how that first wrong atr looks like


this calculates an altered ATR.
..on the first bar of the day, the ATR is (high-low). it doesn't look at previous bar, so ignores day open gaps.
..during the next bars after the first bar of the day, while the bar quantity is less than the length, it calculates an average of those first bars, then the ATR.
..once the day has the same or more bars as the length, a normal ATR is calculated.

can show,
..this altered ATR (cyan)
..a normal ATR (yellow)

as many days start with a larger bar, the first altered ATR value of the day will probably be bigger than most other values.


Code:
# atr_without_first_bar_00

#https://usethinkscript.com/threads/atr-without-taking-in-1st-candle-the-previous-days-close.14512/
#Questions UnAnswered Graveyard 
#UnAnswered ATR without taking in 1st candle the previous day's close
#surreta  Feb 16, 2023

#Hi I am trying to draw the range of each candle to quickly see the risk unit plotted. For that I added as an indicator the ATR of a single candle that is the range of that candle that serves me, but when plotting the first candle takes for the calculation of the previous day's close, so the value is very large and subsequent values is difficult to visualize.
# Do you know how to do so that the first candle only calculates it as first bar close - first bar open? I tried to solve it and I put a coding of another query but it does not work:

declare lower;

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

input Reset_Time = 0930;
input Length = 4;
input Consecutive_Closes_Above = 1;
input Consecutive_Closes_Below = 1;

def EOD = SecondsTillTime(1800) == 0;
def GB = close > open;
def RB = close < open;
def Doji = close == open;

def Reset = SecondsFromTime(Reset_Time) == 0;
def ResetBN = if Reset then BarNumber() else ResetBN[1];
def valid_bn = BarNumber() >= ResetBN + Length;
def daybars = bn - ResetBN + 1;

#-----------------------------

#def fH = if GetValue(Reset, Length - 1) == 1 then Highest(high, Length) else fH[1];
#def fL = if GetValue(Reset, Length - 1) == 1 then Lowest(low, Length) else fL[1];

#def sH = if Reset then fH[-Length + 1] else Double.NaN;
#def sL = if Reset then fL[-Length + 1] else Double.NaN;

#def Hf = if EOD then Double.NaN else if !IsNaN(sH) then sH else Hf[1];
#def Lf = if EOD then Double.NaN else if !IsNaN(sL) then sL else Lf[1];

#def BOC = if Reset then 0 else if !IsNaN(BOC[1]) and BOC[1] == Consecutive_Closes_Above then Double.NaN else if RB or EOD or !valid_bn then 0 else if close > Hf and !Doji and !RB then BOC[1] + 1 else BOC[1];

#def BDC = if Reset then 0 else if !IsNaN(BOC[1]) and BDC[1] == Consecutive_Closes_Below then Double.NaN else if GB or EOD or !valid_bn then 0 else if close < Lf and !Doji and !GB then BDC[1] + 1 else BDC[1];


input averageType = AverageType.simple;
plot zeroline = 0 ;
ZeroLine.SetDefaultColor(GetColor(0));

input show_normal_atr = yes;
def atr1 = MovingAverage(averageType, TrueRange(high, open, low), length);

plot zatr = if show_normal_atr then atr1 else na;
#ATR.SetDefaultColor(GetColor(8));
zATR.SetDefaultColor(color.yellow);

#---------------------

input AtrBands = yes;
plot hATR = if AtrBands then Highest(ATR1,75) else na;;
hATR.SetDefaultColor(GetColor(7));
plot lATR = if AtrBands then Lowest(ATR1,50) else na;
lATR.SetDefaultColor(GetColor(6));

#----------------------------

def tr = TrueRange(high, open, low);

def atr2;
if reset then {
# diff of the current high and the current low
 atr2 = high - low;

} else if valid_bn then {
 atr2 = MovingAverage(averageType, TR, length);

} else {
# loop thru bars after reset bar, add up tr and add (high-low) from first bar , and / x to create an avg,
atr2 = (getvalue(atr2, ( daybars -1) ) +
 (fold i = 0 to length
  with p
#  while !valid_bn
#  while !getvalue(valid_bn, i)
 while !getvalue(reset, i)
  do p + getvalue(tr, i)))
 /daybars;

}

input show_altered_atr = yes;
plot z = if show_altered_atr then atr2 else na;
z.SetDefaultColor(color.cyan);

#--------------------------------

# ref
#atr2 = MovingAverage(averageType, TrueRange(high, open, low), length);

# TR is the greatest of the following:
#  the difference between the current high and the current low
#  the difference between the current high and the previous close
#  the difference between the previous close and the current low


input test1 = no;
addchartbubble(test1, 0,
 tr + "\n" +
 valid_bn + "\n" +
 daybars + "\n" +
 atr2
# + "\n" +
# bodyheight() + "\n" +
# (high-low)
, color.yellow, no);
#
 
Solution

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