How to use multi-timeframe (MTF) functionality?
The MTF functionality is a feature that allows you to change any indicator’s timeframe with just two clicks in Inputs, using the Timeframe dropdown.
Pine coders can use the same timeframe parameter we use in our built-in indicators in their own scripts. By simply adding it to a script's indicator declaration statement, coders now have an easy way to add MTF functionality to scripts and let users decide the timeframe they want the indicator to run on.
Using the timeframe parameter will automatically add a Timeframe input field to your script's Inputs. The parameter's default value can be any valid timeframe string. If, however, it is not included in the choices available from the field's dropdown menu, the default Same as symbol will appear as the default value in the field. Use the empty string ("") to represent the chart's timeframe.
Let’s say, for example, that you want to use a chart with a timeframe of 5m, but want it to show a Moving Average based on a timeframe of 1D. Previously, you could do this by using the request.security function:
//@version=5
indicator(title="Moving Average", shorttitle="MA with security", overlay=true)
len = input.int(9, minval=1, title="Length")
src = input.source(close, title="Source")
out = ta.sma(src, len)
tf = input.timeframe(title="Timeframe", defval="1D")
s1 = request.security(syminfo.tickerid, tf, out, gaps=barmerge.gaps_on)
plot(s1, color=color.red)
Now, however, you’ll just need to add timeframe="D" to your indicator call:
//@version=5
indicator(title="Moving Average", shorttitle="MA with timeframe", overlay=true, timeframe="D", timeframe_gaps=true)
len = input.int(9, minval=1, title="Length")
src = input.source(close, title="Source")
offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)
out = ta.sma(src, len)
plot(out, color=color.blue, title="MA", offset=offset)
The timeframe parameter provides an easy way to add MTF functionality to relatively simple scripts. The addition of timeframe_gaps=true is optional and similar in principle to the `gaps` parameter in the `request.security()` function. More complex Pine scripts will still need request.security to implement advanced calculations using higher timeframe information.
Note that the value of timeframe.* variables will represent the timeframe the script is currently running on as determined by the value of the Timeframe field in the script's Inputs, regardless of the chart's timeframe. Also note that since higher time frame values contain gaps when the timeframe parameter is used in a script (unless timeframe_gaps is set to false), the timeframe.* variables will also contain n/a values between time transitions. This behavior is expected. You can learn more about it in our Help Center.