PINE LIBRARY
HtfCandle

Overview
HtfCandle is a utility library for building higher-timeframe (HTF) candle data from lower-timeframe chart bars.
It provides:
What this library solves
When strategy/indicator logic is executed on lower timeframes, this library helps maintain HTF candle structure in a stateful and reusable way.
How this differs from request.security
Core behavior
On each bar, the library:
Public API
How to use
Notes and limitations
Source visibility
This publication is prepared for protected-source distribution.
The description documents behavior and API to allow practical usage without source visibility.
Disclaimer
For technical analysis infrastructure only. Not investment advice.
HtfCandle is a utility library for building higher-timeframe (HTF) candle data from lower-timeframe chart bars.
It provides:
- Current finalized HTF candle values
- Previous finalized HTF candle values
- HTF high/low and body high/low rolling history helpers
- High/low timestamp tracking inside each finalized HTF candle
What this library solves
When strategy/indicator logic is executed on lower timeframes, this library helps maintain HTF candle structure in a stateful and reusable way.
How this differs from request.security
- request.security can provide HTF OHLC series aligned to the chart context, but it does not directly expose the exact intrabar timestamp where the HTF high or HTF low occurred.
- HtfCandle maintains explicit highTime and lowTime fields for each finalized HTF candle.
- This is useful when downstream logic needs both HTF level values and the precise event time of those extremes.
Core behavior
On each bar, the library:
- Checks whether a new bar started on the selected timeframe.
- Updates running HTF bucket extremes using prior confirmed bar data.
- Finalizes the previous HTF candle when timeframe transition occurs.
- Stores finalized HTF values into rolling history arrays.
Public API
- initCandle(): Creates an empty candle object.
- initOpts(timeframe, numCandles): Creates options with target timeframe and history size.
- initState(opts): Creates runtime state and history arrays.
- update(state, opts): Processes candle state per bar.
- highest(state): Highest finalized HTF high from history window.
- lowest(state): Lowest finalized HTF low from history window.
- bodyHighest(state): Highest finalized HTF body high from history window.
- bodyLowest(state): Lowest finalized HTF body low from history window.
How to use
- Initialize options once with desired timeframe and lookback length.
- Initialize state once.
- Call update(state, opts) on each bar.
- Read finalized values from state.candle, state.candle1, and helper methods.
Notes and limitations
- This is a technical utility library only; it does not generate trading signals or place orders.
- Output depends on chart data quality and symbol session behavior.
- At startup, history-dependent helpers can return na until enough finalized HTF candles are collected.
- Users should validate behavior on their own symbols/timeframes before live usage.
Source visibility
This publication is prepared for protected-source distribution.
The description documents behavior and API to allow practical usage without source visibility.
Disclaimer
For technical analysis infrastructure only. Not investment advice.
مكتبة باين
كمثال للقيم التي تتبناها TradingView، نشر المؤلف شيفرة باين كمكتبة مفتوحة المصدر بحيث يمكن لمبرمجي باين الآخرين من مجتمعنا استخدامه بحرية. تحياتنا للمؤلف! يمكنك استخدام هذه المكتبة بشكل خاص أو في منشورات أخرى مفتوحة المصدر، ولكن إعادة استخدام هذا الرمز في المنشورات تخضع لقواعد الموقع.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.
مكتبة باين
كمثال للقيم التي تتبناها TradingView، نشر المؤلف شيفرة باين كمكتبة مفتوحة المصدر بحيث يمكن لمبرمجي باين الآخرين من مجتمعنا استخدامه بحرية. تحياتنا للمؤلف! يمكنك استخدام هذه المكتبة بشكل خاص أو في منشورات أخرى مفتوحة المصدر، ولكن إعادة استخدام هذا الرمز في المنشورات تخضع لقواعد الموقع.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.