Tradingview: Understanding lookahead, historical and realtime data

As good as the pine script documentation is, there are still those times when things get lost in translation. Other times documentation has a specific target audience in mind. I decided to write this post after spending some time investigating the differences between “realtime” and historical data and how each type can change the calculations an indicator produces. This is especially true when you start adding the lookahead keyword. More on that later…

Note: If you are just getting started with pine script, I have a more gentle introduction here: Trading View: First Script

Historical vs Realtime Data

Realtime data should not be confused with realtime data provided by stock exchanges. In pine script realtime data is any data that arrived whilst a candle is being built (even if the data itself is delayed). Conversely, historical data refers to any candle closed before adding an indicator to the chart. Sounds logical right? Well be aware of a couple of things:

  1. Calculations made on realtime data can and will have different results to historical bars. This may sound obvious but you will see why this can result in undesired effects in the examples below.
  2. Realtime data does not become historical data once the candle is complete. In other words it is not recalculated in the same way historical data is first calculated when you add the indicator. Again, I hope the examples below will make this clear.

Lets move on with a practical example and create an indicator.

The Indicator

The indicator we will develop shall overlay daily high and low values over an intra-day chart. This might be useful if you wanted to look for breakouts above/below the high or low of the day before.

Example 1 – Vanilla

The code below is the simplest example in the post. If like me, you may choose to build the indicator this way on your first attempt. The indicator simply gathers high and low values for the same instrument using the daily timeframe. It then plots those values over the current timeframe.

As usual, copy and paste the code above directly into Tradingview’s pine script editor and add the indicator to your chart.

Result 1

Example 1 Results

In the image above, the historical data is calculated and plots as expected. We have one nice straight line from the start to the end of each day. However, look what happens when the realtime data arrives. The bars shift up in the middle of the day!

After looking more closely at the lines generated after the indicator was added (realtime data), you may notice that the values have shifted to the high / low values for the current day. I hope this example highlights points 1 and 2 above.

Lookahead

Lookahead allows an indicator to look at future values in the for calculations. If we set “lookahead_on“, the indicator will look ahead to find high and low for the rest of the day. It will then use them for plotting each intra-day bar that day, no matter whether we are before or after the high or low for that day. The example below shows this in more detail.

Example 2 – lookahead on

lookahead is on in the example below. Note that if you do not give a keyword argument, the default value is off (barmerge.lookahead_off).

Result 2

Resulting chart for example 2

The example above had just been added to the chart. For historical data the high and the low for the day are plotted before they actually happen intra-day! You should never use this type of indicator for backtesting. For example, you could skew the results by entering a short position at the high of the day then closing before the end of the day. You would have great results but they would not be applicable in the real world.

For realtime data, the calculation is different when price moves beyond the current high and low for the day. Stating the obvious, this is because we cannot look into the future for bars that are yet to form and they could yet go higher or lower.

Below is an example of the chart using realtime data where price moves beyond the known historical high and low data.

Image showing price moving beyond the low value of the lookahead

Example 3 – lookahead on & index.

The last example combines looking ahead with an indexed data. It results in an indicator which works as expected. Perhaps counter-intuitively, when we look ahead on indexed data, we are looking ahead on the data for the day before. (which is back in the past again!)

Result 3

Chart showing results from example 3

Here we can see that the high and low lines show the results from the previous days’ range. Additionally realtime data provides the same value.

Call to Arms!

If you like this article and are thinking to become a Tradingview subscriber, you can support the site by clicking on the affiliate link below before signing up!

https://tradingview.go2cloud.org/SHpB