One function that brings flexibility and customization to Pine Scripts is the input.symbol
function. This function lets script users select a specific trading symbol via the script’s settings. This tutorial will delve into its usage, syntax, and a unique use case example.
Understanding input.symbol
Definition and Syntax
input.symbol
is a function in Pine Script that adds an input field to the Inputs tab of your script’s Settings, thus permitting users to select a specific trading symbol. The chosen symbol is returned as a string, paired with its exchange prefix.
The general syntax is as follows:
input.symbol(defval, title, tooltip, inline, group, confirm) → input string
Function Arguments
The input.symbol
function accepts the following arguments:
defval
(const string): This sets the default value of the input variable, which the user can modify in the script’s “Settings/Inputs” tab.title
(const string): This is the title of the input. If not specified, the variable name is used. If specified but empty, the name will be an empty string.tooltip
(const string): This string is shown to the user when they hover over the tooltip icon.inline
(const string): This argument combines all input calls in one line, using the same argument. It’s not displayed and is only used to identify inputs on the same line.group
(const string): This creates a header above all inputs using the same group argument string.confirm
(const bool): If true, the user will be asked to confirm the input value before the indicator is added to the chart. By default, this is false.
Diving Into an Example
Let’s take a look at an example script utilizing input.symbol
.
//@version=5 indicator("input.symbol example", overlay=true) i_sym = input.symbol("DELL", "Select Symbol") s = request.security(i_sym, 'D', close) plot(s)
Now, let’s break this script down:
//@version=5
: Specifies the version of Pine Script used.indicator("input.symbol example", overlay=true)
: Theindicator
function sets the title of the script and whether it will overlay the existing chart (true in this case).i_sym = input.symbol("DELL", "Select Symbol")
: Theinput.symbol
function creates an input field with a default value of “DELL”. The title for this input field is “Select Symbol”.s = request.security(i_sym, 'D', close)
: Therequest.security
function retrieves the ‘D’ (daily) closing prices of the selected symbol. The symbol is referenced through thei_sym
variable.plot(s)
: This line plots the retrieved closing prices onto the chart.
Remember that the result of the input.symbol
function should always be assigned to a variable. In our case, it’s i_sym
.
Key Takeaways
The input.symbol
function in Pine Script is a powerful tool to create customizable scripts. It allows users to specify a trading symbol, enriching the flexibility of the script. By understanding the syntax and arguments, script writers can better design dynamic and user-friendly indicators or strategies.
Conclusion
To sum up, input.symbol
is an indispensable function in Pine Script, bringing interactivity and adaptability to the scripts. The grasp of this function and its proper utilization can greatly improve the versatility of your scripts, leading to a more engaging and personalized user experience. Whether you are developing a strategy or an indicator, keeping input.symbol
in your toolbox is essential for script customization.