Pine Script – Lesson 3: Basic User Inputs

Pine Script Basics Course Pine Script Mastery Course

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, Pine Script 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.


Video Lesson

If you prefer to learn in a visual/audio manner, then here’s a video version of this lesson:


Getting User Input

First, add this line to your script, right below the study() line:

lookback = input(title="Lookback Period", type=input.integer, defval=50)


This is saying to Pine Script “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 Pine Script 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:

Pine Script Lesson 3 Example


And there you have it. With this simple Pine Script example, 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.

Advanced Course

If you want to take your Pine Script coding to the next level, then I think you’ll be interested in my Pine Script Mastery Course.

If you liked this free content then I promise that you’ll love my premium content where I am able to go into much greater detail and help answer students’ questions!



Source Code

//@version=4
study("Lesson 3", overlay=true)
lookback = input(title="Lookback Period", type=input.integer, defval=50)
highestHigh = highest(high, lookback)
lowestLow = lowest(low, lookback)
plot(highestHigh, color=color.red, linewidth=2)
plot(lowestLow, color=color.blue, linewidth=2)

OVERVIEW


Free Premium Charts!

5 1 vote
Article Rating
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Stig Andersen
Stig Andersen
2 years ago

it is instructive, but the text of Lesson 3 is a repetition of Lesson 2

Johannes Eisgruber
Johannes Eisgruber
2 years ago

I’m learning a lot, well done, sometimes a little to fast for beginners thanks