Compute ATR based on number of available bars for IPO stocks


Category:
0
0

Hi, below is a basic code meant for the daily chart to calculate a 14 period ATR excluding the last/current bar if there are more than 14 bars on the daily chart and averages the ATR for the total number of candles on the daily chart if there are less than 14 bars. The code runs fine when simply calculating the ATR for daily charts that have clearly more than 14 bars but for IPO charts like FROG, SNOW and NNOX it either returns an incorrect ATR value (NNOX) or just N/A (FROG, SNOW). I am stumped right now and would appreciate any help. TIA.
def bars = if IsNaN(close[-1]) then 0 else 1;
def range = if IsNaN(close[-1]) then 0 else high - low;
def sumrange = TotalSum(range);
def totalbars = totalsum(bars);
def ATR = round(Average(TrueRange(high, close, low), 14)[1], 2);
def scan = if totalbars < 14 then round(sumrange/totalbars, 2) else atr;
AddLabel(yes, scan, Color.LIGHT_GRAY);

Marked as spam
Posted by (Questions: 4, Answers: 2)
Asked on September 18, 2020 6:42 pm
97 views
0
Private answer

I'm not sure there is a solution to this. When you include the computation for average true range as a conditional value based on number of bars, the platform itself is recognizing that 14 bars are required to compute the study. Only way around that is to remove the 14 period average true range entirely from your code.

Find another way to measure that value.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on September 19, 2020 12:09 pm