Pine Script, the scripting language for creating custom technical analysis indicators and strategies on the TradingView platform, offers various ways to access and use session information. This article will guide you through understanding and utilizing session information in Pine Script, focusing on Version 5 of the language.
What is Session Information in Pine Script?
Session information in Pine Script pertains to the data related to the trading sessions of a financial instrument. This can include whether the market is in a regular or extended session and details about the trading session a particular bar belongs to.
The syminfo.session
Built-in Variable
- Usage: The
syminfo.session
variable provides information about the current session settings of the chart for a specific symbol. It returns a value that can either besession.regular
orsession.extended
. - Determination of Value:
- The value depends on the “Chart settings/Symbol/Session” field.
- If set to “Extended,”
syminfo.session
will return “extended,” provided the symbol and the user’s data feed support extended sessions.
- Application: This variable is typically used where a session type is necessary, such as the session parameter in
ticker.new()
.
Session State Built-ins in Version 5
- Introduction: Version 5 of Pine Script introduces built-in variables that provide detailed information about the trading session of a bar.
- Examples: These built-ins can include variables that indicate if the bar is in a regular session, an extended session, or if it’s a session break.
Example Use Case in Pine Script
Let’s create a simple script that demonstrates how to use syminfo.session
and session state built-ins in Pine Script Version 5.
//@version=5 indicator("Revised Session Info Example", overlay=true) // Define regular session hours (example: 9:30 AM to 4:00 PM EST) regularSessionStart = timestamp("GMT", year, month, dayofmonth, 09, 30) regularSessionEnd = timestamp("GMT", year, month, dayofmonth, 16, 00) // Check if the current bar is within the regular session isRegularSession = (time >= regularSessionStart and time <= regularSessionEnd) // Visual indication of regular session bars bgcolor(isRegularSession ? color.green : na) // Plotting a simple moving average for illustration plot(ta.sma(close, 14), color=color.blue)
Walkthrough of the Code
- Version Declaration: We start by declaring the use of version 5 of Pine Script.
- Indicator Setup: The
indicator
function is used to set up the script’s properties, like its name and whether it should be overlaid on the price chart. - Using
syminfo.session
:- We store the current session type in
currentSession
. - We then check if the current session is extended using a simple comparison.
- We store the current session type in
- Displaying Session Information:
- We use
bgcolor
to change the background color of extended sessions for easy identification.
- We use
- Using Session State Built-ins:
- We create a boolean variable
isRegularTradingHour
to check if a bar is within regular trading hours.
- We create a boolean variable
- Highlighting Bars: Bars during regular trading hours are colored green.
- Additional Illustration: A simple moving average (SMA) is plotted as an example of an indicator that can be used alongside session information.
Key Features and Takeaways
- Session Type Identification:
syminfo.session
allows scripts to adapt their behavior based on the current session type. - Enhanced Trading Hour Analysis: Version 5’s session state built-ins enable more nuanced analysis based on the trading session of each bar.
- Versatile Applications: These features are useful for strategies and indicators that need to differentiate between regular and extended market hours.
- Customizable Visuals: The script can be enhanced with visual elements like background and bar colors to indicate session types.