TradingView
nazomobile
15. Aug. 2023 11:08

libHTF[without request.security()] 

U.S. Dollar / Swiss FrancFOREX.com

Beschreibung

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:
tfChart = timeframe.period htf1 = tf_higher(tfChart)


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:
[b_new_bar1,m1]=htf_candle(htf1)


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:
htf1open=m1.lastx("open") htf1close=m1.lastx("close") //if you need to use histrical value. lastopen=open[1] lasthtf1open=m1.lastx("open",1)


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:
array a_close=array.new<float>(1,na) a_close.htf_push(b_new_bar1,m1.lastx("close"))


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:
_src="ohlc4" src=set_src(_src) htf1src=set_htfsrc(_src,b_new_bar1,m1) (if you need to use HTF src in series float) s_htf1src=htf1src.lastx()


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
[i_renew,a_high,a_time_high,a_return_high,a_time_return_high,a_low,a_time_low,a_return_low,a_time_return_low] = highlow() [i_renew1,a_high1,a_time_high1,a_return_high1,a_time_return_high1,a_low1,a_time_low1,a_return_low1,a_time_return_low1] = htfhighlow(m1)


2. how to access HighLow data
you can get values using <array>.lastx() method.
example:
if i_renew==1 myhigh=a_high.lastx() //if you need to use histrical value. myhigh=a_high.lastx(1)


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 bar@htf 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)

Versionshinweise

Bug fix:
  • bar closing process of htf_candle()
  • typo in highlow() and htfhighlow() sample

Versionshinweise

Bug fix
  • Incorrect usage of ta.change().
Kommentare
kimgyuhan86446388
Hello, I am testing the library you made.

I am comparing values using request.security as shown below.
There is no problem with real-time open, low, high, and close.

However, the values of the latest fixed bar are different.

The request.security function accurately calculates the 4h_TF High and low of the latest fixed bar, but the values calculated by the library output the High and low values much earlier than 4 hours.

htf1src.lastx(1) != low_4h (1)

What's the problem?

htf1 = libHTFwoRS.tf_higher("240") [new_bar1, matrix1]=libHTFwoRS.htf_candle(htf1,syminfo.timezone) b_new_bar1 = new_bar1 == 1 ? true : false _src="low" TF1_src=libHTFwoRS.set_src(_src) htf1src=libHTFwoRS.set_htfsrc(_src,b_new_bar1,matrix1) s_htf1src=htf1src.lastx() low_4h = request.security(syminfo.tickerid, "240", low, lookahead=barmerge.lookahead_on)
nazomobile
Hi @kimgyuhan86446388, Haven't tryed it yet. But tf_higher("240") is "D", You have to compare it with "D" of request.security().
Or unify the notation of timeframe using the variable "htf1".
Bullyena
Hi, would you be able to help me find the error in my custom PVT indicator? I've been trying to let it allow array <floats> instead of series float using your library.

I've been trying to do this but only end with resulting to 0 or having same results for all different timeframes.

Please note that the functions in my original code is arranged before the variables are initialized and functions are used. I am just organizing it this way for readability

