Home » Input Functions » Input.bool function in Pine Script

Input.bool function in Pine Script

Photo of author
Published on

The input.bool function is a built-in function in Pine Script that enables you to add an input option in the form of a checkbox to the “Inputs” tab of your script’s Settings. This feature facilitates configurability in your scripts by offering choices to the users of your script.

In simpler terms, the input.bool function adds a checkmark to the script’s inputs, providing a boolean variable (true or false) that you can use to control certain aspects of your script. This function can significantly enhance the versatility and user-friendliness of your scripts.

The general syntax of input.bool is as follows:

input.bool(defval, title, tooltip, inline, group, confirm) → input bool

Arguments of input.bool

Let’s break down the arguments of the input.bool function:

defval (const bool)

defval determines the default value of the input variable proposed in the script’s “Settings/Inputs” tab, from where the user can change it. It’s a boolean value (true/false).

title (const string)

title is the name of the input that appears to the user. 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)

tooltip is a string that is displayed to the user when they hover over the tooltip icon. It’s a convenient way to provide additional information or clarification about the input.

inline (const string)

inline combines all the input calls using the same argument in one line. The string used as an argument is not displayed. It is only used to identify inputs belonging to the same line.

group (const string)

group 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)

confirm, if set to true, will prompt the user to confirm the input value before the indicator is added to the chart. The default value is false.

A Practical Example of input.bool Function

Here is a sample code demonstrating the use of input.bool function:

//@version=5
indicator("input.bool example", overlay=true)
i_switch = input.bool(true, "Display line", "Check to display the line", "", "", false)
plot(i_switch ? close : na, color=color.red)
input.bool function

This script plots the closing prices of a financial instrument as a red line on the chart. The line can be toggled on and off using the “Display line” checkbox in the script’s “Inputs” settings.

  • //@version=5 specifies that we’re using version 5 of Pine Script.
  • indicator("input.bool example", overlay=true) defines a new indicator with a given title and sets it to overlay the price data on the chart.
  • i_switch = input.bool(true, "Display line", "Check to display the line", "", "", false) is where we use the input.bool function. The user is presented with a checkbox titled “Display line”. The default state of this checkbox (defval) is set to true, meaning the line will be displayed by default. A tooltip provides some instructions to the user. The remaining arguments are left empty for this example, and the confirm is set to false.
  • plot(i_switch ? close : na, color=color.red) uses a conditional operator to decide what to plot. If i_switch is true (i.e., the checkbox is checked), it plots the closing prices; otherwise, it plots na, which means nothing is plotted. The color of the line is set to red.

Key Takeaway

The input.bool function is an essential tool in Pine Script, allowing you to add configurable options to your scripts. It increases the versatility of your scripts, making them more useful and user-friendly. By understanding and implementing input.bool, you can enhance the functionality and adaptability of your trading scripts.

Conclusion

In this tutorial, we delved into the details of the input.bool function in Pine Script. We explored its purpose, arguments, and practical usage. By adding user inputs in the form of checkboxes, input.bool offers a powerful way to improve the usability and flexibility of your trading scripts. Happy scripting!

Leave a Comment