Skip to content

Commit 6aa1d13

Browse files
runtime: wire syminfo.pointvalue through all money paths; hold ScopedTimezone lock for full scope
- O2: pointvalue now multiplies open_profit, percent/cash sizing, percent commission, margin admission, and trade accessors (default 1.0 — crypto corpus bit-identical; pv=50 scaling test added) - O6: ScopedTimezone held its mutex only inside the ctor; now RAII-held for the full scope (concurrency regression test added) - docs: refresh stale coverage.md claims (19 C-ABI symbols, int64/source getters, VWAP bands, intraday-cap key, risk gates, TA file layout); mark input.source/input.color resolved in audit master - corpus: bump submodule to regenerated generated.cpp (parity unchanged: 245 excellent + 1 known anomaly) Co-authored-by: Cursor <[email protected]>
1 parent 8efe77f commit 6aa1d13

14 files changed

Lines changed: 458 additions & 77 deletions

‎corpus‎

Submodule corpus updated 257 files

‎docs/coverage.md‎

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

‎docs/pine_v6_audit_master.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Items resolved: #7, #8, #9, #10, #33, #36, #37, F5, F22; #29 verified false-posi
3030

3131
- **#28 `input.time`** — RESOLVED. Engine [pineforge-engine#22](https://github.com/pineforge-4pass/pineforge-engine/pull/22) added `get_input_int64`; codegen [#15](https://github.com/pineforge-4pass/pineforge-codegen/pull/15) routes `input.time` to it (Pine v6 returns `series int` Unix-ms; old `get_input_int` int32 overflowed).
3232
- **Drawing-var dangling-identifier minor** — RESOLVED. codegen [#16](https://github.com/pineforge-4pass/pineforge-codegen/pull/16) tracks omitted drawing-typed UDT fields (`_udt_omitted_fields`) and rewrites reads to `/* drawing field omitted */ 0` / strips writes (closes codegen issue #10).
33-
- **#26 / #27 `input.color` / `input.source`** — remaining codegen-helper work tracked by [pineforge-engine#23](https://github.com/pineforge-4pass/pineforge-engine/issues/23) (`get_input_source` + `get_input_color` engine helpers) and codegen issue #9.
33+
- **#26 / #27 `input.color` / `input.source`** — RESOLVED (verified against pineforge-codegen source 2026-06-10, commit `64fc886` "input.source series binding, packed-color defval"). Engine ships `get_input_source(title, default_series) → const Series<double>&` and codegen emits `get_input_source("title", _src_<field>_)[0]` for `input.source`; `input.color` routes to `get_input_int64` with a packed-ARGB defval, so the `get_input_color` engine helper contemplated by [pineforge-engine#23](https://github.com/pineforge-4pass/pineforge-engine/issues/23) was deemed unnecessary.
3434

3535
## Critical issues — consolidated by class
3636

@@ -92,8 +92,8 @@ Identifier resolves to a different runtime symbol than spec.
9292

9393
| # | Identifier | Doc claim | Reality |
9494
|---|---|---|---|
95-
| 26 | `input.color()` | `get_input_string()` + color parse | **[PARTIAL RESOLVED 2026-05-29, PR codegen#13]** Analyzer now requires defval to be `color.<const>` / `color.new(...)` / `color.rgb(...)`; rejects arbitrary expressions. Codegen still emits `get_input_int` (engine has no `get_input_color` helper — full fix is cross-repo follow-up). |
96-
| 27 | `input.source()` | series source | **[BLOCKED]** Codegen still emits `get_input_double` → frozen scalar (verified Phase C investigation: `current_bar_.close` captured once at call site). Fix requires new `get_input_source(name, default_series) → const Series<double>&` helper in pineforge-engine; tracked as cross-repo follow-up on codegen issue #9. |
95+
| 26 | `input.color()` | `get_input_string()` + color parse | **[RESOLVED — verified vs codegen source 2026-06-10]** Analyzer requires defval to be `color.<const>` / `color.new(...)` / `color.rgb(...)` (PR codegen#13); codegen now emits `get_input_int64` with a packed-ARGB defval (`0xAARRGGBB` overflows int32, hence int64). A dedicated engine `get_input_color` helper (engine issue #23) was deemed unnecessary. |
96+
| 27 | `input.source()` | series source | **[RESOLVED — verified vs codegen source 2026-06-10]** Engine ships `get_input_source(name, default_series) → const Series<double>&`; codegen emits `get_input_source("title", _src_<field>_)[0]` against the engine's always-materialized native source series (no more frozen scalar). Remaining engine issue #23 scope (`get_input_color`) deemed unnecessary — see #26. |
9797
| 28 | `input.time()` | `get_input_double()` | **[RESOLVED 2026-05-29, PR engine#22 + codegen#15]** Routes to `get_input_int64` (Pine v6 `input.time` returns `series int` Unix-ms; int32 `get_input_int` overflowed). |
9898
| 29 | `color.rgb()` | optional `transp` arg | **[RESOLVED — false-positive in original audit]** Verified `visit_call.py:1095-1098` correctly passes `args[3]` as transparency in the 4-arg form; 3-arg fallback defaults to 0. Original audit claim was wrong. |
9999

@@ -194,7 +194,7 @@ Phase 2 — 2 clarification agents (F1–F22):
194194

195195
**Remaining open work:**
196196

197-
1. **`input.source` / `input.color` series-/color-aware emission** (#26, #27, codegen issue #9): cross-repo blocker — needs `get_input_source(name, default_series) → const Series<double>&` and `get_input_color` helpers in pineforge-engine. Tracked by [pineforge-engine#23](https://github.com/pineforge-4pass/pineforge-engine/issues/23). `input.source` currently produces a frozen scalar.
197+
1. ~~**`input.source` / `input.color` series-/color-aware emission** (#26, #27, codegen issue #9)~~ — RESOLVED (verified vs codegen source 2026-06-10): engine `get_input_source` landed and codegen binds `input.source` to the native source series; `input.color` uses packed `get_input_int64` (the `get_input_color` helper from [pineforge-engine#23](https://github.com/pineforge-4pass/pineforge-engine/issues/23) deemed unnecessary).
198198
2. **Remaining class A items** — #5 (`extend.*, font.*, hline.*, ...` namespaces) and #6 (`color.from_gradient`) still emit `std::string("<member>")` or hardcoded `0`. Same rejection pattern applies.
199199
3. **Class H #35** — `barmerge.*` / `alert.freq_*` as free expressions still emit `std::string(...)` typed as INT. Same fix pattern as #36/#37.
200200
4. **Doc** — refresh `pine_v6_coverage_detail.md` bucket assignments now that rejections moved several identifiers from "✅ Runtime" or "❓ Unknown" to "❌ Unsupported (loud reject)".

‎include/pineforge/engine.hpp‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -580,10 +580,15 @@ class BacktestEngine {
580580
}
581581

582582
// --- Commission helper ---
583+
// PERCENT commission is a % of the order's notional value in account
584+
// currency, which for futures includes the instrument point value
585+
// (price points × $/point × contracts). pointvalue=1 (crypto/equity
586+
// default) leaves the legacy formula bit-identical. Cash-per-order /
587+
// cash-per-contract are already absolute currency amounts.
583588
double calc_commission(double fill_price, double qty) const {
584589
switch (commission_type_) {
585590
case CommissionType::PERCENT:
586-
return fill_price * qty * (commission_value_ / 100.0);
591+
return fill_price * qty * syminfo_.pointvalue * (commission_value_ / 100.0);
587592
case CommissionType::CASH_PER_ORDER:
588593
return commission_value_;
589594
case CommissionType::CASH_PER_CONTRACT:
@@ -593,6 +598,10 @@ class BacktestEngine {
593598
}
594599

595600
// --- Position sizing helper ---
601+
// PERCENT_OF_EQUITY / CASH convert a currency budget into contracts, so
602+
// one contract's currency exposure is fill_price × pointvalue (futures
603+
// $-per-point multiplier; 1.0 for crypto/equity keeps the legacy math
604+
// bit-identical).
596605
double calc_qty(double fill_price) const {
597606
switch (default_qty_type_) {
598607
case QtyType::FIXED:
@@ -602,10 +611,12 @@ class BacktestEngine {
602611
double cash = equity * (default_qty_value_ / 100.0);
603612
// Reject (qty 0) on a non-finite / non-positive fill price — a
604613
// degenerate $0/NaN print must NOT size as the raw % number.
605-
return (std::isfinite(fill_price) && fill_price > 0) ? (cash / fill_price) : 0.0;
614+
return (std::isfinite(fill_price) && fill_price > 0)
615+
? (cash / (fill_price * syminfo_.pointvalue)) : 0.0;
606616
}
607617
case QtyType::CASH:
608-
return (std::isfinite(fill_price) && fill_price > 0) ? (default_qty_value_ / fill_price) : 0.0;
618+
return (std::isfinite(fill_price) && fill_price > 0)
619+
? (default_qty_value_ / (fill_price * syminfo_.pointvalue)) : 0.0;
609620
}
610621
return default_qty_value_;
611622
}
@@ -669,15 +680,18 @@ class BacktestEngine {
669680
double margin_liquidation_price() const { return na<double>(); }
670681
double open_trades_capital_held() const {
671682
if (position_side_ == PositionSide::FLAT) return 0.0;
672-
return std::abs(position_qty_ * position_entry_price_);
683+
return std::abs(position_qty_ * position_entry_price_) * syminfo_.pointvalue;
673684
}
674685

686+
// Mark-to-market open profit in account currency. The point-value
687+
// multiplier keeps this consistent with realized PnL (emit_close_trade)
688+
// so equity = capital + net_profit + open_profit stays in one unit.
675689
double open_profit(double current_price) const {
676690
if (position_side_ == PositionSide::FLAT) return 0.0;
677691
double diff = (position_side_ == PositionSide::LONG)
678692
? (current_price - position_entry_price_)
679693
: (position_entry_price_ - current_price);
680-
return diff * position_qty_;
694+
return diff * position_qty_ * syminfo_.pointvalue;
681695
}
682696

683697
int count_wintrades() const { return win_trades_count_; }
@@ -968,10 +982,14 @@ class BacktestEngine {
968982
if (idx < 0 || idx >= (int)trades_.size()) return 0.0;
969983
return trades_[idx].max_runup;
970984
}
985+
// Percent excursions: trade.max_runup / max_drawdown are stored in
986+
// account currency (× pointvalue, see emit_close_trade), so the entry
987+
// cost denominator must be in currency too (entry × qty × pointvalue).
988+
// pointvalue=1 cancels out and matches the legacy ratio bit-for-bit.
971989
double closed_trade_max_runup_percent(int idx) const {
972990
if (idx < 0 || idx >= (int)trades_.size()) return 0.0;
973991
const Trade& t = trades_[idx];
974-
double cost = t.entry_price * t.qty;
992+
double cost = t.entry_price * t.qty * syminfo_.pointvalue;
975993
return (cost > 0.0) ? (t.max_runup / cost) * 100.0 : 0.0;
976994
}
977995
double closed_trade_max_drawdown(int idx) const {
@@ -981,7 +999,7 @@ class BacktestEngine {
981999
double closed_trade_max_drawdown_percent(int idx) const {
9821000
if (idx < 0 || idx >= (int)trades_.size()) return 0.0;
9831001
const Trade& t = trades_[idx];
984-
double cost = t.entry_price * t.qty;
1002+
double cost = t.entry_price * t.qty * syminfo_.pointvalue;
9851003
return (cost > 0.0) ? (t.max_drawdown / cost) * 100.0 : 0.0;
9861004
}
9871005

@@ -1315,7 +1333,9 @@ class BacktestEngine {
13151333
// Runtime syminfo injection (by design — the engine stores no instrument
13161334
// metadata of its own; the harness supplies it per run). mintick drives the
13171335
// directional fill snap + slippage*tick economics; pointvalue is the
1318-
// futures $-per-point multiplier applied to realized PnL and excursions.
1336+
// futures $-per-point multiplier applied to every money path (realized
1337+
// PnL + excursions, open profit / equity, percent/cash sizing, percent
1338+
// commission notionals, margin check — see tests/test_pointvalue.cpp).
13191339
// Both default to crypto/equity values (0.01 / 1.0) and only matter when the
13201340
// harness sets a non-default instrument.
13211341
void set_syminfo_mintick(double m) { if (m > 0.0) { syminfo_.mintick = m; syminfo_mintick_ = m; } }

‎include/pineforge/pineforge.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,11 @@ PF_API void strategy_set_syminfo_session(pf_strategy_t s, const char* session);
365365
PF_API void strategy_set_syminfo_mintick(pf_strategy_t s, double mintick);
366366

367367
/** Set the instrument point value (``syminfo.pointvalue``, default 1.0) — the
368-
* $-per-point-per-contract multiplier applied to realized PnL and MFE/MAE. Set
369-
* per-instrument (e.g. 50 for ES). Non-positive ignored. Call before
370-
* #run_backtest*. */
368+
* $-per-point-per-contract multiplier applied to every money path: realized
369+
* PnL and MFE/MAE, open profit / mark-to-market equity (and the drawdown /
370+
* runup extremes), percent-of-equity and cash position sizing, percent
371+
* commission notionals, and the margin admission check. Set per-instrument
372+
* (e.g. 50 for ES). Non-positive ignored. Call before #run_backtest*. */
371373
PF_API void strategy_set_syminfo_pointvalue(pf_strategy_t s, double pointvalue);
372374

373375
/** Inject a fundamental/exchange metadata value by Pine member name

‎scripts/run_corpus.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ if [[ "${SKIP_BUILD:-0}" != "1" ]]; then
6161
-DPINEFORGE_BUILD_CORPUS_STRATEGIES=ON \
6262
-Wno-dev
6363

64-
log "building runtime + 162 strategy targets ($JOBS jobs)"
64+
log "building runtime + 246 strategy targets ($JOBS jobs)"
6565
cmake --build "$BUILD_DIR" --target corpus_strategies -j "$JOBS"
6666
fi
6767

‎src/engine_orders.cpp‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,15 @@ double BacktestEngine::calc_qty_for_type(double fill_price, double qty_value, in
2828
double cash = equity * (qty_value / 100.0);
2929
// Reject (qty 0) on a non-finite / non-positive fill price — a degenerate
3030
// $0/NaN print must NOT size as the raw % number (silent wrong-qty bug).
31-
return (std::isfinite(fill_price) && fill_price > 0.0) ? (cash / fill_price) : 0.0;
31+
// One contract's currency exposure is fill_price × pointvalue (1.0 for
32+
// crypto/equity — legacy math unchanged; futures divide the budget by
33+
// the full per-contract notional).
34+
return (std::isfinite(fill_price) && fill_price > 0.0)
35+
? (cash / (fill_price * syminfo_.pointvalue)) : 0.0;
3236
}
3337
if (qty_type == static_cast<int>(QtyType::CASH)) {
34-
return (std::isfinite(fill_price) && fill_price > 0.0) ? (qty_value / fill_price) : 0.0;
38+
return (std::isfinite(fill_price) && fill_price > 0.0)
39+
? (qty_value / (fill_price * syminfo_.pointvalue)) : 0.0;
3540
}
3641
return qty_value;
3742
}
@@ -283,9 +288,9 @@ void BacktestEngine::emit_close_trade(const PyramidEntry& pe, double close_qty,
283288
// Realized PnL scales by the instrument point value ($ per point per
284289
// contract). Crypto/equity (pointvalue=1) is unchanged; futures (e.g. ES=50)
285290
// multiply the price-difference PnL. pnl_pct is a price-return % and is
286-
// point-value-invariant. Commission is in account currency (cash-per-* is
287-
// already absolute; percent-commission notional×pointvalue is a follow-up
288-
// and does not affect cash-per-contract instruments like ES).
291+
// point-value-invariant. Commission is in account currency: cash-per-* is
292+
// already absolute, and percent commission scales the notional by
293+
// pointvalue inside calc_commission.
289294
const double pv = syminfo_.pointvalue;
290295
double pnl = (was_long ? (fill_price - pe.price) : (pe.price - fill_price))
291296
* close_qty * pv;

‎src/engine_strategy_commands.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ void BacktestEngine::strategy_entry(const std::string& id, bool is_long,
116116
if (!std::isnan(qty) && std::isnan(limit_price) && std::isnan(stop_price)) {
117117
double margin_pct = is_long ? margin_long_ : margin_short_;
118118
if (margin_pct > 0.0 && !std::isnan(current_bar_.close)) {
119+
// Position value in account currency includes the futures
120+
// point-value multiplier (1.0 for crypto/equity — unchanged).
119121
double required_margin = std::abs(qty) * current_bar_.close
122+
* syminfo_.pointvalue
120123
* (margin_pct / 100.0);
121124
double available_equity = current_equity();
122125
double epsilon = std::max(1e-9, std::abs(available_equity) * 1e-12);

‎src/engine_trade_accessors.cpp‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ double BacktestEngine::open_trade_profit(int idx) const {
2323
const PyramidEntry& pe = pyramid_entries_[(size_t)idx];
2424
bool is_long = (position_side_ == PositionSide::LONG);
2525
double px = current_bar_.close;
26-
double pnl = is_long ? (px - pe.price) * pe.qty : (pe.price - px) * pe.qty;
26+
// Currency PnL: price-points × qty × pointvalue, consistent with the
27+
// realized-trade path in emit_close_trade (pointvalue=1 is unchanged).
28+
double pnl = (is_long ? (px - pe.price) * pe.qty : (pe.price - px) * pe.qty)
29+
* syminfo_.pointvalue;
2730
pnl -= calc_commission(pe.price, pe.qty);
2831
return pnl;
2932
}
@@ -82,10 +85,15 @@ double BacktestEngine::open_trade_size(int idx) const {
8285
return pyramid_entries_[(size_t)idx].qty;
8386
}
8487

88+
// pe.max_drawdown / pe.max_runup are tracked in price-points × qty
89+
// (update_per_trade_extremes); the currency accessors scale by pointvalue
90+
// so they match the closed-trade fields emitted by emit_close_trade. The
91+
// percent accessors below intentionally use the UNSCALED excursion over the
92+
// unscaled entry cost — pointvalue cancels in the ratio.
8593
double BacktestEngine::open_trade_max_drawdown(int idx) const {
8694
if (position_side_ == PositionSide::FLAT || idx < 0 || idx >= (int)pyramid_entries_.size())
8795
return 0.0;
88-
return pyramid_entries_[(size_t)idx].max_drawdown;
96+
return pyramid_entries_[(size_t)idx].max_drawdown * syminfo_.pointvalue;
8997
}
9098

9199
double BacktestEngine::open_trade_max_drawdown_percent(int idx) const {
@@ -99,7 +107,7 @@ double BacktestEngine::open_trade_max_drawdown_percent(int idx) const {
99107
double BacktestEngine::open_trade_max_runup(int idx) const {
100108
if (position_side_ == PositionSide::FLAT || idx < 0 || idx >= (int)pyramid_entries_.size())
101109
return 0.0;
102-
return pyramid_entries_[(size_t)idx].max_runup;
110+
return pyramid_entries_[(size_t)idx].max_runup * syminfo_.pointvalue;
103111
}
104112

105113
double BacktestEngine::open_trade_max_runup_percent(int idx) const {

‎src/timezone.cpp‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,38 @@ std::mutex& timezone_mutex() {
1111
return mutex;
1212
}
1313

14+
// Last TZ written into the process environment. Guarded by timezone_mutex();
15+
// lets a same-TZ scope skip the setenv/tzset syscall pair (lazy caching).
1416
std::string g_active_tz = "NOT_SET";
1517

1618
} // namespace
1719

20+
// Acquire the process-global TZ mutex and keep it locked for the lifetime
21+
// of this object. The caller's localtime_r / mktime decomposition runs
22+
// inside the scope, so concurrent harness threads on different chart
23+
// timezones cannot corrupt each other's wall time — this is the guarantee
24+
// documented on strategy_set_chart_timezone in pineforge.h.
25+
//
26+
// The lazy-TZ cache is preserved: when the requested zone is already the
27+
// active one we skip the setenv/tzset pair entirely, but the lock is held
28+
// either way so the environment stays stable for the whole scope.
1829
ScopedTimezone::ScopedTimezone(const std::string& tz)
19-
: lock_(timezone_mutex(), std::defer_lock) {
30+
: lock_(timezone_mutex()) {
2031

2132
std::string target = (tz.empty() || tz == "UTC" || tz == "Etc/UTC") ? "UTC" : tz;
2233

23-
lock_.lock();
2434
if (g_active_tz == target) {
25-
lock_.unlock();
26-
had_old_ = false;
27-
return;
28-
}
29-
30-
const char* old = std::getenv("TZ");
31-
if (old != nullptr) {
32-
old_tz_ = old;
33-
had_old_ = true;
34-
} else {
35-
had_old_ = false;
35+
return; // lock stays held until destruction
3636
}
3737

3838
::setenv("TZ", target.c_str(), 1);
3939
::tzset();
4040
g_active_tz = target;
41-
lock_.unlock();
4241
}
4342

4443
ScopedTimezone::~ScopedTimezone() {
45-
// Destructor does nothing to allow lazy caching across calls!
44+
// lock_ releases the mutex here. TZ is intentionally NOT restored —
45+
// g_active_tz caches it so the next same-zone scope skips setenv/tzset.
4646
}
4747

4848
} // namespace pine_tz

0 commit comments

Comments
 (0)