20MAnia[libTF] This is a sample script of libTF : Find higher/lower TF automatically .
This script shows 20SMAs(ta.sma) and 20EMAs(ta.ema) up to 8TFs.
Firstly about static TF, MAs in 1D/1W/1M are always enabled when CTF is lower than 1D.
CTF and LTF are used preferentially when CTF is 1D or higher.
Secondly about dynamic TF, there are 3 types.
CTF(Current TF) is always enabled.
HTF1-4 are set by CTF and TF scale of libHTF(set by this script: 1,5,15,60,240).
HTF1-4 are enabled when its value is not na.
LTF1 is set by CTF and TF scale of libHTF(set by default: 1,5,15,60,240,1D,1W,1M,3M,12M).
LTF1 is enabled when its value is not na.
In LTF, SMA/EMA is displayed as Ribbon: fill color between oldest value and latest value.
example:
in case of CTF=1,
ltf1=na, ctf=1, htf1=5, htf2=15, htf3=60, htf4=240, and DWM
in case of CTF=60,
ltf1=15, ctf=60, htf1=240, htf2-4=na, and DWM
in case of CTF=1D,
ltf1=240, ctf=1D, htf1-4=na, and WM
in case of CTF=1W,
ltf1=1D, ctf=1W, htf1-4=na, and M
Version6
libTFLibrary "libTF"
libTF: Find higher/lower TF automatically
This library to find higher/lower TF from current timeframe(timeframe.period) for Pine Script version6(or higher).
Basic Algorithm
Using a timeframe scale Array and timeframe.in_seconds() function to find higher/lower timeframe.
Return value is na if could not find TF in the timeframe scale.
The timeframe scale could be changed by the parameter 'scale'(CSV).
How to use
1. Set higher/lower TF
higher()/lower() function returns higher/lower TF.
Default timeframe scale is "1, 5, 15, 60, 240, 1D, 1M, 3M, 12M".
example:
htf1 = higher()
htf2 = higher(htf1)
ltf1 = lower()
ltf2 = lower(ltf1)
2. Set higher/lower TF using your timeframe scale
The timeframe scale could be changed by the parameter.
example:
myscale="1,60,1D,1M,12M"
htf1 = higher(timeframe.period,myscale)
htf2 = higher(htf1,myscale)
ltf1 = lower(timeframe.period,myscale)
ltf2 = lower(ltf1,myscale)
3. How to use with request.*() function
na value is set if no higher/lower TF in timeframe scale.
It returns current timeframe's value, when na value as timeframe parameter in request.*().
As bellow, if it should be na when timeframe is na.
example:
return_value_request_htf1 = na(htf1)?na:request.security(syminfo.tickerid,htf1,timeframe.period)
return_value_request_ltf1 = na(ltf1)?na:request.security(syminfo.tickerid,ltf1,timeframe.period)
higher(tf, scale)
higher: find higher TF from TF string.
Parameters:
tf (string) : default value is timeframe.period.
scale (string) : TF scale in CSV. default is "1,5,15,60,240,1D,1W,1M,3M,12M".
Returns: higher TF string.
lower(tf, scale)
lower: find lower TF from TF string.
Parameters:
tf (string) : default value is timeframe.period.
scale (string) : TF scale in CSV. defalut is "1,5,15,60,240,1D,1W,1M,3M,12M".
Returns: lower TF string.