Extend ATR lines of last bar to the right


Category:
0
0

I have the foundation of the script I think can work, but need help.

As written, this script creates a red and a green line above the “active” candle, based on the ATR of the previous 14 candles (starting with the last completed candle).

My question: Can it be modified to show the pair of red and green lines for the only above/below the current candle, and extend them horizontally to end of day?

Just one pair of lines at any given time, still based on the ATR of the previous 14 completed candles. My ORB study paints all the way to the right. But cannot figure out how to make the lines then “repaint” as each new candle starts and/or hide the other lines. I am stuck.

 

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

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

plot hatr = close[1] + ATR;
plot latr = close[1] – ATR;

hatr.SetDefaultColor(color = Color.DARK_RED);
hatr.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

latr.SetDefaultColor(color = Color.DARK_GREEN);
latr.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

RESOLVED
Marked as spam
Posted by (Questions: 4, Answers: 6)
Asked on November 17, 2020 7:32 pm
125 views
1
Private answer

Your use of the term "repaint" is probably getting you stuck in the weeds. In the world of chart studies, the term "repaint" is used to describe a signal that changes as new bars are added to the chart. Future data, changes the signals that were previously displayed on the chart.

What you actually require here is to "extend" the last values all the way to the right. So I have updated the title of your question to reflect that. The new question title will make it very easy for anyone else seeking this solution to quickly find your post using the search function.

The way your code is designed, the very last value of your plots are shifted one bar to the right. Those lines are continually recalculated each time the price changes on the last bar of the chart. I thought I would point that out for your own benefit as well as the rest of our viewers who will come across this solution. To summarize, the lines are extended, but they are not static.

The method required to extend these plots to the right is by using recursive variables. When I use recursive variables I always use the "rec" definition in place of the "def". This makes the code easier to read and helps me identify recursive variables immediately while reviewing the code.

Here is the updated version that plots only the last value of the lines and extends them into the right-hand expansion area of the chart:

input length = 14;
input averageType = AverageType.WILDERS;
def atr = MovingAverage(averageType, TrueRange(high[1], close[1], low[1]), length);
def lastBarAndBeyond = !IsNaN(atr) and IsNaN(atr[-1]);
rec lastBarHigh = if lastBarAndBeyond then close[1] + atr else lastBarHigh[1];
rec lastBarLow = if lastBarAndBeyond then close[1] – atr else lastBarLow[1];
plot hatr = lastBarHigh;
plot latr = lastBarLow;
hatr.SetDefaultColor(color = Color.DARK_RED);
hatr.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
latr.SetDefaultColor(color = Color.DARK_GREEN);
latr.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

Screenshot is included below to show what this looks like.

Edit: After posting my solution the request was clarified to plot the last static value of these two lines rather than the one computed from the active bar. The solution is pretty simple and just requires adjustments to the following two lines from my solution above:

rec lastBarHigh = if lastBarAndBeyond[-1] then close[1] + atr else lastBarHigh[1];
rec lastBarLow = if lastBarAndBeyond[-1] then close[1] – atr else lastBarLow[1];

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4086)
Answered on November 18, 2020 9:34 am
0
This is amazing. Thank you!
( at November 18, 2020 10:25 am)
0
If I might ask a follow-up (cuz this is awesome) - Would it be possible to "start" the lines above and below the last completed candle? It would likely make the lines static, and the "active" candle would move up and down between the two.
( at November 18, 2020 10:44 am)
0
Yes it should be possible but for this post I have already exceeded the amount of time I allocate for providing free solutions in the Q&A forum.
( at November 18, 2020 11:19 am)
0
One idea did occur to me and it seems to work. Very quick fix. I have updated my answer to provide the adjustment required to make that happen.
( at November 18, 2020 11:26 am)