pineforge-codegen 1.6.3__py3-none-any.whl → 1.7.0__py3-none-any.whl

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.
@@ -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,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
@@ -38,30 +38,30 @@ pineforge_codegen/analyzer/diagnostics.py,sha256=5tJZCEeqPbWbYh658iCK5AK43KhBWJU
38
38
  pineforge_codegen/analyzer/tables.py,sha256=zBhk2bw1UhR_eHTWqgs-ojY5bKCTcaieMsQer4oJKW8,10023
39
39
  pineforge_codegen/analyzer/types.py,sha256=r9aMoeL0N_Yubc1DyLjvrvZUFZ7W8zZ_TyST8HxUmFI,59380
40
40
  pineforge_codegen/codegen/__init__.py,sha256=GU1iNcDHG0wbMBz4_rFffEZAx3W6Qh7vjZPbMbP56Cs,2336
41
- pineforge_codegen/codegen/base.py,sha256=ftenKAtRC1Xun9aS3-Gq1GcFZHuX9_B3HlNPREK9DGE,314803
41
+ pineforge_codegen/codegen/base.py,sha256=FwJmnAFJCHPSxZ7EbMFN9rnIiQhzDTpo0G-SH2I55ig,315522
42
42
  pineforge_codegen/codegen/capabilities.py,sha256=WpzpQiRg-aaAxByl5rcm8tKXyB1Kcz9mWEx6K9QDxuQ,17653
43
43
  pineforge_codegen/codegen/checked_settings.py,sha256=oscgGbEWRrGCIcroYkHqGXerH4F0ezbkrLix1fHW4Co,25939
44
44
  pineforge_codegen/codegen/collection_history.py,sha256=ajb4VV1_tlcmNyOaIvFZGkBA10pBRRGx_Y1uDbD9VY4,16886
45
45
  pineforge_codegen/codegen/constant_fold.py,sha256=39QQnkiccwAVYmg3hUDjra5LqvOwKrVmu1HJKE03qlU,5098
46
46
  pineforge_codegen/codegen/drawing.py,sha256=mnDWeTdERMauYqObiq2QOzVuzKOfos6i7qXP2QcUim4,38289
47
- pineforge_codegen/codegen/emit_top.py,sha256=OttQ9NzcqMCKHBbkfdyv-JVXV7Ro6eOSQY-F9m251IE,154334
48
- pineforge_codegen/codegen/helpers.py,sha256=1Ypmc-MxR86te88CRPQl3g3yoit4J8aKZo76Dz4-SVQ,32601
47
+ pineforge_codegen/codegen/emit_top.py,sha256=gHNm0ukVVd6Qqzfkvz1jwfU57IkpRh6_j1hUAizVmrQ,155858
48
+ pineforge_codegen/codegen/helpers.py,sha256=QcD6Trym9Wpb3k8FRFvOm38WA9pQheS8_hnjhbfoOGM,33322
49
49
  pineforge_codegen/codegen/helpers_syminfo.py,sha256=GFrx9i2HXSCmGIpIds0cFrtJ6IQiznfXxH8aI8h3DQ4,4878
50
- pineforge_codegen/codegen/host_members.py,sha256=hmbkxs22NHAcRQ3l4WD7fU_GcdloNgQRgPTebk5ckcc,4599
50
+ pineforge_codegen/codegen/host_members.py,sha256=EmHaeD17CiDVR4YDiU-egS1F0FOYKSnYFQA7lOkcHGo,4635
51
51
  pineforge_codegen/codegen/input.py,sha256=KlitUiphXryWKiPjfUqhk2VLyTVliyiEt_JqkLtABqw,30091
52
52
  pineforge_codegen/codegen/order_shapes.py,sha256=H20ElOyewRdXXY7e8LMdkdj7pxf36H0Q5Jsg3yVPPRY,14848
53
53
  pineforge_codegen/codegen/run_stops.py,sha256=4gkHBySR_sd5AgXXv3R-7Z83zGmoCWLXZFcPxKPngY8,3541
54
- pineforge_codegen/codegen/security.py,sha256=PvMODMxk3p3Ff8jT-3tOiEtZ5yPVZF2TgVB0C77x0Yo,385527
54
+ pineforge_codegen/codegen/security.py,sha256=stN-o5X1hwiV4DZ9PFrGghgccbhSlCMWf7o2g7KJaKA,385707
55
55
  pineforge_codegen/codegen/session_market.py,sha256=-urGaWozZ0k4uNzZ5lPB2fqftpJrftcZiw4eWWMnJKM,3438
56
56
  pineforge_codegen/codegen/ta.py,sha256=w9nvIqbbLCOL8jLnW7z9vMlu8RqYyF8nTtrQoRKnwcA,106771
57
57
  pineforge_codegen/codegen/tables.py,sha256=KiGrUc_j8HHEwb8kUbczl9KHFnyRS3B6PRTmlN6mc1w,65492
58
58
  pineforge_codegen/codegen/tv_number_format.py,sha256=SZ70zOmMD9vC1ko0WQnt6X0q7PJ_-jT93N7ZtquwPLE,11637
59
59
  pineforge_codegen/codegen/types.py,sha256=imumpPN-rGzRB1ntfLMjx1TPjgnhhviuMrUGI5gg-Dw,212937
60
- pineforge_codegen/codegen/visit_call.py,sha256=YQ4oNt1EHzsrKX9ltWX8v4xl4Sk31RmWQjPD9CavyA4,200114
60
+ pineforge_codegen/codegen/visit_call.py,sha256=lwGaWY51PD7lCHb4TLqFFN1evdf0ZqZq36GqP7N2igI,200401
61
61
  pineforge_codegen/codegen/visit_expr.py,sha256=ZnSDMVGTykt0nX4wQ3-YqPwJDKypOhPSEOIc6Gp-Vzg,112835
62
62
  pineforge_codegen/codegen/visit_stmt.py,sha256=lRTCm28ObOIOOPYPQGJkD4qnYRr9_izZWBnhKnHdGPU,118445
63
- pineforge_codegen-1.6.3.dist-info/METADATA,sha256=thcRORyt6TVIKKEWPA6cMWv70VQFg7Lc7tAtqJglJ4Y,54546
64
- pineforge_codegen-1.6.3.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
65
- pineforge_codegen-1.6.3.dist-info/entry_points.txt,sha256=dSUCCmwV4UpL1PyC6THPdQ-l3ddue7ZH1RgRHpASvhY,74
66
- pineforge_codegen-1.6.3.dist-info/licenses/LICENSE,sha256=X0M0ankijg7iZuoI78biEG1kr6klX8NgzDKZAyaPcg4,11161
67
- pineforge_codegen-1.6.3.dist-info/RECORD,,
63
+ pineforge_codegen-1.7.0.dist-info/METADATA,sha256=UjMbxUGRtax8JYU03f2g1q-hFz7tB4wbF6fMvq6e-5I,54546
64
+ pineforge_codegen-1.7.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
65
+ pineforge_codegen-1.7.0.dist-info/entry_points.txt,sha256=dSUCCmwV4UpL1PyC6THPdQ-l3ddue7ZH1RgRHpASvhY,74
66
+ pineforge_codegen-1.7.0.dist-info/licenses/LICENSE,sha256=X0M0ankijg7iZuoI78biEG1kr6klX8NgzDKZAyaPcg4,11161
67
+ pineforge_codegen-1.7.0.dist-info/RECORD,,