An essential function of Pine Script that often comes in handy is the input.source
function. In this tutorial, we’ll delve into its usage, syntax, and examples.
What is input.source?
The input.source
function in Pine Script is primarily used to add an input to the Inputs tab of your script’s Settings. This grants users the capability to select a source for calculations, such as close, hl2, etc. Notably, users can also choose an output from another indicator on their chart as the source.
Syntax of input.source
Let’s have a look at the syntax:
input.source(defval, title, tooltip, inline, group) → series float
Here are the arguments it takes:
- defval: Determines the default value of the input variable proposed in the script’s “Settings/Inputs” tab, from where the user can change it. Accepts values like open, high, low, close, hl2, hlc3, ohlc4, hlcc4.
- title: Title of the input. The variable name is used as the input’s title if not specified. If specified but empty, the name will be an empty string.
- tooltip: The string shown to the user when hovering over the tooltip icon.
- inline: Combines all the input calls using the same argument in one line. The string used as an argument is not displayed. It only identifies inputs belonging to the same line.
- group: Creates a header above all inputs using the same group argument string. The string is also used as the header’s text.
Use Case Example of input.source
Let’s examine an example to understand its usage better:
//@version=5 indicator("input.source example", overlay=true) i_src = input.source(close, "Source") plot(i_src)
Here’s what each line does:
//@version=5
: This indicates that we’re using the 5th version of Pine Script.indicator("input.source example", overlay=true)
: Theindicator
function is used to set the name of the script (in this case, “input.source example”) and to indicate that the plot of this script should be drawn on the price chart.i_src = input.source(close, "Source")
: This line calls theinput.source
function. We’re setting thedefval
to “close” and the title to “Source”. The result of this function (a series of float values) is assigned to the variablei_src
.plot(i_src)
: Finally, we plot thei_src
series on the chart.
Key Takeaways
The input.source
function is a powerful feature in Pine Script, enabling script creators to offer more flexibility to users in customizing the source of calculations. It provides an interactive element, allowing users to select from multiple predefined values. Understanding and using input.source
effectively is a crucial part of creating interactive and user-friendly Pine scripts.
Conclusion
In this tutorial, we learned about the input.source
function in Pine Script, its syntax, its arguments, and how to use it in a script. We walked through a straightforward example to illustrate its practical application. Remember, the input.source
function always should be assigned to a variable. As you continue your Pine Script journey, mastering functions like these will be crucial in creating effective, dynamic, and user-friendly trading scripts.