Here is the code below:
tf1m = "0" htf_1m = tf_higher(tf1m) [b_new_bar_1m,m1]=htf_candle(htf_1m, "GMT+8") _src = "close" pvt_src = set_src(_src) htf_1msrc = set_htfsrc(_src,b_new_bar_1m,m1) pvt_short = 7 pvt_long = 10 pvt_maLength = 2 pvt_normalizeLookback = 10 pvt_shouldNormalize = true [pvt_value_1m, pvt_ma_1m] = htf_pvt(htf_1msrc, pvt_short,pvt_long,pvt_maLength,1.0,pvt_normalizeLookback,pvt_shouldNormalize) calc_pvt(array<float> sig, int short, int long) => vt = ta.cum(ta.change(sig.lastx())/ sig.lastx(1) * volume) sh = ta.sma(vt, short) lg = ta.sma(vt, long) sh - lg maxAbs(series float series, int len) => max = float(0) for i = 0 to len-1 max := math.max(max, math.abs(series)) max // @function calculate HTF normalized PVT and its MA // @param range_pvt : default is 1.0 // @returns value_pvt and value_ma export htf_pvt(array<float> _pvt_src, int pvt_short_len, int pvt_long_len, int osc_maLen, float _range_pvt, int normalize_lookback_len, bool shouldNormalize) => raw_pvt = calc_pvt(_pvt_src, pvt_short_len, pvt_long_len) maxAbs_pvt = maxAbs(raw_pvt, normalize_lookback_len) float range_pvt = 0. // range to fit values into if na(_range_pvt) range_pvt := 1. else range_pvt := _range_pvt value_pvt = shouldNormalize ? (raw_pvt / maxAbs_pvt * range_pvt) : raw_pvt value_ma = ta.sma(value_pvt, osc_maLen) [value_pvt, value_ma]
nazomobile
Hi @Bullyena, First of all, unfortunately I don't know much about PVT.
I tried to use simple subtraction instead of ta.change() in calc_pvt func.
I confirmed using ta.change(), the same line is drawn even with different tf.
But it's different using simple subtraction, is this what you want?

What I tried is bellow:
indicator("pvttest") import nazomobile/libHTFwoRS/2 as H calc_pvt(array<float> sig, int short, int long) => // vt = ta.cum(ta.change(sig.lastx())/ sig.lastx(1) * volume) vt = ta.cum((sig.lastx(1)-sig.lastx())/ sig.lastx(1) * volume) sh = ta.sma(vt, short) lg = ta.sma(vt, long) sh - lg maxAbs(series float series, int len) => max = float(0) for i = 0 to len-1 max := math.max(max, math.abs(series)) max // @function calculate HTF normalized PVT and its MA // @param range_pvt : default is 1.0 // @returns value_pvt and value_ma htf_pvt(array<float> _pvt_src, int pvt_short_len, int pvt_long_len, int osc_maLen, float _range_pvt, int normalize_lookback_len, bool shouldNormalize) => raw_pvt = calc_pvt(_pvt_src, pvt_short_len, pvt_long_len) maxAbs_pvt = maxAbs(raw_pvt, normalize_lookback_len) float range_pvt = 0. // range to fit values into if na(_range_pvt) range_pvt := 1. else range_pvt := _range_pvt value_pvt = shouldNormalize ? (raw_pvt / maxAbs_pvt * range_pvt) : raw_pvt value_ma = ta.sma(value_pvt, osc_maLen) [value_pvt, value_ma] tf1m = timeframe.period htf_1m = H.tf_higher(tf1m) htf_2m = H.tf_higher(htf_1m) [b_new_bar_1m,m1]=H.htf_candle(htf_1m, "GMT+8") [b_new_bar_2m,m2]=H.htf_candle(htf_2m, "GMT+8") _src = "close" //pvt_src = H.set_src(_src) htf_1msrc = H.set_htfsrc(_src,b_new_bar_1m,m1) htf_2msrc = H.set_htfsrc(_src,b_new_bar_2m,m2) pvt_short = 7 pvt_long = 10 pvt_maLength = 2 pvt_normalizeLookback = 10 pvt_shouldNormalize = true [pvt_value_1m, pvt_ma_1m] = htf_pvt(htf_1msrc, pvt_short,pvt_long,pvt_maLength,1.0,pvt_normalizeLookback,pvt_shouldNormalize) [pvt_value_2m, pvt_ma_2m] = htf_pvt(htf_2msrc, pvt_short,pvt_long,pvt_maLength,1.0,pvt_normalizeLookback,pvt_shouldNormalize) plot(pvt_value_1m,color=color.red) plot(pvt_ma_1m,color=color.blue) offset=1 plot(pvt_value_2m+offset,color=color.orange) plot(pvt_ma_2m+offset,color=color.yellow)
Mehr