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

Understanding syminfo.root Function in Pine Script

Photo of author
Published on

In this tutorial, we delve into the syminfo.root attribute in Pine Script Version 5, exploring its utility, syntax, and application in the context of structured tickers like those of futures.

What is syminfo.root?

Definition and Use

syminfo.root is a built-in variable in Pine Script that retrieves the prefix of the ticker symbol of the currently selected financial instrument. This prefix is particularly useful for dealing with structured tickers, such as those found in futures contracts.

For example:

  • In the ticker “ES1!”, syminfo.root would return “ES”.
  • In the ticker “ZW1!”, it would return “ZW”.

This feature is crucial when you need to write scripts that adapt to different instruments or when dealing with series of related futures contracts.

Syntax and Example

Here’s a simple example to illustrate its use:

//@version=5
indicator("Root Symbol Example", overlay=true)
rootSymbol = syminfo.root
label.new(bar_index, high, text="Root Symbol: " + rootSymbol)
Example

Walkthrough of the Example Code

Let’s break down the code snippet to understand each line:

  1. //@version=5: This line specifies that the script is written in Pine Script Version 5.
  2. indicator("Root Symbol Example", overlay=true): This line declares a new indicator named “Root Symbol Example”. The overlay=true parameter ensures that this indicator is drawn over the main price chart.
  3. rootSymbol = syminfo.root: Here, we assign the value of syminfo.root to the variable rootSymbol. This retrieves the root symbol (prefix) of the current ticker.
  4. label.new(bar_index, high, text="Root Symbol: " + rootSymbol): This line creates a new label on the chart. The label is positioned at the current bar index and at the high price level. It displays the text “Root Symbol:” followed by the value of rootSymbol.

Key Features and Takeaways

  • Functionality: syminfo.root provides the prefix of the ticker symbol, which is essential for scripts that need to adapt to different futures contracts or instruments.
  • Syntax: It’s a read-only variable and does not require any parameters.
  • Application: Useful in creating dynamic scripts for futures, recognizing the underlying asset of the instrument.
  • Version-Specific: This attribute is specific to Pine Script Version 5, ensuring compatibility and up-to-date functionality.

By understanding and utilizing syminfo.root, Pine Script programmers can create more versatile and adaptive scripts, especially when dealing with the complexities of futures contracts and structured tickers. This knowledge is fundamental for anyone looking to deepen their understanding of Pine Script and its application in financial markets.

Leave a Comment