The input
function is an essential part of Pine Script. It allows script developers to provide customizable settings to script users, enhancing the interactivity of the script and the overall user experience. This function auto-detects the type of the argument utilized for ‘defval’ and accordingly selects the appropriate input widget. This blog post aims to explore the input
function in detail and provide a unique use case example, explaining each line of code.
Function Signature
The input
function is available in multiple variations depending on the input type required.
input(defval, title, tooltip, inline, group) → input bool input(defval, title, tooltip, inline, group) → input color input(defval, title, tooltip, inline, group) → input int input(defval, title, tooltip, inline, group) → input float input(defval, title, tooltip, inline, group) → input string input(defval, title, inline, group, tooltip) → series float
Parameters
Each parameter provides a specific functionality:
defval
determines the default value of the input variable in the script’s “Settings/Inputs” tab. Users can change this value.title
is the title of the input. By default, the variable name becomes the input’s title, but specifying a title overrides this.tooltip
is a string displayed to the user when hovering over the tooltip icon.inline
combines all the input calls using the same argument in one line. It doesn’t show the argument string, only identifying inputs belonging to the same line.group
creates a header above all inputs using the same group argument string, which also serves as the header’s text.
Unique Use Case: Customizable Moving Average
Let’s consider an example where we implement a moving average that users can customize. The code and its explanation are as follows:
//@version=5 indicator("Customizable MA", overlay=true) length = input(14, "Length of MA") source = input(close, "Source of Calculation") ma_type = input(title="Type of Moving Average", defval="SMA", options=["SMA", "EMA"]) // Calculate the moving average based on user input ma_value = ma_type == "SMA" ? ta.sma(source, length) : ta.ema(source, length) // Plot the moving average plot(ma_value, title="Moving Average", color=color.blue)
- The
indicator
function sets the title and overlay for the script. - The
input
function gathers user inputs for the moving average’s length, the source of calculation (such asclose
oropen
), and the type of moving average (simple or exponential). - Then, the moving average is calculated based on user input using conditional statements.
- Finally, the
plot
function is used to visualize the moving average on the chart.
Key Takeaways
The input
function in Pine Script is a powerful tool for enhancing script interactivity and providing customization options to users. Understanding its parameters (defval
, title
, tooltip
, inline
, group
) is vital to use it effectively. A unique use case demonstrates how you can use these options to create a fully customizable moving average indicator.
Conclusion
Mastering the input
function in Pine Script is key to developing sophisticated, interactive scripts. The more you understand and apply these concepts, the better you’ll be at creating custom scripts that cater to your specific trading strategy or analytical needs.