Skip to content

Commit 9aacd54

Browse files
luisleo526claude
andauthored
Build an N-day chart on the year-restart N-day grid (#829)
* Build an N-day chart on the year-restart N-day grid A strategy whose chart timeframe is N days (2D, 3D, 5D ...) built from daily bars ran on the days-since-1970 grid, and the chart's bar count, last_bar_index, barstate.islast and open-trade row came from the wrong bars. TradingView builds an N-day chart from N consecutive trading dates counted from the first trading date of each calendar year (the last bucket of a year is cut at its end), and its last_bar_index, barstate.islast and open-trade row follow that chart's own last bar. The engine already had that partition for request.security; this change installs it for the chart itself. The scheduler creates the existing partition source for an aggregated chart with a day count of two or more and hands it to the execution consumer, whose script bucket cursor, interval lookup, chart close and chart-time offsets read it. Only this chart path drops a leading bucket that opens before the input range and seals the final historical bucket. The calendar gains an interval overload that takes the partition's adjacent opens. The epoch-key paths, request.security, the D, W and M charts, intraday charts and runs whose input and chart timeframes are equal do not change, and a run without a partition adds nothing to the state hash. A day count whose seconds do not fit the legacy int conversion is calendar-only. Limits: a live stream keeps the previous chart path, and a selected window on a generated N-day chart partition is refused with the existing coded stop window_mode_unsupported (capability selected_window_chart_timeframe), because the window planner reconstructs the nominal calendar. Time offsets, exchange calendars and intraday input are not measured against TradingView. tests/test_n_day_chart_timeframe.cpp holds the rows on synthetic daily bars: the 3D grid, the last bar index, barstate.islast, the open row at the end, the 2D and 5D grids, the stream and refusal rows, the time offsets and the controls that must not move. It is registered through tests/cr150.d/n-day-chart-timeframe.cmake. scripts/check_native_cpp_versions.py lists the new reads of the retained input bars in the scheduler. TradingView tapes (sha256): 3D chart 1b2ef41a6f1ba4096e6659e12d5ef3f03c8bf2a51e4bb0d8331d814d9aade262, 2D chart ea679d91cbc0cd078c80604e2c73b3c9bb3104ff8d1989695da6abecb8b9e174, 5D chart 5342be10bdda8185b12b2c6a95b9b26101334cd1f58663ceb1f28c42197cb5dc, last_bar_index 05617c83d33cbd8be7a0d64356dc8f40e5b8d2ab20baa1b74b08b8aa66a07d25, barstate.islast 4ae8e0a932b7da6ffb4e0bcd5de9ee4d514cd6b78dbbb3fa4207aba1a2c6420d. Refs #811. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * Re-anchor the line citations of the published pages (digits only) The N-day chart commit moved code in src/native_execution_consumer.cpp, src/native_execution_consumer.hpp, src/source/pine_strategy_host.cpp, src/source/pine_scheduler_native.cpp and src/timeframe.cpp, so the file:line citations of the published pages that point into them went stale, and scripts/check_doc_anchors.py reported 212 bad anchors. This commit changes digits only: the line numbers inside file:line citations of nine pages (145 changed lines, 233 changed numbers) and nothing else. No sentence, word, content pin or table is edited. The numbers were moved with the repository's own tools: scripts/check_doc_anchors.py --fix (the cited symbol sits on exactly one line) and scripts/reanchor_doc_citations.py --ref 360c4ab (a line map from the commit before the N-day chart change, where the pages were correct). The last six citations, which claim no symbol, were carried across the same line map by hand: the cited code line is the same text at its new number. scripts/check_doc_anchors.py reports no bad anchor on the result, and scripts/check_doc_reverts.py reports nothing, since a re-anchor is not an edit. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * Re-anchor the line citations of the published pages (digits only) Merging main moved code in src/native_execution_consumer.cpp, src/source/pine_strategy_host.cpp, src/source/pine_scheduler_native.cpp and the other files the N-day chart change touches, so the file:line citations of the published pages that point into them went stale, and scripts/check_doc_anchors.py reported 208 bad anchors on the merge. This commit changes digits only: the line numbers inside file:line citations of nine pages (140 changed lines) and nothing else. No sentence, word, content pin or table is edited. The numbers were moved with the repository's own tools: scripts/check_doc_anchors.py --fix (the cited symbol sits on exactly one line) and scripts/reanchor_doc_citations.py --ref e163980 (a line map from main, where the pages are correct). The last six citations, which claim no symbol, were carried across by hand: the cited code line is the same text at its new number (one of them, the cursor constructor, gained a parameter on its line). Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Sonnet 5.5 <[email protected]>
1 parent 526416b commit 9aacd54

22 files changed

Lines changed: 950 additions & 167 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ version number follows semantic versioning over the surfaces the
77

88
## Unreleased
99

10+
- Multi-day charts restart their trading-date grid each year; measured on 24x7 daily input for chart bars, last_bar_index, barstate.islast and the open-trade report row. Streams keep the previous chart path; selected windows on generated N-day chart partitions are refused. Time offsets, exchange calendars and intraday input are not measured against TradingView.
1011
- `str.match` no longer reports "no match" after a memory failure; the failure now reaches `out_of_memory`.
1112
A pattern over the limits of the engine's regular-expression library stops the run with `pine_string_error` and reason `regex_resource_limit`, which is not retryable.
1213
An invalid regular expression now stops the run with a typed error, as TradingView does; it used to return an empty string. The message is TradingView's text with the engine's bar prefix, `Error on bar N: `, or `Error before the first bar: ` when no bar is being executed.

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

Lines changed: 37 additions & 37 deletions
Large diffs are not rendered by default.

‎docs/design/arena-bound.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ The carried declaration fields are visible in `PineStrategyConfig` at
162162
| pyramiding | Source entry admission policy, including pending/same-point rules. The optional generic cap is `max_open_lots` at `include/pineforge/native_run_spec.hpp:664`; the Pine projection deliberately leaves it unset (the `max_open_lots` setting spelling is at `src/source/pine_adapter.cpp:2836`). Do not assume this caps strategy.order or use it as an unconditional H. |
163163
| calc_on_order_fills | Extra source callbacks. The adapter passes its 1<<20 guard `kCoofLoopGuard` through the native cadence at `src/source/pine_adapter.cpp:3017`; the `kCoofLoopGuard` literal is declared at `include/pineforge/source/pine_adapter.hpp:77`. The native point budget is a callback bound, not a total fill bound. |
164164
| max_recalculations_per_point | Further executions still apply after callbacks stop; see the cadence comment and `max_recalculations_per_point` declaration at `include/pineforge/native_run_spec.hpp:686-695`. The actual counter check is `src/native_execution_consumer.cpp:8272`. |
165-
| first-open COOF chain | A second source-side guard exists at `src/source/pine_scheduler_native.cpp:888`. Count this source path; a native budget of 8 is not Pine's bound. |
165+
| first-open COOF chain | A second source-side guard exists at `src/source/pine_scheduler_native.cpp:945`. Count this source path; a native budget of 8 is not Pine's bound. |
166166
| process_orders_on_close | Adds a matching opportunity after calculation; projection at `src/source/pine_adapter.cpp:3006`. It does not impose an order/trade count. |
167167
| default_qty, margin, initial capital | Admission/sizing predicates can tighten a separately proved schedule. They provide no universal positive minimum close quantity or minimum loss, hence no universal T. Margin can create additional reductions. |
168168
| max_bars_back | Series-history setting, not a run/trade limit. The native mapping explicitly rules it out at `docs/pages/pine-to-native.md:142`. |
@@ -558,7 +558,7 @@ non-command certificates exactly.
558558
These formulas apply to the EngineRecorded streamed invocation. The
559559
selected recorder appends its anchor and selected observations in one
560560
writer (`equity_scratch` append at `src/engine_report.cpp:1563`), and capture transfers that writer as `metric_curve`
561-
instead of copying its prefix (`src/native_execution_consumer.cpp:13420`).
561+
instead of copying its prefix (`src/native_execution_consumer.cpp:13447`).
562562
HostRecorded array presentation and a second full curve are outside this
563563
profile. Adding another persistent scratch user requires another term and
564564
rounding region.

‎docs/design/consumed-partial-exit-fold.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ The slab uses design (b), maintained transforms. Deferring the sort until a
5656
hash request would put the work on the per-bar path. The scheduler calls
5757
`scheduler_record_broker_hash` once per terminal script dispatch, including
5858
its quiet carried-callback branch
59-
(`src/source/pine_scheduler_native.cpp:639`,
59+
(`src/source/pine_scheduler_native.cpp:696`,
6060
`src/source/pine_scheduler_native.cpp:725`). With recording enabled this
61-
calls `broker_state_hash` (`src/source/pine_strategy_host.cpp:3464`).
61+
calls `broker_state_hash` (`src/source/pine_strategy_host.cpp:3467`).
6262
`on_native_bar` then refreshes the recorded row after the adapter's close
63-
(`src/source/pine_strategy_host.cpp:939`); its `recording` guard
64-
(`src/source/pine_strategy_host.cpp:955`) enables
63+
(`src/source/pine_strategy_host.cpp:942`); its `recording` guard
64+
(`src/source/pine_strategy_host.cpp:958`) enables
6565
`capture_script_continuation_hash`
66-
(`src/source/pine_strategy_host.cpp:870`). Thus an ordinary recorded script
66+
(`src/source/pine_strategy_host.cpp:873`). Thus an ordinary recorded script
6767
bar folds the table twice. Recorded magnifier sub-bars can refresh it too.
6868
Terminal input updates also call `capture_script_continuation_hash`
69-
(`src/source/pine_strategy_host.cpp:895`). Without recording, that refresh
69+
(`src/source/pine_strategy_host.cpp:898`). Without recording, that refresh
7070
does not call the broker hash; explicit broker/stream hash queries still do.
7171
Each broker hash reaches `hash_host_extension`
7272
(`src/engine_state_hash.cpp:129`); the adapter's `hash_state`

‎docs/design/cr150-contracts.md‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,18 +223,18 @@ references; L15a must maintain these anchors and mechanically pin every current
223223

224224
| Reader to route through the view/source | Source anchors in this tree | DESIGN lines |
225225
|---|---|---|
226-
| Kernel instrument-feed pump: current bar, close, next open, count | `NativeExecutionConsumer::pump_instrument_subscription` `src/native_execution_consumer.hpp:1291` | 357-358 |
226+
| Kernel instrument-feed pump: current bar, close, next open, count | `NativeExecutionConsumer::pump_instrument_subscription` `src/native_execution_consumer.hpp:1306` | 357-358 |
227227
| Host feed lookup/delivery and column reads | `installed_symbol_feed` `src/source/pine_security_eval.cpp:1081-1090`; `installed_symbol_feed` `src/source/pine_security_eval.cpp:1313-1323`; `installed_symbol_feed` `src/source/pine_security_eval.cpp:1342-1350` | 359-360 |
228-
| Feed validation: bar values/order, close length/bounds, column length; configure and consumer entry points | `feed.bars` `src/native_run_spec.cpp:259-290`; `instrument_feed_shapes` `src/native_run_spec.cpp:743`; `validate_native_instrument_feeds` `src/native_run_spec.cpp:1019-1022`; `validate_native_timeframe_subscriptions` `src/native_execution_consumer.cpp:10281`; `validate_native_timeframe_subscriptions` `src/native_execution_consumer.cpp:10281` | 361-363 |
228+
| Feed validation: bar values/order, close length/bounds, column length; configure and consumer entry points | `feed.bars` `src/native_run_spec.cpp:259-290`; `instrument_feed_shapes` `src/native_run_spec.cpp:743`; `validate_native_instrument_feeds` `src/native_run_spec.cpp:1019-1022`; `validate_native_timeframe_subscriptions` `src/native_execution_consumer.cpp:10308`; `validate_native_timeframe_subscriptions` `src/native_execution_consumer.cpp:10308` | 361-363 |
229229
| Adapter projection validation: use the context normalizer; host stages after lending and before projection, using a context built from its store | `normalize_projected_spec` `src/source/pine_adapter.cpp:3195` | 361-363; V1/V2 change 5 |
230-
| Instrument-feed digest and cached/uncached continuation folds | `digest_instrument_feed_views` `src/native_run_spec.cpp:1162-1205`; `native_instrument_feeds_digest` `src/native_execution_consumer.cpp:332`; `native_instrument_feeds_digest` `src/native_execution_consumer.cpp:2986`; `native_run_spec_digest` `src/native_execution_consumer.cpp:14576` | 364-365 |
230+
| Instrument-feed digest and cached/uncached continuation folds | `digest_instrument_feed_views` `src/native_run_spec.cpp:1162-1205`; `native_instrument_feeds_digest` `src/native_execution_consumer.cpp:332`; `native_instrument_feeds_digest` `src/native_execution_consumer.cpp:3002`; `native_run_spec_digest` `src/native_execution_consumer.cpp:14646` | 364-365 |
231231
| Staged feed counts in host state and its hash | `installed_symbol_feed` `src/source/pine_strategy_host.cpp:536-546`; `staged_.instrument_feeds` `src/source/pine_state_hash.cpp:339-346` | 366-367 |
232232
| Refresh of symbol-data digest and host hash fold | `refresh_symbol_data_digest` `src/source/pine_security_eval.cpp:1639-1652`; `symbol_data_digest_` `src/source/pine_state_hash.cpp:1240-1246` | 367-368 |
233233
| Restore/store upkeep and column-setter length check | `held.bars` `src/source/pine_security_eval.cpp:1113-1123`; `data.bars` `src/source/pine_security_eval.cpp:1455-1461` | 369-370 |
234-
| Lower-path preflight/validation | `NativeExecutionConsumer::preflight_intrabar_path` `src/native_execution_consumer.hpp:1250` | 371,1221-1222 |
234+
| Lower-path preflight/validation | `NativeExecutionConsumer::preflight_intrabar_path` `src/native_execution_consumer.hpp:1265` | 371,1221-1222 |
235235
| Lower-path digest (count first) and continuation hash | `native_intrabar_path_digest` `src/native_run_spec.cpp:891-919`; `native_intrabar_path_digest` `src/native_execution_consumer.cpp:297` | 371,1223-1224 |
236-
| Per-script-bar lower-path binary search/walk | `candidate` `src/native_execution_consumer.cpp:9343-9350` | 371,1225 |
237-
| Scheduler sub-bar lookup and one-bar look-ahead | `PineScheduler::lower_path_bar_at` `src/source/pine_scheduler_native.cpp:265-287`; `next_open` `src/source/pine_scheduler_native.cpp:328-329` | 371,1226-1227 |
236+
| Per-script-bar lower-path binary search/walk | `candidate` `src/native_execution_consumer.cpp:9359-9366` | 371,1225 |
237+
| Scheduler sub-bar lookup and one-bar look-ahead | `PineScheduler::lower_path_bar_at` `src/source/pine_scheduler_native.cpp:322-344`; `next_open` `src/source/pine_scheduler_native.cpp:385-386` | 371,1226-1227 |
238238
| Lower-path emptiness test | `inactive_sampler_path` `src/source/pine_adapter.cpp:3639` | 371,1228 |
239239

240240
Every count/digest must fold the same bytes/order for copied and borrowed
@@ -318,14 +318,14 @@ L17c/d and L8c use one report path. `fill_trades_section`
318318
open rows for a sink. `report_closed_net_profit`
319319
(`src/engine_report.cpp:537`) starts at `ClosedTradePrefix::pnl` and adds the
320320
resident suffix in order. `finish_streamed_trade_metrics`
321-
(`src/native_execution_consumer.cpp:14396`) finishes copies of the sealed
321+
(`src/native_execution_consumer.cpp:14423`) finishes copies of the sealed
322322
accumulators over resident closed rows, then the presented open rows. The
323323
source host's terminal-quote presentation continues that same closed-row PnL
324324
fold before adding its edited open rows (`net_profit`
325-
`src/source/pine_strategy_host.cpp:2405-2406`).
325+
`src/source/pine_strategy_host.cpp:2408-2409`).
326326

327327
Selected carry-in validation includes the minimum entry time folded at seal
328-
(`first_entry_time` `src/native_execution_consumer.cpp:13269`), so a
328+
(`first_entry_time` `src/native_execution_consumer.cpp:13296`), so a
329329
window starting after a sealed entry refuses with the existing typed
330330
`engine_invariant`. Validation never indexes a retired row. `write_report_sink_trades`
331331
(`src/engine_report.cpp:1700`) resumes at the global T cursor: sealed rows

0 commit comments

Comments
 (0)