Data and time inputs can help you make more sense of your Pine Script indicators and strategies. Adding a time input is pretty simple in Pine Script. You only need to specify the time input and the script will highlight a bar with open equal to the selected time.
Time Input in Pine Script
Here’s the simple code I wrote to demonstrate how to specify time input in Pine Script.
//@version=5 indicator("Input Time from Chart", overlay = true) // Date/Time Input inp_time1 = input.time(timestamp("31 Dec 2022 00:00 +0000"), "Date") bgcolor(time == inp_time1 ? color.red : na)
And here is how it looks like on the chart:
Here is how input parameters look like:
Interactive Time Input
In some unique cases, it might be tricky to set the time for your bar. Fortunately, Pine script supports an interactive input mode for price and time inputs. This allows algo traders to select price and time on the chart instead of using input widgets. This interactive mode is activated by using confirm = true in input.time() function.
//@version=5 indicator("Input Time from Chart", overlay = true) // Date/Time Input inp_time1 = input.time(timestamp("31 Dec 2022 00:00 +0000"), "Date", confirm = true)
This will change the default behavior of your script and you will be able to select a bar when you add your indicator to your chart. It will ask you to choose your time with a message at the bottom of your chart:
Click on any bar you want and this will populate your input with the correct time. You can add multiple inputs with “confirmations” as well.
Follow me on TradingView and YouTube.