Tradingview Understanding lookahead, historical and realtime data - Backtest Rookies PDF

Title Tradingview Understanding lookahead, historical and realtime data - Backtest Rookies
Author Jason Lee Hong
Course Community Project
Institution Universiti Tunku Abdul Rahman
Pages 4
File Size 346.9 KB
File Type PDF
Total Downloads 95
Total Views 209

Summary

Nothing else...


Description

8/29/2019

Tradingview: Understanding lookahead, historical and realtime data - Backtest Rookies

Tradingview: Understanding lookahead, historical and realtime data

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 specic 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rst 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 https://backtest-rookies.com/2017/06/23/tradingview-understanding-lookahead-historical-realtime-data/

1/7

8/29/2019

Tradingview: Understanding lookahead, historical and realtime data - Backtest Rookies

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 rst 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. //@version=3 study("Barmerge Tests", overlay=true) daily_high = security(tickerid, "D", high) daily_low = security(tickerid, "D", low) plot(daily_high, style=cross, color=green) plot(daily_low, style=cross, color=red)

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

Result 1

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

https://backtest-rookies.com/2017/06/23/tradingview-understanding-lookahead-historical-realtime-data/

2/7

8/29/2019

Tradingview: Understanding lookahead, historical and realtime data - Backtest Rookies

Lookahead allows an indicator to look at future values in the for calculations. If we set “lookahead_on“, the indicator will look ahead to nd high and low for the rest of the day. It will then use them for plotting each intraday 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). //@version=3 study("Barmerge Tests", overlay=true) daily_high = security(tickerid, "D", high, lookahead=barmerge.lookahead_on) daily_low = security(tickerid, "D", low, lookahead=barmerge.lookahead_on) plot(daily_high, style=cross, color=green) plot(daily_low, style=cross, color=red)

Result 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.

https://backtest-rookies.com/2017/06/23/tradingview-understanding-lookahead-historical-realtime-data/

3/7

8/29/2019

Tradingview: Understanding lookahead, historical and realtime data - Backtest Rookies

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!) //@version=3 study("Barmerge Tests", overlay=true) daily_high = security(tickerid, "D", high[1], lookahead=barmerge.lookahead_on) daily_low = security(tickerid, "D", low[1], lookahead=barmerge.lookahead_on) plot(daily_high, style=cross, color=green) plot(daily_low, style=cross, color=red)

Result 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.

https://backtest-rookies.com/2017/06/23/tradingview-understanding-lookahead-historical-realtime-data/

4/7...


Similar Free PDFs