Zathrus Writer
Zathrus Writer

Reputation: 4331

Line over 500 bars into past error in Pine Script indicator

I'm currently developing a trendlines-related indicator. Due to limitations of Pine's line length into the past when using xloc.bar_index, I started using xloc.bar_time which worked well until I got an error about xloc.bar_index lines being over 500 bars into the future.

The code is fairly complex but the bit that's causing this issue isn't. Let me explain what it does and where it is.

The suspect is my f_move_line_off_chart( ln ) function, which switches a line's xloc to bar_index, while simultaneously changing the x1 and x2 parameters. I'm using this function because - unfortunately - Pine doesn't allow creating lines into the future using xloc.bar_time. There is nowhere else in code where I use xloc.bar_index except for a one other single place, where it's never used with default indicator settings.

The goal is to change the line to use xloc.bar_index in order to "move" it off the chart and onto the right side, thus limiting the amount of trendlines clutter. However, this don't seem to work correctly, as with default settings, using replay tool, on a 30m Binance BTCUSDTPERP chart on 23. oct 2019 at 00:00, you can see that when you move just 1 bar forward, it generates the strange error. But I don't know how is that possible, as that line of code moves the line to the very right side of chart with a 10 bars width!

Prior to this moment on chart, everything works as intended (apart from getting a few Study server errors, for which TV Support has a ticket). I tried to set X1 and X2 manually prior to changing xloc to bar_index in the abovementioned function to no avail.

Any help would be appreciated.

Here's the indicator (it's too long to fit in here): https://pastebin.com/vGkv16an

Upvotes: 0

Views: 1893

Answers (1)

elod008
elod008

Reputation: 1362

My debugging results:
the source of the problem seems to be on line 320:

// check if we should make the previous trendline off-chart
if untested_lines_on_chart > 0 and array.size( untested ) > untested_lines_on_chart
    line2change = array.get( untested, untested_lines_on_chart )
    f_move_line_off_chart( line2change )

Your code seems to be valid at every point to me.
Unfortunately I was not able to fix it as it is. It seems to me - I may be wrong - that the conversion of xloc.bar_index and time is what pine script don't want to do.
I also tried to apply the x1,x2 changes before xloc to override the numbers at first, but it did not bring any change.
I had a similar issue concerning linefills where my linefill just became "corrupt" after some basic operations (ticket open) so that I couldn't modify it anymore just read its value at most. That's my guess in your case too.

Here is what worked for me and may be an ugly workaround for you:

f_move_line_off_chart( ln, lineArray ) =>
    line.new(bar_index + i_text_offset - 1, line.get_y1(ln), bar_index + i_text_offset - 1 + i_right_line_width, line.get_y2(ln))  
    // then remove it from the array and delete it then readd it to your array
    ...

I know it is a terrible solution but might be the only one that does the job. Or you need to rethink your structure and add lines from a matrix if necessary based on some price levels. I wish you good luck!

Upvotes: 1

Related Questions