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)
Here’s an explanation of each line of code:
//@version=5
: This line specifies the version of Pine Script that the script is written in.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.i_sym = input.symbol("NASDAQ:AAPL")
: Here, an input symbol “NASDAQ:AAPL” is defined and assigned to the variablei_sym
.pref = syminfo.prefix(i_sym)
: Thesyminfo.prefix
function extracts the prefix “NASDAQ” from the symbol and assigns it to the variablepref
.tick = syminfo.ticker(i_sym)
: Thesyminfo.ticker
function retrieves the ticker part “AAPL” from the symbol and assigns it to the variabletick
.t = ticker.new(pref, tick, session.extended)
: This line uses theticker.new
function to create a new ticker identifier using the prefixpref
, tickertick
, and session typesession.extended
. This identifier is stored in the variablet
.s = request.security(t, "1D", close)
: Using therequest.security
function, we request the closing price of the security identified byt
for a 1-day period and assign it to variables
.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.