On TradingView you can access extended hours sessions byright-clicking on a chart and choosing Settings →Symbol → Extended Hours (Intraday only).There are two types of sessions: regular (excluding pre- and post-marketdata) and extended (including pre- and post-market data).Pine scripts may request additional session data using thesecurity function.
The security
function can be called with a symbol name(“EXCHANGE_PREFIX:TICKER”, e.g., “BATS:AAPL”) as its first argument.Used this way, the security
function will return data for the regular session. For example:
//@version=4study("Example 1: Regular Session Data")cc = security("BATS:AAPL", timeframe.period, close, true)plot(cc, style=plot.style_linebr)
If you want the security
call to return extended session data, youmust first use the tickerid functionto build security
call’s first argument:
//@version=4study("Example 2: Extended Session Data")t = tickerid("BATS", "AAPL", session.extended)cc = security(t, timeframe.period, close, true)plot(cc, style=plot.style_linebr)
Notice that the previous chart’s gaps in the script’s plot are now filled. Also keep in mindthat the background coloring on the chart is not produced by our example scripts;it is due to the chart’s settings showing extended hours.
The first argument of the tickerid
function is an exchange prefix (“BATS”) and thesecond argument is a ticker (“AAPL”). The third argument specifies the typeof the session (session.extended
or session.regular
). So Example 1could be rewritten as:
//@version=4study("Example 3: Regular Session Data (using tickerid)")t = tickerid("BATS", "AAPL", session.regular)cc = security("BATS:AAPL", timeframe.period, close, true)plot(cc, style=plot.style_linebr)
If you want to request the same session specification used for the chart’s mainsymbol, omit the third argument; it is optional. Or, if you want your code toexplicitly declare your intention, use the syminfo.session
built-in variable as the third argument to tickerid
function, as itholds the session type of the chart’s main symbol:
//@version=4study("Example 4: Same as Main Symbol Session Type Data")t = tickerid("BATS", "AAPL", syminfo.session)cc = security(t, timeframe.period, close, true)plot(cc, style=plot.style_linebr)