Home » Symbol Information Functions » Understanding syminfo.session Function in Pine Script

Understanding syminfo.session Function in Pine Script

Photo of author
Published on

Understanding syminfo.session in Pine Script Version 5

In Pine Script, a programming language primarily used for creating custom indicators and strategies in TradingView, syminfo.session is a built-in variable that holds significant value for traders and developers. This article will delve into its functionality, use cases, and practical applications.

What is syminfo.session?

syminfo.session is a built-in variable in Pine Script that reflects the current session settings of a chart for a specific trading symbol. This variable becomes especially relevant when you’re dealing with markets that have different trading sessions, such as regular and extended sessions.

Key Characteristics

  • Version Compatibility: It is available in Pine Script Version 5.
  • Session Information: It returns the type of session currently set in the “Chart settings/Symbol/Session” field of a TradingView chart.
  • Extended Session Handling: If set to “Extended”, syminfo.session returns “extended” only if both the symbol and the user’s data feed support extended trading sessions.

Application in Pine Script

1. Usage in ticker.new() Function

One of the primary uses of syminfo.session is as an argument in the ticker.new() function. This function is used to create a new ticker id, and by passing syminfo.session as a parameter, the session information of the current chart is applied to the new ticker.

//@version=5
strategy("Session-Based Strategy", overlay=true)

// Checking if the current session is extended
isExtendedSession = syminfo.session == "extended"

// Strategy logic
if isExtendedSession
    // Apply specific logic for extended sessions
else
    // Apply general strategy logic

// Rest of your strategy...

In this example, the new_ticker variable will inherit the session settings of the current chart.

2. Adjusting Strategies According to Session

Traders can also use syminfo.session to adjust their strategies based on different trading sessions. For instance, a strategy might behave differently in extended sessions due to lower liquidity or higher volatility.

//@version=5
indicator("Session Identifier", overlay=true)

// Retrieving the current session setting
currentSession = syminfo.session

// Determining the session type
sessionType = currentSession == "extended" ? "Extended Session" : "Regular Session"

// Displaying the session type on the chart
label.new(bar_index, high, text=sessionType, color=color.blue, textcolor=color.white, style=label.style_label_down)

// Rest of your script...

Code Explanation

  1. Script Setup: We start by defining the script with //@version=5 and create an indicator titled “Session Identifier”.
  2. Retrieving Session Information: The variable currentSession is assigned the value of syminfo.session, which holds the current session setting of the chart.
  3. Session Type Determination: We use a ternary operator to check if currentSession is equal to "extended". If true, sessionType is set to “Extended Session”; otherwise, it’s set to “Regular Session”.
  4. Displaying on Chart: A label is created using label.new to display the session type on the chart. The label’s position is set to the current bar’s index and its high price. The color and style are customized for better visibility.

Key Takeaways

  • syminfo.session is crucial for accessing session-specific information in Pine Script Version 5.
  • Its primary use is in configuring the session parameter for the ticker.new() function.
  • Traders can leverage this variable to tailor their strategies according to different market sessions, such as regular or extended.

Leave a Comment