Skip to content

Fix request.security math reducers and flat avg price#39

Merged
luisleo526 merged 2 commits into
mainfrom
campaign/security-math-reducer
Jul 5, 2026
Merged

Fix request.security math reducers and flat avg price#39
luisleo526 merged 2 commits into
mainfrom
campaign/security-math-reducer

Conversation

@luisleo526

Copy link
Copy Markdown
Contributor

Summary

  • return committed helper values for rolling math reducers inside request.security contexts
  • return na for strategy.position_avg_price while flat
  • add coverage for request.security math reducer behavior

Validation

  • targeted validation was completed during the campaign before opening this sync PR

luisleo526 and others added 2 commits July 5, 2026 04:24
A rolling math reducer (math.sum) inside a request.security() expression
lowered to `0.0 /* unsupported: math.sum */`, so the whole security
silently returned 0. _build_security_expr routed the math.* node to the
scalar-math inline lowering (_build_security_math_call) before the TA-site
lookup, discarding the math::Sum helper the security machinery had already
precomputed into a committed _secval_*.

Return that committed _secval_ when a math.* call inside a security
expression is a registered rolling TA site. Scalar math
(abs/round/min/max/...) is not a TA site and still lowers inline.

Adds tests/test_security_math_reducer.py locking both directions.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013tuvZmenDcKuPu8LVqfqag
Pine returns na for strategy.position_avg_price when the position is flat
(position_size == 0). The codegen mapped it directly to the engine field
position_entry_price_, which is 0.0 when flat, silently defeating the common
`na(strategy.position_avg_price)` flat/in-position guard idiom (0.0 is not na)
— exit/SL/TP logic then ran against avg=0 (e.g. a 0-priced stop firing
immediately).

Emit the field guarded by the flat test, matching the engine's own avg-price
accessor which already returns na when FLAT:
(signed_position_size() == 0.0 ? na<double>() : position_entry_price_).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013tuvZmenDcKuPu8LVqfqag
@luisleo526 luisleo526 merged commit baf961e into main Jul 5, 2026
9 checks passed
@luisleo526 luisleo526 deleted the campaign/security-math-reducer branch July 5, 2026 08:39
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