How do I plot a point 5 bars before current bar


Category:
0
0

def lastbar = IsNaN(close[-5]) and !IsNaN(close[-5+1]);

plot N1_dot = lastbar[-5];
N1_dot.setDefaultColor (CreateColor(100, 250, 250));
N1_dot.SetLineWeight(2);
N1_dot.SetPaintingStrategy(PaintingStrategy.points);

################################################################################

Can anyone help me correct my mistakes in the script above?  Thanks in advanced.

Attachments:
Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on June 7, 2021 9:26 am
141 views
0
Private answer

I will not try to fix the code you have presented as the example because it does not perform the steps in the way I would do them.

I expect that for a majority of viewers coming across this post, they would prefer the point to be attached to either the high or low of the target bar. So my solution has been adapted to fit the use case that will serve the broadest possible audience.

Here is my interpretation of your request. "Plot a single point at the high of the bar that is 5 bars back from the current bar on the chart"

In your screenshot I see that you have created a boolean variable that plots values of 1 or zero. The reason those appear at the very bottom of your chart is due to a chart setting named "Fit studies". This setting forces the price range to automatically adjust to display all the plots of the study. This usually interferes with the way the price candles are displayed so I always turn that setting off. We can avoid this issue by making sure the values we plot are attached to one of the four data points of each candle: Open, High, Low or Close. Then for any bar we do not want the plot to appear we assign it a value of Double.NaN.

Here is my solution. I have included a user input to allow the user to adjust the number of bars ago in which the dot will be placed. I have also included a second user input to allow the user to adjust the data point used to anchor the plot to the candle.

input barsAgoTarget = 5;
input priceTarget = high;
def lastBar = !IsNaN(close) and IsNaN(close[-1]);
plot oneDot = if lastBar[-barsAgoTarget] then priceTarget else Double.NaN;
oneDot.SetPaintingStrategy(PaintingStrategy.POINTS);
oneDot.SetDefaultColor(Color.CYAN);

Marked as spam
Posted by (Questions: 37, Answers: 4090)
Answered on June 7, 2021 1:16 pm