Skip to content

fix(codegen): request.security TA expression read at a history offset stays in HTF context#31

Merged
luisleo526 merged 1 commit into
mainfrom
fix/security-ta-history-offset
Jun 30, 2026
Merged

fix(codegen): request.security TA expression read at a history offset stays in HTF context#31
luisleo526 merged 1 commit into
mainfrom
fix/security-ta-history-offset

Conversation

@luisleo526

Copy link
Copy Markdown
Contributor

Problem

request.security(tf, ta.<fn>(...)[k], lookahead=...) — a security expression that is a TA call read at a history offset — was mis-transpiled. In security.py::_build_security_expr the Subscript branch only handled Subscript(Identifier) (OHLC fields / helper bindings). A Subscript(FuncCall-TA) fell through to the generic chart-context path, which re-lowered the inner TA to the chart member and gated a _hist_call buffer on is_first_tick_ (chart tick) instead of is_complete (HTF-bar boundary). Without a magnifier is_first_tick_ is true every bar → it produced the chart-timeframe TA, and the correctly-computed security value _secval_* was discarded.

Fix

Mirror the OHLC history-offset machinery from #30 for TA call-sites:

  • declare a per-(sec, site) Series<double> {member}_hist,
  • push the already-committed _secval_* gated on is_complete (so the indicator is not double-stepped),
  • read hist[k-1] for offset k≥1; offset 0 reuses the bare _secval_*.

Handles scalar, tuple (mixed OHLC+TA), and lower_tf security exprs; cleared in all clear_security branches. A non-literal TA history index errors loudly.

Regression-safe: when the security tf == chart tf the engine sets is_complete every bar, so the history advances every bar (identical to chart behaviour); OHLC close[1] stays on the untouched Identifier branch. Lookahead policy unchanged.

Validation

result
officialjackofalltrades-caldera-meridian (ta.ema(close,55)[1] @ "240") 78.5% → 97.5% match vs TV
3commas-triple-rsi-dca (ta.rsi(...)[1] in security) no-trades → 29% (recovered)
quant-synthesis / waranyutrkm unchanged (waranyutrkm's ta.atr(...)[1] now correctly HTF-gated)
full gate PINEFORGE_ENGINE_INCLUDE=... pytest 1384 passed, 1 skipped, 0 failed (incl. 254-strategy corpus compile sweep)
broad 100-strategy sweep ok 88→89, transpile-errors identical set, zero regressions (byte-diff isolated: exactly 3 strategies changed, all ≥)

New test_request_security_ta_history_offset_uses_htf_gating asserts HTF gating (is_complete + security member) and the absence of the chart-context is_first_tick_/_hist_call form; fails before the fix.

🤖 Generated with Claude Code

… stays in HTF context

`request.security(tf, ta.<fn>(...)[k], lookahead=...)` — a security expression
that is a TA call read at a history offset — was mis-transpiled. In
security.py::_build_security_expr the `Subscript` branch only handled
`Subscript(Identifier)` (OHLC fields / helper bindings); a `Subscript(FuncCall-TA)`
fell through to the generic chart-context path, which re-lowered the inner TA to
the CHART member and gated a `_hist_call` buffer on `is_first_tick_` (chart tick)
instead of `is_complete` (HTF-bar boundary). Without a magnifier is_first_tick_
is true every bar, so it produced the chart-timeframe TA — the correctly-computed
security value `_secval_*` was discarded.

Mirror the OHLC history-offset machinery (#30) for TA call-sites: declare a
per-(sec, site) `Series<double> {member}_hist`, push the already-committed
`_secval_*` gated on is_complete (so the indicator is not double-stepped), and
read `hist[k-1]` for offset k>=1 (offset 0 reuses the bare `_secval_*`). Handles
scalar, tuple (mixed OHLC+TA), and lower_tf security exprs; cleared in all
clear_security branches. A non-literal TA history index errors loudly.

Regression-safe: when the security tf == chart tf the engine sets is_complete
every bar, so the history advances every bar (identical to chart behaviour);
OHLC `close[1]` stays on the untouched Identifier branch.

officialjackofalltrades-caldera-meridian (htfBasis = ta.ema(close,55)[1] @ "240"):
78.5% -> 97.5% match vs TradingView. 3commas-triple-rsi-dca (ta.rsi(...)[1] in
security): no-trades -> 29% (recovered). quant-synthesis / waranyutrkm unchanged.
Full gate PINEFORGE_ENGINE_INCLUDE=... pytest: 1384 passed, 1 skipped, 0 failed
(incl. 254-strategy corpus compile sweep). New test_request_security_ta_history_offset_uses_htf_gating
has teeth (fails before the fix).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@luisleo526 luisleo526 merged commit 1c017f8 into main Jun 30, 2026
9 checks passed
@luisleo526 luisleo526 deleted the fix/security-ta-history-offset branch June 30, 2026 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant