How to Work With Custom Inputs in TradingView using PineScript?

How to Work With Custom Inputs in TradingView using PineScript?

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 time period, the timeframe, overbought/oversold thresholds, etc. Having it in the code will make things complicated unnecessarily while customizing it, so an option to change the variables or inputs easily is a must have.

Luckily, Pine Script makes it extremely easy to work with user inputs through their generic script settings interface.

In this episode I will explain how to add basic input settings to the scipt we developed in the previous episode.

Getting User Input

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

period = input(title=”Period”, type=input.integer, defval=50)

This is saying to Pine Script “create a variable named period with the type of integer, and assign it to whatever the user sets it to with default value as 50”.

In this case, title=”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, period)

lowestLow = lowest(low, period)

If you save the script and add it to your chart, you will now be able to change the 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 that’s it. With this simple Pine Script example, we have the beginnings of an adaptable indicator. Getting user inputs for timeframes, numbers, text, booleans (true/false/checkboxes), etc. is just this simple.

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.


Check Out Script Library to get script used for this episode:
https://marketsecrets.in/script-library/

To know more, checkout:

https://youtu.be/UEAmo7h0JQ4

 

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!