Easily changing a variable’s value is possible with an input option, like the numerical input options. But how can we add a true/false checkbox to a script’s settings?

In this article:

Adding a true/false input option to a TradingView script

When we code a trading strategy or indicator in TradingView, we use add variables whose value is set manually in the script’s options. This allows us to easily change certain values (like the length of a moving average or how a strategy closes its positions) without having to edit the script’s source code.

We create such variables by adding an input to a script with the input() function (Pine Script Language Tutorial, n.d.). That function not only creates a setting in the script’s option, it also returns the value that the option is currently set to. We need to store that value in a variable (TradingView, n.d.).

We can create several kind of inputs in TradingView, like a text box or a numerical input. A checkbox is also possible. For that we need to set the input() function’s type argument to bool (Pine Script Language Tutorial, n.d.). This bool type is short for Boolean, a programming data type that can be true or false and that’s named after George Boole (Wikipedia, 2015). Let’s take a closer look at creating a bool input option.

Syntax for a true/false checkbox input

We create a checkbox to a TradingView script with the following default pattern:

boolVariable = input(title, type=bool, defval, confirm)

The input() function can use the following arguments when it’s used with bool (TradingView, n.d.):

Argument Description
title This optional argument sets the title that’s placed before the input. It accepts a string value (like title="Plot arrows").
type A required argument that needs to be set to bool to create a true/false checkbox.
defval This required argument sets the input’s default value. For Boolean inputs, we set defval either to true (defval=true) or false (defval=false). A default value allows the script to always start calculating.
confirm An optional argument that, when set to true, asks the user to confirm the input setting(s) before the script is added to the chart. confirm defaults to false, so when omitted we won’t be asked to confirm the input options.
Note: The input() function returns the current value that the input option is currently set to. This value always needs to be assigned to a variable (TradingView, n.d.).

Boolean inputs in TradingView look like:

Example of checkbox inputs in TradingView

Now, let’s look at two programming examples that create bool inputs in TradingView.

Using TradingView’s true/false input to highlight inside bars

One way to use the bool input is to create a checkbox that enables (or disables) drawing shapes above inside bars. An inside bar is a bar that forms completely inside the trading range of the preceding bar, and usually signals a change in trend (Pring, 2002).

Before looking at the code, here’s the checkbox that it creates:

Example script with checkbox input in TradingView

The example’s code is:

study(title="True/false bool input", overlay=true)

plotCross = input(title="Highlight inside bars", type=bool, defval=true)

insideBar = (plotCross and high < high[1] and low > low[1])

plotshape(series=insideBar, style=shape.xcross, 
     location=location.abovebar, color=blue)

We start with the study() function to set the indicator’s title and overlay settings. Then we add a bool input that’s named "Highlight inside bars" and has a true default value (defvalue). This checkbox’s value is stored in the plotCross variable.

The insideBar variable that we create next has its value set by a true/false condition that checks three things. The first part checks whether the plotCross input variable returns true. Then we evaluate whether the current bar’s high is less than (<) the previous high, which we retrieve with the history referencing operator (high[1]). The third part of the condition is whether the current bar’s low is greater than (>) the previous low (low[1]).

Since those three expressions are combined with the logical and operator, insideBar only holds true when all three are true also. This means that insideBar is true when the input checkbox is enabled and when the bar’s range falls inside the range of the previous bar.

We end the example with plotshape(), a function that draws visual shapes on the chart whenever its series argument is true for a bar (TradingView, n.d.). By setting that argument to the insideBar variable, it draws a shape whenever there’s an inside bar. We set the shape’s type to a small ‘x’ (shape.xcross) and place it above the bar (location.abovebar).

When we add this script to a chart, its checkbox is enabled by default and it looks like this:

TradingView example that uses true/false inputs

And turning off the checkbox removes the shapes:

TradingView example that uses true/false inputs

Colouring the chart’s background with a true/false input

The next example colours the chart depending on an exponential (EMA) and simple moving average (SMA). These are the inputs that the script makes:

Example of input option in TradingView

The programming code is the following:

study(title="True/false input", overlay=true)

