fix(codegen): request.security TA expression read at a history offset stays in HTF context#31
Merged
Merged
Conversation
… 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
request.security(tf, ta.<fn>(...)[k], lookahead=...)— a security expression that is a TA call read at a history offset — was mis-transpiled. Insecurity.py::_build_security_exprtheSubscriptbranch only handledSubscript(Identifier)(OHLC fields / helper bindings). ASubscript(FuncCall-TA)fell through to the generic chart-context path, which re-lowered the inner TA to the chart member and gated a_hist_callbuffer onis_first_tick_(chart tick) instead ofis_complete(HTF-bar boundary). Without a magnifieris_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:
Series<double> {member}_hist,_secval_*gated onis_complete(so the indicator is not double-stepped),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_securitybranches. A non-literal TA history index errors loudly.Regression-safe: when the security tf == chart tf the engine sets
is_completeevery bar, so the history advances every bar (identical to chart behaviour); OHLCclose[1]stays on the untouchedIdentifierbranch. Lookahead policy unchanged.Validation
ta.ema(close,55)[1] @ "240")ta.rsi(...)[1]in security)ta.atr(...)[1]now correctly HTF-gated)PINEFORGE_ENGINE_INCLUDE=... pytestNew
test_request_security_ta_history_offset_uses_htf_gatingasserts HTF gating (is_complete+ security member) and the absence of the chart-contextis_first_tick_/_hist_callform; fails before the fix.🤖 Generated with Claude Code