0
votes

I'm trying to code horizon graphs in Pine Script. However to do this usefully, I need a data series representing percentage change from the value of the first (i.e. left-most) visible bar on the screen.

I know that the normal way of plotting percentage change is simply to configure the y axis to be percentage rather than price - then the left-most visible bar is always shown as 0.00%. But that doesn't help in a script where I need to do further calculations based on the percentage change since the left-most bar.

I know that I can reference historical data by indexing the series for the source data, and initially I thought I could get the price of the left-most bar via that. But it turns out that this operator works backwards from the right-most bar, in order to use it I would first need to know how many bars to count back to get me from the right-hand edge of the chart to the left-hand edge.

I also know that bar_index gives the index of the current bar, that max_bars_back can set the history buffer length, and that a var declaration will initialize a variable only once which is useful for calculating and storing values relating to the first bar in the history. However these and all the other history-related primitives I've looked at all seem to operate on the history buffer, which is often different to the portion of history displayed on the visible chart. And I can't find any function or variable which gives me any data about the left-hand side of the x-axis, e.g. its bar index or time.

All the example scripts I've found online which plot percentage change rely on a fixed size of look-back buffer, or a size which is configurable via an input field. So none of those achieve what I want either.

Am I missing something or is it simply impossible to do what I want in Pine script?

1

1 Answers

1
votes

There's currently no way in Pine to determine which bar_index is the left-most bar in the visible range. I'm afraid you'll have to resort to using a fixed lookback period.