In this post, we are going back to basics. Turn away now if you are anything but a complete beginner!
In pine-script, there are several basics we need to understand in order for our skills to grow and flourish. In fact, some of these basics are so fundamental, that even if you don’t plan on writing full strategies yourself, having a grasp on them can help you to read or tweak some of the many open source strategies and indicators already available in the community. This post cannot cover all of the fundamentals of pine script (it would be too long) but we can at least start to make a dent in them. So let’s start right at the beginning!
Simply put, Indexing is a way of referencing/reading a value stored at a particular place in a list. This is especially handy when we don’t know what is on the list as it allows us to refer to items by a number.
Visual or kinesthetic learners might feel that description was a bit vague. Therefore, a good old-fashioned example will probably be the quickest way to cement that concept. Imagine you have a shopping list which reads:
Then I ask you “what is the third item on the list?” If you answered “Sausages” then we are indexing! I provided a number which represented a position in the list and you gave me the item that was sitting in that position.
Indexing in Pine-Script
In pine script, every “variable” is actually a long list of stored values. After each new bar appears, the whole script is run again and a new value is added to each list. That means the “built-in” variables
close are also just really long lists.
If we just reference
close in our code, the most recent value in the list will be returned. So how do we access other items on the list? We Index! To index the list, we add square brackets
 at the end of the list name and give it a number like
close. Each number represents a different position in the list starting from zero. Zero is the current value and thus the following two statements return the same value:
To see how index numbers relate to values at different points in time, take a look at the following image:
Our imaginary price chart shows the close price on the hourly time frame. Below the chart, we have index positions which are pointing to the hourly close prices. Notice that
0 is the most recent value and
11 is the oldest value on the chart?
So if you wrote
close in a script containing the imaginary chart data and ran it on the bar “NOW“, the returned value would be $10.
It is important to stress that the who script is ran again every bar and the list grows bigger. Since the index always starts from the most recent bar, it means that
close will return a different value after another new bar is loaded. If we ran our script again at 1 pm (which is not yet on our chart),
close would return $30. This is because $30 would now be the 5th most recent value.
Go ahead and open the pine script editor. If you don’t know how to do this, check out the Tradingview: First Script post for screenshots and step by step instructions.
Then just paste the code following code into the edit, tweak the index numbers and add it to your chart.
plot(close, color=blue, linewidth=2)
plot(close, color=orange, linewidth=2)
To visualize, here is a chart with some two examples. The first subplot uses the settings in the code above, the second uses an index of 50.
You will notice that as the index increases the two lines follow the same path, it is just that the orange line is 5 or 50 bars behind the blue line. As such, it also reinforces the idea that the whole script is run and the lists are updated on every new bar. We can see that on each run of the script, the
plot()function for the orange line is taking the close value from 5 or 50 bars ago and plotting it now.
Find This Post Useful?
If this post saved you time and effort, please consider support the site! There are many ways to support us and some won’t even cost you a penny.
Backtest Rookies is a registered with Brave publisher!
Brave users can drop us a tip.
Alternatively, support us by switching to Brave using this referral link and we will receive some BAT!
Enjoying the content and thinking of subscribing to Tradingview? Support this site by clicking the referral link before you sign up!