// Inputs
colourBg  = input(title="Colour background", type=bool, defval=true)
emaLength = input(title="EMA length", type=integer, defval=20)
smaLength = input(title="SMA length", type=integer, defval=50)

// Calculate & plot moving averages
emaValue = ema(close, emaLength)
smaValue = sma(close, smaLength)

plot(series=emaValue, color=orange, linewidth=2)
plot(series=smaValue, color=blue, linewidth=2)

// Colour background
backgroundColour = (colourBg and emaValue > smaValue) ? green :
     (colourBg and emaValue < smaValue) ? red :
bgcolor(color=backgroundColour, transp=80)

We first use study() to set the script’s settings. Then we create three inputs: one bool and two numerical integer inputs. The colourBg input variable has a default value of true, while the emaLength and smaLength input variables initially hold values of 20 and 50.

Then we calculate the moving averages. The EMA is computed with ema(), a function that requires a series of values and a length in number of bars (TradingView, n.d.). We set it to calculate on closing prices (close) for the number of bars set by the input (emaLength). The SMA is calculated likewise with the sma() function. We store the result of both functions in the emaValue and smaValue variables.

We then plot a series of data on the chart with plot() (TradingView, n.d.). The first plot() statement has its series argument set to the emaValue variable. The line with those EMA values is coloured orange and the linewidth of 2 creates a line that’s slightly thicker than normal. The second plot() statement plots the SMA values on the chart with a blue line.

Next we make the backgroundColour variable and give it a value with the help of two conditional ternary operators (?:). That operator evaluates a condition and returns its second value when the condition is true and its third value when it’s false (Pine Script Language Tutorial, n.d.). By using two ?: operators we can evaluate two conditions.

We first check if the colourBg input variable is true and if the current bar’s emaValue value is greater than (>) the smaValue value. When that evaluates to true, green is stored in the backgroundColour variable by the conditional operator. When it’s false, the second condition is processed. That condition checks if colourBg is true and if emaValue is less than (<) smaValue. If that’s the case, red is put into the backgroundColour variable. When neither condition is true (which also means that the input checkbox is unchecked), then na is stored in the variable. That value act as the default colour (Pine Script Language Tutorial, n.d.), meaning that setting a colour to na has no effect.

What this piece of code means in normal English is the following. When the "Colour background" input is enabled, then the backgroundColour variable is green when the EMA is above the SMA and red when the EMA is below the SMA. And that variable holds na (default colour) when the "Colour background" checkbox is disabled.

The example’s last line colours the chart’s background with bgcolor() (Pine Script Language Tutorial, n.d.). We set that function’s color argument to the backgroundColour variable and its transp argument, which sets the transparency and ranges from 0 (not transparent) to 100 (fully transparent) (TradingView, n.d.), is given a value of 80 for a reasonable transparent background colour.

When we add this example script to the chart with its default settings, it looks like:

Colouring the background in a TradingView script

Turning off the ‘Colour Background’ input checkbox changes the chart to:

Using a TradingView input to disable chart colouring

To learn more about the other inputs in TradingView, see adding numerical inputs to a script, creating a text input, and working with TradingView’s source input.


Inputs are added to a script with input(). When we set that function’s type argument to bool, a checkbox is created in the script’s options. The variable in which we store the result returned by input() then holds true or false depending on the state of the checkbox. By using that variable in the script we can use the checkbox’s current value. That way we don’t have to edit the script’s source code to change a value.


Pine Script Language Tutorial (n.d.). Retrieved on August 13, 2015, from https://docs.google.com/document/d/1sCfC873xJEMV7MGzt1L70JTStTE9kcG2q-LDuWWkBeY/

Pring, M.J. (2002). Technical Analysis Explained (4th edition). New York, NY: McGraw-Hill.

TradingView (n.d.). Script Language Reference Manual. Retrieved on September 30, 2015, from https://www.tradingview.com/study-script-reference/

Wikipedia (September 20, 2015). Boolean data type. Retrieved on September 29, 2015, from https://en.wikipedia.org/wiki/Boolean_data_type