PINE LIBRARY

libHTF[without request.security()]

Updated
Library "libHTF"
libHTF: use HTF values without request.security()
This library enables to use HTF candles without request.security().

Basic data structure
Using <array> to access values in the same manner as series variable.
The last member of HTF array is always latest current TF's data.
If new bar in HTF(same as last bar closes), new member is pushed to HTF array.
2nd from the last member of HTF array is latest fixed(closed) bar.

HTF: How to use
1. set TF
tf_higher() function selects higher TF. TF steps are ("1","5","15","60","240","D","W","M","3M","6M","Y").
example:


2. set HTF matrix
htf_candle() function returns 1 bool and 1 matrix.
bool is a flag for start of new candle in HTF context.
matrix is HTF candle data(0:open,1:time_open,2:close,3:time_close,4:high,5:time:high,6:low,7:time_low).
example:


3. how to access HTF candle data
you can get values using <matrix>.lastx() method.
please be careful, return value is always float evenif it is "time". you need to cast to int time value when using for xloc.bartime.
example:


4. how to store Data of HTF context
you have to use array to store data of HTF context.
array.htf_push() method handles the last member of array. if new_bar in HTF, it push new member. otherwise it set value to the last member.
example:


HTFsrc: How to use
1. how to setup src.
set_src() function is set current tf's src from string(open/high/low/close/hl2/hlc3/ohlc4/hlcc4).
set_htfsrc() function returns src array of HTF candle.

example:


HighLow: How to use
1. set HTF arrays
highlow() and htfhighlow() function calculates high/low and return high/low prices and time.
the functions return 1 int and 8arrays.
int is a flag for new high(1) or new low(-1).
arrays are high/low and return high/low data. float for price, int for time.
example


2. how to access HighLow data
you can get values using <array>.lastx() method.
example:


other functions
functions for HTF candle matrix or HTF src array in this script are
htf_sma()/htf_ema()/htf_rma()
htf_rsi()/htf_rci()/htf_dmi()


method lastx(arrayid, lastindex)
  method like array.last. it returns lastindex from the last member, if parameter is set.
  Namespace types: float[]
  Parameters:
    arrayid (float[])
    lastindex (int): (int) default value is "0"(the last member). if you need to access historical value, increment it(same manner as series vars).
  Returns: float value of lastindex from the last member of the array. returns na, if fail.

method lastx(arrayid, lastindex)
  method like array.last. it returns lastindex from the last member, if parameter is set.
  Namespace types: int[]
  Parameters:
    arrayid (int[])
    lastindex (int): (int) default value is "0"(the last member). if you need to access historical value, increment it(same manner as series vars).
  Returns: int value of lastindex from the last member of the array. returns na, if fail.

method lastx(m, _type, lastindex)
  method for handling htf matrix.
  Namespace types: matrix<float>
  Parameters:
    m (matrix<float>): (matrix<float>) matrix for htf candle.
    _type (string): (string) value type of htf candle:["open","time_open","close","time_close","high","time_high","low","time_low"]
    lastindex (int): (int) default value is "0"(the last member).
  Returns: (float) value of htf candle. (caution: need to cast float to int to use time values!)

method set_last(arrayid, val)
  method to set a value of the last member of the array. it sets value to the last member.
  Namespace types: float[]
  Parameters:
    arrayid (float[])
    val (float): (float) value to set.
  Returns: nothing

method htf_push(arrayid, b, val)
  method to push new member to htf context. if new bar in htf, it works as push. else it works as set_last.
  Namespace types: float[]
  Parameters:
    arrayid (float[])
    b (bool): (bool) true:push,false:set_last
    val (float): (float) _f the value to set.
  Returns: nothing

method tf_higher(tf)
  method to set higher tf from tf string. TF steps are ["1","5","15","60","240","D","W","M","3M","6M","Y"].
  Namespace types: series string, simple string, input string, const string
  Parameters:
    tf (string): (string) tf string
  Returns: (string) string of higher tf.

htf_candle(_tf, _TZ)
  build htf candles
  Parameters:
    _tf (string): (string) tf string.
    _TZ (string): [optional string] of timezone. default value is "GMT+3".
  Returns: [bool,(matrix<float>)] bool for new barhtf and matrix for snapshot of htf candle

set_src(_src_type)
  set src.
  Parameters:
    _src_type (string): (string) type of source:["open","high","low","close","hl2","hlc3","ohlc4","hlcc4"]
  Returns: (series float) src value

set_htfsrc(_src_type, _nb, _m)
  set htf src.
  Parameters:
    _src_type (string): (string) type of source:["open","high","low","close","hl2","hlc3","ohlc4","hlcc4"]
    _nb (bool): (bool) flag of new bar
    _m (matrix<float>): (matrix<float>) matrix for htf candle.
  Returns: (array<float>) array of src value

is_up()

last_is_up()

peak_bottom(_latest, _last)
  Parameters:
    _latest (bool)
    _last (bool)

htf_is_up(_m)
  Parameters:
    _m (matrix<float>)

htf_last_is_up(_m)
  Parameters:
    _m (matrix<float>)

highlow(_b_bartime_price)
  Parameters:
    _b_bartime_price (bool)

htfhighlow(_m, _b_bartime_price)
  Parameters:
    _m (matrix<float>)
    _b_bartime_price (bool)

htf_sma(_a_src, _len)
  Parameters:
    _a_src (float[])
    _len (int)

htf_rma(_a_src, _new_bar, _len)
  Parameters:
    _a_src (float[])
    _new_bar (bool)
    _len (int)

htf_ema(_a_src, _new_bar, _len)
  Parameters:
    _a_src (float[])
    _new_bar (bool)
    _len (int)

htf_rsi(_a_src, _new_bar, _len)
  Parameters:
    _a_src (float[])
    _new_bar (bool)
    _len (int)

rci(_src, _len)
  Parameters:
    _src (float)
    _len (int)

htf_rci(_a_src, _len)
  Parameters:
    _a_src (float[])
    _len (int)

htf_dmi(_m, _new_bar, _len, _ma_type)
  Parameters:
    _m (matrix<float>)
    _new_bar (bool)
    _len (int)
    _ma_type (string)
Release Notes
Bug fix:
  • bar closing process of htf_candle()
  • typo in highlow() and htfhighlow() sample
Release Notes
Bug fix
  • Incorrect usage of ta.change().
highertimeframesHTFlibrarymtf

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 a publication is governed by House rules.

Disclaimer