Pine Script tool
Pine Script Timeframe String Converter
Turn a Pine Script timeframe string into seconds and plain words, or build the right string from an amount and a unit. Pine writes four hours as 240, not 4H, and that is the mistake this tool catches.
How Pine Script writes a timeframe
A Pine Script timeframe is a string made of a number and an optional unit letter. With no letter the number counts minutes, so "15" is fifteen minutes and "60" is one hour. The letters are S for seconds, D for days, W for weeks and M for months. A bare letter means one of that unit, so "D" is the daily chart.
There is no hour unit. Writing "4H" or "1h", as many other platforms do, is not valid in Pine. Four hours is "240". This is the most common reason a request.security() call or an input.timeframe() default fails to behave, and the converter above flags it.
Why convert to seconds
Strings cannot be compared, but numbers can. To check that a requested timeframe is at least as long as the chart, convert both to seconds and compare. Minutes, hours, days and weeks have fixed lengths in that conversion, which is why the arithmetic is exact for them. A month does not have a fixed length, so any seconds figure for months is an approximation, and the tool marks it as one.
The reverse conversion is useful too. If you want to double the chart timeframe, you multiply the seconds and turn the result back into a string. That only works when the result is a whole unit within Pine's limits, so the builder returns nothing for amounts such as 25 hours, which is neither a whole day nor within the 1440 minute limit.
What are TradingView and Pine Script?
TradingView is one of the most widely used charting and market-analysis platforms, where traders and analysts study price movement across stocks, crypto, forex, and futures on interactive charts. Pine Script is TradingView's own lightweight programming language, created so anyone can build custom tools that run directly on those charts.
People use Pine Script to build four main kinds of tools. Indicators calculate and plot values on the chart, exactly like the calculation above, but recomputed automatically on every bar. Strategies add explicit entry and exit rules and can be backtested against historical data in TradingView's Strategy Tester to see how they would have behaved. Screeners scan many symbols at once for conditions you define. Alerts notify you the moment a condition you specified occurs, so you do not have to watch the screen.
The value is precision and automation. Instead of eyeballing a chart, you describe exactly what you want measured, visualized, or notified about, and TradingView runs it consistently across any market and timeframe. That is why traders, analysts, and developers write Pine Script: it turns a manual charting idea into a repeatable tool. These tools are for tracking, visualizing, and testing market ideas; they do not tell you what to trade, and that decision always remains yours.
Writing that code by hand means learning Pine Script's syntax, its type system, and the exact names of hundreds of built-in functions. It is a real programming language, and small mistakes stop a script from compiling in the Pine Editor.
Turn this into Pine Script
The converter works on one string at a time. In a script, the same job is done with timeframe.in_seconds() and timeframe.from_seconds(), which accept the same strings and work on whatever timeframe the user picks or the chart is currently showing. Combining them with request.security() is where multi-timeframe scripts usually go wrong, because the lookahead and merge settings matter as much as the string.
//@version=6
indicator("Timeframe helper", overlay = true)
// The timeframe string from the converter above.
higherTf = input.timeframe("240", "Higher timeframe")
// timeframe.in_seconds() turns any timeframe string into seconds.
chartSeconds = timeframe.in_seconds(timeframe.period)
higherSeconds = timeframe.in_seconds(higherTf)
// request.security() should only look at a timeframe at least as long as
// the chart. Comparing seconds makes that check explicit.
validHigher = higherSeconds >= chartSeconds
// timeframe.from_seconds() goes the other way, for example to double the
// chart timeframe and use the result as a string.
doubledTf = timeframe.from_seconds(chartSeconds * 2)
htfClose = validHigher ? request.security(syminfo.tickerid, higherTf, close[1], lookahead = barmerge.lookahead_on) : na
plot(htfClose, "Higher timeframe close", color = color.teal)
if barstate.islast
label.new(bar_index, high, "Chart: " + timeframe.period + " (" + str.tostring(chartSeconds) + "s)\nHigher: " + higherTf + " (" + str.tostring(higherSeconds) + "s)\nDoubled: " + doubledTf)PineScripter is an AI built specifically for Pine Script. You describe what you want in plain English and it writes TradingView-ready v6 code. Because it is specialized on the Pine Script language and its exact function signatures, it tends to produce code that compiles far more reliably than general-purpose models like ChatGPT, which often invent functions that do not exist in Pine Script.
Related free calculators
From the blog
PineScripter is an AI developer tool that helps you write Pine Script code. It is not a financial advisor and will never offer financial, investment, or trading advice. Everything on this page, including the calculator and the explanations, is provided purely for educational and informational purposes. Any decision about how to interpret an indicator or trade a market is entirely your own. See our full disclaimer for more.