pineforge-codegen 1.6.3__tar.gz → 1.7.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/CHANGELOG.md +71 -0
  2. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/CONTRIBUTING.md +19 -0
  3. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/PKG-INFO +1 -1
  4. pineforge_codegen-1.7.0/VERSION +1 -0
  5. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/docs/PUBLIC_CONTRACT.md +44 -0
  6. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/base.py +13 -1
  7. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/emit_top.py +34 -1
  8. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/helpers.py +14 -1
  9. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/host_members.py +1 -0
  10. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/security.py +6 -3
  11. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/visit_call.py +5 -0
  12. pineforge_codegen-1.6.3/VERSION +0 -1
  13. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/.gitignore +0 -0
  14. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/LEGAL.md +0 -0
  15. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/LICENSE +0 -0
  16. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/README.md +0 -0
  17. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/docs/codegen-coverage-gaps.md +0 -0
  18. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/docs/pine-cap-activation.md +0 -0
  19. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/__init__.py +0 -0
  20. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/analyzer/__init__.py +0 -0
  21. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/analyzer/base.py +0 -0
  22. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/analyzer/call_handlers.py +0 -0
  23. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/analyzer/contracts.py +0 -0
  24. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/analyzer/diagnostics.py +0 -0
  25. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/analyzer/tables.py +0 -0
  26. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/analyzer/types.py +0 -0
  27. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/ast_nodes.py +0 -0
  28. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/block_locals.py +0 -0
  29. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/builtin_keywords.py +0 -0
  30. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/__init__.py +0 -0
  31. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/capabilities.py +0 -0
  32. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/checked_settings.py +0 -0
  33. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/collection_history.py +0 -0
  34. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/constant_fold.py +0 -0
  35. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/drawing.py +0 -0
  36. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/helpers_syminfo.py +0 -0
  37. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/input.py +0 -0
  38. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/order_shapes.py +0 -0
  39. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/run_stops.py +0 -0
  40. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/session_market.py +0 -0
  41. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/ta.py +0 -0
  42. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/tables.py +0 -0
  43. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/tv_number_format.py +0 -0
  44. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/types.py +0 -0
  45. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/visit_expr.py +0 -0
  46. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/codegen/visit_stmt.py +0 -0
  47. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/collection_history.py +0 -0
  48. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/cpp_allowlist.py +0 -0
  49. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/diagnostic_codes.py +0 -0
  50. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/diagnostics_catalog.json +0 -0
  51. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/errors.py +0 -0
  52. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/external_requests.py +0 -0
  53. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/finite_ta_length.py +0 -0
  54. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/lexer.py +0 -0
  55. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/library_inline.py +0 -0
  56. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/library_modules.py +0 -0
  57. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/library_v5.py +0 -0
  58. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/limits.py +0 -0
  59. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/matrix_overloads.py +0 -0
  60. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/method_binding.py +0 -0
  61. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/parser.py +0 -0
  62. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/pine_libraries.py +0 -0
  63. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/pine_spelling.py +0 -0
  64. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/pragmas.py +0 -0
  65. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/request_discovery.py +0 -0
  66. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/request_feed_inventory.py +0 -0
  67. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/security_contexts.py +0 -0
  68. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/session_reads.py +0 -0
  69. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/signatures.py +0 -0
  70. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/support_checker.py +0 -0
  71. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/symbols.py +0 -0
  72. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/timeframe_token_cli.py +0 -0
  73. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/tokens.py +0 -0
  74. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pineforge_codegen/tv_input_choices.py +0 -0
  75. {pineforge_codegen-1.6.3 → pineforge_codegen-1.7.0}/pyproject.toml +0 -0
@@ -20,6 +20,77 @@ supported as exact pairs; on the 0.x line they are independent. See the
20
20
  the release commit. A prerelease note describes changes since the preceding
21
21
  prerelease or stable tag; the final stable note consolidates the series.
22
22
 
23
+ ## 1.7.0 — 2026-10-25
24
+
25
+ A minor release of the exact engine/codegen pair, paired with engine `v1.7.0`
26
+ and dated 2026-10-25. This entry records what changed in this repository since
27
+ 1.6.3, and nothing else: three pull requests merged after `v1.6.3`, one of which
28
+ changes the emitted C++ and the other two only the repository's own CI.
29
+
30
+ ### Compatibility and migration
31
+
32
+ - Pairs with engine `v1.7.0` exactly. Regenerate strategy C++ with this release
33
+ and relink every strategy library against the engine `v1.7.0` headers and
34
+ `libpineforge.a`; this applies even when the C ABI number is unchanged
35
+ (README [Engine pairing](README.md#engine-pairing)). Replacing the archive
36
+ alone does not update a statically linked strategy library.
37
+ - The emitted C++ changes, as described below: every generated library gains a
38
+ new exported function, the generated constructor gains a guarded statement
39
+ for scripts that do not read closed-trade rows, and the set of reserved names
40
+ grows. The Python API, the diagnostic codes and severities, and the catalog
41
+ schema are not changed; the order-shapes receipt's `host_reads` list gains
42
+ `set_trade_history_streamable` for scripts that do not read closed-trade rows.
43
+ - Engine behavior changes are described in the paired engine's own release
44
+ notes; this entry does not summarize them.
45
+
46
+ ### Changes since 1.6.3
47
+
48
+ - Declare whether the emitted code reads closed-trade rows, and let scripts that
49
+ do not read them opt into streamed trade history
50
+ ([#213](https://github.com/pineforge-4pass/pineforge-codegen-oss/pull/213)).
51
+ Generated libraries now export `int strategy_reads_trade_history(void)`: `1`
52
+ when the emitted code reads a `strategy.closedtrades.*` accessor, `0` when it
53
+ does not. The closed-trade count (`strategy.closedtrades` alone) and the
54
+ open-trade accessors (`strategy.opentrades.*`) do not count as reads. For a
55
+ script that does not read closed-trade rows, the generated constructor calls
56
+ `this->set_trade_history_streamable(true)`, guarded by
57
+ `#if defined(PINEFORGE_HAS_TRADE_HISTORY_POLICY_V1)` so that it compiles
58
+ against engine headers that lack the setter; a script that reads closed-trade
59
+ rows emits no such call, and the order-shapes receipt's `host_reads` names the
60
+ setter for non-readers only. The constructor's host calls are now qualified
61
+ (`this->configure_pine_strategy(cfg)`). A library generated by an earlier
62
+ release has no such export, and its absence does not establish that the
63
+ script is a non-reader. What the engine does with the streaming setting is
64
+ described in the engine `v1.7.0` release notes.
65
+ - Reserve more names in generated C++ and escape them as `pf_safe_<name>` when a
66
+ Pine identifier uses one (same pull request, documented in the new
67
+ "Identifiers in generated C++" section of
68
+ [docs/PUBLIC_CONTRACT.md](docs/PUBLIC_CONTRACT.md)): the engine namespaces and
69
+ types that `using namespace pineforge;` brings into scope (`report`,
70
+ `report_sink`, `report_sequence`, `source`, `native_calendar`, `pine_color`,
71
+ `RunFailureCode`, `RunFailureInfo`, `QtyType`, `CommissionType`, `XLoc`,
72
+ `YLoc`, `normalize_timezone_for_posix`), the generated C exports
73
+ (`strategy_create`, `strategy_create_checked`, `run_backtest`,
74
+ `run_backtest_full`, `run_backtest_full_checked`, `strategy_free`,
75
+ `report_free`, `strategy_set_input`, `strategy_set_input_checked`,
76
+ `strategy_set_override`, `strategy_set_override_checked`,
77
+ `strategy_set_magnifier_volume_weighted` and the new
78
+ `strategy_reads_trade_history`), the feature macros
79
+ `PINEFORGE_HAS_TRADE_HISTORY_POLICY_V1` and
80
+ `PINEFORGE_HAS_SYMBOL_SECURITY_EVAL_V1`, and the host member
81
+ `set_trade_history_streamable`. A Pine `type report`, for example, now becomes
82
+ `struct pf_safe_report`; before, such a script could fail to compile against the
83
+ 1.7.0 engine headers with an ambiguous reference. User-defined type handles
84
+ and `request.security` evaluators use the escaped spelling too. An identifier
85
+ that does not collide keeps its spelling. The existing `CPP_RESERVED` export lists the new
86
+ names; no new reserved-name API is introduced.
87
+ - Repository and CI only, no effect on emitted C++ or on the Python and JSON
88
+ contract: the `promote-baseline` workflow refuses untrusted heads and actors
89
+ (SEC846,
90
+ [#242](https://github.com/pineforge-4pass/pineforge-codegen-oss/pull/242)),
91
+ and `test.yml` adopts the organization's CI canon (CR-241,
92
+ [#243](https://github.com/pineforge-4pass/pineforge-codegen-oss/pull/243)).
93
+
23
94
  ## 1.6.1 — 2026-10-18
24
95
 
25
96
  A patch release of the exact engine/codegen pair, paired with engine `v1.6.1`
@@ -109,6 +109,25 @@ the Python `build` frontend, run `python -m build`, assemble the npm payload,
109
109
  then run `npm pack --dry-run` from `npm/`. These commands build local artifacts;
110
110
  publishing is a separate release operation.
111
111
 
112
+ `promote-baseline.yml` runs from the base repository on `pull_request_target`
113
+ closes and `workflow_dispatch`, and its credential job depends on
114
+ `promote-guard`. The guard has empty job permissions, holds no secrets, and
115
+ checks out no code. Every pull request close first requires a head in this
116
+ repository with `fork == false`, even when unmerged. An author outside
117
+ `OWNER`/`MEMBER`/`COLLABORATOR` and the
118
+ `dependabot[bot]`/`pineforge-app[bot]` allowlist must have
119
+ `admin`/`maintain`/`write` permission returned by GitHub, including a
120
+ `CONTRIBUTOR` whose organization membership is private. A failed author
121
+ lookup, an empty permission, or a non-write permission refuses the close
122
+ before any unmerged exit or merger check.
123
+ A merged pull request also requires an allowlisted merger or a successful
124
+ `admin`/`maintain`/`write` permission lookup for the merger. An empty merger,
125
+ a failed merger lookup, or an empty or non-write merger permission refuses
126
+ promotion.
127
+ An unmerged close of our own allowed pull request passes the guard with
128
+ promotion skipped. Manual dispatch requires repository write access, and
129
+ every other event is refused.
130
+
112
131
  ## npm publication (operators)
113
132
 
114
133
  The release and npm publication workflows run on GitHub-hosted runners.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: pineforge-codegen
3
- Version: 1.6.3
3
+ Version: 1.7.0
4
4
  Summary: PineScript v6 to C++ transpiler that targets the pineforge-engine runtime.
5
5
  Project-URL: Homepage, https://github.com/pineforge-4pass/pineforge-codegen-oss
6
6
  Project-URL: Issues, https://github.com/pineforge-4pass/pineforge-codegen-oss/issues
@@ -0,0 +1 @@
1
+ 1.7.0
@@ -7,6 +7,50 @@ Text marked **since 1.5.0** describes diagnostic additions that 1.4.0 and
7
7
  earlier releases do not have: the `note` severity, the `user_message` template
8
8
  and the eleven codes that became notes.
9
9
 
10
+ ## Identifiers in generated C++
11
+
12
+ Legal Pine identifiers that collide with C++ keywords, engine names or emitter
13
+ temporaries are escaped in generated C++; they remain valid Pine names. The
14
+ existing `pineforge_codegen.codegen.CPP_RESERVED` export contains the explicit
15
+ reserved names. `_safe_name` also escapes emitter temporary patterns and
16
+ allocates a distinct name when the script already uses the preferred escape.
17
+ An identifier that does not collide keeps its spelling.
18
+
19
+ The imported engine names `report`, `report_sink`, `report_sequence`, `source`,
20
+ `native_calendar`, `pine_color`, `RunFailureCode`, `RunFailureInfo`, `QtyType`,
21
+ `CommissionType`, `XLoc`, `YLoc` and `normalize_timezone_for_posix` are reserved
22
+ because `using namespace pineforge;` brings them into unqualified lookup. A Pine
23
+ `type report`, for example, becomes `struct pf_safe_report` and its references
24
+ use that name. These names live in `codegen/helpers.py`'s
25
+ `CPP_EMITTER_NAMES`, which also reserves the generated C export
26
+ `strategy_reads_trade_history`. A Pine type or function of that name uses
27
+ `pf_safe_strategy_reads_trade_history`; the export keeps its public spelling.
28
+ The same set explicitly reserves the exports `strategy_create`,
29
+ `strategy_create_checked`, `run_backtest`, `run_backtest_full`, `strategy_free`,
30
+ `report_free`, `strategy_set_input`, `strategy_set_override`,
31
+ `strategy_set_magnifier_volume_weighted`, `strategy_set_input_checked`,
32
+ `strategy_set_override_checked` and `run_backtest_full_checked`. Pine identifiers
33
+ use `pf_safe_<name>`; these exports keep their public spellings.
34
+ `CPP_STANDARD_MACROS` reserves `PINEFORGE_HAS_TRADE_HISTORY_POLICY_V1`, so a Pine
35
+ variable of that name uses `pf_safe_PINEFORGE_HAS_TRADE_HISTORY_POLICY_V1`
36
+ instead of expanding the engine's feature macro in a declaration. It also
37
+ explicitly reserves `PINEFORGE_HAS_SYMBOL_SECURITY_EVAL_V1`, using the same
38
+ `pf_safe_` escape for a Pine identifier.
39
+ `codegen/host_members.py`'s `HOST_MEMBER_NAMES` separately reserves the public or
40
+ protected host members the emitter spells, including
41
+ `set_trade_history_streamable`, inherited from `BacktestEngine`. All three sets
42
+ feed the existing `CPP_RESERVED` export; no new reserved-name API is introduced.
43
+
44
+ Since the 1.7.0 trade-history policy addition, generated libraries export
45
+ `int strategy_reads_trade_history(void)`: `1` means the emitted code reads
46
+ closed-trade rows, and `0` means no such read was emitted. A closed-trade count
47
+ alone does not require those rows. For a non-reader, the constructor calls
48
+ `set_trade_history_streamable(true)` under
49
+ `PINEFORGE_HAS_TRADE_HISTORY_POLICY_V1`; a reader emits no policy call.
50
+ The order-shapes receipt's `host_reads` therefore includes the setter only for
51
+ non-readers. Older generated libraries can lack the export; its absence does
52
+ not establish that the script is a non-reader.
53
+
10
54
  ## Optional compiled execution capabilities
11
55
 
12
56
  Availability: **since 1.2.0**, with engine `v1.2.0`'s capability extension and
@@ -413,6 +413,11 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
413
413
  # Set when a chart expression calls ``_pf_session_market_``; its type
414
414
  # and member are emitted once the whole TU is lowered.
415
415
  self._uses_session_market: bool = False
416
+ # Set while lowering a closed-trade accessor. The constructor is
417
+ # emitted before user bodies, so _generate_cpp resolves the deferred
418
+ # policy marker after every expression (including security evaluators
419
+ # and @pf-trace expressions) has been emitted.
420
+ self._reads_trade_history: bool = False
416
421
  # session.* reads inside a request.security payload already warned.
417
422
  self._warned_security_session_sites: set[int] = set()
418
423
  # Top-level lazy-edge TA sites hoisted to every-bar evaluation for the
@@ -1984,7 +1989,7 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
1984
1989
  if udt_spec is not None
1985
1990
  else member_udt_type
1986
1991
  )
1987
- handle_cpp = DRAWING_TYPE_TO_CPP.get(udt_t, udt_t)
1992
+ handle_cpp = DRAWING_TYPE_TO_CPP.get(udt_t, self._safe_name(udt_t))
1988
1993
  lines.append(f" {handle_cpp} {cloned_safe} = {handle_cpp}{{}};")
1989
1994
  elif vname in self._runtime_scalar_var_init_members:
1990
1995
  # Declaration-site state may remain unreached while COOF
@@ -5458,6 +5463,13 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
5458
5463
  # 11b. security evaluators
5459
5464
  self._emit_security_evaluators(lines)
5460
5465
 
5466
+ # ``_emit_constructor`` runs before function bodies and security
5467
+ # evaluators, while the trade-history read flag is set by the call
5468
+ # visitor. Resolve its constructor marker only after all emission is
5469
+ # complete so no generated path can accidentally opt a reader into
5470
+ # streaming.
5471
+ self._settle_trade_history_policy(lines)
5472
+
5461
5473
  # 12. Close class
5462
5474
  lines.append("};")
5463
5475
  lines.append("")
@@ -40,6 +40,9 @@ Mixin contract — host class must provide the following attributes
40
40
  - ``self._map_vars`` (``set[str]``): map-typed var members.
41
41
  - ``self._strategy_series_vars`` (``set[str]``): series mirrors of
42
42
  ``strategy.*`` member values pushed every bar.
43
+ - ``self._reads_trade_history`` (``bool``): set when emitted code reads a
44
+ ``strategy.closedtrades.*`` accessor; drives the deferred retention policy
45
+ and the exported reader bit.
43
46
  - ``self._timeframe_period_vars`` (``set[str]``): identifiers that
44
47
  resolve to ``script_tf_`` in ``configure_security_evaluators``.
45
48
  - ``self._udt_defs`` (``dict[str, list]``): UDT name -> field info,
@@ -106,6 +109,9 @@ from .drawing import DRAWING_LIFETIME_CPP
106
109
  from .tv_number_format import TV_NUMBER_FORMAT_CPP
107
110
 
108
111
 
112
+ _TRADE_HISTORY_POLICY_MARKER = " /* __PINEFORGE_TRADE_HISTORY_POLICY__ */"
113
+
114
+
109
115
  class TopLevelEmitter:
110
116
  """Mixin owning the top-level C++ section emitters and the
111
117
  per-function emitters used by regular Pine functions and UDT
@@ -1234,7 +1240,12 @@ class TopLevelEmitter:
1234
1240
  if self._script_has_input_source():
1235
1241
  ctor_body.append(" cfg.src_series_active = true;")
1236
1242
 
1237
- ctor_body.append(" configure_pine_strategy(cfg);")
1243
+ # The call is resolved after all bodies have been lowered because
1244
+ # closed-trade reads are discovered by the expression visitor. Keep a
1245
+ # marker in the constructor so the policy cannot be enabled for a
1246
+ # reader merely because the constructor is emitted first.
1247
+ ctor_body.append(_TRADE_HISTORY_POLICY_MARKER)
1248
+ ctor_body.append(" this->configure_pine_strategy(cfg);")
1238
1249
 
1239
1250
  if init_parts and ctor_body:
1240
1251
  lines.append(f" explicit GeneratedStrategy() : {', '.join(init_parts)} {{")
@@ -1895,6 +1906,25 @@ class TopLevelEmitter:
1895
1906
  )
1896
1907
  lines.append(f"{pad}}}")
1897
1908
 
1909
+ def _settle_trade_history_policy(self, lines: list[str]) -> None:
1910
+ """Resolve the constructor's deferred trade-history policy marker."""
1911
+ try:
1912
+ marker = lines.index(_TRADE_HISTORY_POLICY_MARKER)
1913
+ except ValueError as exc: # pragma: no cover - generation invariant
1914
+ raise AssertionError(
1915
+ "generated constructor lost its trade-history policy marker"
1916
+ ) from exc
1917
+
1918
+ if self._reads_trade_history:
1919
+ del lines[marker]
1920
+ return
1921
+
1922
+ lines[marker:marker + 1] = [
1923
+ "#if defined(PINEFORGE_HAS_TRADE_HISTORY_POLICY_V1)",
1924
+ " this->set_trade_history_streamable(true);",
1925
+ "#endif",
1926
+ ]
1927
+
1898
1928
  def _emit_extern_c(self, lines: list[str]) -> None:
1899
1929
  lines.append('extern "C" {')
1900
1930
  lines.append("#if defined(PINEFORGE_HAS_RUN_FAILURE_CODES_V1) && defined(PF_SETTINGS_API_VERSION)")
@@ -2016,6 +2046,9 @@ class TopLevelEmitter:
2016
2046
  from .checked_settings import emit_settings_exports
2017
2047
  emit_settings_exports(lines)
2018
2048
  self._capabilities_export_at = len(lines)
2049
+ lines.append(" int strategy_reads_trade_history(void) {")
2050
+ lines.append(f" return {1 if self._reads_trade_history else 0};")
2051
+ lines.append(" }")
2019
2052
  if self._declares_bar_magnifier():
2020
2053
  # TradingView runs a script that declares use_bar_magnifier = true
2021
2054
  # on its bar magnifier; the host reads this export to run it on
@@ -263,6 +263,8 @@ CPP_STANDARD_MACROS = frozenset("""
263
263
  PINEFORGE_HAS_AUX_SECURITY_FEED_V1 PINEFORGE_HAS_SCRIPT_RUN_PREPARE_V1
264
264
  PINEFORGE_HAS_EXPLICIT_PINE_CAP_V1
265
265
  PINEFORGE_HAS_RUN_FAILURE_CODES_V1
266
+ PINEFORGE_HAS_TRADE_HISTORY_POLICY_V1
267
+ PINEFORGE_HAS_SYMBOL_SECURITY_EVAL_V1
266
268
  _PF_NO_DATA_STOP _PF_OTHER_SYMBOL_STOP _PF_ARRAY_STOP _PF_COLLECTION_STOP
267
269
  _PF_NA_STOP _PF_LIMIT_STOP _PF_UNSUPPORTED_STOP _PF_STRING_STOP
268
270
  _PF_ENGINE_INVARIANT _PF_INVARIANT_AT _PF_SETTING_FAILURE
@@ -279,11 +281,16 @@ CPP_STANDARD_MACROS = frozenset("""
279
281
  # Identifiers used by the emitter as class/type names, namespaces, and
280
282
  # generated or inherited methods. A user member with one of these names can
281
283
  # hide a type/callee, or (GeneratedStrategy) conflict with the constructor.
284
+ # Engine namespaces, types and callees are imported by ``using namespace
285
+ # pineforge``; an authored UDT can make its unqualified type reads ambiguous.
282
286
  CPP_EMITTER_NAMES = frozenset("""
283
287
  GeneratedStrategy BacktestEngine Bar Series PineArray PineMap PineMatrix
284
288
  PineGenericMatrix PineStrategyConfig PineStrategyHost StrategyOverrides
285
289
  SymInfo ReportC MagnifierDistribution ChartPoint Line Box Label Linefill
286
- std pineforge ta math
290
+ std pineforge ta math report report_sink report_sequence
291
+ source native_calendar RunFailureCode
292
+ pine_color QtyType CommissionType XLoc YLoc RunFailureInfo
293
+ normalize_timezone_for_posix
287
294
  on_bar on_source_bar prepare_script_run configure_pine_strategy
288
295
  configure_security_evaluators snapshot_script_state restore_script_state
289
296
  commit_script_state set_strategy_override set_input
@@ -292,6 +299,12 @@ CPP_EMITTER_NAMES = frozenset("""
292
299
  strategy_confirmed_bar_api_version strategy_confirmed_bar_receipt
293
300
  strategy_order_shapes_api_version strategy_order_shapes_receipt
294
301
  strategy_settings_api_version strategy_get_effective_settings
302
+ strategy_create strategy_create_checked strategy_free report_free
303
+ run_backtest run_backtest_full run_backtest_full_checked
304
+ strategy_set_input strategy_set_input_checked
305
+ strategy_set_override strategy_set_override_checked
306
+ strategy_set_magnifier_volume_weighted
307
+ strategy_reads_trade_history
295
308
  precalculate
296
309
  strategy_entry strategy_close strategy_close_all strategy_exit
297
310
  strategy_exit_cancel_bracket strategy_cancel strategy_cancel_all strategy_order
@@ -150,6 +150,7 @@ HOST_MEMBER_NAMES = frozenset({
150
150
  "set_pine_risk_max_position_size",
151
151
  "set_recorded_series",
152
152
  "set_strategy_override",
153
+ "set_trade_history_streamable",
153
154
  "signed_position_size",
154
155
  "snapshot_script_state",
155
156
  "strategy_cancel",
@@ -1074,21 +1074,24 @@ class SecurityEmitter:
1074
1074
  for name, bound in frame.items():
1075
1075
  if not isinstance(bound, str):
1076
1076
  continue
1077
+ # Match the expression visitor's C++ spelling, as the
1078
+ # mutable-global rewrite above does.
1079
+ safe = self._safe_name(name)
1077
1080
  series_name = self._security_series_binding_target(bound)
1078
1081
  if series_name is not None:
1079
1082
  ref = self._security_helper_series_ref(series_name)
1080
1083
  result = sub(
1081
- rf"\b{re.escape(name)}\b(?=\s*\[)",
1084
+ rf"\b{re.escape(safe)}\b(?=\s*\[)",
1082
1085
  ref.replace("\\", "\\\\"),
1083
1086
  result,
1084
1087
  )
1085
1088
  result = sub(
1086
- rf"\b{re.escape(name)}\b(?!\s*\[)",
1089
+ rf"\b{re.escape(safe)}\b(?!\s*\[)",
1087
1090
  f"{ref}[0]".replace("\\", "\\\\"),
1088
1091
  result,
1089
1092
  )
1090
1093
  else:
1091
- result = sub(rf"\b{re.escape(name)}\b", bound, result)
1094
+ result = sub(rf"\b{re.escape(safe)}\b", bound, result)
1092
1095
  return result
1093
1096
 
1094
1097
  def _security_lookup_helper_binding(
@@ -3608,6 +3608,11 @@ class CallVisitor:
3608
3608
  if node.args else "0"
3609
3609
  )
3610
3610
  is_open = inner.member == "opentrades"
3611
+ if not is_open:
3612
+ # Closed-trade accessors require the resident trade rows;
3613
+ # counts and history reads are lowered by visit_expr and
3614
+ # never enter this branch.
3615
+ self._reads_trade_history = True
3611
3616
  # Open trades have no exit metadata in Pine
3612
3617
  if is_open and func_name in (
3613
3618
  "exit_price", "exit_time", "exit_comment", "exit_id", "exit_bar_index",
@@ -1 +0,0 @@
1
- 1.6.3