Pine Script, offers a multitude of functions that aid developers in creating custom technical analysis tools. One such function is input.time
, which adds a time input to your script’s “Settings/Inputs” tab. This feature is crucial when creating scripts that need to consider specific time points. This blog will give you a thorough walkthrough on how to use this function effectively, starting with its syntax, followed by a unique use-case example, and finally, key takeaways.
Function Syntax and Parameters
The input.time
function in Pine Script is invoked with the following syntax:
input.time(defval, title, tooltip, inline, group, confirm) → input int
Each parameter plays a unique role, and here is what they represent:
defval
(const int): This determines the default value of the input variable. This value can be a timestamp function, provided it uses a date argument in const string format.title
(const string): This represents the title of the input. If not specified, the variable name is used as the input’s title. If the title is specified but is empty, the name will be an empty string.tooltip
(const string): This string is shown to the user when hovering over the tooltip icon.inline
(const string): This combines all the input calls using the same argument in one line. The string used as an argument is not displayed, it is only used to identify inputs belonging to the same line.group
(const string): This creates a header above all inputs using the same group argument string. The string is also used as the header’s text.confirm
(const bool): If true, the interactive input mode is enabled and the selection is done by clicking on the chart when the indicator is added to the chart.
A Unique Use Case Example
To make this concept clearer, let’s go through a unique use-case. We’ll create a script that identifies the high of a selected date and labels it.
//@version=5 indicator("input.time", overlay=true) i_date = input.time(timestamp("20 May 2023 00:00 +0300"), "Date") l = label.new(i_date, high, "Date" , xloc=xloc.bar_time , size = size.large) label.delete(l[1])
Here’s what each line of the script does:
//@version=5
: This line is specifying the version of Pine Script to use, in this case, version 5. It is important to denote the script’s version at the beginning of your script since different versions may have different functions and syntax.indicator("input.time", overlay=true)
: This line is calling theindicator
function to set up a new indicator with the title “input.time”. The parameteroverlay=true
ensures that this indicator will be drawn directly on the price chart.i_date = input.time(timestamp("20 May 2023 00:00 +0300"), "Date")
: This line uses theinput.time
function to create an input in the settings tab with a default timestamp of “20 May 2023 00:00 +0300”. The user can modify this time input in the settings tab of the script. The “Date” argument serves as the title for this input.l = label.new(i_date, high, "Date" , xloc=xloc.bar_time , size = size.large)
: This line creates a new label on the chart at the bar corresponding to the input datei_date
. The y-coordinate of the label is determined by the high price of that bar. The label’s text will be “Date”, and it will be displayed at the time of the bar (xloc=xloc.bar_time
). The label size will be large (size = size.large
).label.delete(l[1])
: This last line ensures that only the most recent label is displayed on the chart. It deletes the previous label before drawing the new one.
Key Takeaways
input.time
is a powerful tool in Pine Script that allows users to interact with the chart by selecting a specific point in time. This function opens up a range of possibilities for custom scripts that need user-selected time inputs.
Remember, while using the input.time
function:
- You can combine a price input with a time input if both function calls use the same argument for their
inline
parameter. - Interactive mode (enabled by setting
confirm = true
) allows users to select a point in time directly on the chart.
Conclusion
The input.time
function adds a significant degree of interactivity and flexibility to your Pine Script tools. As we’ve seen in our unique use case, the function can be employed to enhance user experience and provide additional customization options. Don’t hesitate to explore this function further, incorporate it into your scripts, and make the most of its potential.