The period variable returns the time frame of the chart that the script calculates on. That returned string value consists out of the chart interval (like ‘5’ for a 5-minute chart) as well as the resolution type (like ’D’ for a daily chart). We can use the period variable with indicator (study) and strategy scripts.

Example: plot another instrument’s price data in the same resolution

The example indicator below plots price data from another instrument. We use the period variable to make sure that the requested data is in the same time frame as the chart currently uses. But first we use study() to set the indicator properties. We have the indicator use the chart’s left axis so the plotted values don’t skew the chart’s main price axis.

Then we make three input options with the input() function. The first is a symbol search box (symbol) that specifies which instrument to load. The other two input options set the length of the quick and slow simple moving average (SMA). We store all input options in variables to access them later on.

Then we load the price data from that other instrument with the security() function. Here we use the period variable to have that price data match the time frame of the current chart. With the close variable we have security() return closing prices that belong to that instrument and time frame.

Then we calculate that instrument’s moving averages by using those closing prices alongside the quickMALen and slowMALen input variables together with the sma() function. We store those calculated SMA values in variables, and then plot them on the chart with the plot() function. Both SMAs appear as solid lines on the chart, a bit thicker than the default linewidth of 1. We show the faster moving average in orange while the slower one is coloured with teal.

//@version=3
study(title="Reference indicator example", overlay=true, scale=scale.left)

// Input options
otherIns   = input(title="Other Instrument", type=symbol, defval="FX:EURUSD")
quickMALen = input(title="Quick MA", type=integer, defval=12)
slowMALen  = input(title="Slow MA", type=integer, defval=30)

// Calculate the other instrument's MA values
closePrices = security(otherIns, period, close)

fastMA = sma(closePrices, quickMALen)
slowMA = sma(closePrices, slowMALen)

// Plot the data of the SMAs
plot(series=fastMA, color=orange, linewidth=2)
plot(series=slowMA, color=teal, linewidth=3)

The example indicator looks like this on a 30-minute chart:

Plotting another instrument's moving average in TradingView Pine

When we switch the chart’s time frame (like a 2-hour chart as shown below), the example indicator changes with it. That’s because we used the period variable to set the time frame of data to load, and period always returns the chart’s current time frame.

Plotting a moving average dependent on the current chart in TradingView

Notes for period

The period variable returns the resolution of the current chart as a string value. TradingView constructs chart resolution as follows (TradingView Wiki, 2017):

Time frame Example values returned by period
Minute chart "1" for a 1-minute chart, "30" for a 30-minute intra-day chart.
Hourly chart "120" for a 2-hour resolution, "90" signals 1.5 hour resolution, while "240" is a time frame of 4 hours.
Daily chart "D" for a daily chart, "5D" for a 5-day chart resolution.
Weekly chart "W" for a one week chart, and "2W" for a two-week chart time frame.
Monthly chart "M" for a one month chart resolution, while "3M" signals a quarterly chart (3 months), and "12M" a yearly chart.

There are a couple of noteworthy features of how TradingView works with chart resolutions. The lowest time frame is a minute chart while a monthly chart is the highest time frame (TradingView Wiki, 2017). And all intra-day chart resolutions are expressed in a number of minutes.


While the period variable returns a value that indicates both the type of chart (like daily or intra-day) and the chart interval, the interval variable just returns the chart’s time frame interval. A comparison for some of the possible values that these variables can return is (TradingView, n.d.):

Chart resolution period returns interval returns
15 minutes "15" 15
60 minutes "60" 60
2 hours "120" 120
3 hours "180" 180
1 day "D" 1
5 days "5D" 5
1 week "W" 1
2 weeks "2W" 2
1 month "M" 1
3 months "3M" 3
1 year "12M" 12

We typically use the period variable with the tostring() function. That way we have security() load price data with the same chart resolution as our chart currently has.


Whereas period returns the time frame of the chart, the interval variable only returns a number with the resolution interval (like 10 for a 10-minute chart and 1 for a one-day chart). And so compared to period, interval doesn’t contain the type of resolution. That means when interval returns 3, we don’t know if the script calculates on a 3-minute chart, 3-day chart, 3-week chart, or 3-month chart.


With the resolution constant of the input() function we create an input option that allows setting a resolution easily. The kinds of values that such an input option returns are similar to the values of the period variable.

Tips for period

To check the kind of chart an indicator or strategy calculates on, it’s often easier to not use period. That’s because that variable returns a value that includes both the chart interval and type of time frame. Instead, use the following variables to know what chart type the script runs on (TradingView, n.d.):

Variable Description
isintraday Returns true if the current resolution is an intra-day resolution, and false for daily and higher resolutions.
isdwm Returns true when the script runs on a daily, weekly, or monthly chart. Returns false when the script runs on an intra-day chart.
isdaily Returns true if the script operates on a daily resolution, and false otherwise.
isweekly Returns true if the current resolution is weekly, and false for chart time frames higher or lower than that.
ismonthly Returns true if the script calculates on a monthly resolution, and false otherwise.

To get even more information about the chart resolution, combine the above variables with the interval variable. For example, when isdaily is true and interval returns 2, then we know that the chart resolution is 2 days. And when isintraday is true while interval returns 120, the chart’s time frame is a 2-hour intra-day chart.

Summary

Features of period
Name period
Description Returns the time frame (resolution + type) of the chart that the script calculates on.
Category Chart information
Type variable
Returns A string value
Usable in Indicator (study) and strategy scripts
Available since (beta release date) TradingView Pine Version 1 (December 13, 2013)
Related code elements
interval Returns the time frame’s resolution of the chart that the script calculates on.
isdaily Indicates whether the script calculates on a daily chart resolution.
isdwm Indicates whether the script calculates on a daily, weekly, or monthly time frame.
isintraday Indicates whether the script calculates on an intra-day chart resolution.
ismonthly Indicates whether the script calculates on a monthly chart resolution.
isweekly Indicates whether the script calculates on a weekly chart resolution.
ticker Returns the symbol for the instrument that the script currently calculates on.
tickerid Returns the symbol with exchange prefix for the instrument that the script calculates on.

References

TradingView (n.d.). Pine Script Language Reference Manual. Retrieved on October 16, 2017, from https://www.tradingview.com/study-script-reference/

TradingView Wiki (2017, April 29). Context Switching, The ‘security’ Function. Retrieved on October 16, 2017, from https://www.tradingview.com/wiki/Context_Switching,_The_%E2%80%98security%E2%80%99_Function