ATR from 1 bar ago label


Category:
0
0

Hello,

I am trying to add a simple label to my chart showing the ATR coming into today. In other words the ATR from one bar ago. This is what I have so far but it’s not accepting the code.

input length = 14;
input averageType = AverageType.WILDERS;

plot ATR[1] = MovingAverage(averageType, TrueRange(high, close, low), length);
addLabel(yes,concat(“ATR=”, ATR),color.Yellow);

 

Please let me know what I am missing. Thank you very much.

Marked as spam
Posted by (Questions: 3, Answers: 4)
Asked on January 10, 2023 10:12 am
70 views
0
Private answer

The mistake in your code is marked by the editor:

plot ATR[1] = MovingAverage(averageType, TrueRange(high, close, low), length);

So we know this line of your code contains an error. Unfortunately the editor does not provide any detail. The problem is the index value you have applied in the plot statement, as follows:

plot ATR[1] =....

You cannot apply an index to any variable declaration. You are on the right track, the only detail you missed is exactly WHERE to apply the index value to offset the label to display the value from the previous bar. That location is within the AddLabel() statement, as follows:

plot ATR[1] = MovingAverage(averageType, TrueRange(high, close, low), length);

Here is what your solution looks like once the errors have been corrected:

input length = 14;
input averageType = AverageType.WILDERS;
plot ATR = MovingAverage(averageType, TrueRange(high, close, low), length);
AddLabel(yes, Concat("ATR = ", ATR[1]), Color.YELLOW);

Please note I have corrected some minor typos in your code and inserted spaces between parameters to make the code easier to read.

Marked as spam
Posted by (Questions: 37, Answers: 4086)
Answered on January 10, 2023 10:21 am
0
It worked. Thank you. Could the script be changed to show the Daily ATR on all time frames? Basically I want the label to show me the daily ATR from 1 bar ago on all time frames. So the value stays the same, it never changes.
( at January 11, 2023 9:18 am)
0
That solution has already been posted here: https://www.hahn-tech.com/ans/modify-chart-label-wording-based-on-daily-atr/ However you will need to adjust that code by adding the [1] index to display the previous day's ATR value instead of the current day.
( at January 11, 2023 9:36 am)