@pineforge/codegen-pyodide 0.7.1 → 0.7.3
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.
- package/package.json +1 -1
- package/pineforge_codegen/codegen/base.py +109 -20
- package/pineforge_codegen/codegen/visit_call.py +5 -0
- package/pineforge_codegen/support_checker.py +62 -19
- package/pineforge_codegen-0.7.3.tar.gz +0 -0
- package/release.json +2 -2
- package/tables.json +3 -4
- package/transpile.worker.mjs +21 -0
- package/pineforge_codegen-0.7.1.tar.gz +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pineforge/codegen-pyodide",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "Gate-validated Pyodide payload for the PineScript v6 -> C++ transpiler: archive (run in Pyodide), unpacked source, introspected tables, and release metadata.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.mjs",
|
|
@@ -185,19 +185,31 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
|
|
|
185
185
|
# This ensures sub-function series vars get cloned for the parent's call sites.
|
|
186
186
|
func_var_originals: dict[str, list[str]] = {} # func_name -> list of original var names
|
|
187
187
|
|
|
188
|
-
# First, collect all function-scoped series vars (union across all functions)
|
|
189
|
-
|
|
188
|
+
# First, collect all function-scoped series vars (union across all functions).
|
|
189
|
+
# Use an ordered, de-duplicated list (NOT a set): set iteration order is
|
|
190
|
+
# PYTHONHASHSEED-randomized, and this order reaches emitted C++ member
|
|
191
|
+
# declarations via ``orig_names`` -> ``func_var_originals`` ->
|
|
192
|
+
# ``_func_cs_var_remap``. ``ctx.func_series_vars`` is a dict whose VALUES
|
|
193
|
+
# are themselves sets (analyzer stores ``dict[str, set]``), so we must
|
|
194
|
+
# iterate each value in ``sorted`` order to be hash-seed independent.
|
|
195
|
+
all_func_scoped_series: list[str] = []
|
|
190
196
|
for svars in ctx.func_series_vars.values():
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
197
|
+
for sv in sorted(svars):
|
|
198
|
+
if sv not in all_func_scoped_series:
|
|
199
|
+
all_func_scoped_series.append(sv)
|
|
200
|
+
# Also include function-scoped var_members (same ordered-list rationale).
|
|
201
|
+
# ``ctx.func_var_members`` values are lists (already insertion-ordered).
|
|
202
|
+
all_func_scoped_vars: list[str] = []
|
|
194
203
|
for vlist in ctx.func_var_members.values():
|
|
195
204
|
for n, _, _ in vlist:
|
|
196
|
-
all_func_scoped_vars
|
|
205
|
+
if n not in all_func_scoped_vars:
|
|
206
|
+
all_func_scoped_vars.append(n)
|
|
197
207
|
|
|
198
208
|
# For each function with call-site cloning (has TA ranges or is called multiple times),
|
|
199
|
-
# include ALL function-scoped series/var vars that could be used in its body
|
|
200
|
-
|
|
209
|
+
# include ALL function-scoped series/var vars that could be used in its body.
|
|
210
|
+
# Iterate the dict directly (insertion-ordered) rather than ``set(...keys())``,
|
|
211
|
+
# which would randomize the order of emitted clones across hash seeds.
|
|
212
|
+
for fname in ctx.func_call_site_counts:
|
|
201
213
|
total_cs = ctx.func_call_site_counts[fname]
|
|
202
214
|
if total_cs <= 1:
|
|
203
215
|
continue # No cloning needed for single-call-site functions
|
|
@@ -207,9 +219,9 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
|
|
|
207
219
|
for n, _, _ in ctx.func_var_members[fname]:
|
|
208
220
|
if n not in orig_names:
|
|
209
221
|
orig_names.append(n)
|
|
210
|
-
# Include function's own series vars
|
|
222
|
+
# Include function's own series vars (set -> sorted for determinism)
|
|
211
223
|
if fname in ctx.func_series_vars:
|
|
212
|
-
for sv in ctx.func_series_vars[fname]:
|
|
224
|
+
for sv in sorted(ctx.func_series_vars[fname]):
|
|
213
225
|
if sv not in orig_names:
|
|
214
226
|
orig_names.append(sv)
|
|
215
227
|
# Include series vars from sub-functions (they share the same class members)
|
|
@@ -457,6 +469,79 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
|
|
|
457
469
|
self._register_global_aggregate_member_types()
|
|
458
470
|
self._uses_matrix = self._detect_matrix_usage()
|
|
459
471
|
|
|
472
|
+
# max_bars_back: the per-variable history depth the engine's Series<T>
|
|
473
|
+
# ring buffer should retain. Pine exposes this two ways — the
|
|
474
|
+
# ``strategy(..., max_bars_back=N)`` kwarg (global) and the
|
|
475
|
+
# ``max_bars_back(var, N)`` function (per-var). The engine's
|
|
476
|
+
# ``Series<T>(int max_len)`` ctor (default 500, include/pineforge/
|
|
477
|
+
# series.hpp) is the wiring point: reads past the retained depth return
|
|
478
|
+
# na, so honoring the directive means constructing each Series with a
|
|
479
|
+
# capacity >= the requested depth. We take the MAX requested N and apply
|
|
480
|
+
# it (via ``_series_decl_suffix`` -> ``{N}``) to the directly-declared
|
|
481
|
+
# ``Series<T>`` members — a safe superset of Pine's per-var semantics
|
|
482
|
+
# (it never retains LESS than Pine, so any history access that succeeds
|
|
483
|
+
# in Pine succeeds here). ``None`` => no directive => keep the engine
|
|
484
|
+
# default 500 (emit a bare ``Series<T>`` with no ctor arg, so
|
|
485
|
+
# directive-free output is byte-identical to before).
|
|
486
|
+
#
|
|
487
|
+
# KNOWN LIMITATION: the lazily-constructed security-helper map series
|
|
488
|
+
# (``_security_helper_series_``, the ``std::unordered_map<std::string,
|
|
489
|
+
# Series<double>>`` ~line 971) do NOT pick up the cap. Their entries are
|
|
490
|
+
# default-constructed on first ``operator[]`` access, so they always use
|
|
491
|
+
# the engine default 500 regardless of the requested ``N``. A
|
|
492
|
+
# max_bars_back directive larger than 500 is therefore not honored for
|
|
493
|
+
# history reads off security-helper series.
|
|
494
|
+
self._max_bars_back_cap: int | None = self._compute_max_bars_back_cap()
|
|
495
|
+
|
|
496
|
+
@staticmethod
|
|
497
|
+
def _int_literal_value(node: ASTNode | None) -> int | None:
|
|
498
|
+
"""Return the integer value of a (possibly unary-minus) NumberLiteral,
|
|
499
|
+
or None if ``node`` is not an integer literal expression."""
|
|
500
|
+
if isinstance(node, UnaryOp) and node.op == "-":
|
|
501
|
+
inner = CodeGen._int_literal_value(node.operand)
|
|
502
|
+
return -inner if inner is not None else None
|
|
503
|
+
if isinstance(node, NumberLiteral) and isinstance(node.value, int):
|
|
504
|
+
return node.value
|
|
505
|
+
if isinstance(node, NumberLiteral) and isinstance(node.value, float):
|
|
506
|
+
# Pine accepts ``max_bars_back=5e2`` style; accept integral floats.
|
|
507
|
+
return int(node.value) if node.value.is_integer() else None
|
|
508
|
+
return None
|
|
509
|
+
|
|
510
|
+
def _compute_max_bars_back_cap(self) -> int | None:
|
|
511
|
+
"""Scan the AST for max_bars_back directives (strategy() kwarg AND the
|
|
512
|
+
bare function call) and return the largest positive integer requested,
|
|
513
|
+
or None if none is present / none is a usable literal."""
|
|
514
|
+
ast = getattr(self.ctx, "ast", None)
|
|
515
|
+
if ast is None:
|
|
516
|
+
return None
|
|
517
|
+
caps: list[int] = []
|
|
518
|
+
for node in self._walk_ast(ast):
|
|
519
|
+
if isinstance(node, StrategyDecl):
|
|
520
|
+
val = self._int_literal_value(node.kwargs.get("max_bars_back"))
|
|
521
|
+
if val is not None and val > 0:
|
|
522
|
+
caps.append(val)
|
|
523
|
+
elif (
|
|
524
|
+
isinstance(node, FuncCall)
|
|
525
|
+
and isinstance(node.callee, Identifier)
|
|
526
|
+
and node.callee.name == "max_bars_back"
|
|
527
|
+
):
|
|
528
|
+
# max_bars_back(var, num) — second positional arg, or the
|
|
529
|
+
# ``num=`` kwarg, is the depth.
|
|
530
|
+
num_node = None
|
|
531
|
+
if len(node.args) >= 2:
|
|
532
|
+
num_node = node.args[1]
|
|
533
|
+
elif "num" in node.kwargs:
|
|
534
|
+
num_node = node.kwargs["num"]
|
|
535
|
+
val = self._int_literal_value(num_node)
|
|
536
|
+
if val is not None and val > 0:
|
|
537
|
+
caps.append(val)
|
|
538
|
+
return max(caps) if caps else None
|
|
539
|
+
|
|
540
|
+
def _series_decl_suffix(self) -> str:
|
|
541
|
+
"""C++ constructor-arg suffix for Series<T> member declarations. Empty
|
|
542
|
+
(engine default 500) unless a max_bars_back directive raised the cap."""
|
|
543
|
+
return f"{{{self._max_bars_back_cap}}}" if self._max_bars_back_cap else ""
|
|
544
|
+
|
|
460
545
|
def _register_global_aggregate_member_types(self) -> None:
|
|
461
546
|
"""Infer matrix/array/map class members for global non-var declarations from RHS AST.
|
|
462
547
|
|
|
@@ -790,6 +875,10 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
|
|
|
790
875
|
|
|
791
876
|
lines: list[str] = []
|
|
792
877
|
|
|
878
|
+
# Series<T> ctor-arg suffix from any max_bars_back directive (empty when
|
|
879
|
+
# absent, so directive-free output is byte-identical to before).
|
|
880
|
+
_mbb = self._series_decl_suffix()
|
|
881
|
+
|
|
793
882
|
# 1. Includes
|
|
794
883
|
self._emit_includes(lines)
|
|
795
884
|
|
|
@@ -863,7 +952,7 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
|
|
|
863
952
|
self._security_ohlc_hist_fields_by_sec.get(sec_id, ())
|
|
864
953
|
):
|
|
865
954
|
lines.append(
|
|
866
|
-
f" Series<double> {self._security_ohlc_hist_series_cpp(sec_id, field)};"
|
|
955
|
+
f" Series<double> {self._security_ohlc_hist_series_cpp(sec_id, field)}{_mbb};"
|
|
867
956
|
)
|
|
868
957
|
continue
|
|
869
958
|
if returns_tuple and tuple_size and tuple_size > 0 and isinstance(expr_node, TupleLiteral):
|
|
@@ -884,7 +973,7 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
|
|
|
884
973
|
lines.append(f" double _req_sec_{sec_id} = na<double>();")
|
|
885
974
|
for field in sorted(self._security_ohlc_hist_fields_by_sec.get(sec_id, ())):
|
|
886
975
|
lines.append(
|
|
887
|
-
f" Series<double> {self._security_ohlc_hist_series_cpp(sec_id, field)};"
|
|
976
|
+
f" Series<double> {self._security_ohlc_hist_series_cpp(sec_id, field)}{_mbb};"
|
|
888
977
|
)
|
|
889
978
|
|
|
890
979
|
if self._security_calls:
|
|
@@ -899,7 +988,7 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
|
|
|
899
988
|
state_name = self._security_state_name(info["sec_id"], name)
|
|
900
989
|
cpp_type = self._security_cpp_type_for_mutable(name, ginfo)
|
|
901
990
|
if getattr(ginfo, "is_series", False):
|
|
902
|
-
lines.append(f" Series<{cpp_type}> {state_name};")
|
|
991
|
+
lines.append(f" Series<{cpp_type}> {state_name}{_mbb};")
|
|
903
992
|
else:
|
|
904
993
|
default = self._default_for_type(cpp_type)
|
|
905
994
|
lines.append(f" {cpp_type} {state_name} = {default};")
|
|
@@ -926,7 +1015,7 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
|
|
|
926
1015
|
|
|
927
1016
|
# 4. Series members for bar field history
|
|
928
1017
|
for field_name in sorted(self.ctx.series_bar_fields):
|
|
929
|
-
lines.append(f" Series<double> _s_{field_name};")
|
|
1018
|
+
lines.append(f" Series<double> _s_{field_name}{_mbb};")
|
|
930
1019
|
|
|
931
1020
|
# 5. var/varip members (deduplicate by name)
|
|
932
1021
|
seen_var_members: set[str] = set()
|
|
@@ -975,7 +1064,7 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
|
|
|
975
1064
|
if cpp_type == "int" and self._is_int64_builtin_init(name):
|
|
976
1065
|
cpp_type = "int64_t"
|
|
977
1066
|
if name in self.ctx.series_vars:
|
|
978
|
-
lines.append(f" Series<{cpp_type}> {safe};")
|
|
1067
|
+
lines.append(f" Series<{cpp_type}> {safe}{_mbb};")
|
|
979
1068
|
else:
|
|
980
1069
|
lines.append(f" {cpp_type} {safe};")
|
|
981
1070
|
|
|
@@ -984,7 +1073,7 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
|
|
|
984
1073
|
if name not in self._var_names:
|
|
985
1074
|
safe = self._safe_name(name)
|
|
986
1075
|
cpp_type = self._series_type_for(name)
|
|
987
|
-
lines.append(f" Series<{cpp_type}> {safe};")
|
|
1076
|
+
lines.append(f" Series<{cpp_type}> {safe}{_mbb};")
|
|
988
1077
|
|
|
989
1078
|
# 7. Fixnan members
|
|
990
1079
|
for site in self.ctx.fixnan_sites:
|
|
@@ -997,9 +1086,9 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
|
|
|
997
1086
|
# Determine type: int for count vars, double for float vars
|
|
998
1087
|
if member in ("closedtrades", "opentrades", "wintrades", "losstrades",
|
|
999
1088
|
"eventrades"):
|
|
1000
|
-
lines.append(f" Series<int> {svar};")
|
|
1089
|
+
lines.append(f" Series<int> {svar}{_mbb};")
|
|
1001
1090
|
else:
|
|
1002
|
-
lines.append(f" Series<double> {svar};")
|
|
1091
|
+
lines.append(f" Series<double> {svar}{_mbb};")
|
|
1003
1092
|
|
|
1004
1093
|
# 8b. Global-scope non-var declarations as class members
|
|
1005
1094
|
# (so user-defined functions can reference them)
|
|
@@ -1051,7 +1140,7 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
|
|
|
1051
1140
|
if self._safe_name(vname) == orig_safe:
|
|
1052
1141
|
cpp_type = PINE_TYPE_TO_CPP.get(ptype, "double")
|
|
1053
1142
|
if vname in self.ctx.series_vars:
|
|
1054
|
-
lines.append(f" Series<{cpp_type}> {cloned_safe};")
|
|
1143
|
+
lines.append(f" Series<{cpp_type}> {cloned_safe}{_mbb};")
|
|
1055
1144
|
elif vname in self._matrix_specs:
|
|
1056
1145
|
lines.append(f" {self._type_spec_to_cpp(self._matrix_specs[vname])} {cloned_safe};")
|
|
1057
1146
|
elif vname in self._array_vars:
|
|
@@ -1066,7 +1155,7 @@ class CodeGen(CallVisitor, ExprVisitor, StmtVisitor, TopLevelEmitter, SecurityEm
|
|
|
1066
1155
|
# Non-var series var
|
|
1067
1156
|
if orig_safe in [self._safe_name(n) for n in self.ctx.series_vars]:
|
|
1068
1157
|
cpp_type = self._series_type_for(orig_safe)
|
|
1069
|
-
lines.append(f" Series<{cpp_type}> {cloned_safe};")
|
|
1158
|
+
lines.append(f" Series<{cpp_type}> {cloned_safe}{_mbb};")
|
|
1070
1159
|
else:
|
|
1071
1160
|
lines.append(f" double {cloned_safe} = 0.0;")
|
|
1072
1161
|
|
|
@@ -444,6 +444,11 @@ class CallVisitor:
|
|
|
444
444
|
return "0"
|
|
445
445
|
if func_name in SKIP_FUNC_NAMES and namespace is None:
|
|
446
446
|
return "0"
|
|
447
|
+
# max_bars_back(var, num): a history-depth DIRECTIVE, not a value.
|
|
448
|
+
# Its effect is captured in CodeGen._compute_max_bars_back_cap (which
|
|
449
|
+
# sizes every Series<T> ring buffer), so the call itself emits nothing.
|
|
450
|
+
if func_name == "max_bars_back" and namespace is None:
|
|
451
|
+
return "0"
|
|
447
452
|
|
|
448
453
|
# request.* calls
|
|
449
454
|
if namespace == "request":
|
|
@@ -10,9 +10,14 @@ Buckets:
|
|
|
10
10
|
* HARD_REJECT_FUNC / HARD_REJECT_NAMESPACE - calls that have no PineForge
|
|
11
11
|
semantics at all (e.g. ``request.financial``, ``ticker.*``).
|
|
12
12
|
* DIVERGENT_VARS - built-in variables whose PineForge value diverges from
|
|
13
|
-
TradingView (e.g. ``bar_index`` depends on
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
TradingView. Most are reported as WARNING (e.g. ``bar_index`` depends on the
|
|
14
|
+
data window, ``timenow`` is not wall-clock) — these often appear in visual or
|
|
15
|
+
logging code that does not affect trade outcomes. A subset
|
|
16
|
+
(DIVERGENT_VARS_ERROR: ``last_bar_index`` aliased to the *current* bar index,
|
|
17
|
+
``time_close`` aliased to the bar *open* timestamp) are silent MIS-ALIASES:
|
|
18
|
+
they produce a plausible-looking but wrong value that flows straight into
|
|
19
|
+
trade logic, so a backtest would be silently wrong. Those are escalated to
|
|
20
|
+
ERROR (rejected) rather than merely warned.
|
|
16
21
|
* NOT_YET - calls the runtime could support but the transpiler does not yet
|
|
17
22
|
emit (e.g. ``max_bars_back``, bare ``barssince``).
|
|
18
23
|
* request.security - only ``symbol`` / ``timeframe`` / ``expression`` allowed,
|
|
@@ -131,16 +136,25 @@ HARD_REJECT_NAMESPACE: dict[str, str] = {
|
|
|
131
136
|
}
|
|
132
137
|
|
|
133
138
|
# Built-in variables whose PineForge value diverges from TradingView semantics.
|
|
134
|
-
#
|
|
135
|
-
# logging or visual logic that does not affect trade outcomes. The checker
|
|
136
|
-
#
|
|
139
|
+
# Most are reported as WARNING — many real strategies use bar_index / timenow in
|
|
140
|
+
# logging or visual logic that does not affect trade outcomes. The checker still
|
|
141
|
+
# flags divergence so users see the risk.
|
|
142
|
+
#
|
|
143
|
+
# DIVERGENT_VARS_ERROR is a SUBSET that is escalated to ERROR (rejected): these
|
|
144
|
+
# are silent MIS-ALIASES, not merely data-window divergences. They return a
|
|
145
|
+
# plausible value that is the WRONG quantity (last_bar_index -> current bar
|
|
146
|
+
# index; time_close -> bar OPEN timestamp) and that value flows directly into
|
|
147
|
+
# trade logic, so the backtest would be silently wrong. A WARNING is not enough.
|
|
137
148
|
DIVERGENT_VARS: dict[str, str] = {
|
|
138
149
|
"bar_index": "bar_index depends on the data window; PineForge and TradingView produce different values for the same script.",
|
|
139
|
-
"last_bar_index": "last_bar_index is
|
|
150
|
+
"last_bar_index": "last_bar_index is aliased to the CURRENT bar index in PineForge codegen (not the index of the last bar); backtest would be silently wrong — rejected.",
|
|
140
151
|
"timenow": "timenow is aliased to the current bar timestamp in PineForge; it is not real wall-clock time.",
|
|
141
|
-
"time_close": "time_close is aliased to the bar
|
|
152
|
+
"time_close": "time_close is aliased to the bar OPEN timestamp in PineForge; it does not represent the bar close time; backtest would be silently wrong — rejected.",
|
|
142
153
|
}
|
|
143
154
|
|
|
155
|
+
# Subset of DIVERGENT_VARS escalated from WARNING to ERROR (see comment above).
|
|
156
|
+
DIVERGENT_VARS_ERROR: frozenset[str] = frozenset({"last_bar_index", "time_close"})
|
|
157
|
+
|
|
144
158
|
BARSTATE_APPROX_VARS: dict[str, str] = {
|
|
145
159
|
"barstate.islast": "barstate.islast is always false in PineForge batch backtests.",
|
|
146
160
|
"barstate.ishistory": "barstate.ishistory is always true in PineForge batch backtests.",
|
|
@@ -191,8 +205,12 @@ STRATEGY_EXIT_PRICE_PARAMS: frozenset[str] = frozenset({
|
|
|
191
205
|
})
|
|
192
206
|
|
|
193
207
|
# Implementable but currently silent in codegen -> reject loudly.
|
|
208
|
+
#
|
|
209
|
+
# max_bars_back was here ("silently dropped") but is now WIRED: codegen sizes
|
|
210
|
+
# every Series<T> ring buffer to the requested depth via the engine's
|
|
211
|
+
# ``Series<T>(int max_len)`` ctor (include/pineforge/series.hpp). It is no
|
|
212
|
+
# longer rejected — see CodeGen._compute_max_bars_back_cap.
|
|
194
213
|
NOT_YET_FUNC: dict[str, str] = {
|
|
195
|
-
"max_bars_back": "max_bars_back is silently dropped by the codegen.",
|
|
196
214
|
"timeframe.from_seconds": "timeframe.from_seconds is not yet implemented; codegen would emit 'false' and silently produce wrong TF strings.",
|
|
197
215
|
}
|
|
198
216
|
|
|
@@ -386,6 +404,12 @@ class SupportChecker:
|
|
|
386
404
|
# request.security (barmerge.* gaps/lookahead values). While > 0 the
|
|
387
405
|
# UNSUPPORTED_CONST_NAMESPACES rejection is suppressed.
|
|
388
406
|
self._const_arg_ctx_depth: int = 0
|
|
407
|
+
# id()s of Identifier/MemberAccess nodes that are the *callee* of a
|
|
408
|
+
# FuncCall. A divergent built-in NAME used as a call target (e.g. the
|
|
409
|
+
# session-aware ``time_close("D")`` function, which is distinct from the
|
|
410
|
+
# bare ``time_close`` variable) must NOT be flagged as a divergent
|
|
411
|
+
# variable read. Populated as _visit_FuncCall descends into children.
|
|
412
|
+
self._callee_node_ids: set[int] = set()
|
|
389
413
|
|
|
390
414
|
# -- Public API --
|
|
391
415
|
|
|
@@ -629,6 +653,12 @@ class SupportChecker:
|
|
|
629
653
|
def _visit_FuncCall(self, node: FuncCall) -> None:
|
|
630
654
|
ns, name = _qualified_name(node.callee)
|
|
631
655
|
|
|
656
|
+
# Mark the callee so the generic child-walk does not treat a divergent
|
|
657
|
+
# built-in *function* name (e.g. ``time_close("D")``) as a divergent
|
|
658
|
+
# *variable* read. The call's own semantics are validated here.
|
|
659
|
+
if node.callee is not None:
|
|
660
|
+
self._callee_node_ids.add(id(node.callee))
|
|
661
|
+
|
|
632
662
|
if ns is None and name is None:
|
|
633
663
|
self._visit_children(node)
|
|
634
664
|
return
|
|
@@ -918,8 +948,9 @@ class SupportChecker:
|
|
|
918
948
|
"code into the strategy script).",
|
|
919
949
|
)
|
|
920
950
|
return
|
|
921
|
-
if node.name in DIVERGENT_VARS:
|
|
922
|
-
self._warn
|
|
951
|
+
if node.name in DIVERGENT_VARS and id(node) not in self._callee_node_ids:
|
|
952
|
+
emit = self._err if node.name in DIVERGENT_VARS_ERROR else self._warn
|
|
953
|
+
emit(
|
|
923
954
|
node,
|
|
924
955
|
f"{node.name} diverges from TradingView semantics in PineForge.",
|
|
925
956
|
hint=DIVERGENT_VARS[node.name],
|
|
@@ -945,8 +976,13 @@ class SupportChecker:
|
|
|
945
976
|
|
|
946
977
|
def _visit_MemberAccess(self, node: MemberAccess) -> None:
|
|
947
978
|
chain = _resolve_member_chain(node)
|
|
948
|
-
if
|
|
949
|
-
|
|
979
|
+
if (
|
|
980
|
+
chain is not None
|
|
981
|
+
and chain in DIVERGENT_VARS
|
|
982
|
+
and id(node) not in self._callee_node_ids
|
|
983
|
+
):
|
|
984
|
+
emit = self._err if chain in DIVERGENT_VARS_ERROR else self._warn
|
|
985
|
+
emit(
|
|
950
986
|
node,
|
|
951
987
|
f"{chain} diverges from TradingView semantics in PineForge.",
|
|
952
988
|
hint=DIVERGENT_VARS[chain],
|
|
@@ -1004,14 +1040,21 @@ class SupportChecker:
|
|
|
1004
1040
|
if isinstance(node.object, Identifier) and node.object.name == "syminfo":
|
|
1005
1041
|
if node.member not in SUPPORTED_SYMINFO:
|
|
1006
1042
|
self._err(node, f"syminfo.{node.member} is not implemented in PineForge runtime.")
|
|
1007
|
-
elif
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1043
|
+
elif node.member in self._SYMINFO_SILENT_GAP_FIELDS:
|
|
1044
|
+
# These fields silently return na in current PineForge. Warn on
|
|
1045
|
+
# EVERY read — not just inside an if/ternary condition — because
|
|
1046
|
+
# a field used directly in a plain expression (e.g. ``x =
|
|
1047
|
+
# syminfo.pricescale * 2``) slips out as na with no signal too.
|
|
1048
|
+
# The conditional phrasing is kept where it applies.
|
|
1049
|
+
extra = (
|
|
1050
|
+
" condition will always be false."
|
|
1051
|
+
if self._in_conditional_depth > 0
|
|
1052
|
+
else " any expression using it will be na."
|
|
1053
|
+
)
|
|
1011
1054
|
self._warn(
|
|
1012
1055
|
node,
|
|
1013
|
-
f"syminfo.{node.member} returns na in current PineForge;
|
|
1014
|
-
"
|
|
1056
|
+
f"syminfo.{node.member} returns na in current PineForge;"
|
|
1057
|
+
f"{extra} "
|
|
1015
1058
|
"Will be backfilled by pineforge-data product.",
|
|
1016
1059
|
)
|
|
1017
1060
|
self._visit_children(node)
|
|
Binary file
|
package/release.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
-
"codegen": "0.7.
|
|
2
|
+
"codegen": "0.7.3",
|
|
3
3
|
"pyodide": "314.0.0",
|
|
4
4
|
"python": "3.14.0",
|
|
5
5
|
"emscripten": "emscripten_5_0_3",
|
|
6
|
-
"sha256": "
|
|
6
|
+
"sha256": "e23536bd4e2bbc4f3974459dd53e0adc72f5d62c78c587ab3536da18d31d581f"
|
|
7
7
|
}
|
package/tables.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"CODEGEN_VERSION": "0.7.
|
|
2
|
+
"CODEGEN_VERSION": "0.7.3",
|
|
3
3
|
"TA_CLASS_MAP_KEYS": [
|
|
4
4
|
"sma",
|
|
5
5
|
"ema",
|
|
@@ -1449,14 +1449,13 @@
|
|
|
1449
1449
|
},
|
|
1450
1450
|
"HARD_REJECT_NAMESPACE": {},
|
|
1451
1451
|
"NOT_YET_FUNC": {
|
|
1452
|
-
"max_bars_back": "max_bars_back is silently dropped by the codegen.",
|
|
1453
1452
|
"timeframe.from_seconds": "timeframe.from_seconds is not yet implemented; codegen would emit 'false' and silently produce wrong TF strings."
|
|
1454
1453
|
},
|
|
1455
1454
|
"DIVERGENT_VARS": {
|
|
1456
1455
|
"bar_index": "bar_index depends on the data window; PineForge and TradingView produce different values for the same script.",
|
|
1457
|
-
"last_bar_index": "last_bar_index is
|
|
1456
|
+
"last_bar_index": "last_bar_index is aliased to the CURRENT bar index in PineForge codegen (not the index of the last bar); backtest would be silently wrong — rejected.",
|
|
1458
1457
|
"timenow": "timenow is aliased to the current bar timestamp in PineForge; it is not real wall-clock time.",
|
|
1459
|
-
"time_close": "time_close is aliased to the bar
|
|
1458
|
+
"time_close": "time_close is aliased to the bar OPEN timestamp in PineForge; it does not represent the bar close time; backtest would be silently wrong — rejected."
|
|
1460
1459
|
},
|
|
1461
1460
|
"BARSTATE_APPROX_VARS": {
|
|
1462
1461
|
"barstate.islast": "barstate.islast is always false in PineForge batch backtests.",
|
package/transpile.worker.mjs
CHANGED
|
@@ -47,6 +47,14 @@ def transpile_json(source: str) -> str:
|
|
|
47
47
|
const post = (m) => self.postMessage(m);
|
|
48
48
|
let transpileJson = null;
|
|
49
49
|
|
|
50
|
+
// Hex-encode the SHA-256 of an ArrayBuffer using the worker's WebCrypto.
|
|
51
|
+
async function sha256Hex(buf) {
|
|
52
|
+
const digest = await crypto.subtle.digest("SHA-256", buf);
|
|
53
|
+
return Array.from(new Uint8Array(digest))
|
|
54
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
55
|
+
.join("");
|
|
56
|
+
}
|
|
57
|
+
|
|
50
58
|
async function init() {
|
|
51
59
|
try {
|
|
52
60
|
const pyodide = await loadPyodide({ indexURL: "/pyodide/" });
|
|
@@ -56,6 +64,19 @@ async function init() {
|
|
|
56
64
|
const archiveRes = await fetch(`/pyodide/${manifest.archive}`);
|
|
57
65
|
if (!archiveRes.ok) throw new Error(`fetch /pyodide/${manifest.archive}: ${archiveRes.status}`);
|
|
58
66
|
const buf = await archiveRes.arrayBuffer();
|
|
67
|
+
// Defensive integrity check: verify the archive bytes against the manifest's
|
|
68
|
+
// sha256 BEFORE unpacking/running. Verify-if-present — older manifests that
|
|
69
|
+
// predate the sha256 field are accepted unchanged (forward/backward compat).
|
|
70
|
+
if (manifest.sha256) {
|
|
71
|
+
const actual = await sha256Hex(buf);
|
|
72
|
+
if (actual !== manifest.sha256) {
|
|
73
|
+
post({
|
|
74
|
+
type: "init-error",
|
|
75
|
+
error: `codegen archive sha256 mismatch — expected ${manifest.sha256} got ${actual}`,
|
|
76
|
+
});
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
59
80
|
pyodide.unpackArchive(buf, "gztar", { extractDir: "/codegen" });
|
|
60
81
|
pyodide.runPython(GLUE);
|
|
61
82
|
const fn = pyodide.globals.get("transpile_json");
|
|
Binary file
|