How to code a time with date input in TradingView's Pine Script?
How to code a time with date input in TradingView’s Pine Script?
Section titled “How to code a time with date input in TradingView’s Pine Script?”TL;DR
Combineinput.time()withtimestamp()to capture user-selected moments and drive time-based logic in your scripts.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 10-15 min |
| Category | Inputs |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5indicator("Time input demo", overlay=true)
startTime = input.time(timestamp("2024-01-01T09:30:00"), "Start")isAfter = time >= startTime
bgcolor(isAfter ? color.new(color.teal, 85) : na)Why It Matters
Section titled “Why It Matters”Time inputs let users specify when scripts should start trading, draw lines, or highlight sessions. They’re ideal for backtest windows, launch dates, or marking events.
What You’ll Learn
Section titled “What You’ll Learn”- Creating time inputs with friendly defaults and tooltips
- Converting user-selected timestamps into visual markers or logic triggers
- Building date ranges with pairs of
input.time() - Handling missing or future dates safely
Quick Reference
Section titled “Quick Reference”| Call | Purpose |
|---|---|
input.time(defval, title, tooltip, group, inline, confirm, timezone) | Creates a time selector in the settings panel. |
timestamp(year, month, day, hour, minute) | Produces readable defaults for time inputs. |
line.new, bgcolor, alertcondition | Common consumers of time input values. |
input.session | Combine with time input to build complex schedules. |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Create the input with a sensible default
Usetimestamp()or ISO strings for readability.startDate = input.time(timestamp("2024-01-01T00:00:00"), "Start date") -
Apply logic or drawing at the selected time
Comparetimeagainst the input to trigger actions.if time == startDatelabel.new(bar_index, high, "Start", style=label.style_label_down) -
Optionally define end times or ranges
Use two inputs to create date windows for analysis/backtesting.endDate = input.time(timestamp("2024-02-01T00:00:00"), "End date")inRange = time >= startDate and time <= endDate
Example Playbook
Section titled “Example Playbook”//@version=5indicator("Time input range", overlay=true)
startDate = input.time(timestamp("2023-01-01T09:30:00"), "Range start", tooltip="First bar to highlight")endDate = input.time(timestamp("2023-01-31T16:00:00"), "Range end", tooltip="Last bar to highlight")
inRange = time >= startDate and time <= endDatebgcolor(inRange ? color.new(color.orange, 80) : na)
if inRange and barstate.islastconfirmedhistory alert("Current bar inside selected range")- Users can adjust the start/end timestamps without editing code.
- The range logic highlights exactly the bars between the chosen dates.
- Alerts leverage the same inputs for flexible scheduling.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”- Time inputs use the chart’s time zone unless you set the
timezoneargument explicitly. - Use
confirm=trueif a change should require user confirmation (prevents accidental recalculations). - Pair with
input.sessionorinput.boolto build more advanced schedules. - Guard against
startDate > endDateby swapping or usingmath.min/max.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”Why doesn’t my script trigger at the selected time?
Section titled “Why doesn’t my script trigger at the selected time?”Check that you compare against the instrument’s exchange time (all Pine time functions are exchange-based). Also ensure the bar resolution is fine-grained enough to include the specified timestamp.
Can I allow users to pick only a date (no time)?
Section titled “Can I allow users to pick only a date (no time)?”input.time() always stores milliseconds; simply default the time to 00:00 and ignore the time portion in your logic if only the date matters.
Key Takeaways
Section titled “Key Takeaways”input.time()gives users direct control over when code should run or highlight data.- Use pairs of time inputs to define custom analysis windows or backtest periods.
- Combine with visual cues (labels, backgrounds) and alerts for interactive workflows.
Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse PineScript Examples for ready-to-run templates
- Connect to Live Trading when you are ready to automate
Adapted from tradingcode.net, optimised for Algo Trade Analytics users.