banner



how to find x bar

Practically every TradingView script refers to previous price bars in its calculations. But indicators and strategies can also work with the bar numbers themselves. Let's see how.

IN THIS ARTICLE:

# Get the bar number in TradingView Pine: bar_index

Even though it doesn't show on TradingView charts, each bar has a particular number. Scripts can use that number to place drawings and to see how many bars the chart got. So how do indicators and strategies access that value?

TradingView's bar_index variable returns the current bar number (TradingView, n.d. a). It reports that value as a whole (integer) number.

The value of bar_index is zero-based (TradingView, n.d. a). That means it returns 0 for bar number 1, 1 for bar number 2, and so on. It also means that the total number of bars on the chart is bar_index + 1.

If we want to know which number a previous bar had, we can combine bar_index with the history referencing operator ([]). For example, bar_index[10] returns the bar number from 10 bars ago. Or we simply subtract from the current bar number: bar_index - 10 gives the same value.

The bar_index variable is relatively new to TradingView Pine. If your script uses Pine version 3 or earlier, then you access bar numbers with the n variable (TradingView, n.d. b).

# Ways to use bar numbers in TradingView Pine

Indicators and strategies can use the bar_index variable in a couple of ways:

  • We often use bar_index when we place drawings on the chart. Trend lines and labels , for instance, by default use bar numbers for their x-axis (time) coordinates.
  • When we store the value of bar_index in a variable when some condition happens, we can later compare the then-current bar number with the stored value. That tells us how many bars happened since.
  • Sometimes bar_index is used to limit the strategy's backtest period. For example, we might only want to generate new trades when bar_index is above 5000. This approach isn't that good, however. Not every chart has the same amount of bars, and different time frames also have a different amount of bars. A better option is to limit a backtest with a start and end date.
  • On the last bar, bar_index + 1 says how many price bars the chart has. (With the barstate.islast variable we can check if the script processes that last bar.)

# Example: count bars and position a label with bar_index

Let's see how we can use the bar_index variable in a TradingView script. The indicator below uses that variable to count the chart's bars. But we also position a label with bar_index.

The indicator's complete code is:

                          //@version=4              study("Example of bar_index variable"              ,              overlay=              true)              // Make a new label once              var              label              myLabel              =              label.new(x=              bar_index              ,              y=              high              +              tr,      textcolor=              color.white              ,              color=              color.blue)              // On the last bar, show the chart's bar count              if              (barstate.islast)              // Set the label content              label.set_text(id=myLabel,              text=              "Bars on\nthe chart:\n"              +              tostring(bar_index              +              1))              // Update the label's location              label.set_x(id=myLabel,              x=              bar_index)              label.set_y(id=myLabel,              y=              high              +              tr)                      

We start with the study() function to configure the indicator's settings.

Then we create a single label:

                          // Make a new label once              var              label              myLabel              =              label.new(x=              bar_index              ,              y=              high              +              tr,      textcolor=              color.white              ,              color=              color.blue)                      

Here we make a label with TradingView's label.new() function. We position that drawing on the current bar number (bar_index) and the high plus true range (high + tr). Its text colour is white (color.white) and the label's colour is blue (color.blue).

We put the label reference in the myLabel variable for use later. Because a var statement only runs once, this piece of code makes just one label for the entire chart.

Next we update the label's location:

                          // On the last bar, show the chart's bar count              if              (barstate.islast)              // Set the label content              label.set_text(id=myLabel,              text=              "Bars on\nthe chart:\n"              +              tostring(bar_index              +              1))              // Update the label's location              label.set_x(id=myLabel,              x=              bar_index)              label.set_y(id=myLabel,              y=              high              +              tr)                      

This if statement checks barstate.islast. That variable is true when the script processes the last bar of the chart. On other bars its value is false.

On that last bar we first set the label's text. So we call the label.set_text() function. First we identify our label with the myLabel variable. Then we set the label's text to a string ("Bars on\nthe chart:\n") and the number of bars on the chart (bar_index + 1). We convert that latter number to text with the tostring() function.

Next we update the label's position to the last bar. The label.set_x() function sets its time coordinate to the current bar number (bar_index). And the label.set_y() function puts the label above the current bar's high (high + tr).

This is how the indicator and its label look on the chart:

Display value of TradingView's `bar_index` variable on the chart

Here we see that the EuroStoxx 50 CFD chart has 6,885 bars. We got that information from the bar_index variable. And that variable also helped to set the label's time coordinate.

# Summary

TradingView scripts access the bar numbers with bar_index. That variable starts counting at 0 for the first bar, and then increases with one for each additional price bar. On the last bar of the chart bar_index + 1 tells how many bars the chart has.

We often use bar_index to position drawings (like trend lines and labels) on the chart. It can also say how many bars happened between events. And it of course tells how much historical data our chart has.

Published .

« All TradingView price and bar data articles

how to find x bar

Source: https://kodify.net/tradingview/bar-data/bar-number/

Posted by: zookcolove.blogspot.com

0 Response to "how to find x bar"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel