the input.string
function, is integral to enabling user interactivity in your scripts.
input.string
function permits users to input a string value through your script’s settings. This functionality creates an interactive and highly customizable experience for users, allowing them to alter the input value to suit their needs. In this tutorial, we’ll discuss the usage of input.string
function, its syntax, arguments, and a unique use case to better understand its working.
Syntax and Arguments of input.string
Let’s begin by looking at the basic syntax of input.string
function:
input.string(defval, title, options, tooltip, inline, group, confirm) → input string
The function takes seven arguments:
defval
defval
is the default value of the input variable displayed in the script’s “Settings/Inputs” tab. Users can modify this value according to their requirements. When used with the options
parameter, the value must be one of the options listed.
title
title
is the title of the input. If not specified, the variable name is utilized as the input’s title. If the title is provided, but it’s empty, the name will be an empty string.
options
options
allows you to provide a list of string values from which the user can select.
tooltip
tooltip
is the string displayed to the user when they hover over the tooltip icon. It can provide additional information or guidance about the input.
inline
inline
is used to combine all the input calls using the same argument in one line. The string used as an argument is not displayed. It’s used only to identify inputs belonging to the same line.
group
group
is used to create a header above all inputs using the same group argument string. This string is also used as the header’s text.
confirm
confirm
is a boolean value. If true, the user will be asked to confirm the input value before the indicator is added to the chart. Its default value is false.
Unique Use Case of input.string
Here is a practical example showcasing how input.string
function can be used in a unique context:
//@version=5 indicator("User Message on Trend Reversal", overlay=true) defval = "Trend Reversal Detected!" i_text = input.string(defval, "Alert Message") var float prevClose = na if barstate.isconfirmed if close > prevClose label.new(bar_index, low, i_text, color=color.green , size= size.large , style = label.style_label_up) else if close < prevClose label.new(bar_index, high, i_text, color=color.red , size= size.large) prevClose := close
In this example, a custom alert message is displayed on the chart whenever a trend reversal is detected. The user can modify this alert message through the script’s settings, thanks to input.string
.
defval
is set to “Trend Reversal Detected!”, which will be the default alert message.i_text
takes the user’s input as the alert message, with the default value set todefval
.prevClose
is used to track the previous closing value of the bar.- The script checks for a trend reversal at the close of each bar and displays the user’s alert message on the chart.
Key Takeaway
The input.string
function in Pine Script enables users to interact with your scripts by allowing them to input a string value. It enhances the adaptability of your scripts, making them more user-friendly and flexible to individual needs.
Conclusion
In summary, understanding and using input.string
effectively can greatly improve the user’s experience with your scripts. It’s a powerful tool for script developers that facilitates user interaction and customization. Happy coding!