Skip to content

Commit 2922ade

Browse files
committed
Adapter: a process_orders_on_close add stays under the pyramiding cap when the pending opposite entry is not in the close pass (R5 lane PAR-ORDERS, H-MEASURE Finding 6c)
Under the magnifier with process_orders_on_close and calc_on_order_fills, H-MEASURE's differential script (variant v3, pyramiding=1) opened 86 lots on one bar: every fill's recalculation re-issued strategy.entry("ML") as a same-side market add and each one filled. TradingView's tape of that run (hm-mag-diff-v3) books 12 rows and ends flat. Root cause. entry()'s pyramiding check (pine_adapter.cpp, the accepted_in_cycle block) is skipped under process_orders_on_close whenever any opposite opening is pending (opposite_opening_pending): the cap is deferred to the close pass, where ab9714b judged a market add at its fill (add_to_pyramid_market: position_entry_count_ >= pyramiding) and TradingView against the trades then open. That pass fills an opposite opening first only if it processes it; here the pending opposite entry was a PS limit short resting from the previous bar, so nothing moved the book, and no fill-time check exists for a fixed-size add (validate_precommit's lot test covers the variable default sizes only). Each exempted add filled, the fill recalculated, and the next add came. Fix, adapter only (ADR-0001 rule 2: Pine's pyramiding is adapter command policy; no kernel line changes). The exemption still applies when the opposite opening is in the close pass -- a market entry, or one this script bar's own calculation placed -- where the add may be a reversal by the time it fills. With only a priced opposite entry resting from an earlier bar, the add is held to the lots open now: the count ab9714b and TradingView apply at the fill. The row: test_magnified_aggregated_tape gains "the pyramiding cap": no path of any of the eight variants holds more lots than its pyramiding at any script call, and the v3 magnified run books TradingView's 12 rows and ends flat (tape hm-mag-diff-v3 committed from H-MEASURE's evidence, README row added). Against the base library the row fails: "v3 ..., magnifier: 86 lots open at once, pyramiding 1", 8500 units open at the end. After: 176 checks, 0 failures. Every command the change drops, traced on the tapes and the regression batteries, is a same-side add with the lots then open already at the cap (v3 magnified: ML at bars 33 and 59, 1 lot at pyramiding 1; quiet-bar Random10 bar 42: 3 lots at pyramiding 2; Random14 bar 41: 2 at 2; Random21 bar 9: 1 at 1). Those three random runs are the only quiet-bar runs whose trades move; test_publication_witness (4 runs) and test_adapter_live_state_equivalence (9 digests) move with them. All are re-pinned in the lane's named hash commit. Docs: the design's magnified-residual paragraph records the bypass as closed; digits re-anchored. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> (cherry picked from commit 8d312d81fed3802d98f495f93791a3663285f21e) INT26 pick note: the adapter, test and fixture hunks applied without conflict; the ADR and design pages merged on citation digits only.
1 parent a6e6a00 commit 2922ade

10 files changed

Lines changed: 285 additions & 68 deletions

File tree

‎docs/adr/0001-kernel-adapter-boundary.md‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ kernel and own these quirks, each at its site:
209209
- **Seams the adapter overrides.** The kernel is driven, not patched. `PineStrategyHost` overrides
210210
`resolve_execution_terms` (`pine_strategy_host.cpp:676`) for TV sizing and fill spelling;
211211
`validate_execution_precommit` (`pine_strategy_host.cpp:701`), whose
212-
`validate_precommit` (`pine_adapter.cpp:13046`) returns `AdmitWithHostMargin`
213-
(`pine_adapter.cpp:13291`) to own the opening margin decision; `owns_lot_excursions() = true`
212+
`validate_precommit` (`pine_adapter.cpp:13076`) returns `AdmitWithHostMargin`
213+
(`pine_adapter.cpp:13321`) to own the opening margin decision; `owns_lot_excursions() = true`
214214
(`pine_strategy_host.hpp:284-285`) with `closed_lot_excursion` (`pine_strategy_host.cpp:786`), so
215215
the Pine host keeps its own per-lot MFE/MAE, `ab9714be`'s model (RULING A48). Measured against
216216
TradingView by R5 lane H-MEASURE (`tests/test_e19_excursion_tape.cpp`, `lab tv` tapes): where
@@ -232,9 +232,9 @@ kernel and own these quirks, each at its site:
232232
(`select_exit_activation` src/compat/pine/exit_activation.cpp:42, `select_exit_suspension` src/compat/pine/exit_lifecycle.cpp:10, `select_replacement_revival_definition` src/compat/pine/exit_lifecycle.cpp:46) and historical birth reach
233233
(`order_birth.cpp` `src/compat/pine/order_birth.cpp:5-14`).
234234
- **Trail and tick conventions.** The half-tick arm threshold measured against tick-quantized
235-
extremes while the raw running best is retained (`has_trail_request` `pine_adapter.cpp:9591-9596`); the half-tick
235+
extremes while the raw running best is retained (`has_trail_request` `pine_adapter.cpp:9621-9626`); the half-tick
236236
trigger threshold (`source_trigger_threshold` pine_adapter.cpp:333) and raw-vs-booked fill spelling, behind the
237-
adapter's terms seam (`resolve_terms` `pine_adapter.cpp:11683`).
237+
adapter's terms seam (`resolve_terms` `pine_adapter.cpp:11713`).
238238
- **Money arithmetic.** Ten-significant-digit half-up money (`source_money_round`
239239
`pine_adapter.cpp:454-460`, twin `tv_money_round` `pine_policy_support.hpp:9-15`).
240240
- **Quantity dust.** After every applied execution the Pine host erases any lot of at most
@@ -249,8 +249,8 @@ kernel and own these quirks, each at its site:
249249
(`select_reservation_growth_sources` src/compat/pine/reservation_expansion.cpp:9).
250250
- **Margin.** The kernel owns the margin *mechanism* — the level solve, the check points, the
251251
kernel request, its re-pricing, the receipt — and the adapter answers its three policy hooks
252-
with TradingView's: `margin_check_allowed` (`pine_adapter.cpp:14081`) for the scheduling,
253-
`resolve_margin_requirement` (`pine_adapter.cpp:14117`) for the ten-significant-digit money,
252+
with TradingView's: `margin_check_allowed` (`pine_adapter.cpp:14111`) for the scheduling,
253+
`resolve_margin_requirement` (`pine_adapter.cpp:14147`) for the ten-significant-digit money,
254254
and `resolve_margin_call_units` for the lot-floored 4x restore. That hook answers every call
255255
on purpose (R5 lane F7): TradingView floors the restore onto the lot grid before the multiple,
256256
so on a gridded tape the kernel's own `ShortfallMultiple 4` books 5 lots where TradingView
@@ -261,7 +261,7 @@ kernel and own these quirks, each at its site:
261261
margin since F7), while the roll point measures at the remaining path's adverse mark (MG-FX:
262262
0.4116 @ 97 there against TradingView's 0.3996 @ 100). What has no kernel check point at all
263263
stays adapter-side: the `process_orders_on_close` chronology exception
264-
(`non_pooc_commissioned_short` `pine_adapter.cpp:16816`) and
264+
(`non_pooc_commissioned_short` `pine_adapter.cpp:16846`) and
265265
the 1x-long money call — plus the TV admission scopes
266266
(`explicit_pair_scope` `src/compat/pine/market_admission.cpp:33-46`) and the review fold they feed
267267
(`awaits_pair_review` `:67-72`, `fold_admission_history` `:79-136`).
@@ -282,7 +282,7 @@ kernel and own these quirks, each at its site:
282282
projects `NativeCalculationTrigger::BarCloseAndFills` with TradingView's guard literal as
283283
`max_recalculations_per_point`. What stays are the specifics COOF adds on top —
284284
the language-state snapshot/restore around a recalculation, the waypoint-only refill deferral
285-
(`next_source_path_waypoint` `pine_adapter.cpp:8282`), the first-open execution chain and its
285+
(`next_source_path_waypoint` `pine_adapter.cpp:8312`), the first-open execution chain and its
286286
own loop guard (`kFirstOpenLoopGuard` `pine_scheduler_native.cpp:680`), and the two fills Pine refuses to
287287
recalculate on (`suppress_grouped_stop_recalc` `pine_adapter.cpp:5641`).
288288
- **Pine language state and harness flags.** Series, the tick-level barstate flags and
@@ -643,8 +643,8 @@ Every line number in this section names its symbol on this tree, which
643643
| `NativeRunSpec` field | ruling | what the adapter runs instead | measurement and ruling of record | executed native consumers |
644644
|---|---|---|---|---|
645645
| `price_grid`, `grid_rounding` | **native-only** | TradingView's per-order-kind tick rules on top of `None`: `source_trigger_threshold` (`pine_adapter.cpp:342`), `source_level_on_price_grid` (`:328`), `nearest_tick` (`:266`) / `source_bar_fill_tick` (`:287`) / `directional_tick` (`:311`), behind the terms seam | R5-3 and design risk E7 ruled `None` for the adapter before the lane ran; lanes R7 and N13 measured the alternative anyway (raw levels submitted, `QuantizeFillsAndTriggers` with `HalfUp` declared). L8b closed the first blocker (no run aborts). The second has no remedy on either side: TradingView quantizes per order kind (stop and limit legs and a trail's activation on the quantized bar; the trail stop, the running best, stop-limit entries and the `calc_on_order_fills` cursors raw), the grid is one rule for the run, and the old N13 trial moved 30 pinned checks in 4 units; this is historical evidence, not a current-head result; a per-kind mask would spell that inconsistency into the kernel. The corpus cannot arbitrate (every probe runs a 0.01 tick on an on-grid feed; 5 of 312 differ in an engine-only column). Permanent witness: `tests/test_adapter_grid_relower.cpp`, whose section 5 pins the trail stop the grid fires a bar early. Design row PG and §3.6 | `examples/native/native_price_grid_strategy.cpp`, `examples/native/native_price_grid_c.c`, `tests/test_native_price_grid.cpp` |
646-
| `risk` | **native-only** | all of `strategy.risk.*` but the direction: `update_risk_state` (`pine_adapter.cpp:13879`), `SourceDayLedger`, `submit_intraday_loss_close` (`pine_adapter.cpp:14934`), `chart_day_key` (`pine_adapter.cpp:13776`), `compat::pine::IntradayCap` with `IntradayOrderBudget` (337 lines) | Structural first: Pine's risk calls are per-bar statements, so a limit reaches the adapter on script bar 0, after `project()` (`pine_strategy_host.cpp:324`) and `configure_native` (`pine_strategy_host.cpp:325`) have fixed and digested the spec. In substance (lane N12, `tests/test_adapter_risk_relower.cpp`, 62 checks over nine paired scenarios): the drawdown latch samples at the close only and still admits a reversal; the loss-day streak counts trades, not days; the intraday loss closes at the path's adverse extreme, refuses every placement and withdraws the book; the fill cap charges slots, transfers quota and closes at the bar's better extreme on the chart timezone's day. With the kernel seeded on the corpus, 3 of 4 cap probes diverge (3840 of 3916, 312 of 604, 2370 of 2384 rows; re-derived by `scripts/check_seeded_risk_experiment.sh` since R5 lane H-MEASURE). `strategy.risk.allow_entry_in` is the one rule that is the kernel's already (`allowed_open_directions`). Design §3.6 | `examples/native/native_risk_limits_strategy.cpp`, `examples/native/native_trail_risk_strategy.cpp`, `tests/test_native_risk_limits.cpp`, `tests/test_native_c_api.c` |
647-
| `max_abs_units` | **adapter-policy** | `strategy.risk.max_position_size` as a gate on the LIVE book before the fill (`max_position_size` `pine_adapter.cpp:13238-13239`): an entry is refused once the book already holds the limit | design row MG2 (R5-1): the resulting-book cap is the generic one. Measured by N12's scenario `PS` in `tests/test_adapter_risk_relower.cpp`: two-unit entries against a limit of 3 leave the adapter at 4 and the kernel cap at 2 | `tests/test_native_resting_matching_contract.cpp`, `tests/test_native_run_spec.cpp` |
646+
| `risk` | **native-only** | all of `strategy.risk.*` but the direction: `update_risk_state` (`pine_adapter.cpp:13909`), `SourceDayLedger`, `submit_intraday_loss_close` (`pine_adapter.cpp:14964`), `chart_day_key` (`pine_adapter.cpp:13806`), `compat::pine::IntradayCap` with `IntradayOrderBudget` (337 lines) | Structural first: Pine's risk calls are per-bar statements, so a limit reaches the adapter on script bar 0, after `project()` (`pine_strategy_host.cpp:324`) and `configure_native` (`pine_strategy_host.cpp:325`) have fixed and digested the spec. In substance (lane N12, `tests/test_adapter_risk_relower.cpp`, 62 checks over nine paired scenarios): the drawdown latch samples at the close only and still admits a reversal; the loss-day streak counts trades, not days; the intraday loss closes at the path's adverse extreme, refuses every placement and withdraws the book; the fill cap charges slots, transfers quota and closes at the bar's better extreme on the chart timezone's day. With the kernel seeded on the corpus, 3 of 4 cap probes diverge (3840 of 3916, 312 of 604, 2370 of 2384 rows; re-derived by `scripts/check_seeded_risk_experiment.sh` since R5 lane H-MEASURE). `strategy.risk.allow_entry_in` is the one rule that is the kernel's already (`allowed_open_directions`). Design §3.6 | `examples/native/native_risk_limits_strategy.cpp`, `examples/native/native_trail_risk_strategy.cpp`, `tests/test_native_risk_limits.cpp`, `tests/test_native_c_api.c` |
647+
| `max_abs_units` | **adapter-policy** | `strategy.risk.max_position_size` as a gate on the LIVE book before the fill (`max_position_size` `pine_adapter.cpp:13268-13269`): an entry is refused once the book already holds the limit | design row MG2 (R5-1): the resulting-book cap is the generic one. Measured by N12's scenario `PS` in `tests/test_adapter_risk_relower.cpp`: two-unit entries against a limit of 3 leave the adapter at 4 and the kernel cap at 2 | `tests/test_native_resting_matching_contract.cpp`, `tests/test_native_run_spec.cpp` |
648648
| `max_open_lots` | **adapter-policy** | Pine pyramiding is a per-cycle entry count in the adapter's command policy; a resting source entry must not consume a physical-lot cap before it fills, so `project()` leaves the cap unset (`max_open_lots` `pine_adapter.cpp:2360-2362`) | design row MG3 (R5-1); the contract comment in `project()`. Measured against TradingView by R5 lane H-MEASURE (`tests/test_pyramiding_count_differential.cpp`, 15 scenarios on three `lab tv` tapes): TradingView checks an entry once, at its first eligible point, against the trades then open, and does not check a resting entry again at its fill; the kernel's cap books 14 of the 15 as TradingView does, the adapter's per-cycle count 8 (it counts a resting entry, admits three market entries on a flat bar and keeps a slot another entry's exit drained). A recorded divergence: the retention stands pending a re-lowering ruling | `tests/test_native_resting_matching_contract.cpp`, `tests/test_native_margin_model.cpp` |
649649
| `initial_margin_fraction` | **adapter-policy** | TradingView's ten-significant-digit money admission against the signal-time tuple, answered as `AdmitWithHostMargin`; the `margin` model the adapter does declare is maintenance-only (`NativeMarginModel` `pine_adapter.cpp:2387-2395`, `margin.maintenance_long` `pine_adapter.cpp:2402`) | design row MG4 and the wave-4 ruling recorded in `project()`: a positive initial requirement would decline openings TradingView takes. Measured both ways by R5 lane H-MEASURE (`tests/test_adapter_margin_schedule_differential.cpp`, M11): the kernel's gate declines a 1x long whose entry fee the adapter admits (`InitialMargin`), and admits a gap-up add the adapter refuses against its signal-time equity; `ab9714be` books the adapter's answer both times | `tests/test_native_precommit_view.cpp`, `tests/test_native_margin_model.cpp` |
650650
| `report_open_position_at_end` | **adapter-policy** | TradingView's range-end report re-marks the curve's last point and re-folds every extreme from it (`scheduler_record_range_end` pine_strategy_host.cpp:1713): report shape, not a mark-to-market row (`KernelRecordedAtHostMarks` pine_adapter.cpp:2298) | design row RP5; the kernel reads the field under `KernelRecorded` only, which `scripts/check_adapter_spec_shadowing.py` gates | `examples/native/native_sized_report_strategy.cpp`, `tests/test_native_report_truth.cpp` |

0 commit comments

Comments
 (0)