Skip to content

An empty timeframe string in timeframe.in_seconds("") and timeframe.change("") is not read as the chart timeframe: 0 seconds, and never a change #394

Closed

Description

@letiep97

Summary

An empty timeframe string stands for the chart timeframe in Pine (as in request.security(syminfo.tickerid, "", close), which #294 case 2 made work in 1.11.0), and timeframe.in_seconds("") is a common way to write the chart timeframe in seconds. Codegen 1.12.0 passes the empty string through:

secs = ... tf_to_seconds(std::string("")) ...

and the engine's tf_to_seconds (src/timeframe.cpp) returns 0 for an empty string, so the value is 0 on every bar. With no argument, or with timeframe.period, Codegen emits tf_to_seconds(script_tf_) and the value is the chart's 3600 on a 1h chart. Nothing fails, so a script that divides or compares by this value runs with a silently wrong number.

timeframe.change("") has the same cause: it is emitted as tf_change(prev_bar_timestamp_, current_bar_.timestamp, std::string(""), syminfo_.timezone, syminfo_.session) and is never true, while timeframe.change(timeframe.period) is true on every bar after the first.

Versions

pineforge-codegen 1.12.0 with pineforge-engine v1.12.0 (6e27d0f2). Apple clang 17.0.0, macOS 15.8 arm64. BTCUSDT 1h bars from 2024-01-01 to 2024-03-01 (1,441 bars), initial capital 10000, no commission or slippage.

TradingView: BINANCE:BTCUSDT, 1h, Deep Backtesting over the same dates, measured on 2026-11-14: the two scripts and their controls save without error and give the same 26 trades (the last one still open at the end of the range), net profit 29.93, as the controls on PineForge. timeframe.in_seconds("") is 3600 on each of the 1,360 bars up to the last entry, as timeframe.in_seconds(timeframe.period); timeframe.change("") is false on the first bar of the range and true on the 1,359 bars after it up to the last entry, as timeframe.change(timeframe.period). The values were read through the order comment of each entry, as the minimum and maximum over the bars so far.

Reproduction

1. timeframe.in_seconds("")

//@version=6
strategy("timeframe.in_seconds empty string", overlay = true, initial_capital = 10000)
fast = ta.ema(close, 10)
slow = ta.ema(close, 30)
secs = timeframe.in_seconds("")
// @pf-trace secs=secs
if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long, qty = 0.01)
if ta.crossunder(fast, slow)
    strategy.close("Long")

secs traces 0 on all 1,441 bars.

Control, one change: timeframe.in_seconds(timeframe.period). It traces 3600 on all 1,441 bars.

2. timeframe.change("")

//@version=6
strategy("timeframe.change empty string", overlay = true, initial_capital = 10000)
fast = ta.ema(close, 10)
slow = ta.ema(close, 30)
chg = timeframe.change("") ? 1 : 0
// @pf-trace chg=chg
if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long, qty = 0.01)
if ta.crossunder(fast, slow)
    strategy.close("Long")

chg traces 0 on all 1,441 bars.

Control, one change: timeframe.change(timeframe.period). It traces 0 on bar 0 and 1 on bars 1 to 1440.

Both scripts and both controls run with 26 trades, net profit 29.93.

Expected

timeframe.in_seconds("") gives the chart timeframe in seconds, as the control: 3600 on a 1h chart; timeframe.change("") follows the chart timeframe as timeframe.change(timeframe.period) does. Mapping "" to script_tf_ where the argument is emitted, as already done for the call with no argument, would cover both.

Workaround

Pass timeframe.period, or call timeframe.in_seconds() / timeframe.change() with no argument.

Activity

  1. luisleo526 commented on Nov 14, 2026

    @luisleo526
    Contributor

    Thank you for the detailed report and the controls; they make each case easy to reproduce.

    We will reproduce the empty-timeframe cases (timeframe.in_seconds("") giving 0 and timeframe.change("") never firing) on Codegen 1.12.0 and on current main, find where the translation goes wrong, and post the findings here by 2026-11-15, with a fix target. This one is near the front of our queue because it can give a silently wrong value rather than a build error.

    🤖 Generated with Claude Code

  2. luisleo526 commented on Nov 15, 2026

    @luisleo526
    Contributor

    Findings 2026-11-15 (UTC+8), as promised.

    • Both cases reproduce on Codegen 1.12.0 and on current main: timeframe.in_seconds("") gives 0 and timeframe.change("") never fires.
    • Cause: these two functions do not treat the empty string as the chart's timeframe, as request.security already does since 1.11.0.
    • Fix: the empty string now means the chart timeframe in both functions. Review of the fix found work that remains before it ships: an invalid numeric timeframe argument (such as a literal 0) must be refused with its own clear error, some numeric argument forms are not refused yet, a bool argument (which TradingView refuses) must be refused in every form, and we are checking what TradingView returns for timeframe.change(na) before we settle that case.

    Fix target: we cannot give one honestly yet, because those changes still have to pass review and verification, and the fix is not in today's batch. We will post the date here as soon as they have, and the merge commit when it lands.

  3. luisleo526 commented on Nov 15, 2026

    @luisleo526
    Contributor

    Fixed on main in 0ba632a (#405). Empty timeframe string arguments now resolve as the chart timeframe across the timeframe functions: timeframe.in_seconds("") returns the chart's seconds, and timeframe.change("") fires when the chart period changes. Regression tests cover each empty, explicit and omitted timeframe argument form; they fail on the previous main and pass now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions