PINE LIBRARY
已更新

BarUtils: Get Bar Index from Date

186
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.
版本注释
v2

Added:
rateOfReturn(source, referenceBarIndex)
  Parameters:
    source (float)
    referenceBarIndex (int)
版本注释
v3
版本注释
v4
版本注释
v5

Generic Pivot high and Pivot low
版本注释
v6

Added:
pivotHighGeneric(src, leftBars, rightBars)
  Parameters:
    src (float)
    leftBars (int)
    rightBars (int)

pivotLowGeneric(src, leftBars, rightBars)
  Parameters:
    src (float)
    leftBars (int)
    rightBars (int)
版本注释
v7

Added:
getColorGradient(val)
  Parameters:
    val (float)

getColorGradientEx(val, win, useLog, colLow, colHigh)
  Parameters:
    val (float)
    win (simple int)
    useLog (bool)
    colLow (color)
    colHigh (color)
版本注释
v8

Added:
normalize(adj, base, length)
  Parameters:
    adj (float)
    base (float)
    length (int)
版本注释
v9

Added:
pearsonR(srcSeries, length)
  Parameters:
    srcSeries (float)
    length (int)

免责声明

这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。