OPEN-SOURCE SCRIPT

Test OHLCV Library

115
This indicator, "Test OHLCV Library," serves as a practical example of how to use the OHLCVData library to fetch historical candle data from a specific timeframe (like 4H) in a way that is largely impervious to the chart's currently selected time frame.

Here's a breakdown of its purpose and how it addresses request.security limitations:

Indicator Purpose:

The main goal of this indicator is to demonstrate and verify that the OHLCVData library can reliably provide confirmed historical OHLCV data for a user-specified timeframe (e.g., 4H), and that a collection of these data points (the last 10 completed candles) remains consistent even when the user switches the chart's time frame (e.g., from 5-second to Daily).

It does this by:

Importing the OHLCVData library.
Using the library's getTimeframeData function on every bar of the chart.
Checking the isTargetBarClosed flag returned by the library to identify the exact moment a candle in the target timeframe (e.g., 4H) has closed.
When isTargetBarClosed is true, it captures the confirmed OHLCV data provided by the library for that moment and stores it in a persistent var array.
It maintains a list of the last 10 captured historical 4H candle opens in this array.
It displays these last 10 confirmed opens in a table.
It uses the isAdjustedToChartTF flag from the library to show a warning if the chart's time frame is higher than the target timeframe, indicating that the data fetched by request.security is being aligned to that higher resolution.
Circumventing request.security Limitations:

The primary limitation of request.security that this setup addresses is the challenge of getting a consistent, non-repainting collection of historical data points from a different timeframe when the chart's time frame is changed.

The Problem: Standard request.security calls, while capable of fetching data from other timeframes, align that data to the bars of the current chart. When you switch the chart's time frame, the set of chart bars changes, and the way the requested data aligns to these new bars changes. If you simply collected data on every chart bar where request.security returned a non-na value, the resulting collection would differ depending on the chart's resolution. Furthermore, using request.security without lookahead=barmerge.lookahead_off or an offset ([1]) can lead to repainting on historical bars, where values change as the script recalculates.
How the Library/Indicator Setup Helps:
Confirmed Data: The OHLCVData library uses lookahead=barmerge.lookahead_off and, more importantly, provides the isTargetBarClosed flag. This flag is calculated using a reliable method (checking for a change in the target timeframe's time series) that accurately identifies the precise chart bar corresponding to the completion of a candle in the target timeframe (e.g., a 4H candle), regardless of the chart's time frame.
Precise Capture: The indicator only captures and stores the OHLCV data into its var array when this isTargetBarClosed flag is true. This means it's capturing the confirmed, finalized data for the target timeframe candle at the exact moment it closes.
Persistent Storage: The var array in the indicator persists its contents across the bars of the chart's history. As the script runs through the historical bars, it selectively adds confirmed 4H candle data points to this array only when the trigger is met.
Impervious Collection: Because the array is populated based on the completion of the target timeframe candles (detected reliably by the library) rather than simply collecting data on every chart bar, the final contents of the array (the list of the last 10 confirmed 4H opens) will be the same regardless of the chart's time frame. The table then displays this static collection.
In essence, this setup doesn't change how request.security fundamentally works or aligns data to the chart's bars. Instead, it uses the capabilities of request.security (fetching data from another timeframe) and Pine Script's execution model (bar-by-bar processing, var persistence) in a specific way, guided by the library's logic, to build a historical collection of data points that represent the target timeframe's candles and are independent of the chart's display resolution.

Penafian

Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.