How to paint lines in the future in PineScript?

50 views Asked by At

I am trying to draw on times beyond the time of the current bar. I read Converting a bar_index in the future to a date in Pinescript BUT I am having issues understanding the code in the answer.

Problem statement: Given two bar indices as input, one for a bar in the past and one for a bar in the future, I would like to paint vertical lines in futures dates past the last bar index. The time locations of this bar should be those to the future times where the distance between the past and the future bar is mathces, doubled, and tripled.

Example: Please refer to the following screenshot: Example input

The input bar indices are such that the first red square represents the width of the study window. If I replicate the red square again, that would represent a forecast lenght of 1x the input window size. My current script is as follows:

//@version=5
indicator("Drawing in the future!", shorttitle="FutDraw", overlay=true, precision=0, max_bars_back=5000)

groupInput         = "Input parameters"
startBarIdx        = input.int(0, "Start bar index", group = groupInput)
endBarIdx          = input.int(0, "End bar index", group = groupInput)
sourceInput        = input.source(close, "Price source", group = groupInput)

plotchar(bar_index, "Bar Index", "", location=location.top)

lengthInput = endBarIdx - startBarIdx + 1

if bar_index == endBarIdx
    // Plot.
    line.new(chart.point.from_index(endBarIdx,   high), chart.point.from_index(endBarIdx + lengthInput,   high))

However, the blue lines at the bottom of the previous Figure show that the script incorrectly paints in the future.

Current research: Given the limitations of using bar_index when drawing, I am moving away from using the bar index and using the bar time. Reading Converting a bar_index in the future to a date in Pinescript I noticed the author extracts information from the bar indices and converts the input into time. In the author's solution, we can find this:

b = bar_index
bNext = bar_index + 20
ms = timeframe.in_seconds()*1000

So here is my current script:

//@version=5
indicator("Drawing in the future!", shorttitle="FutDraw", overlay=true, precision=0, max_bars_back=5000)

groupInput         = "Input parameters"
startBarIdx        = input.int(0, "Start bar index", group = groupInput)
endBarIdx          = input.int(0, "End bar index", group = groupInput)
sourceInput        = input.source(close, "Price source", group = groupInput)

plotchar(bar_index, "Bar Index", "", location=location.top)

lengthInput = endBarIdx - startBarIdx + 1
forecastLength = lengthInput*1

// Drawing using bar index.
if bar_index == endBarIdx
    line.new(chart.point.from_index(endBarIdx + lengthInput, high), chart.point.from_index(endBarIdx + lengthInput,   high))

// Drawing using bar time.
if bar_index == endBarIdx
    timeOffset = timeframe.in_seconds() * 1000  // Converts to milliseconds to add to the value of time.
    finalTime = time + (timeOffset  * forecastLength)
    line.new(time, high, finalTime, high, xloc= xloc.bar_time)

However, both the lines drawn using the bar's index, as well as the lines drawn using the bar's time, are both incorrect: Figure current script

The line drawn using the bar index goes beyond the red square, and the line drawn using the bar time falls short. Furthermore, using the bar's index is limited to paint no further than 500 bars into the future.

Any hint as to how to properly define the time component of the line to correctly specify the length of the square? I.e. how can I specify the time component of a plot correctly.

0

There are 0 answers