From 85b9100d39fcecbfadcbbb815ebbd854e4cd7483 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 00:27:53 +0800 Subject: [PATCH 01/16] Resolve empty timeframe arguments to the chart period --- CHANGELOG.md | 4 + pineforge_codegen/codegen/helpers.py | 1 + pineforge_codegen/codegen/visit_call.py | 39 ++-- tests/fixtures/empty_timeframe/arguments.pine | 186 +++++++++++++++ .../empty_timeframe/reporter-change.pine | 10 + .../empty_timeframe/reporter-seconds.pine | 10 + tests/test_e2e_empty_timeframe.py | 219 ++++++++++++++++++ tests/test_e2e_temporary_name_hygiene.py | 10 +- 8 files changed, 464 insertions(+), 15 deletions(-) create mode 100644 tests/fixtures/empty_timeframe/arguments.pine create mode 100644 tests/fixtures/empty_timeframe/reporter-change.pine create mode 100644 tests/fixtures/empty_timeframe/reporter-seconds.pine create mode 100644 tests/test_e2e_empty_timeframe.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 83af3833..bd73cb2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,10 @@ supported as exact pairs; on the 0.x line they are independent. See the ## Unreleased (next release) +- `timeframe.in_seconds` and `timeframe.change` resolve an evaluated empty + string to the chart period, including input, variable, function-return and + named arguments. The argument is evaluated once, and nonempty strings keep + their explicit period. (#394) - A zone name the engine rejects now makes calendar-form `timestamp()` stop the run at its first evaluation, instead of silently calculating a UTC value. The paired engine reports a typed timezone error identifying the failed bar. diff --git a/pineforge_codegen/codegen/helpers.py b/pineforge_codegen/codegen/helpers.py index f292996f..aacef1e4 100644 --- a/pineforge_codegen/codegen/helpers.py +++ b/pineforge_codegen/codegen/helpers.py @@ -533,6 +533,7 @@ def cpp_code_only(cpp: str) -> str: _pf_series_raw _pf_raw _pf_val_t _pf_out _pf_prior _pf_hist_offset_raw _pf_hist_offset_numeric _pf_hist_offset _pf_hist_bar _pf_calendar_ts _pf_calendar_tz + _pf_timeframe_arg _tz _yr _mo _dy _hr _min _sc _res _old _old_tz _had_old _secs _last_tz _last_yr _last_mo _last_dy _last_hr _last_min _last_sc _last_res diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index efb19c5c..fb34cb61 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -3211,22 +3211,35 @@ def _visit_func_call(self, node: FuncCall) -> str: return f'{log_funcs[func_name]}({self._visit_expr(message)})' return '"" /* unsupported log */' - # timeframe.* calls (e.g., timeframe.change) — not supported in single-TF backtest + # An empty timeframe argument denotes the chart period, whether it + # comes from a literal, an input or a user call. Bind by parameter + # name and resolve the evaluated string, not the argument's AST form. if namespace == "timeframe": - if func_name == "change": - tf_arg = self._visit_expr(node.args[0]) if node.args else 'script_tf_' - # Symbol-clock overload: D/W/M boundaries fall on the SYMBOL's - # session day (17:00 ET on OANDA forex, 09:30 ET RTH on - # equities), intraday buckets on the session-open grid — the - # same clock request.security aggregation uses. UTC/24x7 - # syminfo reduces to the tz-less epoch math bit-for-bit. + if func_name in ("change", "in_seconds"): + argument = self._resolve_func_args( + node, f"timeframe.{func_name}", + ).get("timeframe") + tf_arg = ( + "script_tf_" if argument is None else + "(_pf_timeframe_arg.empty() ? script_tf_ : _pf_timeframe_arg)" + ) + if func_name == "change": + # Keep the symbol-clock overload for calendar and + # session-anchored intraday boundaries. + call = ( + f'tf_change(prev_bar_timestamp_, current_bar_.timestamp, {tf_arg}, ' + 'syminfo_.timezone, syminfo_.session)' + ) + else: + call = f'tf_to_seconds({tf_arg})' + if argument is None: + return call + # A function-return argument can carry state. Evaluate it + # once before testing emptiness and passing the chosen value. return ( - f'tf_change(prev_bar_timestamp_, current_bar_.timestamp, {tf_arg}, ' - 'syminfo_.timezone, syminfo_.session)' + '([&]() { const std::string _pf_timeframe_arg = ' + f'({self._visit_expr(argument)}); return {call}; }}())' ) - if func_name == "in_seconds": - tf_arg = self._visit_expr(node.args[0]) if node.args else 'script_tf_' - return f'tf_to_seconds({tf_arg})' # Defensive: support_checker.NOT_YET_FUNC should already have rejected # any unhandled timeframe.* call. Reaching here implies the checker was # bypassed. diff --git a/tests/fixtures/empty_timeframe/arguments.pine b/tests/fixtures/empty_timeframe/arguments.pine new file mode 100644 index 00000000..99afec55 --- /dev/null +++ b/tests/fixtures/empty_timeframe/arguments.pine @@ -0,0 +1,186 @@ +//@version=6 +strategy("Empty timeframe arguments", currency = currency.NONE) +frameInput = input.timeframe("", "Frame input") +frameString = input.string("", "String input") +frameVariable = frameString +_pf_timeframe_arg = frameString +pf_safe__pf_timeframe_arg = "240" +plainVariable = "" +var string persistentFrame = "" +emptyFrame() => "" +returnedFrame(string frameArg) => frameArg +secondsPos(string frameArg) => timeframe.in_seconds(frameArg) +secondsNamed(string frameArg) => timeframe.in_seconds(timeframe = frameArg) +changePos(string frameArg) => timeframe.change(frameArg) +changeNamed(string frameArg) => timeframe.change(timeframe = frameArg) +secondsCapturedPos(string _pf_timeframe_arg) => timeframe.in_seconds(_pf_timeframe_arg) +secondsCapturedNamed(string _pf_timeframe_arg) => timeframe.in_seconds(timeframe = _pf_timeframe_arg) +changeCapturedPos(string _pf_timeframe_arg) => timeframe.change(_pf_timeframe_arg) +changeCapturedNamed(string _pf_timeframe_arg) => timeframe.change(timeframe = _pf_timeframe_arg) +secondsStatefulPos(string frameArg) => + var string savedFrame = frameArg + timeframe.in_seconds(savedFrame) +secondsStatefulNamed(string frameArg) => + var string savedFrame = frameArg + timeframe.in_seconds(timeframe = savedFrame) +changeStatefulPos(string frameArg) => + var string savedFrame = frameArg + timeframe.change(savedFrame) +changeStatefulNamed(string frameArg) => + var string savedFrame = frameArg + timeframe.change(timeframe = savedFrame) +countedFrame(array counter, string frameArg) => + array.set(counter, 0, array.get(counter, 0) + 1) + frameArg +S_literal_pos = timeframe.in_seconds("") +S_literal_named = timeframe.in_seconds(timeframe = "") +S_input_pos = timeframe.in_seconds(frameInput) +S_input_named = timeframe.in_seconds(timeframe = frameInput) +S_string_input_pos = timeframe.in_seconds(frameString) +S_string_input_named = timeframe.in_seconds(timeframe = frameString) +S_variable_pos = timeframe.in_seconds(frameVariable) +S_variable_named = timeframe.in_seconds(timeframe = frameVariable) +S_temporary_name_pos = timeframe.in_seconds(_pf_timeframe_arg) +S_temporary_name_named = timeframe.in_seconds(timeframe = _pf_timeframe_arg) +S_parameter_name_pos = secondsCapturedPos(frameString) +S_parameter_name_named = secondsCapturedNamed(frameString) +S_occupied_escape_pos = timeframe.in_seconds(pf_safe__pf_timeframe_arg) +S_occupied_escape_named = timeframe.in_seconds(timeframe = pf_safe__pf_timeframe_arg) +S_plain_variable_pos = timeframe.in_seconds(plainVariable) +S_plain_variable_named = timeframe.in_seconds(timeframe = plainVariable) +S_persistent_pos = timeframe.in_seconds(persistentFrame) +S_persistent_named = timeframe.in_seconds(timeframe = persistentFrame) +S_return_pos = timeframe.in_seconds(emptyFrame()) +S_return_named = timeframe.in_seconds(timeframe = emptyFrame()) +S_return_input_pos = timeframe.in_seconds(returnedFrame(frameInput)) +S_return_input_named = timeframe.in_seconds(timeframe = returnedFrame(frameInput)) +S_period_pos = timeframe.in_seconds(timeframe.period) +S_period_named = timeframe.in_seconds(timeframe = timeframe.period) +S_explicit_pos = timeframe.in_seconds("240") +S_explicit_named = timeframe.in_seconds(timeframe = "240") +S_helper_empty_pos = secondsPos("") +S_helper_explicit_pos = secondsPos("240") +S_helper_empty_named = secondsNamed("") +S_helper_explicit_named = secondsNamed("240") +S_stateful_empty_pos = secondsStatefulPos("") +S_stateful_explicit_pos = secondsStatefulPos("240") +S_stateful_empty_named = secondsStatefulNamed("") +S_stateful_explicit_named = secondsStatefulNamed("240") +S_omitted = timeframe.in_seconds() +C_literal_pos = timeframe.change("") ? 1 : 0 +C_literal_named = timeframe.change(timeframe = "") ? 1 : 0 +C_input_pos = timeframe.change(frameInput) ? 1 : 0 +C_input_named = timeframe.change(timeframe = frameInput) ? 1 : 0 +C_string_input_pos = timeframe.change(frameString) ? 1 : 0 +C_string_input_named = timeframe.change(timeframe = frameString) ? 1 : 0 +C_variable_pos = timeframe.change(frameVariable) ? 1 : 0 +C_variable_named = timeframe.change(timeframe = frameVariable) ? 1 : 0 +C_temporary_name_pos = timeframe.change(_pf_timeframe_arg) ? 1 : 0 +C_temporary_name_named = timeframe.change(timeframe = _pf_timeframe_arg) ? 1 : 0 +C_parameter_name_pos = changeCapturedPos(frameString) ? 1 : 0 +C_parameter_name_named = changeCapturedNamed(frameString) ? 1 : 0 +C_occupied_escape_pos = timeframe.change(pf_safe__pf_timeframe_arg) ? 1 : 0 +C_occupied_escape_named = timeframe.change(timeframe = pf_safe__pf_timeframe_arg) ? 1 : 0 +C_plain_variable_pos = timeframe.change(plainVariable) ? 1 : 0 +C_plain_variable_named = timeframe.change(timeframe = plainVariable) ? 1 : 0 +C_persistent_pos = timeframe.change(persistentFrame) ? 1 : 0 +C_persistent_named = timeframe.change(timeframe = persistentFrame) ? 1 : 0 +C_return_pos = timeframe.change(emptyFrame()) ? 1 : 0 +C_return_named = timeframe.change(timeframe = emptyFrame()) ? 1 : 0 +C_return_input_pos = timeframe.change(returnedFrame(frameInput)) ? 1 : 0 +C_return_input_named = timeframe.change(timeframe = returnedFrame(frameInput)) ? 1 : 0 +C_period_pos = timeframe.change(timeframe.period) ? 1 : 0 +C_period_named = timeframe.change(timeframe = timeframe.period) ? 1 : 0 +C_explicit_pos = timeframe.change("240") ? 1 : 0 +C_explicit_named = timeframe.change(timeframe = "240") ? 1 : 0 +C_helper_empty_pos = changePos("") ? 1 : 0 +C_helper_explicit_pos = changePos("240") ? 1 : 0 +C_helper_empty_named = changeNamed("") ? 1 : 0 +C_helper_explicit_named = changeNamed("240") ? 1 : 0 +C_stateful_empty_pos = changeStatefulPos("") ? 1 : 0 +C_stateful_explicit_pos = changeStatefulPos("240") ? 1 : 0 +C_stateful_empty_named = changeStatefulNamed("") ? 1 : 0 +C_stateful_explicit_named = changeStatefulNamed("240") ? 1 : 0 +C_omitted = timeframe.change() ? 1 : 0 +dynamicFrame = bar_index % 2 == 0 ? "" : "240" +C_dynamic_pos = timeframe.change(dynamicFrame) ? 1 : 0 +C_dynamic_named = timeframe.change(timeframe = dynamicFrame) ? 1 : 0 +var calls = array.new_int(1, 0) +C_once_pos = timeframe.change(countedFrame(calls, frameInput)) ? 1 : 0 +C_once_named = timeframe.change(timeframe = countedFrame(calls, frameInput)) ? 1 : 0 +callCount = array.get(calls, 0) +// @pf-trace S_literal_pos=S_literal_pos +// @pf-trace S_literal_named=S_literal_named +// @pf-trace S_input_pos=S_input_pos +// @pf-trace S_input_named=S_input_named +// @pf-trace S_string_input_pos=S_string_input_pos +// @pf-trace S_string_input_named=S_string_input_named +// @pf-trace S_variable_pos=S_variable_pos +// @pf-trace S_variable_named=S_variable_named +// @pf-trace S_temporary_name_pos=S_temporary_name_pos +// @pf-trace S_temporary_name_named=S_temporary_name_named +// @pf-trace S_parameter_name_pos=S_parameter_name_pos +// @pf-trace S_parameter_name_named=S_parameter_name_named +// @pf-trace S_occupied_escape_pos=S_occupied_escape_pos +// @pf-trace S_occupied_escape_named=S_occupied_escape_named +// @pf-trace S_plain_variable_pos=S_plain_variable_pos +// @pf-trace S_plain_variable_named=S_plain_variable_named +// @pf-trace S_persistent_pos=S_persistent_pos +// @pf-trace S_persistent_named=S_persistent_named +// @pf-trace S_return_pos=S_return_pos +// @pf-trace S_return_named=S_return_named +// @pf-trace S_return_input_pos=S_return_input_pos +// @pf-trace S_return_input_named=S_return_input_named +// @pf-trace S_period_pos=S_period_pos +// @pf-trace S_period_named=S_period_named +// @pf-trace S_explicit_pos=S_explicit_pos +// @pf-trace S_explicit_named=S_explicit_named +// @pf-trace S_helper_empty_pos=S_helper_empty_pos +// @pf-trace S_helper_explicit_pos=S_helper_explicit_pos +// @pf-trace S_helper_empty_named=S_helper_empty_named +// @pf-trace S_helper_explicit_named=S_helper_explicit_named +// @pf-trace S_stateful_empty_pos=S_stateful_empty_pos +// @pf-trace S_stateful_explicit_pos=S_stateful_explicit_pos +// @pf-trace S_stateful_empty_named=S_stateful_empty_named +// @pf-trace S_stateful_explicit_named=S_stateful_explicit_named +// @pf-trace S_omitted=S_omitted +// @pf-trace C_literal_pos=C_literal_pos +// @pf-trace C_literal_named=C_literal_named +// @pf-trace C_input_pos=C_input_pos +// @pf-trace C_input_named=C_input_named +// @pf-trace C_string_input_pos=C_string_input_pos +// @pf-trace C_string_input_named=C_string_input_named +// @pf-trace C_variable_pos=C_variable_pos +// @pf-trace C_variable_named=C_variable_named +// @pf-trace C_temporary_name_pos=C_temporary_name_pos +// @pf-trace C_temporary_name_named=C_temporary_name_named +// @pf-trace C_parameter_name_pos=C_parameter_name_pos +// @pf-trace C_parameter_name_named=C_parameter_name_named +// @pf-trace C_occupied_escape_pos=C_occupied_escape_pos +// @pf-trace C_occupied_escape_named=C_occupied_escape_named +// @pf-trace C_plain_variable_pos=C_plain_variable_pos +// @pf-trace C_plain_variable_named=C_plain_variable_named +// @pf-trace C_persistent_pos=C_persistent_pos +// @pf-trace C_persistent_named=C_persistent_named +// @pf-trace C_return_pos=C_return_pos +// @pf-trace C_return_named=C_return_named +// @pf-trace C_return_input_pos=C_return_input_pos +// @pf-trace C_return_input_named=C_return_input_named +// @pf-trace C_period_pos=C_period_pos +// @pf-trace C_period_named=C_period_named +// @pf-trace C_explicit_pos=C_explicit_pos +// @pf-trace C_explicit_named=C_explicit_named +// @pf-trace C_helper_empty_pos=C_helper_empty_pos +// @pf-trace C_helper_explicit_pos=C_helper_explicit_pos +// @pf-trace C_helper_empty_named=C_helper_empty_named +// @pf-trace C_helper_explicit_named=C_helper_explicit_named +// @pf-trace C_stateful_empty_pos=C_stateful_empty_pos +// @pf-trace C_stateful_explicit_pos=C_stateful_explicit_pos +// @pf-trace C_stateful_empty_named=C_stateful_empty_named +// @pf-trace C_stateful_explicit_named=C_stateful_explicit_named +// @pf-trace C_omitted=C_omitted +// @pf-trace C_dynamic_pos=C_dynamic_pos +// @pf-trace C_dynamic_named=C_dynamic_named +// @pf-trace C_once_pos=C_once_pos +// @pf-trace C_once_named=C_once_named +// @pf-trace callCount=callCount diff --git a/tests/fixtures/empty_timeframe/reporter-change.pine b/tests/fixtures/empty_timeframe/reporter-change.pine new file mode 100644 index 00000000..7b69fac4 --- /dev/null +++ b/tests/fixtures/empty_timeframe/reporter-change.pine @@ -0,0 +1,10 @@ +//@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") diff --git a/tests/fixtures/empty_timeframe/reporter-seconds.pine b/tests/fixtures/empty_timeframe/reporter-seconds.pine new file mode 100644 index 00000000..e9667c37 --- /dev/null +++ b/tests/fixtures/empty_timeframe/reporter-seconds.pine @@ -0,0 +1,10 @@ +//@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") diff --git a/tests/test_e2e_empty_timeframe.py b/tests/test_e2e_empty_timeframe.py new file mode 100644 index 00000000..f7670011 --- /dev/null +++ b/tests/test_e2e_empty_timeframe.py @@ -0,0 +1,219 @@ +"""Empty timeframe strings resolve after argument evaluation. + +The complete reporter strategies pin the two original defects. The argument +matrix observes native values for inputs, variables, returns and helpers on +several chart periods, and counts evaluations of a stateful string argument. +""" + +from __future__ import annotations + +from datetime import datetime, timezone +import math +from pathlib import Path + +import pytest + +from tests._e2e import ( + Build, assert_same_runs, build_strategy_library, chart_feed_head, + execute_all, ok, run_strategy, skip_unless_e2e_env, transpile_json, +) +from tests._requests import Feed + + +FIXTURES = Path(__file__).parent / "fixtures" / "empty_timeframe" +REPORTER_BARS = 1441 +FIXED_SECONDS = {"1S": 1, "5": 300, "60": 3600, "D": 86400, "W": 604800} +ARGUMENTS = { + "literal": "chart", + "input": "input", + "string_input": "input", + "variable": "input", + "temporary_name": "input", + "parameter_name": "input", + "occupied_escape": "explicit", + "plain_variable": "chart", + "persistent": "chart", + "return": "chart", + "return_input": "input", + "period": "chart", + "explicit": "explicit", +} +BUILTINS = {"S": "in_seconds", "C": "change"} +STYLES = ("pos", "named") + + +@pytest.fixture(scope="module") +def reporter_runs(tmp_path_factory): + engine = skip_unless_e2e_env() + base = tmp_path_factory.mktemp("reporter_empty_timeframe") + # A public corpus replay, distinct from the reporter's BTC date window. + input_bars = REPORTER_BARS * 4 + expanded = chart_feed_head(engine, base, input_bars + 4) + rows = expanded.read_text().splitlines() + first = next(index for index, line in enumerate(rows[1:], 1) + if int(line.split(",", 1)[0]) % 3_600_000 == 0) + selected = rows[first:first + input_bars] + assert len(selected) == input_bars, "public feed has too few hourly chart inputs" + feed = base / "aligned_15m.csv" + feed.write_text("\n".join([rows[0], *selected]) + "\n") + builds = {} + for kind, function in (("seconds", "in_seconds"), ("change", "change")): + source = (FIXTURES / f"reporter-{kind}.pine").read_text() + original = f'timeframe.{function}("")' + assert source.count(original) == 1 + for variant, argument in (("empty", '""'), ("period", "timeframe.period"), + ("omitted", "")): + builds[f"{kind}_{variant}"] = Build( + source.replace(original, f"timeframe.{function}({argument})", 1), + overrides={ + "input_tf": "15", "script_tf": "60", + "runtime_overrides": {"currency": "USDT", "timezone": "UTC"}, + }, + trace=True, + ) + return execute_all(engine, feed, base, builds) + + +@pytest.mark.parametrize("kind", ("seconds", "change"), ids=("in_seconds", "change")) +def test_reporter_empty_matches_chart_controls(kind, reporter_runs): + subject = ok(reporter_runs, f"{kind}_empty") + period = ok(reporter_runs, f"{kind}_period") + omitted = ok(reporter_runs, f"{kind}_omitted") + label = "secs" if kind == "seconds" else "chg" + for tag, count, seconds in (("default", REPORTER_BARS * 4, 900.0), + ("override", REPORTER_BARS, 3600.0)): + control = period.traces[tag] + assert len(control) == count + assert {row["name"] for row in control} == {label} + assert [row["bar_index"] for row in control] == list(range(count)) + assert all(b["timestamp"] - a["timestamp"] == seconds * 1000 + for a, b in zip(control, control[1:])) + expected = ([seconds] * count if kind == "seconds" + else [0.0] + [1.0] * (count - 1)) + assert [row["value"] for row in control] == expected + assert_same_runs(omitted, period) + assert_same_runs(subject, period) + + +def _clock_stamps(period): + epoch = int(datetime(2024, 1, 1, tzinfo=timezone.utc).timestamp() * 1000) + if period == "M": + return [int(datetime(2024, month, 1, tzinfo=timezone.utc).timestamp() * 1000) + for month in range(1, 6)] + return [epoch + index * FIXED_SECONDS[period] * 1000 for index in range(33)] + + +@pytest.fixture(scope="module") +def argument_runs(tmp_path_factory): + engine = skip_unless_e2e_env() + base = tmp_path_factory.mktemp("empty_timeframe_arguments") + pine = FIXTURES / "arguments.pine" + source = pine.read_text() + result = transpile_json(pine) + assert result.get("ok"), result.get("diagnostics") + build_strategy_library(result["cpp"], base) + expected_names = {line.split()[2].split("=", 1)[0] + for line in source.splitlines() if line.startswith("// @pf-trace ")} + outcomes = {} + for period in (*FIXED_SECONDS, "M"): + stamps = _clock_stamps(period) + last_close = ( + int(datetime(2024, 6, 1, tzinfo=timezone.utc).timestamp() * 1000) + if period == "M" else stamps[-1] + stamps[-1] - stamps[-2] + ) + bars = [(stamp, close_stamp, 100.0 + index) + for index, (stamp, close_stamp) + in enumerate(zip(stamps, [*stamps[1:], last_close]))] + feed = base / f"clock_{period}.csv" + feed.write_bytes(Feed("BINANCE:BTCUSDT", period, bars).csv()) + for argument in ("", "240"): + tag = f"{period}_{'empty' if not argument else 'explicit'}" + overrides = { + "input_tf": period, "script_tf": period, + "Frame input": argument, "String input": argument, + "runtime_overrides": { + "currency": "USDT", "timezone": "UTC", "session": "24x7", + }, + } + _trades, records, stderr = run_strategy( + engine, base, feed, overrides, tag, trace=True, + ) + (base / f"run_{tag}.stderr").write_text(stderr) + columns = {} + for row in records: + columns.setdefault(row["name"], []).append(row) + assert set(columns) == expected_names, f"{tag}: missing native trace names" + for name, series in columns.items(): + assert len(series) == len(stamps), (tag, name, "missing native calls") + assert [row["bar_index"] for row in series] == list(range(len(stamps))) + assert [row["timestamp"] for row in series] == stamps + outcomes[period, argument] = { + name: [row["value"] for row in series] for name, series in columns.items() + } + return outcomes + + +def _expected(frame, stamps, chart_seconds): + if frame == "chart": + return [chart_seconds] * len(stamps), [0.0] + [1.0] * (len(stamps) - 1) + assert frame == "explicit" + size = 240 * 60 * 1000 + return ([14400.0] * len(stamps), + [0.0] + [float(now // size != before // size) + for before, now in zip(stamps, stamps[1:])]) + + +@pytest.mark.parametrize("builtin", BUILTINS, ids=("in_seconds", "change")) +@pytest.mark.parametrize("name", ARGUMENTS) +@pytest.mark.parametrize("style", STYLES) +def test_empty_argument_source_uses_the_evaluated_string(builtin, name, style, argument_runs): + for (period, argument), columns in argument_runs.items(): + stamps = _clock_stamps(period) + chart_seconds = columns["S_period_pos"][0] + assert math.isfinite(chart_seconds) + if period in FIXED_SECONDS: + assert chart_seconds == FIXED_SECONDS[period] + # Calendar-only engine periods retain their existing conversion; + # equality to the period control is the empty-argument contract. + assert columns["S_period_pos"] == [chart_seconds] * len(stamps) + assert columns["C_period_pos"] == [0.0] + [1.0] * (len(stamps) - 1) + resolution = ARGUMENTS[name] + frame = ("explicit" if resolution == "explicit" + or (resolution == "input" and argument) else "chart") + seconds, changes = _expected(frame, stamps, chart_seconds) + assert columns[f"{builtin}_{name}_{style}"] == ( + seconds if builtin == "S" else changes + ), (period, argument, builtin, name, style) + + +@pytest.mark.parametrize("builtin", BUILTINS, ids=("in_seconds", "change")) +@pytest.mark.parametrize("style", STYLES) +@pytest.mark.parametrize("body", ("helper", "stateful")) +def test_each_helper_body_preserves_empty_and_explicit_arguments(builtin, style, body, argument_runs): + for (period, argument), columns in argument_runs.items(): + stamps = _clock_stamps(period) + for suffix, frame in (("empty", "chart"), ("explicit", "explicit")): + seconds, changes = _expected(frame, stamps, columns["S_period_pos"][0]) + assert columns[f"{builtin}_{body}_{suffix}_{style}"] == ( + seconds if builtin == "S" else changes + ), (period, argument, builtin, style, body, suffix) + + +def test_omitted_arguments_keep_the_existing_chart_resolution(argument_runs): + for columns in argument_runs.values(): + for builtin in BUILTINS: + assert columns[f"{builtin}_omitted"] == columns[f"{builtin}_period_pos"] + + +def test_change_evaluates_each_string_once_and_can_switch_to_explicit(argument_runs): + for (period, argument), columns in argument_runs.items(): + stamps = _clock_stamps(period) + _, chart = _expected("chart", stamps, columns["S_period_pos"][0]) + _, explicit = _expected("explicit", stamps, columns["S_period_pos"][0]) + dynamic = [chart[index] if index % 2 == 0 else explicit[index] + for index in range(len(stamps))] + assert columns["C_dynamic_pos"] == dynamic + assert columns["C_dynamic_named"] == dynamic + assert columns["C_once_pos"] == (explicit if argument else chart) + assert columns["C_once_named"] == (explicit if argument else chart) + assert columns["callCount"] == [2.0 * (index + 1) for index in range(len(stamps))] diff --git a/tests/test_e2e_temporary_name_hygiene.py b/tests/test_e2e_temporary_name_hygiene.py index e9295016..2e3ec38f 100644 --- a/tests/test_e2e_temporary_name_hygiene.py +++ b/tests/test_e2e_temporary_name_hygiene.py @@ -117,8 +117,14 @@ def test_every_temporary_the_emitter_declares_is_reserved(): # Temporaries derived from emitted C++: every underscore-prefixed local # declared inside a generated lambda that is not a name of the script. names = set() - for name in TAPES: - source = (FIXTURES / f"{name}.pine").read_text() + sources = [(FIXTURES / f"{name}.pine").read_text() for name in TAPES] + sources.append( + HEAD + 'frameChoice = input.timeframe("")\n' + 'secondsValue = timeframe.in_seconds(frameChoice)\n' + 'changedValue = timeframe.change(timeframe = frameChoice)\n' + 'plot(secondsValue)\nplot(changedValue ? 1 : 0)\n' + ) + for source in sources: authored = {t.value for t in Lexer(source).tokenize() if t.type.name == "IDENT"} for line in transpile(source).splitlines(): if "[&]" in line: From 6453949ced44ed9da25d6c29c24b87648e5294f7 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 03:24:39 +0800 Subject: [PATCH 02/16] Preserve timeframe arguments and generated-name safety Resolve dotted builtin signatures consistently and preserve explicit strings, chart-period fallback and single evaluation. Reserve generated conversion names. --- CHANGELOG.md | 6 ++- pineforge_codegen/codegen/helpers.py | 6 ++- pineforge_codegen/codegen/visit_call.py | 56 +++++++++++++++---------- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57f4e358..fb497781 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,11 @@ supported as exact pairs; on the 0.x line they are independent. See the - `timeframe.in_seconds` and `timeframe.change` resolve an evaluated empty string to the chart period, including input, variable, function-return and named arguments. The argument is evaluated once, and nonempty strings keep - their explicit period. (#394) + their explicit period. Dotted builtin signatures retain supplied arguments, + including escaped string literals and VWAP anchors. (#394) +- Script identifiers spelled like integer-to-float conversion temporaries, + including the underscore suffixes of nested conversions, now use the + generator's reserved-name escaping. - A zone name the engine rejects now makes calendar-form `timestamp()` stop the run at its first evaluation, instead of silently calculating a UTC value. The paired engine reports a typed timezone error identifying the failed bar. diff --git a/pineforge_codegen/codegen/helpers.py b/pineforge_codegen/codegen/helpers.py index 9b5236cc..40a79b20 100644 --- a/pineforge_codegen/codegen/helpers.py +++ b/pineforge_codegen/codegen/helpers.py @@ -543,7 +543,7 @@ def cpp_code_only(cpp: str) -> str: _pf_number_text _pf_substring_text _pf_substring_begin _pf_substring_end _pf_substring_error _pf_array_new_size _pf_array_new_value _pf_array_new_size_type _pf_array_new_error - _pf_read _pf_v5_l _pf_v5_r _pf_wide_l _pf_wide_r + _pf_read _pf_v5_l _pf_v5_r _pf_wide_l _pf_wide_r _pf_w _pf_symbol _pf_colon _pf_color _pf_bar_back _pf_ema_seeding _pf_history_array _pf_history_offset _pf_matrix_target _pf_array_index_type @@ -551,7 +551,8 @@ def cpp_code_only(cpp: str) -> str: _pf_sort_left _pf_sort_right _pf_sort_left_na _pf_sort_right_na """.split()) # These numbered members and locals keep their source-order spelling rather -# than allocating against authored names. Reserve their complete spellings, +# than allocating against authored names. Double coercions append underscores +# to _pf_w to distinguish nested temporaries. Reserve their complete spellings, # just as for fixed temporaries, without reserving ordinary underscore names. _CPP_TEMPORARY_PATTERN = re.compile( r"_v\d+|_secval_\d+(?:_v\d+)?|_tuple_result_\d+(?:_elem_\d+)?|_tuple_unused_\d+|__switch_val_\d+" @@ -561,6 +562,7 @@ def cpp_code_only(cpp: str) -> str: r"|_pf_every_bar_ta_\d+|_pf_shared_\d+_\d+|__pf_array\w*|__pf_raw_\w+" r"|_pf_collection_hist_\w+" r"|_pf_substring_arg\d+" + r"|_pf_w_+" r"|_req_sec_(?:lower_tf_)?\d+(?:_\d+)?|_precalc_\w+|_sec\d+_\w+" r"|_pf_(?:sec|rec)_missing_\d+|_eval_security_\d+" r"|_RuntimeSec\d+|_runtime_(?:feed|request)_\d+" diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index fb34cb61..eb97f7bd 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -3219,26 +3219,36 @@ def _visit_func_call(self, node: FuncCall) -> str: argument = self._resolve_func_args( node, f"timeframe.{func_name}", ).get("timeframe") - tf_arg = ( - "script_tf_" if argument is None else - "(_pf_timeframe_arg.empty() ? script_tf_ : _pf_timeframe_arg)" - ) - if func_name == "change": - # Keep the symbol-clock overload for calendar and - # session-anchored intraday boundaries. - call = ( - f'tf_change(prev_bar_timestamp_, current_bar_.timestamp, {tf_arg}, ' - 'syminfo_.timezone, syminfo_.session)' - ) - else: - call = f'tf_to_seconds({tf_arg})' + + def timeframe_call(tf_arg: str) -> str: + if func_name == "change": + # Keep the symbol-clock overload for calendar and + # session-anchored intraday boundaries. + return ( + f'tf_change(prev_bar_timestamp_, current_bar_.timestamp, {tf_arg}, ' + 'syminfo_.timezone, syminfo_.session)' + ) + return f'tf_to_seconds({tf_arg})' + if argument is None: - return call + return timeframe_call("script_tf_") + argument_cpp = self._visit_expr(argument) + if cpp_is_plain_read(argument_cpp): + # Keep resolved locals and parameters as direct engine + # arguments when nonempty. Stable reads can be checked + # without introducing another binding. + return ( + f'(({argument_cpp}).empty() ? {timeframe_call("script_tf_")} : ' + f'{timeframe_call(argument_cpp)})' + ) # A function-return argument can carry state. Evaluate it # once before testing emptiness and passing the chosen value. + call = timeframe_call( + "(_pf_timeframe_arg.empty() ? script_tf_ : _pf_timeframe_arg)" + ) return ( '([&]() { const std::string _pf_timeframe_arg = ' - f'({self._visit_expr(argument)}); return {call}; }}())' + f'({argument_cpp}); return {call}; }}())' ) # Defensive: support_checker.NOT_YET_FUNC should already have rejected # any unhandled timeframe.* call. Reaching here implies the checker was @@ -4418,18 +4428,20 @@ def _resolve_func_args(self, node: FuncCall, sig_key: str) -> dict: Uses the PineScript parameter ordering from signatures registry. """ - # sig_key is like "strategy.entry" -> namespace="strategy", func_name="entry" + # Namespace records use their short name; dotted builtin records use + # the complete key in BUILTIN_FUNCTIONS (e.g. timeframe.change). parts = sig_key.split(".", 1) + intrinsic = ( + sigs.get_intrinsic(parts[0], parts[1]) if len(parts) == 2 else None + ) + if intrinsic is None: + intrinsic = sigs.get_intrinsic(None, sig_key) validate_intrinsic_keywords( - sigs.get_intrinsic(parts[0], parts[1]) if len(parts) == 2 - else sigs.get_intrinsic(None, sig_key), + intrinsic, node.kwargs, pine_version=self.ctx.ast.version, ) - if len(parts) == 2: - param_names = sigs.get_param_names(parts[0], parts[1]) or [] - else: - param_names = sigs.get_param_names(None, sig_key) or [] + param_names = intrinsic.param_names if intrinsic is not None else [] result: dict = {} # Map positional args by parameter name for i, arg in enumerate(node.args): From cd3b13f43b7f5da4660c9cb5725792f5333900ea Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 06:03:06 +0800 Subject: [PATCH 03/16] Update anchor test expectations for conditional timeframe calls The existing symbol-clock anchor tests now check both calls in the emitted empty-argument conditional. Add pure-Python checks of branch direction and single evaluation, and make the temporary-name sweep reach the argument lambda. Product code is unchanged by this test update. --- tests/test_anchor_tf_change.py | 25 +++++++++++- tests/test_anchor_vwap.py | 8 +++- tests/test_e2e_temporary_name_hygiene.py | 6 ++- tests/test_empty_timeframe_emission.py | 48 ++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 tests/test_empty_timeframe_emission.py diff --git a/tests/test_anchor_tf_change.py b/tests/test_anchor_tf_change.py index d8e08b23..9a0f2730 100644 --- a/tests/test_anchor_tf_change.py +++ b/tests/test_anchor_tf_change.py @@ -26,7 +26,30 @@ def _pine(body: str) -> str: def _calls(cpp: str, symbol: str) -> list[str]: """Every `symbol(...)` call expression in the generated C++ (one line each).""" - return [m.group(0) for m in re.finditer(rf"\b{re.escape(symbol)}\([^;]*", cpp)] + calls = [] + for match in re.finditer(rf"\b{re.escape(symbol)}\(", cpp): + depth, quote, escaped = 0, None, False + for index in range(match.end() - 1, len(cpp)): + char = cpp[index] + if quote: + if escaped: + escaped = False + elif char == "\\": + escaped = True + elif char == quote: + quote = None + elif char in ("\"", "'"): + quote = char + elif char == "(": + depth += 1 + elif char == ")": + depth -= 1 + if depth == 0: + calls.append(cpp[match.start():index + 1]) + break + else: + raise AssertionError("Unclosed generated call: " + symbol) + return calls def test_timeframe_change_uses_symbol_clock_overload(): diff --git a/tests/test_anchor_vwap.py b/tests/test_anchor_vwap.py index a97291c6..8376915d 100644 --- a/tests/test_anchor_vwap.py +++ b/tests/test_anchor_vwap.py @@ -67,7 +67,13 @@ def test_ta_vwap_bands_form_threads_symbol_clock(): assert calls, cpp for call in calls: assert ("current_bar_.timestamp, syminfo_.timezone, syminfo_.session, " - "tf_change(") in call, call + "((std::string(\"D\")).empty() ? tf_change(") in call, call + assert ( + 'tf_change(prev_bar_timestamp_, current_bar_.timestamp, script_tf_, ' + 'syminfo_.timezone, syminfo_.session) : ' + 'tf_change(prev_bar_timestamp_, current_bar_.timestamp, std::string("D"), ' + 'syminfo_.timezone, syminfo_.session)' + ) in call, call def test_vwap_prelude_shim_gated_on_usage(): diff --git a/tests/test_e2e_temporary_name_hygiene.py b/tests/test_e2e_temporary_name_hygiene.py index 2e3ec38f..7f80f775 100644 --- a/tests/test_e2e_temporary_name_hygiene.py +++ b/tests/test_e2e_temporary_name_hygiene.py @@ -120,8 +120,9 @@ def test_every_temporary_the_emitter_declares_is_reserved(): sources = [(FIXTURES / f"{name}.pine").read_text() for name in TAPES] sources.append( HEAD + 'frameChoice = input.timeframe("")\n' - 'secondsValue = timeframe.in_seconds(frameChoice)\n' - 'changedValue = timeframe.change(timeframe = frameChoice)\n' + 'chosenFrame() => frameChoice\n' + 'secondsValue = timeframe.in_seconds(chosenFrame())\n' + 'changedValue = timeframe.change(timeframe = chosenFrame())\n' 'plot(secondsValue)\nplot(changedValue ? 1 : 0)\n' ) for source in sources: @@ -131,4 +132,5 @@ def test_every_temporary_the_emitter_declares_is_reserved(): names |= {n for n in DECL.findall(line) if n not in authored and not n.startswith("pf_safe_")} assert names, "the tapes' C++ declares no temporary" + assert "_pf_timeframe_arg" in names assert sorted(n for n in names if not is_emitter_temporary(n)) == [] diff --git a/tests/test_empty_timeframe_emission.py b/tests/test_empty_timeframe_emission.py new file mode 100644 index 00000000..5ca2a65b --- /dev/null +++ b/tests/test_empty_timeframe_emission.py @@ -0,0 +1,48 @@ +"""Empty argument selection is covered without native engine inputs.""" + +import pytest + +from pineforge_codegen import transpile + + +def engine_call(function, argument): + if function == "change": + return ( + "tf_change(prev_bar_timestamp_, current_bar_.timestamp, " + + argument + ", syminfo_.timezone, syminfo_.session)" + ) + return "tf_to_seconds(" + argument + ")" + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("argument,lowered", [ + ('""', 'std::string("")'), + ('"240"', 'std::string("240")'), + ("frameChoice", "frameChoice"), +]) +def test_empty_plain_argument_selects_chart_and_nonempty_selects_argument(function, argument, lowered): + cpp = transpile( + '//@version=6\nstrategy("Chart timeframe selection")\n' + 'frameChoice = input.timeframe("")\n' + f"value = timeframe.{function}({argument})\n" + ) + expected = ( + "((" + lowered + ").empty() ? " + + engine_call(function, "script_tf_") + " : " + + engine_call(function, lowered) + ")" + ) + assert expected in cpp + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +def test_call_argument_is_evaluated_once_before_chart_selection(function): + cpp = transpile( + '//@version=6\nstrategy("Chart timeframe selection")\n' + 'frameChoice = input.timeframe("")\n' + 'chosenFrame() => frameChoice\n' + f"value = timeframe.{function}(chosenFrame())\n" + ) + assert "const std::string _pf_timeframe_arg = " in cpp + selected = "(_pf_timeframe_arg.empty() ? script_tf_ : _pf_timeframe_arg)" + assert engine_call(function, selected) in cpp + assert cpp.count("const std::string _pf_timeframe_arg = (chosenFrame());") == 1 From 8802058aee5a62c6f304f8f6beab0f0b37fe675e Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 07:50:59 +0800 Subject: [PATCH 04/16] Validate timeframe argument types Use the chart period for a generic missing timeframe argument and refuse numeric values for string parameters during analysis. Cover named arguments, helper calls, source locations and generated translation units. --- CHANGELOG.md | 4 +- pineforge_codegen/analyzer/base.py | 25 +++++++ pineforge_codegen/codegen/visit_call.py | 4 +- scripts/gen_diagnostics_catalog.py | 13 +++- tests/test_empty_timeframe_emission.py | 87 ++++++++++++++++++++++++- 5 files changed, 127 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb497781..32064caa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,9 @@ supported as exact pairs; on the 0.x line they are independent. See the - `timeframe.in_seconds` and `timeframe.change` resolve an evaluated empty string to the chart period, including input, variable, function-return and named arguments. The argument is evaluated once, and nonempty strings keep - their explicit period. Dotted builtin signatures retain supplied arguments, + their explicit period. A generic `na` argument also selects the chart period; + numeric timeframe arguments are refused during analysis with a source + location. Dotted builtin signatures retain supplied arguments, including escaped string literals and VWAP anchors. (#394) - Script identifiers spelled like integer-to-float conversion temporaries, including the underscore suffixes of nested conversions, now use the diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index 772e2426..6f903a6a 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -7589,6 +7589,31 @@ def _visit_FuncCall(self, node: FuncCall) -> PineType: visited_member_arg_types[id(arg)] = self._visit(arg) for val in node.kwargs.values(): visited_member_arg_types[id(val)] = self._visit(val) + if (self._pine_version == 6 + and not self._in_v5_library_definition(node) + and isinstance(obj, Identifier) and obj.name == "timeframe" + and self._value_bindings.declaration_bindings.get(id(obj)) is None): + intrinsic = sigs.BUILTIN_FUNCTIONS.get(f"{obj.name}.{member}") + if intrinsic is not None: + bound = self._bind_callable_args(node, intrinsic.param_names) + for parameter, value in zip(intrinsic.primary.params, bound): + if value is None or parameter.pine_type != PineType.STRING: + continue + # An untyped helper's provisional float is not proof + # that the caller supplies a numeric value. + actual_type = self._certain_scalar_type(value) + if actual_type in (PineType.INT, PineType.FLOAT): + # Generic na adopts the parameter's string type; + # a numeric value cannot satisfy that signature. + function_name = intrinsic.name + argument_type = actual_type.value + parameter_type = parameter.pine_type.value + name = parameter.name + self._error( + f"Cannot call '{function_name}' with a {argument_type} " + f"argument for {parameter_type} parameter '{name}'.", + value.loc, + ) # UDT method call-site typing. Method definitions may contain an # untyped parameter history read; resolve receiver + args here and # apply the same deferred map-history gate as regular UDFs before diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index eb97f7bd..4b3faf94 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -3232,7 +3232,9 @@ def timeframe_call(tf_arg: str) -> str: if argument is None: return timeframe_call("script_tf_") - argument_cpp = self._visit_expr(argument) + argument_cpp = self._visit_rhs_value( + argument, target_cpp_type="std::string", + ) if cpp_is_plain_read(argument_cpp): # Keep resolved locals and parameters as direct engine # arguments when nonempty. Stable reads can be checked diff --git a/scripts/gen_diagnostics_catalog.py b/scripts/gen_diagnostics_catalog.py index 9a7e3578..d0858ed6 100644 --- a/scripts/gen_diagnostics_catalog.py +++ b/scripts/gen_diagnostics_catalog.py @@ -1071,8 +1071,8 @@ def dump(catalog: dict) -> str: } -# Declaration presentation follows the extracted template through code allocation. -STRATEGY_DECLARATION_PRESENTATION = { +# Authored presentation follows the extracted template through code allocation. +DIAGNOSTIC_PRESENTATION = { "strategy(): too many positional arguments (expected {expected_count}, got {actual_count})": ( "A strategy declaration has more positional values than its signature accepts. " "Remove the extra values or use the declared parameter names.", @@ -1088,6 +1088,13 @@ def dump(catalog: dict) -> str: "Supply a literal or an expression over earlier constants.", "Strategy setting ''{name}'' must be a compile-time constant.", ), + "Cannot call ''{function_name}'' with a {argument_type} argument for " + "{parameter_type} parameter ''{name}''.": ( + "A numeric value cannot be used for a string parameter. Pass a string value " + "of the declared parameter type; a permitted generic na adopts that type.", + "Argument ''{name}'' of ''{function_name}'' has type {argument_type}; " + "expected {parameter_type}.", + ), } # Public reason vocabulary for the located series-timeframe diagnostic. @@ -1137,7 +1144,7 @@ def main(argv=None) -> int: entry["user_message"] = sentence entry["explanation"] = entry["explanation"] or _ALLOWLIST_EXPLANATION for entry in catalog["codes"].values(): - presentation = STRATEGY_DECLARATION_PRESENTATION.get(entry["message"]) + presentation = DIAGNOSTIC_PRESENTATION.get(entry["message"]) if presentation is not None: explanation, sentence = presentation entry["explanation"] = entry["explanation"] or explanation diff --git a/tests/test_empty_timeframe_emission.py b/tests/test_empty_timeframe_emission.py index 5ca2a65b..eb4bafc0 100644 --- a/tests/test_empty_timeframe_emission.py +++ b/tests/test_empty_timeframe_emission.py @@ -1,8 +1,13 @@ -"""Empty argument selection is covered without native engine inputs.""" +"""Timeframe argument typing and selection, with opt-in full-TU checks.""" import pytest from pineforge_codegen import transpile +from pineforge_codegen.analyzer import Analyzer +from pineforge_codegen.diagnostic_codes import UNCATALOGUED +from pineforge_codegen.errors import CompileError, Level, Phase +from pineforge_codegen.lexer import Lexer +from pineforge_codegen.parser import Parser def engine_call(function, argument): @@ -46,3 +51,83 @@ def test_call_argument_is_evaluated_once_before_chart_selection(function): selected = "(_pf_timeframe_arg.empty() ? script_tf_ : _pf_timeframe_arg)" assert engine_call(function, selected) in cpp assert cpp.count("const std::string _pf_timeframe_arg = (chosenFrame());") == 1 + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +def test_na_argument_uses_string_missing_value_before_chart_selection(function, named): + argument = "timeframe = na" if named else "na" + cpp = transpile( + '//@version=6\nstrategy("Missing timeframe")\n' + f"value = timeframe.{function}({argument})\n" + ) + missing = "na()" + expected = ( + "((" + missing + ").empty() ? " + + engine_call(function, "script_tf_") + " : " + + engine_call(function, missing) + ")" + ) + assert expected in cpp + assert "(na()).empty()" not in cpp + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("parameter", ["frameArg", "string frameArg"]) +def test_helper_string_parameter_keeps_chart_selection(function, parameter): + cpp = transpile( + '//@version=6\nstrategy("Helper timeframe")\n' + f"withFrame({parameter}) => timeframe.{function}(frameArg)\n" + 'value = withFrame("")\n' + ) + expected = ( + "((frameArg).empty() ? " + + engine_call(function, "script_tf_") + " : " + + engine_call(function, "frameArg") + ")" + ) + assert expected in cpp + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +@pytest.mark.parametrize("argument,pine_type", [ + ("0", "int"), + ("17", "int"), + ("-3", "int"), + ("0.0", "float"), + ("1.25", "float"), + ("-2.5", "float"), +]) +def test_numeric_timeframe_is_refused_during_analysis(function, named, argument, pine_type): + supplied = f"timeframe = {argument}" if named else argument + source = ( + '//@version=6\nstrategy("Numeric timeframe")\n' + f"value = timeframe.{function}({supplied})\n" + ) + ast = Parser(Lexer(source).tokenize(), source=source).parse() + with pytest.raises(CompileError) as refused: + Analyzer(ast).analyze() + diagnostic, = refused.value.diagnostics + assert diagnostic.level == Level.ERROR + assert diagnostic.phase == Phase.ANALYZER + assert diagnostic.code.startswith("PF-E") + assert diagnostic.code not in UNCATALOGUED.values() + assert f"timeframe.{function}" in diagnostic.message + assert pine_type in diagnostic.message + assert "string" in diagnostic.message + assert diagnostic.location.line == 3 + assert diagnostic.location.col == source.splitlines()[2].index(argument) + 1 + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +@pytest.mark.parametrize("optimization", ["-O0", "-O2"]) +def test_na_timeframe_full_translation_unit_compiles(function, named, optimization): + from tests._compile import compile_cpp + + argument = "timeframe = na" if named else "na" + cpp = transpile( + '//@version=6\nstrategy("Missing timeframe compilation")\n' + f"value = timeframe.{function}({argument})\n" + ) + compile_cpp(cpp, label=f"timeframe.{function}({argument}) {optimization}", + extra_flags=[optimization]) From 7a28a2968ea9dd520f414712b7347c6bb656e1f7 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 08:52:54 +0800 Subject: [PATCH 05/16] Register the timeframe argument diagnostic Include only the stock-generated PF-E2095 definition and pin. Preserve all prior catalog entry bytes and their existing order. --- pineforge_codegen/diagnostics_catalog.json | 1 + tests/fixtures/diagnostic_codes_pin.json | 1 + 2 files changed, 2 insertions(+) diff --git a/pineforge_codegen/diagnostics_catalog.json b/pineforge_codegen/diagnostics_catalog.json index eb4bc5ff..91f7a3c6 100644 --- a/pineforge_codegen/diagnostics_catalog.json +++ b/pineforge_codegen/diagnostics_catalog.json @@ -235,6 +235,7 @@ "PF-E2090": {"severity": "error", "area": "analysis", "message": "input.time() needs a constant default in Pine v6: timestamp() with a time zone and calendar numbers is not a constant. Use the date-string form, for example timestamp(\"01 Jan 2023 00:00 +0000\").", "hint": "Use a single date string in timestamp or an integer timestamp literal for the default.", "explanation": "The time-zone overload of timestamp, which takes a zone and calendar numbers, returns a simple int, while a Pine v6 input.time default requires a const int.", "args": {}, "user_message": "input.time() requires a constant default in Pine v6."}, "PF-E2091": {"severity": "error", "area": "analysis", "message": "plot() cannot have a linewidth below 1 in Pine v6: use a value of 1 or greater.", "hint": "Set the plot linewidth to an integer of at least 1.", "explanation": "A Pine v6 plot call cannot use an integer literal below one for linewidth; variable and calculated widths retain their existing handling.", "args": {}, "user_message": "plot() requires a linewidth of at least 1 in Pine v6."}, "PF-E2092": {"severity": "error", "area": "analysis", "message": "\"range\" cannot be used as a variable or function name in Pine v6: use another name.", "hint": "Rename the variable, function or method declaration.", "explanation": "Pine v6 reserves range as a variable and callable declaration name; parameters, user type fields and member access retain their existing handling.", "args": {}, "user_message": "Choose another variable, function or method name instead of range in Pine v6."}, + "PF-E2095": {"severity": "error", "area": "analysis", "message": "Cannot call ''{function_name}'' with a {argument_type} argument for {parameter_type} parameter ''{name}''.", "hint": null, "explanation": "A numeric value cannot be used for a string parameter. Pass a string value of the declared parameter type; a permitted generic na adopts that type.", "args": {"function_name": {"kind": "identifier"}, "argument_type": {"kind": "type"}, "parameter_type": {"kind": "type"}, "name": {"kind": "identifier"}}, "user_message": "Argument ''{name}'' of ''{function_name}'' has type {argument_type}; expected {parameter_type}."}, "PF-E3001": {"severity": "error", "area": "request-security", "message": "request.security timeframe ''{name}'' cannot be resolved before the first bar: PineForge registers every requested timeframe before the script runs.", "hint": "Pass a literal, an input or a global to the helper that holds request.security.", "explanation": "The timeframe given to request.security is a name whose value PineForge cannot compute before the first bar; use a literal, an input or a global.", "args": {"name": {"kind": "identifier"}}, "user_message": "The timeframe given to request.security is a name whose value PineForge cannot compute before the first bar."}, "PF-E3002": {"severity": "error", "area": "request-security", "message": "request.security timeframe expression cannot be resolved before the first bar: PineForge registers every requested timeframe before the script runs.", "hint": "Pass a literal, an input or a global to the helper that holds request.security.", "explanation": "The timeframe expression given to request.security cannot be computed before the first bar; use a literal, an input or a global.", "args": {}, "user_message": "The timeframe expression given to request.security cannot be computed before the first bar."}, "PF-E3003": {"severity": "error", "area": "request-security", "message": "request.security timeframe switch arms must contain one expression", "hint": "Compute multi-statement timeframe logic before the request.security call or rewrite it as pure switch arms.", "explanation": "A switch that picks a request.security timeframe must have exactly one expression in each arm; compute multi-statement logic beforehand.", "args": {}, "user_message": "A switch that picks a request.security timeframe must have exactly one expression in each arm."}, diff --git a/tests/fixtures/diagnostic_codes_pin.json b/tests/fixtures/diagnostic_codes_pin.json index 0d4aaa73..e8719a8e 100644 --- a/tests/fixtures/diagnostic_codes_pin.json +++ b/tests/fixtures/diagnostic_codes_pin.json @@ -236,6 +236,7 @@ "PF-E2090": "dcc4d05f50b881e5345de8c73ec7b7951bedc86c30a24d40bc0077fca5242a0d", "PF-E2091": "2d1b04fd25a4198619628b94bf8f90b4002f33e4081c2b780fea510c49e0b44a", "PF-E2092": "74b483fe343d239ec254f03e839b257611db5295b0433eabd15072f919950f3d", + "PF-E2095": "2f11b08a085935c9d4583e461d14594a607ca43b593b6a706bc042c425f3aa7c", "PF-E3001": "585dad07e797bbe2cfffa9f45e14dee040be5570cc13f8e46fa4c31e953fc92a", "PF-E3002": "db089893825afe5ad1d91934c21107fc58451cadf3d71de2539b9c5805549433", "PF-E3003": "02571beee5163d1596d8a11fe49336dfcf65f86d4fed13254bac6b36023c3c79", From c04553dfffdf0a617fcdab5abba0f02934aa2168 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:04:24 +0800 Subject: [PATCH 06/16] Disambiguate diagnostic templates by argument kinds Use recognized Pine types in declared type slots to resolve equally literal diagnostic templates. Keep stable code order for other ties and preserve qualifier identities, raw arguments and rendering. Pin the timeframe type diagnostic and its named arguments with direct regression controls. --- CHANGELOG.md | 6 ++-- pineforge_codegen/diagnostic_codes.py | 34 +++++++++++++++++++++-- tests/test_diagnostic_codes.py | 38 ++++++++++++++++++++++++++ tests/test_empty_timeframe_emission.py | 22 +++++++++++---- 4 files changed, 89 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db7be37d..98784ae1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,8 +26,10 @@ supported as exact pairs; on the 0.x line they are independent. See the string to the chart period, including input, variable, function-return and named arguments. The argument is evaluated once, and nonempty strings keep their explicit period. A generic `na` argument also selects the chart period; - numeric timeframe arguments are refused during analysis with a source - location. Dotted builtin signatures retain supplied arguments, + numeric timeframe arguments are refused during analysis with the stable + `PF-E2095` type diagnostic and a source location. Equally specific diagnostic + templates distinguish primitive types from qualifiers using their catalog + argument kinds. Dotted builtin signatures retain supplied arguments, including escaped string literals and VWAP anchors. (#394) - Script identifiers spelled like integer-to-float conversion temporaries, including the underscore suffixes of nested conversions, now use the diff --git a/pineforge_codegen/diagnostic_codes.py b/pineforge_codegen/diagnostic_codes.py index 4b8ca4c7..bcd6c7a8 100644 --- a/pineforge_codegen/diagnostic_codes.py +++ b/pineforge_codegen/diagnostic_codes.py @@ -14,6 +14,11 @@ never guessed: a text no template renders gets the uncatalogued code of its severity (``PF-E0000`` / ``PF-W0000``), which the test suite refuses. +For templates with the same amount of literal text, recognized Pine primitive +types in catalog-declared type arguments take precedence over open string +arguments. Remaining ties keep the stable code order; every candidate still +renders the same text. + The match never backtracks over the text's characters: each literal segment of a template goes to its leftmost place after the previous one, the last to the text's end, and every argument is the text between its literals -- the @@ -38,6 +43,8 @@ from pathlib import Path from typing import Any +from .symbols import PineType + CATALOG_PATH = Path(__file__).with_name("diagnostics_catalog.json") CATALOG_SCHEMA = "pineforge-diagnostics-catalog/v1" # Notes keep historical PF-W identities. The nonfatal fallback is shared; @@ -178,6 +185,7 @@ def render_diagnostic(code: str, args: dict) -> tuple[str, str | None]: _CANONICAL_INT = re.compile(r"-?(?:0|[1-9][0-9]*)") _CANONICAL_FLOAT = re.compile(r"-?(?:0|[1-9][0-9]*)\.[0-9]+") +_PINE_TYPE_NAMES = frozenset(pine_type.value for pine_type in PineType) def _segments(parts: list) -> tuple[str, tuple[tuple[str, str], ...]]: @@ -313,6 +321,15 @@ def match(self, message: str, hint: str | None) -> dict | None: args[name] = value return args + def type_specificity(self, args: dict) -> int: + """Count recognized primitive types in this template's type arguments. + + Other spellings stay raw: this only breaks a literal-specificity tie, + without rejecting identifiers, composite types or open diagnostic text. + """ + return sum(value in _PINE_TYPE_NAMES for name, value in args.items() + if self._kinds.get(name) == "type") + @lru_cache(maxsize=1) def _matchers() -> dict[str, list[_Matcher]]: @@ -322,8 +339,8 @@ def _matchers() -> dict[str, list[_Matcher]]: continue by_severity[entry["severity"]].append(_Matcher(code, entry)) for matchers in by_severity.values(): - # The template with the most literal text wins: a message two templates - # render reads as the more specific one (both render it byte for byte). + # Prefer literal text first. Argument types can resolve equal literal + # specificity in classify; remaining ties keep this stable code order. matchers.sort(key=lambda m: (-m.specificity, m.code)) return by_severity @@ -336,14 +353,25 @@ def classify(severity: str, message: str, hint: str | None = None) -> tuple[str, no template renders gets ``PF-E0000`` for an error or ``PF-W0000`` for a warning/note, with its text as ``args`` and no new fallback identity. """ + best: tuple[str, dict] | None = None + best_specificity = -1 + best_types = -1 for matcher in _matchers().get(severity, ()): + if best is not None and matcher.specificity < best_specificity: + break if matcher.has_hint != (hint is not None): continue if not message.startswith(matcher.prefix) or not message.endswith(matcher.suffix): continue args = matcher.match(message, hint) if args is not None: - return matcher.code, args + types = matcher.type_specificity(args) + if best is None or types > best_types: + best = (matcher.code, args) + best_specificity = matcher.specificity + best_types = types + if best is not None: + return best args = {"message": message} if hint is not None: args["hint"] = hint diff --git a/tests/test_diagnostic_codes.py b/tests/test_diagnostic_codes.py index 8090ba70..203a1831 100644 --- a/tests/test_diagnostic_codes.py +++ b/tests/test_diagnostic_codes.py @@ -141,6 +141,44 @@ def test_classify_reads_arguments_raw(): assert render_diagnostic(code, args) == ("Undefined variable: 'rsiLen'", None) +@pytest.mark.parametrize("message, expected_code, expected_args", [ + ( + "Cannot call 'timeframe.change' with a int argument for string parameter 'timeframe'.", + "PF-E2095", + {"function_name": "timeframe.change", "name": "timeframe", + "argument_type": "int", "parameter_type": "string"}, + ), + ( + "Cannot call 'chooseFrame' with a float argument for string parameter 'candidate'.", + "PF-E2095", + {"function_name": "chooseFrame", "name": "candidate", + "argument_type": "float", "parameter_type": "string"}, + ), + ( + "Cannot call 'strategy.risk.allow_entry_in' with a series argument for simple parameter 'value'.", + "PF-E2080", + {"function_name": "strategy.risk.allow_entry_in", "name": "value", + "argument_qualifier": "series", "parameter_qualifier": "simple"}, + ), + ( + "Cannot call 'keep' with a input argument for const parameter 'amount'.", + "PF-E2080", + {"function_name": "keep", "name": "amount", + "argument_qualifier": "input", "parameter_qualifier": "const"}, + ), + ( + "matrix>: nested collection element types not supported in v1.", + "PF-E1004", + {"element_type": "array"}, + ), +]) +def test_catalog_argument_kinds_disambiguate_matching_templates( + message, expected_code, expected_args, +): + assert classify("error", message) == (expected_code, expected_args) + assert render_diagnostic(expected_code, expected_args) == (message, None) + + def test_uncatalogued_text_keeps_a_code(): diagnostic = Diagnostic(Level.ERROR, Phase.ANALYZER, SourceLocation("t.pine", 1, 1, 1), "boom") diff --git a/tests/test_empty_timeframe_emission.py b/tests/test_empty_timeframe_emission.py index eb4bafc0..07b198a7 100644 --- a/tests/test_empty_timeframe_emission.py +++ b/tests/test_empty_timeframe_emission.py @@ -4,7 +4,7 @@ from pineforge_codegen import transpile from pineforge_codegen.analyzer import Analyzer -from pineforge_codegen.diagnostic_codes import UNCATALOGUED +from pineforge_codegen.diagnostic_codes import render_diagnostic from pineforge_codegen.errors import CompileError, Level, Phase from pineforge_codegen.lexer import Lexer from pineforge_codegen.parser import Parser @@ -109,11 +109,21 @@ def test_numeric_timeframe_is_refused_during_analysis(function, named, argument, diagnostic, = refused.value.diagnostics assert diagnostic.level == Level.ERROR assert diagnostic.phase == Phase.ANALYZER - assert diagnostic.code.startswith("PF-E") - assert diagnostic.code not in UNCATALOGUED.values() - assert f"timeframe.{function}" in diagnostic.message - assert pine_type in diagnostic.message - assert "string" in diagnostic.message + assert diagnostic.code == "PF-E2095" + assert diagnostic.args == { + "function_name": f"timeframe.{function}", + "name": "timeframe", + "argument_type": pine_type, + "parameter_type": "string", + } + assert diagnostic.message == ( + f"Cannot call 'timeframe.{function}' with a {pine_type} " + "argument for string parameter 'timeframe'." + ) + assert diagnostic.hint is None + assert render_diagnostic(diagnostic.code, diagnostic.args) == ( + diagnostic.message, diagnostic.hint, + ) assert diagnostic.location.line == 3 assert diagnostic.location.col == source.splitlines()[2].index(argument) + 1 From f2f6ec8123b58bf53a96da4c8bfefe2e751154a8 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:57:38 +0800 Subject: [PATCH 07/16] Validate timeframe arguments using semantic types Reject non-string timeframe arguments through the intrinsic signature before C++ lowering, including helper parameters and boolean values. Preserve valid string calls and exercise complete native controls at distinct optimization levels. Declare the new diagnostic in the existing catalog delta fixture. --- CHANGELOG.md | 14 +- pineforge_codegen/analyzer/base.py | 71 ++++++---- pineforge_codegen/analyzer/types.py | 3 + pineforge_codegen/codegen/visit_call.py | 39 ++++++ tests/fixtures/diagnostic_notes/delta.json | 3 +- tests/test_empty_timeframe_emission.py | 33 ++++- tests/test_timeframe_argument_types.py | 155 +++++++++++++++++++++ tests/test_timeframe_string_tu.py | 77 ++++++++++ 8 files changed, 360 insertions(+), 35 deletions(-) create mode 100644 tests/test_timeframe_argument_types.py create mode 100644 tests/test_timeframe_string_tu.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 98784ae1..41136894 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,12 +25,14 @@ supported as exact pairs; on the 0.x line they are independent. See the - `timeframe.in_seconds` and `timeframe.change` resolve an evaluated empty string to the chart period, including input, variable, function-return and named arguments. The argument is evaluated once, and nonempty strings keep - their explicit period. A generic `na` argument also selects the chart period; - numeric timeframe arguments are refused during analysis with the stable - `PF-E2095` type diagnostic and a source location. Equally specific diagnostic - templates distinguish primitive types from qualifiers using their catalog - argument kinds. Dotted builtin signatures retain supplied arguments, - including escaped string literals and VWAP anchors. (#394) + their explicit period. Bare `na` remains accepted; the value returned by + `timeframe.change(na)` is still awaiting verification against TradingView. + Non-string timeframe arguments are refused before C++ emission with the + stable `PF-E2095` type diagnostic and a source location, including input + values, reassignments, indicator results and helper parameters. Equally + specific diagnostic templates distinguish primitive types from qualifiers + using their catalog argument kinds. Dotted builtin signatures retain supplied + arguments, including escaped string literals and VWAP anchors. (#394) - Script identifiers spelled like integer-to-float conversion temporaries, including the underscore suffixes of nested conversions, now use the generator's reserved-name escaping. diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index 6f903a6a..293ae5bb 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -7459,6 +7459,51 @@ def _visit_Ternary(self, node: Ternary) -> PineType: return main_type return result_type + def _check_timeframe_string_arguments( + self, node: FuncCall, + parameter_types: dict[str, PineType | TypeSpec] | None = None, + ) -> None: + """Check string signature slots using lexical and written-call types.""" + if self._pine_version != 6 or self._in_v5_library_definition(node): + return + callee = node.callee + if (not isinstance(callee, MemberAccess) + or not isinstance(callee.object, Identifier) + or callee.object.name != "timeframe" + or self._value_bindings.declaration_bindings.get(id(callee.object)) is not None + or (parameter_types is not None and callee.object.name in parameter_types)): + return + intrinsic = sigs.BUILTIN_FUNCTIONS.get(f"timeframe.{callee.member}") + if intrinsic is None: + return + bound = self._bind_callable_args(node, intrinsic.param_names) + for parameter, value in zip(intrinsic.primary.params, bound): + if value is None or parameter.pine_type != PineType.STRING: + continue + actual = self._operator_operand_type(value, parameter_types) + if (actual is None and isinstance(value, MemberAccess) + and isinstance(value.object, Identifier) + and self._value_bindings.declaration_bindings.get(id(value.object)) is None + and (parameter_types is None or value.object.name not in parameter_types)): + variable_type = sigs.get_variable_type(value.object.name, value.member) + if variable_type != PineType.UNKNOWN: + actual = TypeSpec.primitive(variable_type.value) + if actual is None or actual in { + TypeSpec.primitive("string"), TypeSpec.primitive("na"), + }: + # Untyped helper parameters stay open until their written + # call supplies a type. Bare na adopts the string signature. + continue + function_name = intrinsic.name + argument_type = method_receiver_type_name(actual) or str(actual) + parameter_type = parameter.pine_type.value + name = parameter.name + self._error( + f"Cannot call '{function_name}' with a {argument_type} " + f"argument for {parameter_type} parameter '{name}'.", + value.loc, + ) + def _visit_FuncCall(self, node: FuncCall) -> PineType: if self._pine_version == 6 and not self._in_v5_library_definition(node): self._parameter_qualifiers.check_builtin(node) @@ -7589,31 +7634,7 @@ def _visit_FuncCall(self, node: FuncCall) -> PineType: visited_member_arg_types[id(arg)] = self._visit(arg) for val in node.kwargs.values(): visited_member_arg_types[id(val)] = self._visit(val) - if (self._pine_version == 6 - and not self._in_v5_library_definition(node) - and isinstance(obj, Identifier) and obj.name == "timeframe" - and self._value_bindings.declaration_bindings.get(id(obj)) is None): - intrinsic = sigs.BUILTIN_FUNCTIONS.get(f"{obj.name}.{member}") - if intrinsic is not None: - bound = self._bind_callable_args(node, intrinsic.param_names) - for parameter, value in zip(intrinsic.primary.params, bound): - if value is None or parameter.pine_type != PineType.STRING: - continue - # An untyped helper's provisional float is not proof - # that the caller supplies a numeric value. - actual_type = self._certain_scalar_type(value) - if actual_type in (PineType.INT, PineType.FLOAT): - # Generic na adopts the parameter's string type; - # a numeric value cannot satisfy that signature. - function_name = intrinsic.name - argument_type = actual_type.value - parameter_type = parameter.pine_type.value - name = parameter.name - self._error( - f"Cannot call '{function_name}' with a {argument_type} " - f"argument for {parameter_type} parameter '{name}'.", - value.loc, - ) + self._check_timeframe_string_arguments(node) # UDT method call-site typing. Method definitions may contain an # untyped parameter history read; resolve receiver + args here and # apply the same deferred map-history gate as regular UDFs before diff --git a/pineforge_codegen/analyzer/types.py b/pineforge_codegen/analyzer/types.py index f65daf91..c62ffa06 100644 --- a/pineforge_codegen/analyzer/types.py +++ b/pineforge_codegen/analyzer/types.py @@ -1535,6 +1535,9 @@ def _check_node(self, node): op, operands = node.op, (node.operand,) elif isinstance(node, Ternary): op, operands = "?:", (node.condition, node.true_val, node.false_val) + elif isinstance(node, FuncCall): + self._analyzer._check_timeframe_string_arguments(node, self._parameter_types) + return else: return self._analyzer._check_operator_operands(node, op, operands, self._parameter_types) diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index 4b3faf94..28191ca7 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -159,6 +159,7 @@ from ..limits import iter_ast_nodes from ..security_contexts import ticker_symbol_arg from ..symbols import PineType, TypeSpec, method_receiver_type_name +from ..analyzer.value_bindings import ValueBindings from ..method_binding import ( MethodBindError, bind_function_defaults, @@ -3232,6 +3233,44 @@ def timeframe_call(tf_arg: str) -> str: if argument is None: return timeframe_call("script_tf_") + actual = self._type_spec_from_expr(argument) + if (actual is None and isinstance(argument, MemberAccess) + and isinstance(argument.object, Identifier) + and self.ctx.identifier_decl_bindings.get(id(argument.object)) is None + and ValueBindings.declaration_of(argument.object) is None): + variable_type = sigs.get_variable_type( + argument.object.name, argument.member, + ) + if variable_type != PineType.UNKNOWN: + actual = TypeSpec.primitive(variable_type.value) + actual_cpp_type = self._text_type(argument) + argument_is_string = ( + actual == TypeSpec.primitive("string") + or (actual is None and actual_cpp_type == "std::string") + ) + if (not isinstance(argument, NaLiteral) + and (not argument_is_string or actual_cpp_type == "bool")): + # A call-site variant may resolve an untyped helper after + # analysis. Check before bool-to-text compatibility can + # turn a non-string value into a C++ string. + argument_type = ( + "bool" if actual_cpp_type == "bool" else + (method_receiver_type_name(actual) or str(actual)) + if actual is not None else + {"int": "int", "int64_t": "int", "double": "float"}.get( + actual_cpp_type, actual_cpp_type, + ) + ) + intrinsic = sigs.BUILTIN_FUNCTIONS[f"{namespace}.{func_name}"] + parameter = intrinsic.primary.params[0] + function_name = intrinsic.name + parameter_type = parameter.pine_type.value + name = parameter.name + self._codegen_error( + argument, + f"Cannot call '{function_name}' with a {argument_type} " + f"argument for {parameter_type} parameter '{name}'.", + ) argument_cpp = self._visit_rhs_value( argument, target_cpp_type="std::string", ) diff --git a/tests/fixtures/diagnostic_notes/delta.json b/tests/fixtures/diagnostic_notes/delta.json index 5027fc9a..fae0cb0d 100644 --- a/tests/fixtures/diagnostic_notes/delta.json +++ b/tests/fixtures/diagnostic_notes/delta.json @@ -4,7 +4,8 @@ "user_message" ], "added_codes": [ - "PF-E5087" + "PF-E5087", + "PF-E2095" ], "catalog_additions": { "PF-E5088": { diff --git a/tests/test_empty_timeframe_emission.py b/tests/test_empty_timeframe_emission.py index 07b198a7..add66391 100644 --- a/tests/test_empty_timeframe_emission.py +++ b/tests/test_empty_timeframe_emission.py @@ -132,12 +132,39 @@ def test_numeric_timeframe_is_refused_during_analysis(function, named, argument, @pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) @pytest.mark.parametrize("optimization", ["-O0", "-O2"]) def test_na_timeframe_full_translation_unit_compiles(function, named, optimization): - from tests._compile import compile_cpp + from tests._compile import run_emitted_tu argument = "timeframe = na" if named else "na" cpp = transpile( '//@version=6\nstrategy("Missing timeframe compilation")\n' f"value = timeframe.{function}({argument})\n" ) - compile_cpp(cpp, label=f"timeframe.{function}({argument}) {optimization}", - extra_flags=[optimization]) + driver = r''' +#include +#include +int main() { + std::vector bars(4); + for (std::size_t i = 0; i < bars.size(); ++i) { + bars[i].open = bars[i].high = bars[i].low = bars[i].close = 100.0 + i; + bars[i].volume = 1.0; + bars[i].timestamp = 1704067200000LL + static_cast(i) * 3600000LL; + } + GeneratedStrategy strategy; + strategy.run(bars.data(), bars.size(), "60", "60"); + if (!strategy.last_error().empty()) { + std::cerr << strategy.last_error() << "\n"; + return 1; + } + std::cout << "executed " << static_cast(strategy.value) << "\n"; +} +''' + output = run_emitted_tu( + cpp, driver, opt=optimization, + label=f"timeframe.{function}({argument})", + ) + # This row proves a real optimized build and execution. It records the + # observed value without settling the separate TradingView na-value check. + marker, observed = output.strip().split() + assert marker == "executed" + float(observed) + print(f"timeframe.{function}({argument}) {optimization}: {output.strip()}") diff --git a/tests/test_timeframe_argument_types.py b/tests/test_timeframe_argument_types.py new file mode 100644 index 00000000..b691358a --- /dev/null +++ b/tests/test_timeframe_argument_types.py @@ -0,0 +1,155 @@ +"""Timeframe string parameters use semantic types before C++ conversion.""" + +import pytest + +from pineforge_codegen import transpile +from pineforge_codegen.analyzer import Analyzer +from pineforge_codegen.diagnostic_codes import render_diagnostic +from pineforge_codegen.errors import CompileError, Level, Phase +from pineforge_codegen.lexer import Lexer +from pineforge_codegen.parser import Parser + + +HEADER = '//@version=6\nstrategy("Timeframe argument types")\n' +NON_STRINGS = [ + pytest.param("frames = input.int(14)\n", "frames", "int", id="integer-input"), + pytest.param("", "input.int(7)", "int", id="inline-integer-input"), + pytest.param("counter = 0\ncounter := counter + 1\n", "counter", "int", + id="reassigned-counter"), + pytest.param("", "syminfo.mintick", "float", id="member-value"), + pytest.param("", "timeframe.multiplier", "int", id="integer-timeframe-member"), + pytest.param("", "timeframe.isdaily", "bool", id="boolean-timeframe-member"), + pytest.param("", "strategy.closedtrades", "int", id="integer-strategy-member"), + pytest.param("", "barstate.isfirst", "bool", id="boolean-barstate-member"), + pytest.param("", "ta.sma(close, 3)", "float", id="indicator-result"), + pytest.param("", "input.float(2.25)", "float", id="float-input"), + pytest.param("", "true", "bool", id="boolean-literal"), + pytest.param("", "close > open", "bool", id="boolean-series"), + pytest.param("flag = input.bool(true)\n", "flag", "bool", id="boolean-input"), + pytest.param("flag = false\nflag := close > open\n", "flag", "bool", + id="reassigned-boolean"), + pytest.param("", "color.red", "color", id="color"), + pytest.param("frames = array.new(2, 1)\n", "frames", "array", + id="array"), + pytest.param("frames = matrix.new(1, 1, 2.0)\n", "frames", + "matrix", id="matrix"), + pytest.param("frames = map.new()\n", "frames", + "map", id="map"), + pytest.param('type FrameBox\n string frame\n' + 'boxValue = FrameBox.new("60")\n', "boxValue", "FrameBox", + id="object"), +] + + +def _assert_type_diagnostic(diagnostic, function, pine_type): + assert diagnostic.level == Level.ERROR + assert diagnostic.code == "PF-E2095" + assert diagnostic.args == { + "function_name": f"timeframe.{function}", + "name": "timeframe", + "argument_type": pine_type, + "parameter_type": "string", + } + assert diagnostic.hint is None + assert render_diagnostic(diagnostic.code, diagnostic.args) == ( + diagnostic.message, diagnostic.hint, + ) + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +@pytest.mark.parametrize("scope", [ + "direct", "helper", "helper-local", "named-helper", "typed-helper", +]) +@pytest.mark.parametrize("conversion", [False, True], ids=["alone", "bool-to-text"]) +@pytest.mark.parametrize("declarations,argument,pine_type", NON_STRINGS) +def test_non_string_timeframes_are_refused_by_semantic_type( + function, named, scope, conversion, declarations, argument, pine_type, +): + source = HEADER + declarations + if conversion: + source += "unrelatedString = str.tostring(close > open)\n" + value = argument if scope == "direct" else "frameArg" + if scope == "helper-local": + value = "localFrame" + supplied = f"timeframe = {value}" if named else value + call = f"timeframe.{function}({supplied})" + if scope == "direct": + source += f"value = {call}\n" + elif scope in ("helper", "named-helper", "typed-helper"): + parameter = f"{pine_type} frameArg" if scope == "typed-helper" else "frameArg" + actual = f"frameArg = {argument}" if scope == "named-helper" else argument + source += f"withFrame({parameter}) => {call}\nvalue = withFrame({actual})\n" + else: + source += ( + "withFrame(frameArg) =>\n" + " localFrame = frameArg\n" + f" {call}\n" + f"value = withFrame({argument})\n" + ) + ast = Parser(Lexer(source).tokenize(), source=source).parse() + with pytest.raises(CompileError) as refused: + Analyzer(ast).analyze() + diagnostic, = refused.value.diagnostics + _assert_type_diagnostic(diagnostic, function, pine_type) + assert diagnostic.phase == Phase.ANALYZER + line, = [(index, line) for index, line in enumerate(source.splitlines(), 1) + if call in line] + assert diagnostic.location.line == line[0] + assert diagnostic.location.col == line[1].index(supplied) + ( + len("timeframe = ") if named else 0 + ) + 1 + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +@pytest.mark.parametrize("conversion", [False, True], ids=["alone", "bool-to-text"]) +@pytest.mark.parametrize("body,pine_type", [ + ("input.int(14)", "int"), + ("syminfo.mintick", "float"), + ("close > open", "bool"), +]) +def test_helper_return_cannot_be_converted_to_a_timeframe_string( + function, named, conversion, body, pine_type, +): + source = HEADER + if conversion: + source += "unrelatedString = str.tostring(close > open)\n" + source += f"chosenFrame() => {body}\n" + supplied = "timeframe = chosenFrame()" if named else "chosenFrame()" + source += f"value = timeframe.{function}({supplied})\n" + with pytest.raises(CompileError) as refused: + transpile(source) + diagnostic, = refused.value.diagnostics + _assert_type_diagnostic(diagnostic, function, pine_type) + assert diagnostic.phase in (Phase.ANALYZER, Phase.CODEGEN) + assert diagnostic.location.line == len(source.splitlines()) + assert diagnostic.location.col == source.splitlines()[-1].index("chosenFrame()") + 1 + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +@pytest.mark.parametrize("argument,pine_type", [ + ("input.int(14)", "int"), + ("syminfo.mintick", "float"), + ("true", "bool"), +]) +def test_emitter_checks_type_before_implicit_string_conversion( + monkeypatch, function, named, argument, pine_type, +): + # An internal caller can reach emission after bypassing an analyzer guard. + # The emitter must still refuse the semantic type before bool text lowering. + monkeypatch.setattr(Analyzer, "_check_timeframe_string_arguments", + lambda *args, **kwargs: None, raising=False) + supplied = f"timeframe = {argument}" if named else argument + source = ( + HEADER + "unrelatedString = str.tostring(close > open)\n" + f"value = timeframe.{function}({supplied})\n" + ) + with pytest.raises(CompileError) as refused: + transpile(source) + diagnostic, = refused.value.diagnostics + _assert_type_diagnostic(diagnostic, function, pine_type) + assert diagnostic.phase == Phase.CODEGEN + assert diagnostic.location.line == 4 + assert diagnostic.location.col == source.splitlines()[3].index(argument) + 1 diff --git a/tests/test_timeframe_string_tu.py b/tests/test_timeframe_string_tu.py new file mode 100644 index 00000000..fb67f87b --- /dev/null +++ b/tests/test_timeframe_string_tu.py @@ -0,0 +1,77 @@ +"""Build and execute complete string-argument TUs at distinct optimizations.""" + +import pytest + +from pineforge_codegen import transpile +from tests._compile import run_emitted_tu + + +@pytest.mark.parametrize("optimization", ["-O0", "-O2"]) +def test_string_timeframe_controls_build_and_run(optimization): + source = ( + '//@version=6\nstrategy("String timeframe compilation")\n' + 'frameChoice = input.timeframe("")\n' + 'string missingFrame = na\n' + 'unrelatedString = str.tostring(close > open)\n' + 'chosenFrame() => frameChoice\n' + 'withSeconds(frameArg) => timeframe.in_seconds(frameArg)\n' + 'withChange(frameArg) => timeframe.change(timeframe = frameArg)\n' + 'typedSeconds(string frameArg) => timeframe.in_seconds(timeframe = frameArg)\n' + 'typedChange(string frameArg) => timeframe.change(frameArg)\n' + ) + arguments = { + "explicit": '"240"', + "empty": '""', + "variable": "frameChoice", + "return": "chosenFrame()", + "formatted": "str.tostring(60)", + "hostString": "strategy.position_entry_name", + "typedMissing": "missingFrame", + } + names = [] + for function in ("in_seconds", "change"): + for kind, argument in arguments.items(): + for named in (False, True): + name = f"{function}_{kind}_{'named' if named else 'positional'}" + names.append(name) + supplied = f"timeframe = {argument}" if named else argument + source += f"{name} = timeframe.{function}({supplied})\n" + for helper in ("withSeconds", "withChange", "typedSeconds", "typedChange"): + for kind, argument in (("empty", '""'), ("explicit", '"240"')): + name = f"{helper}_{kind}" + names.append(name) + source += f"{name} = {helper}({argument})\n" + driver = r''' +#include +#include +int main() { + std::vector bars(5); + for (std::size_t i = 0; i < bars.size(); ++i) { + bars[i].open = bars[i].high = bars[i].low = bars[i].close = 100.0 + i; + bars[i].volume = 1.0; + bars[i].timestamp = 1704067200000LL + static_cast(i) * 3600000LL; + } + GeneratedStrategy strategy; + strategy.run(bars.data(), bars.size(), "60", "60"); + if (!strategy.last_error().empty()) { + std::cerr << strategy.last_error() << "\n"; + return 1; + } +''' + for name in names: + driver += ( + f' std::cout << "{name} " << ' + f'static_cast(strategy.{name}) << "\\n";\n' + ) + driver += "}\n" + output = run_emitted_tu( + transpile(source), driver, opt=optimization, label="string timeframe controls", + ) + # Value parity has its own replay oracles. This test's target is building + # every control and executing it through the real generated strategy. + rows = [line.split() for line in output.splitlines()] + assert [row[0] for row in rows] == names + assert all(len(row) == 2 for row in rows) + for _, value in rows: + float(value) + print(f"string timeframe controls {optimization}:\n{output}", end="") From c9bb034f6e5c5022abc2869974945195a490cb63 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:03:44 +0800 Subject: [PATCH 08/16] Use the generated timeframe type diagnostic presentation Carry the generated argument-type entry and its pin into the member while preserving all other catalog bytes and order. Use the grammatical message and exact approved user presentation with the existing argument kinds. Retain the same numeric refusal cases and qualifier controls. --- pineforge_codegen/analyzer/base.py | 4 ++-- pineforge_codegen/codegen/visit_call.py | 4 ++-- pineforge_codegen/diagnostics_catalog.json | 2 +- scripts/gen_diagnostics_catalog.py | 8 ++++---- tests/fixtures/diagnostic_codes_pin.json | 2 +- tests/test_diagnostic_codes.py | 4 ++-- tests/test_empty_timeframe_emission.py | 4 ++-- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index 293ae5bb..3f1f01f8 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -7499,8 +7499,8 @@ def _check_timeframe_string_arguments( parameter_type = parameter.pine_type.value name = parameter.name self._error( - f"Cannot call '{function_name}' with a {argument_type} " - f"argument for {parameter_type} parameter '{name}'.", + f"Cannot call '{function_name}' with an argument of type {argument_type} " + f"for {parameter_type} parameter '{name}'.", value.loc, ) diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index 28191ca7..4b659470 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -3268,8 +3268,8 @@ def timeframe_call(tf_arg: str) -> str: name = parameter.name self._codegen_error( argument, - f"Cannot call '{function_name}' with a {argument_type} " - f"argument for {parameter_type} parameter '{name}'.", + f"Cannot call '{function_name}' with an argument of type {argument_type} " + f"for {parameter_type} parameter '{name}'.", ) argument_cpp = self._visit_rhs_value( argument, target_cpp_type="std::string", diff --git a/pineforge_codegen/diagnostics_catalog.json b/pineforge_codegen/diagnostics_catalog.json index 91f7a3c6..c6eef36f 100644 --- a/pineforge_codegen/diagnostics_catalog.json +++ b/pineforge_codegen/diagnostics_catalog.json @@ -235,7 +235,7 @@ "PF-E2090": {"severity": "error", "area": "analysis", "message": "input.time() needs a constant default in Pine v6: timestamp() with a time zone and calendar numbers is not a constant. Use the date-string form, for example timestamp(\"01 Jan 2023 00:00 +0000\").", "hint": "Use a single date string in timestamp or an integer timestamp literal for the default.", "explanation": "The time-zone overload of timestamp, which takes a zone and calendar numbers, returns a simple int, while a Pine v6 input.time default requires a const int.", "args": {}, "user_message": "input.time() requires a constant default in Pine v6."}, "PF-E2091": {"severity": "error", "area": "analysis", "message": "plot() cannot have a linewidth below 1 in Pine v6: use a value of 1 or greater.", "hint": "Set the plot linewidth to an integer of at least 1.", "explanation": "A Pine v6 plot call cannot use an integer literal below one for linewidth; variable and calculated widths retain their existing handling.", "args": {}, "user_message": "plot() requires a linewidth of at least 1 in Pine v6."}, "PF-E2092": {"severity": "error", "area": "analysis", "message": "\"range\" cannot be used as a variable or function name in Pine v6: use another name.", "hint": "Rename the variable, function or method declaration.", "explanation": "Pine v6 reserves range as a variable and callable declaration name; parameters, user type fields and member access retain their existing handling.", "args": {}, "user_message": "Choose another variable, function or method name instead of range in Pine v6."}, - "PF-E2095": {"severity": "error", "area": "analysis", "message": "Cannot call ''{function_name}'' with a {argument_type} argument for {parameter_type} parameter ''{name}''.", "hint": null, "explanation": "A numeric value cannot be used for a string parameter. Pass a string value of the declared parameter type; a permitted generic na adopts that type.", "args": {"function_name": {"kind": "identifier"}, "argument_type": {"kind": "type"}, "parameter_type": {"kind": "type"}, "name": {"kind": "identifier"}}, "user_message": "Argument ''{name}'' of ''{function_name}'' has type {argument_type}; expected {parameter_type}."}, + "PF-E2095": {"severity": "error", "area": "analysis", "message": "Cannot call ''{function_name}'' with an argument of type {argument_type} for {parameter_type} parameter ''{name}''.", "hint": null, "explanation": "A non-string value cannot be used for a string parameter. Pass a string value of the declared parameter type; a permitted generic na adopts that type.", "args": {"function_name": {"kind": "identifier"}, "argument_type": {"kind": "type"}, "parameter_type": {"kind": "type"}, "name": {"kind": "identifier"}}, "user_message": "Argument ''{name}'' of ''{function_name}'' has type {argument_type}, but requires {parameter_type}."}, "PF-E3001": {"severity": "error", "area": "request-security", "message": "request.security timeframe ''{name}'' cannot be resolved before the first bar: PineForge registers every requested timeframe before the script runs.", "hint": "Pass a literal, an input or a global to the helper that holds request.security.", "explanation": "The timeframe given to request.security is a name whose value PineForge cannot compute before the first bar; use a literal, an input or a global.", "args": {"name": {"kind": "identifier"}}, "user_message": "The timeframe given to request.security is a name whose value PineForge cannot compute before the first bar."}, "PF-E3002": {"severity": "error", "area": "request-security", "message": "request.security timeframe expression cannot be resolved before the first bar: PineForge registers every requested timeframe before the script runs.", "hint": "Pass a literal, an input or a global to the helper that holds request.security.", "explanation": "The timeframe expression given to request.security cannot be computed before the first bar; use a literal, an input or a global.", "args": {}, "user_message": "The timeframe expression given to request.security cannot be computed before the first bar."}, "PF-E3003": {"severity": "error", "area": "request-security", "message": "request.security timeframe switch arms must contain one expression", "hint": "Compute multi-statement timeframe logic before the request.security call or rewrite it as pure switch arms.", "explanation": "A switch that picks a request.security timeframe must have exactly one expression in each arm; compute multi-statement logic beforehand.", "args": {}, "user_message": "A switch that picks a request.security timeframe must have exactly one expression in each arm."}, diff --git a/scripts/gen_diagnostics_catalog.py b/scripts/gen_diagnostics_catalog.py index d0858ed6..ba5f4ca4 100644 --- a/scripts/gen_diagnostics_catalog.py +++ b/scripts/gen_diagnostics_catalog.py @@ -1088,12 +1088,12 @@ def dump(catalog: dict) -> str: "Supply a literal or an expression over earlier constants.", "Strategy setting ''{name}'' must be a compile-time constant.", ), - "Cannot call ''{function_name}'' with a {argument_type} argument for " + "Cannot call ''{function_name}'' with an argument of type {argument_type} for " "{parameter_type} parameter ''{name}''.": ( - "A numeric value cannot be used for a string parameter. Pass a string value " + "A non-string value cannot be used for a string parameter. Pass a string value " "of the declared parameter type; a permitted generic na adopts that type.", - "Argument ''{name}'' of ''{function_name}'' has type {argument_type}; " - "expected {parameter_type}.", + "Argument ''{name}'' of ''{function_name}'' has type {argument_type}, " + "but requires {parameter_type}.", ), } diff --git a/tests/fixtures/diagnostic_codes_pin.json b/tests/fixtures/diagnostic_codes_pin.json index e8719a8e..a59e136b 100644 --- a/tests/fixtures/diagnostic_codes_pin.json +++ b/tests/fixtures/diagnostic_codes_pin.json @@ -236,7 +236,7 @@ "PF-E2090": "dcc4d05f50b881e5345de8c73ec7b7951bedc86c30a24d40bc0077fca5242a0d", "PF-E2091": "2d1b04fd25a4198619628b94bf8f90b4002f33e4081c2b780fea510c49e0b44a", "PF-E2092": "74b483fe343d239ec254f03e839b257611db5295b0433eabd15072f919950f3d", - "PF-E2095": "2f11b08a085935c9d4583e461d14594a607ca43b593b6a706bc042c425f3aa7c", + "PF-E2095": "a2bf71e6ab48d39b72365b5a69960a8622c7b38deb0aace009215b46dc3cb89d", "PF-E3001": "585dad07e797bbe2cfffa9f45e14dee040be5570cc13f8e46fa4c31e953fc92a", "PF-E3002": "db089893825afe5ad1d91934c21107fc58451cadf3d71de2539b9c5805549433", "PF-E3003": "02571beee5163d1596d8a11fe49336dfcf65f86d4fed13254bac6b36023c3c79", diff --git a/tests/test_diagnostic_codes.py b/tests/test_diagnostic_codes.py index 203a1831..0db7aeaf 100644 --- a/tests/test_diagnostic_codes.py +++ b/tests/test_diagnostic_codes.py @@ -143,13 +143,13 @@ def test_classify_reads_arguments_raw(): @pytest.mark.parametrize("message, expected_code, expected_args", [ ( - "Cannot call 'timeframe.change' with a int argument for string parameter 'timeframe'.", + "Cannot call 'timeframe.change' with an argument of type int for string parameter 'timeframe'.", "PF-E2095", {"function_name": "timeframe.change", "name": "timeframe", "argument_type": "int", "parameter_type": "string"}, ), ( - "Cannot call 'chooseFrame' with a float argument for string parameter 'candidate'.", + "Cannot call 'chooseFrame' with an argument of type float for string parameter 'candidate'.", "PF-E2095", {"function_name": "chooseFrame", "name": "candidate", "argument_type": "float", "parameter_type": "string"}, diff --git a/tests/test_empty_timeframe_emission.py b/tests/test_empty_timeframe_emission.py index add66391..43d197fa 100644 --- a/tests/test_empty_timeframe_emission.py +++ b/tests/test_empty_timeframe_emission.py @@ -117,8 +117,8 @@ def test_numeric_timeframe_is_refused_during_analysis(function, named, argument, "parameter_type": "string", } assert diagnostic.message == ( - f"Cannot call 'timeframe.{function}' with a {pine_type} " - "argument for string parameter 'timeframe'." + f"Cannot call 'timeframe.{function}' with an argument of type {pine_type} " + "for string parameter 'timeframe'." ) assert diagnostic.hint is None assert render_diagnostic(diagnostic.code, diagnostic.args) == ( From 5beeddab303d115f13c00f3cf3925aab188bb90c Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:43:56 +0800 Subject: [PATCH 09/16] Locate helper argument expectations inside the builtin call Use the argument occurrence within the call instead of an identically named helper parameter. Keep the exact line and column assertions and all source inputs, diagnostic identities and test rows unchanged. --- tests/test_timeframe_argument_types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_timeframe_argument_types.py b/tests/test_timeframe_argument_types.py index b691358a..9c241aaf 100644 --- a/tests/test_timeframe_argument_types.py +++ b/tests/test_timeframe_argument_types.py @@ -96,7 +96,7 @@ def test_non_string_timeframes_are_refused_by_semantic_type( line, = [(index, line) for index, line in enumerate(source.splitlines(), 1) if call in line] assert diagnostic.location.line == line[0] - assert diagnostic.location.col == line[1].index(supplied) + ( + assert diagnostic.location.col == line[1].index(supplied, line[1].index(call)) + ( len("timeframe = ") if named else 0 ) + 1 From bd8a3c6adf4143f2c692fa854dd1dd8e54346789 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:32:28 +0800 Subject: [PATCH 10/16] Resolve registered argument types and report their source starts Carry registered namespace types through the semantic query used for written helper calls while respecting lexical shadowing and version boundaries. Reuse the existing expression-start locator for timeframe type refusals. --- pineforge_codegen/analyzer/base.py | 20 +++----------------- pineforge_codegen/analyzer/types.py | 19 +++++++++++++++++++ pineforge_codegen/codegen/visit_call.py | 4 +++- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index 3f1f01f8..ea0b88e8 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -7157,15 +7157,6 @@ def _operator_operand_type( or authored_method ): result = None - if result is None and isinstance(node, MemberAccess): - if isinstance(node.object, Identifier): - ns = node.object.name - if ns == "syminfo": - ptype = sigs.SYMINFO_VARIABLES.get(f"syminfo.{node.member}") - if ptype is not None: - result = TypeSpec.primitive(ptype.value) - elif ns == "timeframe" and node.member == "period": - result = TypeSpec.primitive("string") if result is None and isinstance(node, FuncCall): callee = node.callee if isinstance(callee, Identifier): @@ -7481,13 +7472,6 @@ def _check_timeframe_string_arguments( if value is None or parameter.pine_type != PineType.STRING: continue actual = self._operator_operand_type(value, parameter_types) - if (actual is None and isinstance(value, MemberAccess) - and isinstance(value.object, Identifier) - and self._value_bindings.declaration_bindings.get(id(value.object)) is None - and (parameter_types is None or value.object.name not in parameter_types)): - variable_type = sigs.get_variable_type(value.object.name, value.member) - if variable_type != PineType.UNKNOWN: - actual = TypeSpec.primitive(variable_type.value) if actual is None or actual in { TypeSpec.primitive("string"), TypeSpec.primitive("na"), }: @@ -7498,10 +7482,12 @@ def _check_timeframe_string_arguments( argument_type = method_receiver_type_name(actual) or str(actual) parameter_type = parameter.pine_type.value name = parameter.name + from ..pine_spelling import expr_start + self._error( f"Cannot call '{function_name}' with an argument of type {argument_type} " f"for {parameter_type} parameter '{name}'.", - value.loc, + expr_start(value).loc or value.loc, ) def _visit_FuncCall(self, node: FuncCall) -> PineType: diff --git a/pineforge_codegen/analyzer/types.py b/pineforge_codegen/analyzer/types.py index c62ffa06..a8fa4568 100644 --- a/pineforge_codegen/analyzer/types.py +++ b/pineforge_codegen/analyzer/types.py @@ -1547,6 +1547,25 @@ def _type_spec_from_expr(self, value, **kwargs): return self._analyzer._operator_operand_type( value, self._parameter_types, ) + if (isinstance(value, MemberAccess) + and isinstance(value.object, Identifier) + and self._pine_version == 6 + and not self._in_v5_library_definition(value)): + receiver = value.object + bindings = self._value_bindings.declaration_bindings + # Require the receiver's recorded lexical namespace use. A + # missing record is not proof, and even an unresolved parameter + # or local in the written-call environment shadows a namespace. + if (id(receiver) in bindings and bindings[id(receiver)] is None + and (self._parameter_types is None + or receiver.name not in self._parameter_types) + and receiver.name not in self._udt_fields + and receiver.name not in self._enum_defs): + spec = self._pine_type_to_spec( + sigs.get_variable_type(receiver.name, value.member), + ) + if spec is not None: + return spec return super()._type_spec_from_expr(value, **kwargs) def _visit(self, node): diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index 4b659470..6bb0d804 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -3266,8 +3266,10 @@ def timeframe_call(tf_arg: str) -> str: function_name = intrinsic.name parameter_type = parameter.pine_type.value name = parameter.name + from ..pine_spelling import expr_start + self._codegen_error( - argument, + expr_start(argument), f"Cannot call '{function_name}' with an argument of type {argument_type} " f"for {parameter_type} parameter '{name}'.", ) From 95e5924d9bba13c1cd6c2b644ecbe92be8a08853 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:48:26 +0800 Subject: [PATCH 11/16] Preserve sizing string values across expressions and host settings --- CHANGELOG.md | 4 + pineforge_codegen/analyzer/base.py | 8 +- pineforge_codegen/analyzer/types.py | 26 ++- pineforge_codegen/codegen/emit_top.py | 16 +- pineforge_codegen/codegen/order_shapes.py | 3 +- .../codegen/strategy_declaration.py | 8 +- pineforge_codegen/codegen/types.py | 22 ++ pineforge_codegen/codegen/visit_call.py | 47 +++- pineforge_codegen/codegen/visit_expr.py | 10 +- pineforge_codegen/codegen/visit_stmt.py | 2 +- pineforge_codegen/signatures.py | 113 ++++++++-- pineforge_codegen/symbols.py | 26 +++ .../test_namespace_operator_semantic_facts.py | 25 ++ tests/test_namespace_sizing_consumers.py | 213 ++++++++++++++++++ 14 files changed, 465 insertions(+), 58 deletions(-) create mode 100644 tests/test_namespace_operator_semantic_facts.py create mode 100644 tests/test_namespace_sizing_consumers.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 41136894..8e64c52b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,10 @@ supported as exact pairs; on the 0.x line they are independent. See the ## Unreleased (next release) +- Strategy sizing constants retain their string values in expressions, aliases + and helper calls. Host order and risk settings convert these values to their + numeric representation only at the setting boundary. + - `timeframe.in_seconds` and `timeframe.change` resolve an evaluated empty string to the chart period, including input, variable, function-return and named arguments. The argument is evaluated once, and nonempty strings keep diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index ea0b88e8..19c8a1b8 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -27,6 +27,7 @@ SymbolTable, TypeSpec, method_receiver_type_name, + public_type_name, unify_pine_types, ) from ..errors import SourceLocation, Diagnostic, CompileError, Level, Phase @@ -7479,7 +7480,7 @@ def _check_timeframe_string_arguments( # call supplies a type. Bare na adopts the string signature. continue function_name = intrinsic.name - argument_type = method_receiver_type_name(actual) or str(actual) + argument_type = public_type_name(actual) or str(actual) parameter_type = parameter.pine_type.value name = parameter.name from ..pine_spelling import expr_start @@ -8990,6 +8991,8 @@ def _visit_MemberAccess(self, node: MemberAccess) -> PineType: if drawing_all_kind(node) not in {None, "polyline"}: # Collections use TypeSpec for their shape; PineType stays scalar. return PineType.FLOAT + if self._builtin_sizing_constant(node) is not None: + return PineType.STRING # Handle specific namespaces if isinstance(node.object, Identifier): ns = node.object.name @@ -8999,9 +9002,6 @@ def _visit_MemberAccess(self, node: MemberAccess) -> PineType: # Direction constants if node.member in ("long", "short"): return PineType.INT - # Qty type constants - if node.member in ("percent_of_equity", "fixed", "cash"): - return PineType.INT # Commission type constants if node.member in ("commission",): return PineType.VOID # namespace prefix for .percent etc. diff --git a/pineforge_codegen/analyzer/types.py b/pineforge_codegen/analyzer/types.py index a8fa4568..8fe25194 100644 --- a/pineforge_codegen/analyzer/types.py +++ b/pineforge_codegen/analyzer/types.py @@ -91,6 +91,26 @@ class TypeHelper: docstring above. """ + def _builtin_sizing_constant(self, value): + if not (isinstance(value, MemberAccess) + and isinstance(value.object, Identifier)): + return None + receiver = value.object + constant = sigs.get_sizing_constant(receiver.name, value.member) + if constant is None: + return None + parameters = getattr(self, "_parameter_types", None) + bindings = self._value_bindings.declaration_bindings + if ((self._pine_version == 6 and id(receiver) not in bindings) + or self._value_bindings.binding_of(receiver) is not None + or (self._pine_version != 6 + and self._symbols.resolve(receiver.name) is not None) + or (parameters is not None and receiver.name in parameters) + or receiver.name in self._udt_fields + or receiver.name in self._enum_defs): + return None + return constant + def _split_top_level_type_args(self, text: str) -> list[str]: args: list[str] = [] cur: list[str] = [] @@ -807,6 +827,8 @@ def direct_user_udt_ctor_name(node: ASTNode) -> str | None: owner = self._type_spec_from_expr(value.object) if owner is not None and owner.kind == "udt" and owner.name: return (self._udt_field_type_specs.get(owner.name) or {}).get(value.member) + if self._builtin_sizing_constant(value) is not None: + return TypeSpec.primitive("string") return None @staticmethod @@ -1561,9 +1583,7 @@ def _type_spec_from_expr(self, value, **kwargs): or receiver.name not in self._parameter_types) and receiver.name not in self._udt_fields and receiver.name not in self._enum_defs): - spec = self._pine_type_to_spec( - sigs.get_variable_type(receiver.name, value.member), - ) + spec = sigs.get_variable_type_spec(receiver.name, value.member) if spec is not None: return spec return super()._type_spec_from_expr(value, **kwargs) diff --git a/pineforge_codegen/codegen/emit_top.py b/pineforge_codegen/codegen/emit_top.py index d9c67b33..2f2198c5 100644 --- a/pineforge_codegen/codegen/emit_top.py +++ b/pineforge_codegen/codegen/emit_top.py @@ -87,6 +87,7 @@ import re +from ..signatures import STRATEGY_SIZING_CONSTANTS from .run_stops import RUN_STOP_SHIMS_CPP, TIMEZONE_STOP_SHIM_CPP from ..ast_nodes import ( @@ -1353,9 +1354,8 @@ def declared_or_v6_default(key: str): # default_qty_type: strategy.fixed / strategy.percent_of_equity / strategy.cash qty_type_map = { - "strategy.fixed": "QtyType::FIXED", - "strategy.percent_of_equity": "QtyType::PERCENT_OF_EQUITY", - "strategy.cash": "QtyType::CASH", + name: f"QtyType::{item.host_name}" + for name, item in STRATEGY_SIZING_CONSTANTS.items() } qty_type = declared_or_v6_default("default_qty_type") if qty_type in qty_type_map: @@ -1446,9 +1446,13 @@ def declared_or_v6_default(key: str): lines.append(' } else if (key == "close_entries_rule") {') lines.append(' overrides.close_entries_rule = (value == "ANY" || value == "any" || value == "1");') lines.append(' } else if (key == "default_qty_type") {') - lines.append(' if (value == "fixed" || value == "strategy.fixed" || value == "0") overrides.default_qty_type = static_cast(QtyType::FIXED);') - lines.append(' else if (value == "percent_of_equity" || value == "strategy.percent_of_equity" || value == "1") overrides.default_qty_type = static_cast(QtyType::PERCENT_OF_EQUITY);') - lines.append(' else if (value == "cash" || value == "strategy.cash" || value == "2") overrides.default_qty_type = static_cast(QtyType::CASH);') + for index, (name, item) in enumerate(STRATEGY_SIZING_CONSTANTS.items()): + branch = "if" if index == 0 else "else if" + lines.append( + f' {branch} (value == "{item.value}" || value == "{name}" ' + f'|| value == "{item.host_value}") overrides.default_qty_type = ' + f'static_cast(QtyType::{item.host_name});' + ) lines.append(" else return;") lines.append(' } else if (key == "commission_type") {') lines.append(' if (value == "percent" || value == "strategy.commission.percent" || value == "0") overrides.commission_type = static_cast(CommissionType::PERCENT);') diff --git a/pineforge_codegen/codegen/order_shapes.py b/pineforge_codegen/codegen/order_shapes.py index 86d426c8..f8230fc3 100644 --- a/pineforge_codegen/codegen/order_shapes.py +++ b/pineforge_codegen/codegen/order_shapes.py @@ -12,6 +12,7 @@ TupleAssign, UnaryOp, VarDecl, WhileStmt, ) from ..limits import iter_ast_nodes, syntax_children +from ..signatures import STRATEGY_SIZING_CONSTANTS from .capabilities import READ_ONLY_STRATEGY_CALLS, _expression from .host_members import HOST_MEMBER_NAMES @@ -23,7 +24,7 @@ FINITE_BAR_VALUES = frozenset(("open", "high", "low", "close", "hl2", "hlc3", "ohlc4", "hlcc4")) ENUM_VALUES = { "strategy.oca.none": 0, "strategy.oca.cancel": 1, "strategy.oca.reduce": 2, - "strategy.fixed": 0, "strategy.percent_of_equity": 1, "strategy.cash": 2, + **{name: item.host_value for name, item in STRATEGY_SIZING_CONSTANTS.items()}, } SETTING_DEFAULTS = { "initial_capital": 10000.0, "default_qty_type": "fixed", "default_qty_value": 1.0, diff --git a/pineforge_codegen/codegen/strategy_declaration.py b/pineforge_codegen/codegen/strategy_declaration.py index 5efe9c8f..a0731458 100644 --- a/pineforge_codegen/codegen/strategy_declaration.py +++ b/pineforge_codegen/codegen/strategy_declaration.py @@ -5,7 +5,7 @@ from ..ast_nodes import EnumDecl, FuncDef, Identifier, StrategyDecl, StringLiteral, TypeDecl, VarDecl from ..errors import CompileError, Diagnostic, Level, Phase, SourceLocation from ..limits import iter_ast_nodes -from ..signatures import MATH_CONSTANTS, STRATEGY_PARAMETERS +from ..signatures import MATH_CONSTANTS, STRATEGY_PARAMETERS, STRATEGY_SIZING_CONSTANTS from .constant_fold import NON_CONSTANT, NamedConstant, fold_constant_expression from .tables import DAYOFWEEK_MAP, NAME_ECHO_STRING_MEMBERS @@ -16,12 +16,16 @@ _NAMED_CONSTANTS = { f"strategy.{name}": NamedConstant(f"strategy.{name}", name.rsplit(".", 1)[-1]) for name in ( - "fixed", "cash", "percent_of_equity", "commission.percent", + "commission.percent", "commission.cash_per_order", "commission.cash_per_contract", "long", "short", "oca.none", "oca.cancel", "oca.reduce", "direction.all", "direction.long", "direction.short", ) } +_NAMED_CONSTANTS.update({ + name: NamedConstant(name, item.value) + for name, item in STRATEGY_SIZING_CONSTANTS.items() +}) _NAMED_CONSTANTS.update({ f"{namespace}.{member}": NamedConstant(f"{namespace}.{member}", member) for namespace in ("currency", "format") diff --git a/pineforge_codegen/codegen/types.py b/pineforge_codegen/codegen/types.py index abc3fb2b..bb3099d1 100644 --- a/pineforge_codegen/codegen/types.py +++ b/pineforge_codegen/codegen/types.py @@ -3061,6 +3061,8 @@ def _type_spec_from_expr_raw( plan = getattr(self, "_array_widths", None) return (plan.widen(spec, {("field", owner.name, node.member)}) if plan is not None else spec) + if self._builtin_sizing_constant(node) is not None: + return TypeSpec.primitive("string") return None if isinstance(node, FuncCall): func_name, namespace = self._resolve_callee(node.callee) @@ -6138,6 +6140,26 @@ def _infer_cpp_type_for_security_elem(self, node) -> str: return "std::vector" return "double" + def _builtin_sizing_constant(self, node): + """Resolve registered constants only through an unshadowed namespace.""" + if not (isinstance(node, MemberAccess) + and isinstance(node.object, Identifier)): + return None + receiver = node.object + name = receiver.name + constant = sigs.get_sizing_constant(name, node.member) + if constant is None: + return None + if (self.ctx.identifier_decl_bindings.get(id(receiver)) is not None + or ValueBindings.declaration_of(receiver) is not None + or name in self._current_func_param_types + or name in self._current_func_locals + or name in self._current_loop_vars + or name in self._udt_defs + or name in self._enum_defs): + return None + return constant + def _text_type(self, node) -> str: """Pine bool provenance is consumed only by explicit text conversion.""" if not self._pine_v5_body: diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index 6bb0d804..909efdad 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -158,7 +158,13 @@ from ..intrinsic_binding import bind_intrinsic_arguments, validate_intrinsic_keywords from ..limits import iter_ast_nodes from ..security_contexts import ticker_symbol_arg -from ..symbols import PineType, TypeSpec, method_receiver_type_name +from ..symbols import ( + PineType, + TypeSpec, + method_receiver_type_name, + public_cpp_type_name, + public_type_name, +) from ..analyzer.value_bindings import ValueBindings from ..method_binding import ( MethodBindError, @@ -3238,11 +3244,11 @@ def timeframe_call(tf_arg: str) -> str: and isinstance(argument.object, Identifier) and self.ctx.identifier_decl_bindings.get(id(argument.object)) is None and ValueBindings.declaration_of(argument.object) is None): - variable_type = sigs.get_variable_type( + variable_spec = sigs.get_variable_type_spec( argument.object.name, argument.member, ) - if variable_type != PineType.UNKNOWN: - actual = TypeSpec.primitive(variable_type.value) + if variable_spec is not None: + actual = variable_spec actual_cpp_type = self._text_type(argument) argument_is_string = ( actual == TypeSpec.primitive("string") @@ -3254,12 +3260,9 @@ def timeframe_call(tf_arg: str) -> str: # analysis. Check before bool-to-text compatibility can # turn a non-string value into a C++ string. argument_type = ( - "bool" if actual_cpp_type == "bool" else - (method_receiver_type_name(actual) or str(actual)) - if actual is not None else - {"int": "int", "int64_t": "int", "double": "float"}.get( - actual_cpp_type, actual_cpp_type, - ) + public_type_name(actual) + if actual is not None + else public_cpp_type_name(actual_cpp_type) ) intrinsic = sigs.BUILTIN_FUNCTIONS[f"{namespace}.{func_name}"] parameter = intrinsic.primary.params[0] @@ -3820,6 +3823,28 @@ def _strategy_string_arg(self, node) -> str: """A missing Pine string uses the host's empty-string sentinel.""" return '""' if node is None or isinstance(node, NaLiteral) else self._visit_expr(node) + def _strategy_qty_type_arg(self, node) -> str: + """Convert a Pine sizing string only where the host expects its code.""" + if node is None: + return "-1" + constant = self._builtin_sizing_constant(node) + if constant is not None: + return str(constant.host_value) + value = self._visit_expr(node) + if self._infer_type(node) != "std::string": + # Retain the legacy numeric host API, including its -1 sentinel. + return value + cases = " ".join( + f"if (_pf_qty_type == {self._visit_expr(StringLiteral(value=item.value))}) " + f"return {item.host_value};" + for item in sigs.STRATEGY_SIZING_CONSTANTS.values() + ) + return ( + "([&]() -> int { const std::string _pf_qty_type = " + f"({value}); {cases} " + 'throw std::invalid_argument("Unknown strategy sizing value"); }())' + ) + def _visit_strategy_call(self, func_name: str, node: FuncCall) -> str: from .order_shapes import record_order_call if func_name in ("convert_to_account", "convert_to_symbol"): @@ -3855,7 +3880,7 @@ def _visit_strategy_call(self, func_name: str, node: FuncCall) -> str: comment_val = self._strategy_string_arg(comment) oca_name_val = self._strategy_string_arg(oca_name) oca_type_val = self._visit_expr(oca_type) if oca_type else "0" - qty_type_val = self._visit_expr(qty_type) if qty_type else "-1" + qty_type_val = self._strategy_qty_type_arg(qty_type) qty_val = self._visit_float_arg(qty) if stop is not None or limit is not None or qty is not None or oca_name is not None or oca_type is not None or qty_type is not None: limit_val = self._visit_float_arg(limit) diff --git a/pineforge_codegen/codegen/visit_expr.py b/pineforge_codegen/codegen/visit_expr.py index da1f99a1..3b0e3e04 100644 --- a/pineforge_codegen/codegen/visit_expr.py +++ b/pineforge_codegen/codegen/visit_expr.py @@ -997,6 +997,9 @@ def _visit_member_access(self, node: MemberAccess) -> str: # call reaches the array it holds. return f"(*{field})" return field + sizing = self._builtin_sizing_constant(node) + if sizing is not None: + return self._visit_expr(StringLiteral(value=sizing.value)) if isinstance(node.object, Identifier): ns = node.object.name if ns == "alert" and node.member in ALERT_FREQ_VALUES: @@ -1085,13 +1088,6 @@ def _visit_member_access(self, node: MemberAccess) -> str: return "avg_losing_trade_percent()" if node.member == "margin_liquidation_price": return "margin_liquidation_price()" - # Qty type / commission constants - if node.member == "fixed": - return "0" - if node.member == "percent_of_equity": - return "1" - if node.member == "cash": - return "2" if node.member == "account_currency": return self._strategy_account_currency_expr() # Sub-namespaces (oca, direction, risk, commission) handled below diff --git a/pineforge_codegen/codegen/visit_stmt.py b/pineforge_codegen/codegen/visit_stmt.py index ffdb77e0..352ff880 100644 --- a/pineforge_codegen/codegen/visit_stmt.py +++ b/pineforge_codegen/codegen/visit_stmt.py @@ -493,7 +493,7 @@ def _visit_stmt(self, node: ASTNode, lines: list[str], indent: int) -> None: if is_pct is None: # Mutable aliases and computed enums must use # the value at this call, not an initializer. - type_value = self._visit_expr(type_arg) + type_value = self._strategy_qty_type_arg(type_arg) percent_flag = ( f"(({type_value}) == " "static_cast(QtyType::PERCENT_OF_EQUITY))" diff --git a/pineforge_codegen/signatures.py b/pineforge_codegen/signatures.py index 6ab3522f..0f4483d5 100644 --- a/pineforge_codegen/signatures.py +++ b/pineforge_codegen/signatures.py @@ -14,7 +14,7 @@ from dataclasses import dataclass, field from typing import Any -from .symbols import PineType +from .symbols import PineType, TypeSpec @dataclass @@ -694,6 +694,39 @@ def get_value_function(name: str) -> IntrinsicFunc | None: # Built-in variables (not functions) # ============================================================================ +@dataclass(frozen=True) +class _StorageOnlyType: + """Legacy host representation without a proven scalar Pine type.""" + + storage_type: PineType + + +@dataclass(frozen=True) +class SizingConstant: + """A Pine string value and its conversion at a host sizing boundary.""" + + value: str + host_value: int + host_name: str + + +# The complete strategy sizing constant class. Its numeric codes belong only +# to the host API; an ordinary expression reads the string value. +STRATEGY_SIZING_CONSTANTS = { + "strategy.fixed": SizingConstant("fixed", 0, "FIXED"), + "strategy.percent_of_equity": SizingConstant("percent_of_equity", 1, "PERCENT_OF_EQUITY"), + "strategy.cash": SizingConstant("cash", 2, "CASH"), +} + + +def _variable_storage_type(value: PineType | _StorageOnlyType | SizingConstant) -> PineType: + if isinstance(value, _StorageOnlyType): + return value.storage_type + if isinstance(value, SizingConstant): + return PineType.STRING + return value + + BUILTIN_VARIABLES: dict[str, PineType] = { # Price series "open": F, "high": F, "low": F, "close": F, "volume": F, @@ -735,7 +768,7 @@ def get_value_function(name: str) -> IntrinsicFunc | None: ) # Strategy variables (read-only properties, not function calls) -STRATEGY_VARIABLES: dict[str, PineType] = { +_STRATEGY_VARIABLE_DECLARATIONS: dict[str, PineType | _StorageOnlyType | SizingConstant] = { "strategy.position_size": F, "strategy.position_avg_price": F, "strategy.position_entry_name": S, @@ -771,25 +804,27 @@ def get_value_function(name: str) -> IntrinsicFunc | None: "strategy.account_currency": S, "strategy.opentrades.capital_held": F, # Direction constants - "strategy.long": B, - "strategy.short": B, + "strategy.long": _StorageOnlyType(B), + "strategy.short": _StorageOnlyType(B), # Qty type constants - "strategy.fixed": I, - "strategy.cash": I, - "strategy.percent_of_equity": I, + **STRATEGY_SIZING_CONSTANTS, # Commission type constants - "strategy.commission.percent": I, - "strategy.commission.cash_per_contract": I, - "strategy.commission.cash_per_order": I, + "strategy.commission.percent": _StorageOnlyType(I), + "strategy.commission.cash_per_contract": _StorageOnlyType(I), + "strategy.commission.cash_per_order": _StorageOnlyType(I), # OCA constants - "strategy.oca.cancel": I, - "strategy.oca.reduce": I, - "strategy.oca.none": I, + "strategy.oca.cancel": _StorageOnlyType(I), + "strategy.oca.reduce": _StorageOnlyType(I), + "strategy.oca.none": _StorageOnlyType(I), # Direction constants "strategy.direction.all": S, "strategy.direction.long": S, "strategy.direction.short": S, } +STRATEGY_VARIABLES: dict[str, PineType] = { + name: _variable_storage_type(value) + for name, value in _STRATEGY_VARIABLE_DECLARATIONS.items() +} # Barstate variables BARSTATE_VARIABLES: dict[str, PineType] = { @@ -866,13 +901,17 @@ def get_value_function(name: str) -> IntrinsicFunc | None: } # display.* plot_display constants (PineScript v6 — values for C++ codegen) +_DISPLAY_VARIABLE_DECLARATIONS: dict[str, _StorageOnlyType] = { + "display.all": _StorageOnlyType(I), + "display.none": _StorageOnlyType(I), + "display.pane": _StorageOnlyType(I), + "display.data_window": _StorageOnlyType(I), + "display.status_line": _StorageOnlyType(I), + "display.price_scale": _StorageOnlyType(I), +} DISPLAY_VARIABLES: dict[str, PineType] = { - "display.all": I, - "display.none": I, - "display.pane": I, - "display.data_window": I, - "display.status_line": I, - "display.price_scale": I, + name: value.storage_type + for name, value in _DISPLAY_VARIABLE_DECLARATIONS.items() } @@ -1040,20 +1079,48 @@ def is_intrinsic_function(namespace: str | None, func_name: str) -> bool: return False -def get_variable_type(namespace: str | None, name: str) -> PineType: - """Return a builtin variable's declared type, or UNKNOWN for other names.""" +def _variable_declaration( + namespace: str | None, name: str, +) -> PineType | _StorageOnlyType | SizingConstant: if namespace is None: return BUILTIN_VARIABLES.get(name, PineType.UNKNOWN) full = f"{namespace}.{name}" for table in ( - STRATEGY_VARIABLES, BARSTATE_VARIABLES, SYMINFO_VARIABLES, - TIMEFRAME_VARIABLES, BUILTIN_VARIABLES, DISPLAY_VARIABLES, + _STRATEGY_VARIABLE_DECLARATIONS, BARSTATE_VARIABLES, SYMINFO_VARIABLES, + TIMEFRAME_VARIABLES, BUILTIN_VARIABLES, _DISPLAY_VARIABLE_DECLARATIONS, ): if full in table: return table[full] return PineType.UNKNOWN +def get_variable_type(namespace: str | None, name: str) -> PineType: + """Return a builtin's legacy storage type, not an operator-domain proof.""" + return _variable_storage_type(_variable_declaration(namespace, name)) + + +def get_sizing_constant(namespace: str | None, name: str) -> SizingConstant | None: + """Return a sizing constant's Pine value and host conversion together.""" + return STRATEGY_SIZING_CONSTANTS.get(f"{namespace}.{name}") + + +def get_variable_type_spec( + namespace: str | None, name: str, +) -> TypeSpec | None: + """Return a builtin variable's semantic Pine type specification. + + The variable tables also serve storage and lowering consumers, so this + lookup carries only the semantic facts that are safe for operator proofs. + """ + declaration = _variable_declaration(namespace, name) + if isinstance(declaration, _StorageOnlyType): + return None + primitive = _variable_storage_type(declaration) + if primitive in (PineType.UNKNOWN, PineType.VOID): + return None + return TypeSpec.primitive(primitive.value) + + def is_intrinsic_variable(namespace: str | None, name: str) -> bool: """Check if a name is a known TradingView built-in variable.""" return get_variable_type(namespace, name) != PineType.UNKNOWN diff --git a/pineforge_codegen/symbols.py b/pineforge_codegen/symbols.py index 71ead7fd..91eb86a0 100644 --- a/pineforge_codegen/symbols.py +++ b/pineforge_codegen/symbols.py @@ -123,6 +123,32 @@ def method_receiver_type_name(spec: TypeSpec | None) -> str | None: return None +def public_type_name(spec: TypeSpec | None) -> str | None: + """Return a Pine type name suitable for a public diagnostic. + + An unresolved numeric domain retains its uncertainty; it is not evidence + for either concrete Pine scalar type. Structured specs keep their spelling. + """ + if spec is None: + return None + name = method_receiver_type_name(spec) + if name is not None: + return name + return str(spec) + + +def public_cpp_type_name(cpp_type: str) -> str: + """Map the scalar C++ fallbacks used by codegen to Pine names.""" + return { + "bool": "bool", + "int": "int", + "int64_t": "int", + "float": "float", + "double": "float", + "std::string": "string", + }.get(cpp_type, "unknown") + + def method_receiver_cpp_token( spec: TypeSpec | None, fallback_name: str | None = None, diff --git a/tests/test_namespace_operator_semantic_facts.py b/tests/test_namespace_operator_semantic_facts.py new file mode 100644 index 00000000..9a95b520 --- /dev/null +++ b/tests/test_namespace_operator_semantic_facts.py @@ -0,0 +1,25 @@ +"""Valid operator operands must survive legacy namespace storage types.""" + +from pineforge_codegen import transpile + + +SIZING_LABEL_SOURCE = '''//@version=6 +strategy("Sizing label") +txt = "Sizing: " + strategy.cash +if barstate.islast + label.new(bar_index, high, txt) +''' + +ORDINARY_CONTROL_SOURCE = '''//@version=6 +strategy("Ordinary addition") +plot(close + open) +''' + + +def test_strategy_sizing_string_constant_still_transpiles(): + # Acceptance only: the old numeric emission is a separate value defect. + transpile(SIZING_LABEL_SOURCE) + + +def test_ordinary_numeric_operator_still_transpiles(): + transpile(ORDINARY_CONTROL_SOURCE) diff --git a/tests/test_namespace_sizing_consumers.py b/tests/test_namespace_sizing_consumers.py new file mode 100644 index 00000000..dc1dd15c --- /dev/null +++ b/tests/test_namespace_sizing_consumers.py @@ -0,0 +1,213 @@ +"""Sizing constants keep their Pine values through live emitted consumers.""" + +import re + +import pytest + +from pineforge_codegen import signatures as sigs, transpile +from pineforge_codegen.analyzer import Analyzer +from pineforge_codegen.errors import CompileError +from pineforge_codegen.lexer import Lexer +from pineforge_codegen.parser import Parser +from pineforge_codegen.symbols import PineType, TypeSpec, public_type_name + + +SIZING_MEMBERS = ("fixed", "cash", "percent_of_equity") +HEADER = '//@version=6\nstrategy("Sizing consumers")\n' + + +def _source_body(cpp): + match = re.search( + r"void on_source_bar\([^)]*\) override \{(.*?)\n \}", + cpp, re.DOTALL, + ) + assert match is not None + return match.group(1) + + +def _engine_call(function, argument): + if function == "change": + return ( + "tf_change(prev_bar_timestamp_, current_bar_.timestamp, " + f"{argument}, syminfo_.timezone, syminfo_.session)" + ) + return f"tf_to_seconds({argument})" + + +def _live_timeframe_source(member, function, form): + argument = f"strategy.{member}" + declarations = "" + supplied = f"timeframe = {argument}" if form == "named" else argument + call = f"timeframe.{function}({supplied})" + if form in ("helper", "named-helper"): + supplied = "timeframe = frameArg" if form == "named-helper" else "frameArg" + declarations = f"withFrame(frameArg) => timeframe.{function}({supplied})\n" + call = f"withFrame({argument})" + elif form == "alias": + declarations = f"frameMode = {argument}\n" + call = f"timeframe.{function}(frameMode)" + elif form == "return": + declarations = f"chosenFrame() => {argument}\n" + call = f"timeframe.{function}(chosenFrame())" + value = f"{call} ? 1 : 0" if function == "change" else call + return ( + HEADER + declarations + f"sampleValue = {value}\n" + "if sampleValue > 0\n" + ' strategy.entry("L", strategy.long)\n' + ) + + +@pytest.mark.parametrize("member", SIZING_MEMBERS) +@pytest.mark.parametrize("function", ("change", "in_seconds")) +@pytest.mark.parametrize( + "form", ("direct", "named", "helper", "named-helper", "alias", "return"), +) +def test_sizing_timeframe_arguments_reach_live_string_lowering(member, function, form): + cpp = transpile(_live_timeframe_source(member, function, form)) + body = _source_body(cpp) + assert re.search(r"\bsampleValue\s*=", body) + assert re.search(r"if \([^;]*\bsampleValue\b", body) + assert 'strategy_entry(std::string("L"), true' in body + literal = f'std::string("{member}")' + if form in ("helper", "named-helper"): + assert f"withFrame({literal})" in body + assert re.search(r"\bwithFrame\(std::string frameArg\)", cpp) + assert _engine_call(function, "frameArg") in cpp + assert "(frameArg).empty()" in cpp + elif form == "alias": + assert f"frameMode = {literal};" in body + assert _engine_call(function, "frameMode") in body + assert "(frameMode).empty()" in body + elif form == "return": + assert re.search(r"\bstd::string chosenFrame\(", cpp) + assert f"return {literal};" in cpp + assert "const std::string _pf_timeframe_arg = (chosenFrame());" in body + assert _engine_call( + function, "(_pf_timeframe_arg.empty() ? script_tf_ : _pf_timeframe_arg)", + ) in body + else: + assert _engine_call(function, literal) in body + assert f"({literal}).empty()" in body + assert not re.search(r"\(\s*(?:[+-]?\d+(?:\.\d*)?|true|false)\s*\)\.empty\(", cpp) + + +@pytest.mark.parametrize("member", SIZING_MEMBERS) +@pytest.mark.parametrize("consumer", ("concat", "equality", "selection", "collection")) +def test_sizing_strings_keep_their_type_in_other_live_consumers(member, consumer): + if consumer == "concat": + declaration = f'sampleName = "Sizing: " + strategy.{member}\n' + elif consumer == "equality": + declaration = ( + f'rightMode = input.string("{member}")\n' + f'sampleName = strategy.{member} == rightMode ? "equal" : "different"\n' + ) + elif consumer == "selection": + declaration = ( + "chooseMode = input.bool(true)\n" + f'sampleName = chooseMode ? strategy.{member} : "other"\n' + ) + else: + declaration = ( + f'modes = array.from(strategy.{member}, "other")\n' + "sampleName = array.get(modes, input.int(0))\n" + ) + cpp = transpile( + HEADER + declaration + "if barstate.islast\n" + " strategy.entry(sampleName, strategy.long)\n" + ) + body = _source_body(cpp) + assert f'std::string("{member}")' in body + assert "strategy_entry(sampleName, true" in body + assert re.search(r"\bstd::string sampleName\b", cpp) + if consumer == "collection": + assert "std::vector" in cpp + + +@pytest.mark.parametrize("member,host_value", (("fixed", 0), ("percent_of_equity", 1), ("cash", 2))) +def test_host_qty_boundary_preserves_codes_without_changing_pine_values(member, host_value): + cpp = transpile( + HEADER + f"modeName = strategy.{member}\n" + f'strategy.entry(modeName, strategy.long, qty=1, qty_type=strategy.{member})\n' + ) + body = _source_body(cpp) + assert f'modeName = std::string("{member}");' in body + assert f", {host_value})" in body + assert sigs.get_variable_type("strategy", member) == PineType.STRING + assert sigs.STRATEGY_VARIABLES[f"strategy.{member}"] == PineType.STRING + assert sigs.get_variable_type_spec("strategy", member) == TypeSpec.primitive("string") + + +@pytest.mark.parametrize("consumer", ("order", "risk")) +def test_computed_sizing_strings_convert_once_at_numeric_host_boundaries(consumer): + source = ( + HEADER + "chooseMode = input.bool(true)\n" + "chosenMode() => chooseMode ? strategy.cash : strategy.percent_of_equity\n" + ) + if consumer == "order": + source += 'strategy.entry("L", strategy.long, qty=1, qty_type=chosenMode())\n' + else: + source += ( + "strategy.risk.max_drawdown(10, chosenMode())\n" + 'strategy.entry("L", strategy.long)\n' + ) + body = _source_body(transpile(source)) + assert body.count("const std::string _pf_qty_type = (chosenMode());") == 1 + assert '_pf_qty_type == std::string("cash")' in body + assert '_pf_qty_type == std::string("percent_of_equity")' in body + assert "strategy_entry(" in body + if consumer == "risk": + assert "set_pine_risk_max_drawdown(" in body + + +@pytest.mark.parametrize("namespace,member", ( + ("strategy", "long"), ("strategy", "short"), + ("strategy.commission", "percent"), + ("strategy.commission", "cash_per_contract"), + ("strategy.commission", "cash_per_order"), + ("strategy.oca", "none"), ("strategy.oca", "cancel"), ("strategy.oca", "reduce"), + *(("display", member) for member in ( + "all", "none", "pane", "data_window", "status_line", "price_scale", + )), +)) +def test_legacy_constant_storage_is_not_a_semantic_operator_proof(namespace, member): + assert sigs.get_variable_type(namespace, member) != PineType.UNKNOWN + assert sigs.get_variable_type_spec(namespace, member) is None + + +@pytest.mark.parametrize("version", (5, 6)) +def test_authored_namespace_receiver_keeps_its_field_type(version): + cpp = transpile( + f'//@version={version}\nstrategy("Sizing receiver")\n' + "type Mode\n int cash\n" + "readMode(Mode strategy) => strategy.cash\n" + "sampleValue = readMode(Mode.new(3))\n" + "if sampleValue > 0\n" + ' strategy.entry("L", strategy.long)\n' + ) + body = _source_body(cpp) + assert "readMode(" in body + assert "strategy_entry(" in body + assert 'std::string("cash")' not in body + + +def test_ordinary_live_numeric_control_keeps_its_operands(): + body = _source_body(transpile( + HEADER + "sampleValue = close + open\n" + "if sampleValue > 0\n" + ' strategy.entry("L", strategy.long)\n' + )) + assert "(current_bar_.close + current_bar_.open)" in body + assert "sampleValue" in body + assert "strategy_entry(" in body + + +@pytest.mark.parametrize("function", ("change", "in_seconds")) +def test_open_numeric_domain_is_not_reported_as_a_guessed_float(function): + source = HEADER + f"value = timeframe.{function}(math.round(close))\n" + ast = Parser(Lexer(source).tokenize(), source=source).parse() + with pytest.raises(CompileError) as refused: + Analyzer(ast).analyze() + diagnostic, = refused.value.diagnostics + assert diagnostic.code == "PF-E2095" + assert diagnostic.args["argument_type"] == "number" + assert public_type_name(TypeSpec.primitive("number")) == "number" From 149cb9b83f614c2d9a4e63c60a677317d89de351 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:05:58 +0800 Subject: [PATCH 12/16] Exercise v5 receiver shadowing through an imported library --- tests/test_namespace_sizing_consumers.py | 30 +++++++++++++++++------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/tests/test_namespace_sizing_consumers.py b/tests/test_namespace_sizing_consumers.py index dc1dd15c..89f25603 100644 --- a/tests/test_namespace_sizing_consumers.py +++ b/tests/test_namespace_sizing_consumers.py @@ -176,14 +176,28 @@ def test_legacy_constant_storage_is_not_a_semantic_operator_proof(namespace, mem @pytest.mark.parametrize("version", (5, 6)) def test_authored_namespace_receiver_keeps_its_field_type(version): - cpp = transpile( - f'//@version={version}\nstrategy("Sizing receiver")\n' - "type Mode\n int cash\n" - "readMode(Mode strategy) => strategy.cash\n" - "sampleValue = readMode(Mode.new(3))\n" - "if sampleValue > 0\n" - ' strategy.entry("L", strategy.long)\n' - ) + if version == 5: + libraries = {"pftest/SizingReceiver/1": ( + '//@version=5\nlibrary("SizingReceiver")\n' + "export type Mode\n int cash\n" + "export readMode(Mode strategy) => strategy.cash\n" + )} + source = ( + HEADER + "import pftest/SizingReceiver/1 as L\n" + "sampleValue = L.readMode(L.Mode.new(3))\n" + "if sampleValue > 0\n" + ' strategy.entry("L", strategy.long)\n' + ) + cpp = transpile(source, libraries=libraries) + else: + cpp = transpile( + f'//@version={version}\nstrategy("Sizing receiver")\n' + "type Mode\n int cash\n" + "readMode(Mode strategy) => strategy.cash\n" + "sampleValue = readMode(Mode.new(3))\n" + "if sampleValue > 0\n" + ' strategy.entry("L", strategy.long)\n' + ) body = _source_body(cpp) assert "readMode(" in body assert "strategy_entry(" in body From 95b26d803c0f9f75ae2f530ddb619cdfbda5f38b Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:51:57 +0800 Subject: [PATCH 13/16] Preserve integer risk type boundary codes --- pineforge_codegen/codegen/visit_stmt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pineforge_codegen/codegen/visit_stmt.py b/pineforge_codegen/codegen/visit_stmt.py index 352ff880..ffdb77e0 100644 --- a/pineforge_codegen/codegen/visit_stmt.py +++ b/pineforge_codegen/codegen/visit_stmt.py @@ -493,7 +493,7 @@ def _visit_stmt(self, node: ASTNode, lines: list[str], indent: int) -> None: if is_pct is None: # Mutable aliases and computed enums must use # the value at this call, not an initializer. - type_value = self._strategy_qty_type_arg(type_arg) + type_value = self._visit_expr(type_arg) percent_flag = ( f"(({type_value}) == " "static_cast(QtyType::PERCENT_OF_EQUITY))" From 6e9d119014ef5232910343db5bcc8051ae9c50c9 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 21:32:40 +0800 Subject: [PATCH 14/16] Strengthen sizing regression and reserve generated name --- pineforge_codegen/codegen/helpers.py | 2 +- tests/test_e2e_temporary_name_hygiene.py | 4 ++++ tests/test_namespace_sizing_consumers.py | 22 ++++++++++++++++++---- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/pineforge_codegen/codegen/helpers.py b/pineforge_codegen/codegen/helpers.py index 40a79b20..d161d6ca 100644 --- a/pineforge_codegen/codegen/helpers.py +++ b/pineforge_codegen/codegen/helpers.py @@ -533,7 +533,7 @@ def cpp_code_only(cpp: str) -> str: _pf_series_raw _pf_raw _pf_val_t _pf_out _pf_prior _pf_hist_offset_raw _pf_hist_offset_numeric _pf_hist_offset _pf_hist_bar _pf_calendar_ts _pf_calendar_tz - _pf_timeframe_arg + _pf_timeframe_arg _pf_qty_type _tz _yr _mo _dy _hr _min _sc _res _old _old_tz _had_old _secs _last_tz _last_yr _last_mo _last_dy _last_hr _last_min _last_sc _last_res diff --git a/tests/test_e2e_temporary_name_hygiene.py b/tests/test_e2e_temporary_name_hygiene.py index 7f80f775..e019e8c8 100644 --- a/tests/test_e2e_temporary_name_hygiene.py +++ b/tests/test_e2e_temporary_name_hygiene.py @@ -125,6 +125,10 @@ def test_every_temporary_the_emitter_declares_is_reserved(): 'changedValue = timeframe.change(timeframe = chosenFrame())\n' 'plot(secondsValue)\nplot(changedValue ? 1 : 0)\n' ) + sources.append( + HEAD + 'qtyMode = input.string("cash", options = ["cash", "percent_of_equity"])\n' + 'strategy.entry("L", strategy.long, qty = 1, qty_type = qtyMode)\n' + ) for source in sources: authored = {t.value for t in Lexer(source).tokenize() if t.type.name == "IDENT"} for line in transpile(source).splitlines(): diff --git a/tests/test_namespace_sizing_consumers.py b/tests/test_namespace_sizing_consumers.py index 89f25603..4d1e919e 100644 --- a/tests/test_namespace_sizing_consumers.py +++ b/tests/test_namespace_sizing_consumers.py @@ -6,10 +6,11 @@ from pineforge_codegen import signatures as sigs, transpile from pineforge_codegen.analyzer import Analyzer +from pineforge_codegen.codegen.helpers import CPP_TEMPORARY_NAMES from pineforge_codegen.errors import CompileError from pineforge_codegen.lexer import Lexer from pineforge_codegen.parser import Parser -from pineforge_codegen.symbols import PineType, TypeSpec, public_type_name +from pineforge_codegen.symbols import PineType, TypeSpec SIZING_MEMBERS = ("fixed", "cash", "percent_of_equity") @@ -185,6 +186,7 @@ def test_authored_namespace_receiver_keeps_its_field_type(version): source = ( HEADER + "import pftest/SizingReceiver/1 as L\n" "sampleValue = L.readMode(L.Mode.new(3))\n" + 'sizingText = "Sizing: " + strategy.cash\n' "if sampleValue > 0\n" ' strategy.entry("L", strategy.long)\n' ) @@ -195,13 +197,25 @@ def test_authored_namespace_receiver_keeps_its_field_type(version): "type Mode\n int cash\n" "readMode(Mode strategy) => strategy.cash\n" "sampleValue = readMode(Mode.new(3))\n" + 'sizingText = "Sizing: " + strategy.cash\n' "if sampleValue > 0\n" ' strategy.entry("L", strategy.long)\n' ) body = _source_body(cpp) - assert "readMode(" in body + assert ".cash" in cpp + assert re.search(r"sizingText\s*=.*cash", body) assert "strategy_entry(" in body - assert 'std::string("cash")' not in body + + +def test_computed_qty_type_temporary_is_reserved(): + source = ( + HEADER + 'qtyMode = input.string("cash", options = ["cash", "percent_of_equity"])\n' + 'strategy.entry("L", strategy.long, qty = 1, qty_type = qtyMode)\n' + ) + cpp = transpile(source) + assert "_pf_qty_type" in cpp + assert "_pf_qty_type" in CPP_TEMPORARY_NAMES def test_ordinary_live_numeric_control_keeps_its_operands(): @@ -224,4 +238,4 @@ def test_open_numeric_domain_is_not_reported_as_a_guessed_float(function): diagnostic, = refused.value.diagnostics assert diagnostic.code == "PF-E2095" assert diagnostic.args["argument_type"] == "number" - assert public_type_name(TypeSpec.primitive("number")) == "number" + assert str(TypeSpec.primitive("number")) == "number" From d9a643e6bcbc28cd23f29afc9c77642b65efd87e Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 21:52:03 +0800 Subject: [PATCH 15/16] Lower computed risk type arms at numeric boundary --- pineforge_codegen/codegen/visit_stmt.py | 2 +- tests/test_namespace_sizing_consumers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pineforge_codegen/codegen/visit_stmt.py b/pineforge_codegen/codegen/visit_stmt.py index ffdb77e0..352ff880 100644 --- a/pineforge_codegen/codegen/visit_stmt.py +++ b/pineforge_codegen/codegen/visit_stmt.py @@ -493,7 +493,7 @@ def _visit_stmt(self, node: ASTNode, lines: list[str], indent: int) -> None: if is_pct is None: # Mutable aliases and computed enums must use # the value at this call, not an initializer. - type_value = self._visit_expr(type_arg) + type_value = self._strategy_qty_type_arg(type_arg) percent_flag = ( f"(({type_value}) == " "static_cast(QtyType::PERCENT_OF_EQUITY))" diff --git a/tests/test_namespace_sizing_consumers.py b/tests/test_namespace_sizing_consumers.py index 4d1e919e..58ddd3df 100644 --- a/tests/test_namespace_sizing_consumers.py +++ b/tests/test_namespace_sizing_consumers.py @@ -209,7 +209,7 @@ def test_authored_namespace_receiver_keeps_its_field_type(version): def test_computed_qty_type_temporary_is_reserved(): source = ( - HEADER + HEADER + 'qtyMode = input.string("cash", options = ["cash", "percent_of_equity"])\n' 'strategy.entry("L", strategy.long, qty = 1, qty_type = qtyMode)\n' ) From 57d0db6d762702dd0f441fc1531f9d3fbb3d77de Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 21:59:23 +0800 Subject: [PATCH 16/16] Convert sizing selection arms at host boundaries --- pineforge_codegen/codegen/visit_call.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index 909efdad..b1f75432 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -3830,6 +3830,12 @@ def _strategy_qty_type_arg(self, node) -> str: constant = self._builtin_sizing_constant(node) if constant is not None: return str(constant.host_value) + if isinstance(node, Ternary): + return ( + f"(({self._visit_expr(node.condition)}) ? " + f"({self._strategy_qty_type_arg(node.true_val)}) : " + f"({self._strategy_qty_type_arg(node.false_val)}))" + ) value = self._visit_expr(node) if self._infer_type(node) != "std::string": # Retain the legacy numeric host API, including its -1 sentinel.