Library · Pine Script v6 · Library

MirPapa_Handler_HTF

by alvinng214· code credited to goodia

MirPapa_Handler_HTF is a Pine Script v6 utility library designed for handling higher timeframe conversions and multi-timeframe data retrieval.

View on GitHub

GitHub stars2on the repository
Pine versionv6current
Lines of code161
Settings0inputs
LicenceNone statedfile: MPL-2.0
Last update2 months ago5 Aug 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new library, select all of the template text and paste. Press Save and give it a name.
  4. 4Publish or import itA library draws nothing. Publish it from the editor, then import its functions in your own scripts.

Source Code

161 lines of Pine Script v6 from MirPapa-ICT-HTF- FVG OB Threeple/MirPapa_Handler_HTF.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

MirPapa_Handler_HTF.txt
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodia

//@version=6

// @description
// High Time Frame Handler Library:
// Provides utilities for working with High Time Frame (HTF) and chart (LTF) conversions and data retrieval.
library("MirPapa_Handler_HTF", overlay = false)

////////////////////////////////////////////////////////////////////////////////
// ────────────────────────────────────────────────────────────────────────────
// HandlerHTF (inlined)
// @description
// High Time Frame Handler: Functions for converting and retrieving HTF values.
// ────────────────────────────────────────────────────────────────────────────
////////////////////////////////////////////////////////////////////////////////

// @function parseMinutesFromTF
// @description
//   Parse a timeframe string into its equivalent number of minutes.
// @param _tf
//   A timeframe string (examples: "1", "5", "15", "1D", "1W", "1M").
// @return
//   Returns the integer number of minutes corresponding to the timeframe, or na if the string is not recognized.
parseMinutesFromTF(string _tf) =>
     _tf == "1"    ? 1 :
     _tf == "3"    ? 3 :
     _tf == "5"    ? 5 :
     _tf == "15"   ? 15 :
     _tf == "30"   ? 30 :
     _tf == "60"   ? 60 :
     _tf == "120"  ? 120 :
     _tf == "240"  ? 240 :
     _tf == "1D"   ? 1440 :
     _tf == "1W"   ? 10080 :
     _tf == "1M"   ? 43200 :
     _tf == "12M"  ? 525600 : na

// @function calHTFtoRatioLTF
// @description
//   Calculate the ratio of a High Time Frame (HTF) to the current chart’s Lower Time Frame (LTF) in minutes.
// @param _chartTf
//   The current chart’s timeframe string (examples: "5", "15", "1D").
// @param _htfTf
//   The target High Time Frame string to compare against (examples: "60", "1D").
// @return
//   Returns the rounded integer ratio of HTF minutes to chart minutes, or na if either conversion fails.
calHTFtoRatioLTF(string _chartTf, string _htfTf) =>
    _chartMin = parseMinutesFromTF(_chartTf)
    _htfMin   = parseMinutesFromTF(_htfTf)
    _chartMin > 0 and _htfMin > 0 ? math.round(_htfMin / _chartMin) : na

// @function IsChartTFcomparisonHTF
// @description
//   Determine whether the given High Time Frame (HTF) is greater than or equal to the current chart timeframe.
// @param _chartTf
//   The current chart’s timeframe string (examples: "5", "15", "1D").
// @param _htfTf
//   The High Time Frame string to compare (examples: "60", "1D").
// @return
//   Returns true if HTF minutes ≥ chart minutes, false otherwise or na if conversion fails.
export IsChartTFcomparisonHTF(string _chartTf, string _htfTf) =>
    _chartMin = parseMinutesFromTF(_chartTf)
    _htfMin   = parseMinutesFromTF(_htfTf)
    not na(_chartMin) and not na(_htfMin) and _htfMin >= _chartMin

// @function GetHTFrevised
// @description
//   Retrieve a specific bar value from a Higher Time Frame (HTF) series.
//   Supports current and historical OHLC values, based on a case identifier.
// @param _tf
//   The target HTF string (examples: "60", "1D").
// @param _case
//   A case string determining which OHLC value and bar offset to request:
//   "b"   → HTF bar_index
//   "o"   → HTF open
//   "h"   → HTF high
//   "l"   → HTF low
//   "c"   → HTF close
//   "o1"  → HTF open one bar ago
//   "h1"  → HTF high one bar ago
//  "l1"  → HTF low one bar ago
//   "c1"  → HTF close one bar ago
//   … up to "o5", "h5", "l5", "c5" for five bars ago.
// @return
//   Returns the requested HTF value or na if _case does not match any condition.
export GetHTFrevised(string _tf) =>
    request.security(syminfo.tickerid, _tf, bar_index)

export GetHTFrevised(string _tf, string _case) =>
    switch _case
        "b"  => request.security(syminfo.tickerid, _tf, bar_index)
        "o"  => request.security(syminfo.tickerid, _tf, open)
        "h"  => request.security(syminfo.tickerid, _tf, high)
        "l"  => request.security(syminfo.tickerid, _tf, low)
        "c"  => request.security(syminfo.tickerid, _tf, close)
        "o1" => request.security(syminfo.tickerid, _tf, open[1])
        "h1" => request.security(syminfo.tickerid, _tf, high[1])
        "l1" => request.security(syminfo.tickerid, _tf, low[1])
        "c1" => request.security(syminfo.tickerid, _tf, close[1])
        "o2" => request.security(syminfo.tickerid, _tf, open[2])
        "h2" => request.security(syminfo.tickerid, _tf, high[2])
        "l2" => request.security(syminfo.tickerid, _tf, low[2])
        "c2" => request.security(syminfo.tickerid, _tf, close[2])
        "o3" => request.security(syminfo.tickerid, _tf, open[3])
        "h3" => request.security(syminfo.tickerid, _tf, high[3])
        "l3" => request.security(syminfo.tickerid, _tf, low[3])
        "c3" => request.security(syminfo.tickerid, _tf, close[3])
        "o4" => request.security(syminfo.tickerid, _tf, open[4])
        "h4" => request.security(syminfo.tickerid, _tf, high[4])
        "l4" => request.security(syminfo.tickerid, _tf, low[4])
        "c4" => request.security(syminfo.tickerid, _tf, close[4])
        "o5" => request.security(syminfo.tickerid, _tf, open[5])
        "h5" => request.security(syminfo.tickerid, _tf, high[5])
        "l5" => request.security(syminfo.tickerid, _tf, low[5])
        "c5" => request.security(syminfo.tickerid, _tf, close[5])
        => na

// @function GetHTFfromLabel
// @description
//   Convert a Korean HTF label into a Pine Script-recognizable timeframe string.
//   Examples:
//     "5분"  → "5"
//     "1시간" → "60"
//     "일봉"  → "1D"
//     "주봉"  → "1W"
//     "월봉"  → "1M"
//     "연봉"  → "12M"
// @param _label
//   The Korean HTF label string (examples: "5분", "1시간", "일봉").
// @return
//   Returns the Pine Script timeframe string corresponding to the label, or "1W" if no match is found.
export GetHTFfromLabel(string _label) =>
     _label == "5분"   ? "5" :
     _label == "15분"  ? "15" :
     _label == "1시간" ? "60" :
     _label == "4시간" ? "240" :
     _label == "일봉"  ? "1D" :
     _label == "주봉"  ? "1W" :
     _label == "월봉"  ? "1M" :
     _label == "연봉"  ? "12M" : "1W"

// @function GetHTFoffsetToLTFoffset
// @description
//   Adjust an HTF bar index and offset so that it aligns with the current chart’s bar index.
//   Useful for retrieving historical HTF data on an LTF chart.
// @param _offset
//   The HTF bar offset (0 means current HTF bar, 1 means one bar ago, etc.).
// @param _chartTf
//   The current chart’s timeframe string (examples: "5", "15", "1D").
// @param _htfTf
//   The High Time Frame string to align (examples: "60", "1D").
// @return
//   Returns the corresponding LTF bar index after applying HTF offset. If result is negative, returns 0.
export GetHTFoffsetToLTFoffset(int _offset, string _chartTf, string _htfTf) =>
    ratio = calHTFtoRatioLTF(_chartTf, _htfTf)
    _os = _chartTf == _htfTf ? _offset : _offset + 1
    _result = bar_index - (ratio * _os)
    _result := 0 > _result ? 0 : _result
    _result

Overview

MirPapa_Handler_HTF converts standard timeframe strings into minute counts, computes the numerical ratio between higher and lower timeframes, and validates whether a target interval is greater than or equal to the active chart. It also provides helper routines to query higher timeframe OHLC data series. Because this script functions as a developer library, it draws no chart elements and produces no standalone signals.

Details

Published by

alvinng214 on GitHub

Code credit

goodia, as written in the script's header

Type

Library, Pine Script v6

Draws

Library (no plots)

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

261 stars

AIActivationFunctionsBipolarSigmoid is a Pine Script library that provides mathematical implementations of the bipolar sigmoid activation function and its derivatives for machine learning algorithms. It calculates bipolar sigmoid values bounded between -1 and

Library v5 AI / machine learning
Updated 4 years ago · None stated GitHub repository of AIActivationFunctionsBipolarSigmoid

KernelFunctions

by artificial-intelligence-edge
75 stars

KernelFunctions is a Pine Script v5 library that exports kernel weight functions for Nadaraya-Watson style estimation. It calculates a Rational Quadratic kernel, a Gaussian kernel based on a radial basis function, and a periodic kernel that models repeating be

Library v5
Updated 2 days ago · MIT GitHub repository of KernelFunctions

Ta

by nersent
29 stars

This Pine Script v5 library exports thin wrappers around built-in technical analysis functions. It covers moving averages, momentum measures such as RSI and CCI, volatility tools including ATR and standard deviation, range extremes, cross detection, and runnin

Library v5
Updated 11 months ago · Custom licence GitHub repository of Ta

Bands

by heyphat
26 stars

This library exports a Band type and functions that build a simple-moving-average basis with upper and lower envelopes from standard deviation. It classifies a source as above, below, or inside those envelopes, measures band width, and scales an imported z-sco

Library v6 Volatility & bandsMoving averages
Updated 2 months ago · AGPL-3.0 GitHub repository of Bands

Mylib

by ferranbt
21 stars

This script serves as a minimal utility library written in Pine Script v6. It provides an exported helper function that calculates the sum of two numeric arguments. As a reusable code library, it produces no visual plots, charts, or trading signals, and it con

Library v6
Updated 7 weeks ago · MPL-2.0 GitHub repository of Mylib

HLevel

by stupenkov
14 stars

HLevel is a Pine Script v5 utility library designed to manage horizontal price levels for cryptocurrency charts. It defines custom types for pivots and levels, along with methods to initialize, add, track, and expire price levels based on pivot high and pivot

Library v5
Updated 3 years ago · None stated GitHub repository of HLevel

More libraries

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers