MTF Selection Framework - PineCoders FAQ

Our MTF Selection Framework allows Pine coders to add multi-timeframe capabilities to their script with the following features:

► Timeframe selection
The higher timeframe can be selected using 3 different ways:
• By steps (60 min., 1D, 3D, 1W, 1M, 1Y).
• As a multiple of the current chart's resolution, which can be fractional, so 3.5 will work.
• Fixed.

► Non-repainting or Repainting mode can be selected.

► Smoothing of the HTF line
Can be turned on/off and a smoothing factor allows the user to select the degree of smoothing he requires.

The framework is used here to create a higher timeframe version of a simple RSI line, but it can be used to access HTF information for almost any signal.

Functions used

Converts the current timeframe.multiplier plus the TF into minutes of type float.
• In Pine, the timeframe.multiplier is an integer representing the resolution, but a value of 1 can mean one day or one minute. This function converts that information in a standard fractional float minutes format that can then be used by the other functions in the framework.
• If the chart's current resolution is 15 seconds, the function will return 0.25. If the chart's resolution is one day, it will return 1440.

Returns resolution of _resolution period in minutes.
• This function does the same as f_resInMinutes(), but on the target resolution supplied as a parameter in the timeframe.period string format.

Given a current resolution in fractional float minutes, returns its corresponding stepped HTF in the timeframe.period string format.
• This allows the implementation of the step HTF selection mode.

f_multipleOfRes(_res, _mult)
Given a current resolution in fractional float minutes and a fractional multiplier, returns a multiple of the resolution as a string in "timeframe.period" format usable with "security()".
• A multiple like 3.5 is allowed.
• Note that with seconds resolutions, the result returned is constrained by the discrete seconds resolutions available on TV.

f_htfLabel(_txt, _y, _color)
Used to display a label showing either:
• A warning when the chart's resolution is not lower than the HTF.
• The HTF resolution currently used.
The y position used to position the label will require adaptation to the signal you are using. For use in "overlay = true" mode, a technique that works well is commented out in the code.

版本注释: Updated comments and label function.
版本注释: Fixed alignment in code.
版本注释: Changed variable name in label function.
版本注释: Optimized f_tfResInMinutes(_resolution). Thanks to MichelT!
版本注释: Typo. Thx scarf!
版本注释: .
• Added steps to the shorter TFs in f_resNextStep(). The steps are now: 15 min., 60 min., 4H, 1D, 3D, 1W, 1M, 1Y.
• Also added a compact version of the functions at the end of the script, which uses only 7 lines.
版本注释: .
Added one function:
Given a resolution in fractional float minutes, returns it as a string in "timeframe.period" format usable with "security()".
版本注释: Added 2 functions:
They return the theoretical and the average number of chart bars (the dilation) in the higher resolution. The average number will vary because of non-trading period or irregularities in datasets.
从常用脚本中删除 添加到常用脚本
Tools and ideas for all Pine coders:
Pine Wizards:
Our Pine FAQ & Code:
Get Pine news and tips on Telegram:


LucF, A quick question as I am struggeling with the smoothing factor. Right now you have somekind a an automatic smooth factor defined in line 123 (vHtfSmoothLen = max(2, rsiLength / vHtfSmooothFact))
But would it be possible to use the timeframe multiplier for this? Something like f_multipleOfRes(vResInMinutes, vHtfType2) / timeframe.period ???

Right now the smoothing isn't that great
@GeVe, You can make the smoothing factor whatever you want. Keeping it of the "simple int" type will make it usable as a length with `ema()`, so will avoid having to use a special averaging function able to work with a "series" period. If your objective is to adapt smoothing to the mode 2 type of selection using a multiple of the current TF, you could try just using that multiple as is, like so:
 vHtfSmoothLen = int(max(2, vHtfType2))
If you wanted to make the smoothing length independent of the MTF selection mode and just use the ratio of the HTF/TF, regardless of how the HTF was calculated, then you could use something like:
vHtfSmoothLen = int(max(2, f_tfResInMinutes(vHtf)/vResInMinutes))
Sma(src,p) => a = cum(src), (a - a[max(p,0)])/max(p,0)
rHtf := vHtfOn and vHtfSmooth ? Sma(Sma(Sma(rHtf, vHtfSmoothLen), vHtfSmoothLen), vHtfSmoothLen) : rHtf
This uses alexgrover's `Sma()` function which accepts a series value for its period. It's lifted from here:
GeVe PineCoders
@PineCoders, This was exactly what I was looking for Luc. It's magic. Thanks a lot for your crispy explanation
Looks nice! Is it possible to plot warning text if the user selects a TF lower than the chart TF?
PineCoders coininvestor
@coininvestor, Yes. Look at lines 117-118.
+1 回复
Beautiful work my friend
The new "Volatility Stop MTF" published by the @TradingView account uses this framework, btw. It demonstrates how to use "security()" in conjunction with a function returning a tuple (more than one result).
LucF, quick question,

When you need to code for each variable in your script 2 securitycalls() to get it into HigherTimeframe.... Then you bump into the problem of too many securitycall() in your script.

Do you have an idea how to prevent this,.... so is there a more generic manner??

==> talking about this line: ..... rHtf = not vHtfOn ? r : vHtfRepaints ? security(syminfo.tickerid, vHtf, r) : security(syminfo.tickerid, vHtf, r, lookahead = barmerge.lookahead_on)
@GeVe, The only ways I know of will be:
1. Eliminate one of the 2, so either hard-code whether you are repainting or not.
2. Use this structure to control repainting:
// —————————— Alternate but more complex way of avoiding repainting using barmerge.lookahead_off.
// It is also possible to avoid repainting using "lookahead_off" (the default in v3 and v4), but we then need to include logic 
//   to use different "security()" calls for historical and real-time bars, so it's more complicated:
//      1. for the REALTIME bar we use:  security(syminfo.tickerid, higherTf, data[1])
//      2. for HISTORICAL bars we use:   security(syminfo.tickerid, higherTf, data)[1]
indexHighTf = barstate.isrealtime ? 1 : 0
indexCurrTf = barstate.isrealtime ? 0 : 1
sec2ff = security(syminfo.tickerid, higherTf, data[indexHighTf])[indexCurrTf]

which is taken from here:
首页 股票筛选器 外汇筛选器 加密货币筛选器 财经日历 如何运作 图表功能 价格 推荐朋友 网站规则 帮助中心 网站 & 经纪商解决方案 插件 图表解决方案 轻量图表库 博客 & 新闻 Twitter
概览 个人资料设置 账户和账单 推荐朋友 我的客服工单 帮助中心 已发表观点 粉丝 正在关注 私人消息 在线聊天 退出