0

I am wondering if there is a way to get the number of bar of the timeframe that my chart is in it from the beginning.

My goal is to use the final bar_index as a variable in my code, but the issue is that the bar index will increase by time. so I was wonderingwhat would be the solution here.

To explain more I am providing the example here. Lets say I am in 2H charts and there are in total 20K bar_index there, if I know the 20K for the beginning, I can then provide the condition that whatever total bars that my chart will have should be divided with n and then just apply my functions on a range of the results. but I dont know how I can know the total number of bars in the chart from the beginning...

I guess my question can also be ask as follow: can we somehow at the beginning of the code (not at the end via barstate.islast) know what is the total number of bars in the chart based on the timeframe we are in?

sey eeet
  • 229
  • 2
  • 8

1 Answers1

0

No, this is not possible to know. Your script will be executed on each bar starting from the first very bar. All the built-in variables such as bar_index will also be calculated on each bar. So, you cannot get that information until you wait till the last bar.

You have two options:

  1. Use a for loop to do your calculations in a range (not recommended for performance reasons)
  2. Enter the range yourself (bar index or some time) via the user settings
vitruvius
  • 15,740
  • 3
  • 16
  • 26