Home » Symbol Information Functions » syminfo.prefix function in Pine Script

syminfo.prefix function in Pine Script

Photo of author
Published on

Today, we’ll delve into a particular function of Pine Script called syminfo.prefix. This function has broad utility in accessing and managing symbol information from exchanges.

What is syminfo.prefix?

The syminfo.prefix function is used to return the exchange prefix of a particular trading symbol. For instance, if you input the symbol “NASDAQ:AAPL”, the function will return “NASDAQ”. The function signature looks like this:

syminfo.prefix(symbol) → simple string
syminfo.prefix(symbol) → series string

Let’s look at an example and break down how the syminfo.prefix function is used. Below is a simple example:

//@version=5
indicator("syminfo.prefix fun", overlay=true)
i_sym = input.symbol("NASDAQ:AAPL")
pref = syminfo.prefix(i_sym)
tick = syminfo.ticker(i_sym)
t = ticker.new(pref, tick, session.extended)
s = request.security(t, "1D", close)
plot(s)
syminfo.prefix function

Here’s an explanation of each line of code:

  1. //@version=5: This line specifies the version of Pine Script that the script is written in.
  2. indicator("syminfo.prefix fun", overlay=true): This line creates an indicator with the title “syminfo.prefix fun” and sets the overlay to true, which displays the indicator directly on the price chart.
  3. i_sym = input.symbol("NASDAQ:AAPL"): Here, an input symbol “NASDAQ:AAPL” is defined and assigned to the variable i_sym.
  4. pref = syminfo.prefix(i_sym): The syminfo.prefix function extracts the prefix “NASDAQ” from the symbol and assigns it to the variable pref.
  5. tick = syminfo.ticker(i_sym): The syminfo.ticker function retrieves the ticker part “AAPL” from the symbol and assigns it to the variable tick.
  6. t = ticker.new(pref, tick, session.extended): This line uses the ticker.new function to create a new ticker identifier using the prefix pref, ticker tick, and session type session.extended. This identifier is stored in the variable t.
  7. s = request.security(t, "1D", close): Using the request.security function, we request the closing price of the security identified by t for a 1-day period and assign it to variable s.
  8. plot(s): Lastly, we plot the closing prices on the chart.

Key Takeaways

The syminfo.prefix function plays an essential role when working with symbols in Pine Script. It helps extract the exchange prefix from a given symbol, a task that often comes up when creating new ticker identifiers or requesting security data.

Understanding this function, along with others such as syminfo.ticker and request.security, enables you to effectively manipulate and manage symbol information in Pine Script.

Conclusion

The syminfo.prefix function provides a straightforward way of managing symbols and their associated exchange information. Understanding its use is critical for writing efficient and effective Pine Scripts for trading strategies and technical analysis tools. Its primary use in conjunction with other functions such as ticker.new and request.security also demonstrates the interconnected nature of Pine Script functions, providing you with powerful tools to extract, manipulate, and utilize trading data.

Leave a Comment