Bar Index โ Time

Utilizes runtime memory to store the ๐๐๐๐ and ๐๐๐๐_๐๐๐๐๐ values of every bar on the chart (and optional future bars), with the ability of storing additional custom values for every chart bar.
โโPREFACE
This library aims to tackle some problems that pine coders (from beginners to advanced) often come across, such as:
This library can solve the above problems (and many more) with the addition of a few lines of code, rather than requiring the coder to refactor their script to accommodate the limitations.
- I'm trying to draw an object with a ๐๐๐_๐๐๐๐๐ก that is more than 10,000 bars into the past, but this causes my script to fail. How can I convert the ๐๐๐_๐๐๐๐๐ก to a UNIX time so that I can draw visuals using xloc.bar_time?
- I have a diagonal line drawing and I want to get the "y" value at a specific time, but line.get_price() only accepts a bar index value. How can I convert the timestamp into a bar index value so that I can still use this function?
- I want to get a previous ๐๐๐๐ value that occurred at a specific timestamp. How can I convert the timestamp into a historical offset so that I can use ๐๐๐๐[๐๐๐๐๐๐]?
- I want to reference a very old value for a variable. How can I access a previous value that is older than the maximum historical buffer size of ๐๐๐๐๐๐๐๐[5000]?
โโOVERVIEW
The core functionality provided is conversion between xloc.bar_index and xloc.bar_time values.
The main component of the library is the ๐ฒ๐๐๐๐๐ณ๐๐๐ object, created via the ๐๐๐๐๐๐๐๐ฒ๐๐๐๐๐ณ๐๐๐() function which basically stores the ๐๐๐๐ and ๐๐๐๐_๐๐๐๐๐ of every bar on the chart, and there are 3 more overloads to this function that allow collecting and storing additional data. Once a ๐ฒ๐๐๐๐๐ณ๐๐๐ object is created, use any of the exported methods:
- Methods to convert a UNIX timestamp into a bar index or bar offset:
๐๐๐๐๐๐๐๐๐๐๐๐ฑ๐๐๐ธ๐๐๐๐ก(), ๐๐๐๐ฝ๐๐๐๐๐๐พ๐๐ฑ๐๐๐๐ฑ๐๐๐() - Methods to retrieve the stored data for a bar index:
๐๐๐๐๐ฐ๐๐ฑ๐๐๐ธ๐๐๐๐ก(), ๐๐๐๐๐ฒ๐๐๐๐๐ฐ๐๐ฑ๐๐๐ธ๐๐๐๐ก(), ๐๐๐๐๐๐ฐ๐๐ฑ๐๐๐ธ๐๐๐๐ก(), ๐๐๐๐ฐ๐๐๐ ๐๐๐๐๐๐๐๐๐ฐ๐๐ฑ๐๐๐ธ๐๐๐๐ก() - Methods to retrieve the stored data at a number of bars back (i.e., historical offset):
๐๐๐๐(), ๐๐๐๐๐ฒ๐๐๐๐(), ๐๐๐๐๐() - Methods to retrieve all the data points from the earliest bar (or latest bar) stored in memory, which can be useful for debugging purposes:
๐๐๐๐ด๐๐๐๐๐๐๐๐๐๐๐๐๐๐ณ๐๐๐(), ๐๐๐๐ป๐๐๐๐๐๐๐๐๐๐๐๐ณ๐๐๐()
โโUSAGE
Step 1
Import the library. Replace <version> with the latest available version number for this library.
Step 2
Create a ๐ฒ๐๐๐๐๐ณ๐๐๐ object to collect data on every bar. Do not declare as `var` or `varip`.
Step 3
Call any method(s) on the ๐ฒ๐๐๐๐๐ณ๐๐๐ object. Do not modify its fields directly.
โโEXAMPLES
โข Collect Future Times
The overloaded ๐๐๐๐๐๐๐๐ฒ๐๐๐๐๐ณ๐๐๐() functions that accept a ๐๐๐๐๐ต๐๐๐ ๐๐๐ argument can additionally store time values for up to 500 bars into the future.
โข Collect Custom Data
The overloaded ๐๐๐๐๐๐๐๐ฒ๐๐๐๐๐ณ๐๐๐() functions that accept a ๐๐๐๐๐๐๐๐๐ argument can additionally store custom user-specified values for every bar on the chart.
โข xloc.bar_index โ xloc.bar_time
The ๐๐๐๐ value (or ๐๐๐๐_๐๐๐๐๐ value) can be retrieved for any bar index that is stored in memory by the ๐ฒ๐๐๐๐๐ณ๐๐๐ object.
โข xloc.bar_time โ xloc.bar_index
Use ๐๐๐๐๐๐๐๐๐๐๐๐ฑ๐๐๐ธ๐๐๐๐ก() to find the bar that a timestamp belongs to.
If the timestamp falls in between the close of one bar and the open of the next bar,
the ๐๐๐๐ parameter can be used to determine which bar to choose:
๐๐๐๐.๐ป๐ด๐ต๐ - prefer to choose the leftmost bar (typically used for closing times)
๐๐๐๐.๐๐ธ๐ถ๐ท๐ - prefer to choose the rightmost bar (typically used for opening times)
๐๐๐๐.๐ณ๐ด๐ต๐ฐ๐๐ป๐ (or ๐๐) - copies the same behavior as xloc.bar_time uses for drawing objects
โข Get Price of Line at Timestamp
The pine script built-in function line.get_price() requires working with bar index values. To get the price of a line in terms of a timestamp, convert the timestamp into a bar index or offset.
โโRUNTIME ERROR MESSAGES
This library's functions will generate a custom runtime error message in the following cases:
- ๐๐๐๐๐๐๐๐ฒ๐๐๐๐๐ณ๐๐๐() is not called consecutively, or is called more than once on a single bar
- Invalid ๐๐๐๐๐ต๐๐๐ ๐๐๐ argument in the ๐๐๐๐๐๐๐๐ฒ๐๐๐๐๐ณ๐๐๐() function
- Invalid ๐๐๐๐๐๐๐๐๐ argument in the ๐๐๐๐๐๐๐๐ฒ๐๐๐๐๐ณ๐๐๐() function
- Invalid ๐๐๐๐๐๐ argument in any of the functions that accept a number of bars back
Any other runtime errors are due to incorrect usage of the library.
โโNOTES
โข Function Descriptions
The library source code uses Markdown for the exported functions. Hover over a function/method call in the Pine Editor to display formatted, detailed information about the function/method.
โข Historical vs. Realtime Behavior
Under the hood, the data collector for this library is declared as `var`. Because of this, the ๐ฒ๐๐๐๐๐ณ๐๐๐ object will always reflect the latest available data on realtime updates. Any data that is recorded for historical bars will remain unchanged throughout the execution of a script.
โข Collecting Chart Data for Other Contexts
If your use case requires collecting chart data from another context, avoid directly retrieving the ๐ฒ๐๐๐๐๐ณ๐๐๐ object as this may exceed memory limits.
โข Memory Usage
The library's convenience and ease of use comes at the cost of increased usage of computational resources. For simple scripts, using this library will likely not cause any issues with exceeding memory limits. But for large and complex scripts, you can reduce memory issues by specifying a lower ๐๐๐๐_๐๐๐๐_๐๐๐๐๐ amount in the indicator() or strategy() declaration statement.
โโEXPORTED ENUMS
Snap
โโBehavior for determining the bar that a timestamp belongs to.
โโFields:
โโโโLEFT: Snap to the leftmost bar.
โโโโRIGHT: Snap to the rightmost bar.
โโโโDEFAULT: Default `xloc.bar_time` behavior.
Note: this enum is used for the ๐๐๐๐ parameter of ๐๐๐๐๐๐๐๐๐๐๐๐ฑ๐๐๐ธ๐๐๐๐ก().
โโEXPORTED TYPES
Note: users of the library do not need to worry about directly accessing the fields of these types; all computations are done through method calls on an object of the ๐ฒ๐๐๐๐๐ณ๐๐๐ type.
Variable
โโRepresents a user-specified variable that can be tracked on every chart bar.
โโFields:
โโโโname (series string): Unique identifier for the variable.
โโโโvalues (array<float>): The array of stored values (one value per chart bar).
ChartData
โโRepresents data for all bars on a chart.
โโFields:
โโโโbars (series int): Current number of bars on the chart.
โโโโtimeValues (array<int>): The `time` values of all chart (and future) bars.
โโโโtimeCloseValues (array<int>): The `time_close` values of all chart (and future) bars.
โโโโvariables (array<Variable>): Additional custom values to track on all chart bars.
โโEXPORTED FUNCTIONS
collectChartData()
โโCollects and tracks the `time` and `time_close` value of every bar on the chart.
โโReturns: `ChartData` object to convert between `xloc.bar_index` and `xloc.bar_time`.
collectChartData(barsForward)
โโCollects and tracks the `time` and `time_close` value of every bar on the chart as well as a specified number of future bars.
โโParameters:
โโโโbarsForward (simple int): Number of future bars to collect data for.
โโReturns: `ChartData` object to convert between `xloc.bar_index` and `xloc.bar_time`.
collectChartData(variables)
โโCollects and tracks the `time` and `time_close` value of every bar on the chart. Additionally, tracks a custom set of variables for every chart bar.
โโParameters:
โโโโvariables (simple map<string, float>): Custom values to collect on every chart bar.
โโReturns: `ChartData` object to convert between `xloc.bar_index` and `xloc.bar_time`.
collectChartData(barsForward, variables)
โโCollects and tracks the `time` and `time_close` value of every bar on the chart as well as a specified number of future bars. Additionally, tracks a custom set of variables for every chart bar.
โโParameters:
โโโโbarsForward (simple int): Number of future bars to collect data for.
โโโโvariables (simple map<string, float>): Custom values to collect on every chart bar.
โโReturns: `ChartData` object to convert between `xloc.bar_index` and `xloc.bar_time`.
โโEXPORTED METHODS
method timestampToBarIndex(chartData, timestamp, snap)
โโConverts a UNIX timestamp to a bar index.
โโNamespace types: ChartData
โโParameters:
โโโโchartData (series ChartData): The `ChartData` object.
โโโโtimestamp (series int): A UNIX time.
โโโโsnap (series Snap): A `Snap` enum value.
โโReturns: A bar index, or `na` if unable to find the appropriate bar index.
method getNumberOfBarsBack(chartData, timestamp)
โโConverts a UNIX timestamp to a history-referencing length (i.e., number of bars back).
โโNamespace types: ChartData
โโParameters:
โโโโchartData (series ChartData): The `ChartData` object.
โโโโtimestamp (series int): A UNIX time.
โโReturns: A bar offset, or `na` if unable to find a valid number of bars back.
method timeAtBarIndex(chartData, barIndex)
โโRetrieves the `time` value for the specified bar index.
โโNamespace types: ChartData
โโParameters:
โโโโchartData (series ChartData): The `ChartData` object.
โโโโbarIndex (int): The bar index.
โโReturns: The `time` value, or `na` if there is no `time` stored for the bar index.
method time(chartData, length)
โโRetrieves the `time` value of the bar that is `length` bars back relative to the latest bar.
โโNamespace types: ChartData
โโParameters:
โโโโchartData (series ChartData): The `ChartData` object.
โโโโlength (series int): Number of bars back.
โโReturns: The `time` value `length` bars ago, or `na` if there is no `time` stored for that bar.
method timeCloseAtBarIndex(chartData, barIndex)
โโRetrieves the `time_close` value for the specified bar index.
โโNamespace types: ChartData
โโParameters:
โโโโchartData (series ChartData): The `ChartData` object.
โโโโbarIndex (series int): The bar index.
โโReturns: The `time_close` value, or `na` if there is no `time_close` stored for the bar index.
method timeClose(chartData, length)
โโRetrieves the `time_close` value of the bar that is `length` bars back from the latest bar.
โโNamespace types: ChartData
โโParameters:
โโโโchartData (series ChartData): The `ChartData` object.
โโโโlength (series int): Number of bars back.
โโReturns: The `time_close` value `length` bars ago, or `na` if there is none stored.
method valueAtBarIndex(chartData, name, barIndex)
โโRetrieves the value of a custom variable for the specified bar index.
โโNamespace types: ChartData
โโParameters:
โโโโchartData (series ChartData): The `ChartData` object.
โโโโname (series string): The variable name.
โโโโbarIndex (series int): The bar index.
โโReturns: The value of the variable, or `na` if that variable is not stored for the bar index.
method value(chartData, name, length)
โโRetrieves a variable value of the bar that is `length` bars back relative to the latest bar.
โโNamespace types: ChartData
โโParameters:
โโโโchartData (series ChartData): The `ChartData` object.
โโโโname (series string): The variable name.
โโโโlength (series int): Number of bars back.
โโReturns: The value `length` bars ago, or `na` if that variable is not stored for the bar index.
method getAllVariablesAtBarIndex(chartData, barIndex)
โโRetrieves all custom variables for the specified bar index.
โโNamespace types: ChartData
โโParameters:
โโโโchartData (series ChartData): The `ChartData` object.
โโโโbarIndex (series int): The bar index.
โโReturns: Map of all custom variables that are stored for the specified bar index.
method getEarliestStoredData(chartData)
โโGets all values from the earliest bar data that is currently stored in memory.
โโNamespace types: ChartData
โโParameters:
โโโโchartData (series ChartData): The `ChartData` object.
โโReturns: A tuple: [barIndex, time, timeClose, variables]
method getLatestStoredData(chartData, futureData)
โโGets all values from the latest bar data that is currently stored in memory.
โโNamespace types: ChartData
โโParameters:
โโโโchartData (series ChartData): The `ChartData` object.
โโโโfutureData (series bool): Whether to include the future data that is stored in memory.
โโReturns: A tuple: [barIndex, time, timeClose, variables]
Perpustakaan Pine
Dalam semangat sebenar TradingView, penulis telah menerbitkan kod Pine ini sebagai perpustakaan sumber terbuka supaya pengaturcara Pine lain dari komuniti kami boleh menggunakannya semula. Sorakan kepada penulis! Anda juga boleh menggunakan perpustakaan ini secara peribadi atau dalam penerbitan sumber terbuka lain, tetapi penggunaan semula kod ini dalam penerbitan adalah tertakluk kepada Peraturan Dalaman.
Penafian
Perpustakaan Pine
Dalam semangat sebenar TradingView, penulis telah menerbitkan kod Pine ini sebagai perpustakaan sumber terbuka supaya pengaturcara Pine lain dari komuniti kami boleh menggunakannya semula. Sorakan kepada penulis! Anda juga boleh menggunakan perpustakaan ini secara peribadi atau dalam penerbitan sumber terbuka lain, tetapi penggunaan semula kod ini dalam penerbitan adalah tertakluk kepada Peraturan Dalaman.