Pine script provides a range of input functions for different data types, one of which is input.price
. This tutorial will delve deep into the usage and functionality of this function, along with providing a unique use-case example for better understanding.
What is input.price?
input.price
is a function in Pine Script that allows the user to add a price input to the script’s “Settings/Inputs” tab. It’s an effective tool to make your script more interactive and customizable as it enables the end-user to modify the default values to adapt to different situations or strategies. This function returns a floating-point number, representing the value of the input variable.
Syntax of input.price
The syntax for input.price
is as follows:
input.price(defval, title, tooltip, inline, group, confirm) → input float
Arguments
Let’s break down each argument in this function:
defval (const int/float)
: This determines the default value of the input variable proposed in the script’s “Settings/Inputs” tab, where the user can modify it.title (const string)
: This is the title of the input. If not specified, the variable name is used as the input’s title. If the title is specified, but it is empty, the name will be an empty string.tooltip (const string)
: This is the string that will be shown to the user when hovering over the tooltip icon.inline (const string)
: This combines all the input calls using the same argument in one line. The string used as an argument is not displayed. It’s only used to identify inputs belonging to the same line.group (const string)
: This creates a header above all inputs using the same group argument string. The string is also used as the header’s text.confirm (const bool)
: If true, the interactive input mode is enabled, and the selection is done by clicking on the chart when the indicator is added to the chart, or by selecting the indicator and moving the selection after that. This is optional, and the default is false.
Example
Now, let’s dive into an example to illustrate the usage of input.price
:
//@version=5 indicator("input.price example", overlay=true) price1 = input.price(title="Support", defval=140.05, tooltip="Set support price level") price2 = input.price(140.7, title="Resistance", tooltip="Set resistance price level") plot(price1, color=color.green) plot(price2, color=color.red)
In this example, we are using input.price
to set two price levels: support and resistance.
price1
represents the support level, andprice2
represents the resistance level.- We set the default value of
price1
to 140.05 andprice2
to 140.7. Users can change these default values in the script’s “Settings/Inputs” tab. - The
title
parameter is used to label each input clearly. - A
tooltip
is added to provide a brief description for each input. This helps the user understand what each input does when they hover over the tooltip icon. - Finally, the
plot
function is used to plot these price levels on the chart. Support is plotted in green and resistance in red.
Key Takeaways
The input.price
function is a powerful feature in Pine Script that enables user interactivity and customization in a script. By understanding its usage and function, we can create more flexible and user-friendly scripts for a variety of trading strategies.
Conclusion
By providing an input function for price data, Pine Script allows users to customize their trading strategies more effectively. The input.price
function is essential for creating interactive scripts, whether for personal use or for sharing with the TradingView community. Understanding its usage and functionality can help you make the most of your Pine Script coding experience.