PineScript – Lesson 3: Basic User Inputs
How to Work With User Inputs
Most indicators you make will not be suited to permanently hard-coded variables. Often you will want to change certain settings on the fly such as the lookback period, the timeframe, overbought/oversold thresholds etc.
Luckily PineScript makes it extremely easy to work with user inputs through their generic script settings interface.
For this lesson I will explain how to add basic input settings to our high/low channel script.
First, add this line to your script, right below the study() line:
lookback = input(title="Lookback Period", type=integer, defval=50)
This is saying to PineScript ‘create a variable named lookback with the type of integer, and assign it to whatever the user sets it to’.
In this case, title=”Lookback Period” sets the text description in the script interface, type=integer says to treat this input as a number, and defval=50 says set the default value to 50.
Now we must tell PineScript what to do with this number. Change these two lines in your code to look like this:
highestHigh = highest(high, lookback) lowestLow = lowest(low, lookback)
If you save the script and add it to your chart you will now be able to change the lookback period in the settings. Click the settings icon next to the indicator title on your chart and this window will appear:
Now if you change this setting the script will adjust the lines to the new lookback period. For example, if you set this number to 100 the chart would look like this:
And there you have it. With this script, you have the beginnings of an adaptable indicator. Getting user inputs for timeframes, numbers, text, booleans (true/false/checkboxes) etc. is a breeze.
I will demonstrate how to use more complex user inputs in later lessons, but for now this is a great start and covers a lot of basic use cases.
//@version=3 study("Lesson 3", overlay=true) lookback = input(title="Lookback Period", type=integer, defval=50) highestHigh = highest(high, lookback) lowestLow = lowest(low, lookback) plot(highestHigh, color=red, linewidth=2) plot(lowestLow, color=blue, linewidth=2)