PINE LIBRARY
Updated BarUtils: Get Bar Index from Date

Library "BarUtils"
getBarIndexFromDate(targetTimestamp)
Parameters:
targetTimestamp (int)
**Description**:
This utility provides a reliable way to calculate the `bar_index` of a specific calendar date, regardless of chart resolution. It's especially useful for anchoring scripts to historical events, labeling macroeconomic moments, or marking custom time-based signals that must remain consistent across timeframes.
Unlike hardcoded `bar_index - N` approaches, this function dynamically estimates the number of bars between a given `timestamp()` and the current bar using the actual time-per-bar (`time - time[1]`). It works correctly on intraday, daily, weekly, and monthly charts.
### 💡 **Function Provided**:
import TradeTitan120/BarUtils/1
* `getBarIndexFromDate(int targetTimestamp)`
→ Returns the estimated `bar_index` that aligns with a given timestamp
### ✅ **Use Cases**:
* Marking past events like FOMC meetings, market crashes, or personal signals
* Backtesting entry/exit conditions from specific calendar dates
* Anchoring visual elements (shapes, lines, labels) across resolutions
This tool is simple, fast, and built for accuracy. Use it to enhance multi-timeframe compatibility in any script.
getBarIndexFromDate(targetTimestamp)
Parameters:
targetTimestamp (int)
**Description**:
This utility provides a reliable way to calculate the `bar_index` of a specific calendar date, regardless of chart resolution. It's especially useful for anchoring scripts to historical events, labeling macroeconomic moments, or marking custom time-based signals that must remain consistent across timeframes.
Unlike hardcoded `bar_index - N` approaches, this function dynamically estimates the number of bars between a given `timestamp()` and the current bar using the actual time-per-bar (`time - time[1]`). It works correctly on intraday, daily, weekly, and monthly charts.
### 💡 **Function Provided**:
import TradeTitan120/BarUtils/1
* `getBarIndexFromDate(int targetTimestamp)`
→ Returns the estimated `bar_index` that aligns with a given timestamp
### ✅ **Use Cases**:
* Marking past events like FOMC meetings, market crashes, or personal signals
* Backtesting entry/exit conditions from specific calendar dates
* Anchoring visual elements (shapes, lines, labels) across resolutions
This tool is simple, fast, and built for accuracy. Use it to enhance multi-timeframe compatibility in any script.
Release Notes
v2Added:
rateOfReturn(source, referenceBarIndex)
Parameters:
source (float)
referenceBarIndex (int)
Release Notes
v3Release Notes
v4Pine library
In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in publications is governed by House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Pine library
In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in publications is governed by House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.