Skip to content

Commit 23cc8c5

Browse files
luisleo526claude
andcommitted
fix: session.ismarket holds on every bar of a daily-or-higher chart
TradingView's rule for the session.is* market-state variables on a chart bar: on "1D" and above session.ismarket is true on every bar and session.ispremarket / session.ispostmarket are false — a daily bar covers whole session days, not a time of day. Intraday charts keep the time-of-day test byte for byte. On such a chart the bar is its whole session, so session.isfirstbar and session.islastbar both equal session.ismarket there. Evidence: roi10x-shiva-lt-ls-blend on OANDA:XAUUSD @1d. The symbol's session is 1800-1700 America/New_York and its daily bars are stamped at the 17:00 ET break (minute 1020, outside the wrapped window [1080, 1020)), so the time-of-day test never held; every signal is ANDed with session.ismarket, and the engine took 0 trades against TradingView's 57. - session_time: chart-timeframe forms of pine_session_ismarket / ispremarket / ispostmarket (fourth argument chart_tf) that apply the daily rule and otherwise defer to the three-argument forms; tf_is_daily_or_higher in timeframe.hpp. - engine: BacktestEngine members of the same name wrap them with the script timeframe. Codegen lowers the variables to the unqualified call pine_session_is*(syminfo_.session, syminfo_.timezone, current_bar_.timestamp) inside the generated class, so class-scope lookup resolves to these members and the emitted code is unchanged. The streaming clock's tick-less-interval skip keeps the raw time-of-day form, qualified explicitly. - the bar pumps (simple, aggregation, magnifier, stream) set the per-bar predicates through set_session_bar_state, which applies the daily-chart first/last rule. - tests/test_session_predicates_daily_chart pins the XAUUSD tape on a 1D feed (cloud-caller shape, explicit "1D", single-timeframe run), the same session on a 15m feed unchanged, 0930-1600 on a 1D feed, a daily chart aggregated from 60m, the stream's realtime daily bar, and the chart-timeframe forms directly. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01TMHfywLQuXUYNxD2ZbWABd
1 parent 11e61d4 commit 23cc8c5

9 files changed

Lines changed: 538 additions & 37 deletions

‎docs/pine_v6_coverage_detail.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,13 @@
160160

161161
| Identifier | Kind | Status | Backing | Notes |
162162
|---|---|---|---|---|
163-
| `session.isfirstbar` | var | ✅ Runtime | `session_isfirstbar_` on engine; per-bar lookahead in `engine_run.cpp` | Sprint A |
163+
| `session.isfirstbar` | var | ✅ Runtime | `session_isfirstbar_` on engine; per-bar lookahead in `engine_run.cpp`; on a 1D+ chart every bar (the bar is its whole session) | Sprint A |
164164
| `session.isfirstbar_regular` | var | ✅ Runtime | Aliased to `session.isfirstbar` — engine has single session string, cannot distinguish RTH vs ETH (documented limitation) | Sprint A |
165-
| `session.islastbar` | var | ✅ Runtime | `session_islastbar_` on engine; per-bar lookahead | Sprint A |
165+
| `session.islastbar` | var | ✅ Runtime | `session_islastbar_` on engine; per-bar lookahead; on a 1D+ chart every bar (the bar is its whole session) | Sprint A |
166166
| `session.islastbar_regular` | var | ✅ Runtime | Aliased to `session.islastbar` | Sprint A |
167-
| `session.ismarket` | var | ✅ Runtime | `pine_session_ismarket(session, tz, bar_ms)` in `session_time.hpp` | Sprint A |
168-
| `session.ispostmarket` | var | ✅ Runtime | `pine_session_ispostmarket(...)` — standard ETH window `RTH_close-2000` local | Sprint A |
169-
| `session.ispremarket` | var | ✅ Runtime | `pine_session_ispremarket(...)` — standard ETH window `0400-RTH_open` local | Sprint A |
167+
| `session.ismarket` | var | ✅ Runtime | `pine_session_ismarket(session, tz, bar_ms, chart_tf)` in `session_time.hpp` (via `BacktestEngine::pine_session_ismarket`); always true on a 1D+ chart, as TradingView documents | Sprint A |
168+
| `session.ispostmarket` | var | ✅ Runtime | `pine_session_ispostmarket(...)` — standard ETH window `RTH_close-2000` local; always false on a 1D+ chart | Sprint A |
169+
| `session.ispremarket` | var | ✅ Runtime | `pine_session_ispremarket(...)` — standard ETH window `0400-RTH_open` local; always false on a 1D+ chart | Sprint A |
170170

171171
### Variables — strategy
172172

‎include/pineforge/engine.hpp‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2152,11 +2152,52 @@ class BacktestEngine {
21522152
// and session.islastbar (prev_in_session_ && !in_session).
21532153
bool prev_in_session_ = false;
21542154
// Current-bar session predicates — recomputed at the start of each bar
2155-
// by update_session_state() in engine_run.cpp.
2155+
// by set_session_bar_state() (engine_run.cpp) on every bar pump.
21562156
bool session_ismarket_ = false;
21572157
bool session_isfirstbar_ = false;
21582158
bool session_islastbar_ = false;
21592159

2160+
// session.ismarket of the CHART bar stamped bar_ms on the symbol's
2161+
// session clock — the chart-timeframe-aware rule of session_time.hpp:
2162+
// every bar of a daily-or-higher chart is the regular-session bar,
2163+
// intraday bars keep the time-of-day test.
2164+
bool chart_bar_ismarket(int64_t bar_ms) const;
2165+
// Set the three per-bar predicates for the chart bar being dispatched.
2166+
// in_session is chart_bar_ismarket(that bar); intraday_islastbar is the
2167+
// pump's own lookahead verdict for an intraday chart (peek at the next
2168+
// bar, barstate.islast, or never in magnifier mode). On a D/W/M chart
2169+
// the bar IS the whole session — its own first and last bar — so
2170+
// session.isfirstbar and session.islastbar both equal in_session there
2171+
// and the prev/next bookkeeping does not apply.
2172+
void set_session_bar_state(bool in_session, bool intraday_islastbar);
2173+
2174+
// The session.is* market-state variables as the generated strategy
2175+
// evaluates them. Codegen lowers session.ismarket / ispremarket /
2176+
// ispostmarket to the unqualified call
2177+
// pine_session_is*(syminfo_.session, syminfo_.timezone, current_bar_.timestamp)
2178+
// inside the generated class, and class-scope lookup resolves it to these
2179+
// members (a member hides the namespace-scope function of the same name
2180+
// and suppresses ADL), so the chart timeframe joins the predicate with
2181+
// the emitted code unchanged. TradingView: on "1D" and above
2182+
// session.ismarket is always true, ispremarket / ispostmarket always
2183+
// false — OANDA:XAUUSD @1D (1800-1700, bars stamped 17:00 ET) took 0
2184+
// trades against TradingView's 57 while the time-of-day test decided.
2185+
// Every BacktestEngine member resolves here too; the one caller that
2186+
// wants the raw time-of-day test (the streaming clock's closed-interval
2187+
// skip) qualifies pineforge:: explicitly.
2188+
bool pine_session_ismarket(const std::string& session,
2189+
const std::string& tz, int64_t bar_ms) const {
2190+
return pineforge::pine_session_ismarket(session, tz, bar_ms, script_tf_);
2191+
}
2192+
bool pine_session_ispremarket(const std::string& session,
2193+
const std::string& tz, int64_t bar_ms) const {
2194+
return pineforge::pine_session_ispremarket(session, tz, bar_ms, script_tf_);
2195+
}
2196+
bool pine_session_ispostmarket(const std::string& session,
2197+
const std::string& tz, int64_t bar_ms) const {
2198+
return pineforge::pine_session_ispostmarket(session, tz, bar_ms, script_tf_);
2199+
}
2200+
21602201
// --- Timeframe state ---
21612202
std::string input_tf_;
21622203
std::string script_tf_;

‎include/pineforge/session_time.hpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,30 @@ bool pine_session_ispostmarket(const std::string& session,
163163
const std::string& tz,
164164
int64_t bar_ms);
165165

166+
// Chart-timeframe forms: the rule the session.is* variables of a CHART bar
167+
// follow. TradingView evaluates them on the chart bar, and a daily-or-higher
168+
// bar covers whole session days rather than a time of day, so on "1D" and
169+
// above it documents session.ismarket as always true and session.ispremarket
170+
// / session.ispostmarket as always false (Pine docs, Sessions). OANDA:XAUUSD
171+
// @1D made the gap physical: an 1800-1700 America/New_York session and a tape
172+
// stamped at the 17:00 ET break put every daily open OUTSIDE the time-of-day
173+
// windows above, session.ismarket never held, and a script that ANDs every
174+
// signal with it took 0 trades against TradingView's 57. With an intraday or
175+
// empty chart_tf these forms defer to the three-argument time-of-day forms
176+
// above, byte for byte.
177+
bool pine_session_ismarket(const std::string& session,
178+
const std::string& tz,
179+
int64_t bar_ms,
180+
const std::string& chart_tf);
181+
182+
bool pine_session_ispremarket(const std::string& session,
183+
const std::string& tz,
184+
int64_t bar_ms,
185+
const std::string& chart_tf);
186+
187+
bool pine_session_ispostmarket(const std::string& session,
188+
const std::string& tz,
189+
int64_t bar_ms,
190+
const std::string& chart_tf);
191+
166192
} // namespace pineforge

‎include/pineforge/timeframe.hpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ enum class CalendarPeriod { NONE, DAY, WEEK, MONTH };
8282
/// Determine the calendar period for a target TF string.
8383
CalendarPeriod calendar_period_for(const std::string& tf);
8484

85+
/// True for a daily-or-higher chart timeframe ("D", "1D", "2D", "W", "M"):
86+
/// a bar that covers whole session days rather than a time of day (the
87+
/// aggregation path's CALENDAR classification). Intraday timeframes and an
88+
/// empty (undetected) one are false.
89+
inline bool tf_is_daily_or_higher(const std::string& tf) {
90+
return calendar_period_for(tf) != CalendarPeriod::NONE;
91+
}
92+
8593
/// Check if two timestamps (Unix milliseconds) fall in different calendar periods.
8694
bool crosses_boundary(int64_t prev_ms, int64_t curr_ms, CalendarPeriod period);
8795

‎src/engine_run.cpp‎

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,28 @@ void BacktestEngine::clear_historical_security_lookahead_projections() {
15351535
}
15361536

15371537

1538+
bool BacktestEngine::chart_bar_ismarket(int64_t bar_ms) const {
1539+
return pineforge::pine_session_ismarket(syminfo_.session, syminfo_.timezone,
1540+
bar_ms, script_tf_);
1541+
}
1542+
1543+
void BacktestEngine::set_session_bar_state(bool in_session,
1544+
bool intraday_islastbar) {
1545+
session_ismarket_ = in_session;
1546+
if (tf_is_daily_or_higher(script_tf_)) {
1547+
// A daily-or-higher chart bar covers its whole session day(s): it is
1548+
// the session's first bar and its last bar at once (TradingView's
1549+
// "first / last bar of the day's session", read on a bar that IS the
1550+
// day), so both predicates hold on every bar of such a chart.
1551+
session_isfirstbar_ = in_session;
1552+
session_islastbar_ = in_session;
1553+
return;
1554+
}
1555+
session_isfirstbar_ = in_session && !prev_in_session_;
1556+
session_islastbar_ = intraday_islastbar;
1557+
}
1558+
1559+
15381560
// Bar pump for the no-aggregation, no-magnifier case: every input bar is a
15391561
// script bar, fed straight to on_bar with the standard
15401562
// pending-orders / per-trade-extremes / on_bar / equity-update sequence
@@ -1571,20 +1593,16 @@ void BacktestEngine::run_simple_bar_loop(const Bar* input_bars, int n_input) {
15711593
}
15721594

15731595
// Update session predicates for session.ismarket / isfirstbar / islastbar.
1574-
session_ismarket_ = pine_session_ismarket(syminfo_.session, syminfo_.timezone,
1575-
current_bar_.timestamp);
1576-
session_isfirstbar_ = session_ismarket_ && !prev_in_session_;
1577-
// islastbar: fire when this bar is in-session but the NEXT bar won't be
1578-
// (lookahead: peek at next bar's timestamp if available, else fire on last bar).
1579-
if (session_ismarket_) {
1596+
// Intraday islastbar: fire when this bar is in-session but the NEXT bar
1597+
// won't be (lookahead: peek at the next bar's timestamp if available,
1598+
// else fire on the last bar).
1599+
{
1600+
const bool in_session = chart_bar_ismarket(current_bar_.timestamp);
15801601
bool next_in_session = false;
1581-
if (i + 1 < n_input) {
1582-
next_in_session = pine_session_ismarket(syminfo_.session, syminfo_.timezone,
1583-
input_bars[i + 1].timestamp);
1602+
if (in_session && i + 1 < n_input) {
1603+
next_in_session = chart_bar_ismarket(input_bars[i + 1].timestamp);
15841604
}
1585-
session_islastbar_ = !next_in_session;
1586-
} else {
1587-
session_islastbar_ = false;
1605+
set_session_bar_state(in_session, in_session && !next_in_session);
15881606
}
15891607

15901608
dispatch_bar();
@@ -1673,10 +1691,10 @@ void BacktestEngine::run_aggregation_bar_loop(const Bar* input_bars, int n_input
16731691
if (bar_magnifier && !group_sub_bars.empty()) {
16741692
// Magnifier mode: update session state using script-bar timestamp
16751693
// (first sub-bar's timestamp represents the aggregated bar).
1676-
session_ismarket_ = pine_session_ismarket(syminfo_.session, syminfo_.timezone,
1677-
group_sub_bars.front().timestamp);
1678-
session_isfirstbar_ = session_ismarket_ && !prev_in_session_;
1679-
session_islastbar_ = false; // not deterministic in magnifier mode without lookahead
1694+
// Intraday islastbar is not deterministic here without lookahead.
1695+
set_session_bar_state(
1696+
chart_bar_ismarket(group_sub_bars.front().timestamp),
1697+
/*intraday_islastbar=*/false);
16801698
run_magnified_bar(
16811699
group_sub_bars, script_bar_ts, completed_on_boundary);
16821700
prev_in_session_ = session_ismarket_;
@@ -1697,10 +1715,10 @@ void BacktestEngine::run_aggregation_bar_loop(const Bar* input_bars, int n_input
16971715
// (09:30-anchored, not UTC-aligned) label correctly too.
16981716
current_bar_ = ab.bar;
16991717
// Update session predicates.
1700-
session_ismarket_ = pine_session_ismarket(syminfo_.session, syminfo_.timezone,
1701-
current_bar_.timestamp);
1702-
session_isfirstbar_ = session_ismarket_ && !prev_in_session_;
1703-
session_islastbar_ = session_ismarket_ && barstate_islast_;
1718+
{
1719+
const bool in_session = chart_bar_ismarket(current_bar_.timestamp);
1720+
set_session_bar_state(in_session, in_session && barstate_islast_);
1721+
}
17041722
dispatch_bar();
17051723
prev_in_session_ = session_ismarket_;
17061724
}

‎src/engine_stream.cpp‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,12 @@ bool BacktestEngine::stream_end(bool finalize_partial_input_bar) {
256256
bool BacktestEngine::stream_finalize_until(int64_t timestamp_ms) {
257257
while (timestamp_ms >= stream_next_input_open_ms_ + stream_input_tf_ms_) {
258258
const bool had_tick = stream_has_input_bar_;
259-
const bool in_session = pine_session_ismarket(
259+
// The raw time-of-day session test (namespace-scope form), not the
260+
// chart-bar rule BacktestEngine::pine_session_ismarket applies: this
261+
// decides whether a tick-less INPUT interval is a closed market that
262+
// must not become a synthetic bar, and stays byte-identical on
263+
// daily-or-higher feeds too.
264+
const bool in_session = pineforge::pine_session_ismarket(
260265
syminfo_.session, syminfo_.timezone,
261266
stream_next_input_open_ms_);
262267

@@ -374,16 +379,17 @@ void BacktestEngine::stream_dispatch_script_bar(const Bar& bar, bool had_tick) {
374379
}
375380

376381
current_bar_ = bar;
377-
session_ismarket_ = pine_session_ismarket(
378-
syminfo_.session, syminfo_.timezone, current_bar_.timestamp);
379-
session_isfirstbar_ = session_ismarket_ && !prev_in_session_;
380-
if (session_ismarket_ && script_tf_seconds_ > 0) {
381-
const int64_t next_ts = current_bar_.timestamp
382-
+ static_cast<int64_t>(script_tf_seconds_) * 1000;
383-
session_islastbar_ = !pine_session_ismarket(
384-
syminfo_.session, syminfo_.timezone, next_ts);
385-
} else {
386-
session_islastbar_ = false;
382+
{
383+
const bool in_session = chart_bar_ismarket(current_bar_.timestamp);
384+
// Intraday islastbar: the next script bar opens one bar width ahead
385+
// on the stream clock; fire when that open is out of session.
386+
bool intraday_islastbar = false;
387+
if (in_session && script_tf_seconds_ > 0) {
388+
const int64_t next_ts = current_bar_.timestamp
389+
+ static_cast<int64_t>(script_tf_seconds_) * 1000;
390+
intraday_islastbar = !chart_bar_ismarket(next_ts);
391+
}
392+
set_session_bar_state(in_session, intraday_islastbar);
387393
}
388394

389395
_push_source_series();

‎src/session_time.cpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,35 @@ bool pine_session_ispostmarket(const std::string& session,
541541
return (mod >= rth_close_min && mod < post_close_min);
542542
}
543543

544+
// Chart-timeframe forms (see session_time.hpp): a D/W/M chart bar is the
545+
// symbol's regular-session bar whatever time of day its stamp reads.
546+
bool pine_session_ismarket(const std::string& session,
547+
const std::string& tz,
548+
int64_t bar_ms,
549+
const std::string& chart_tf) {
550+
if (tf_is_daily_or_higher(chart_tf))
551+
return true;
552+
return pine_session_ismarket(session, tz, bar_ms);
553+
}
554+
555+
bool pine_session_ispremarket(const std::string& session,
556+
const std::string& tz,
557+
int64_t bar_ms,
558+
const std::string& chart_tf) {
559+
if (tf_is_daily_or_higher(chart_tf))
560+
return false;
561+
return pine_session_ispremarket(session, tz, bar_ms);
562+
}
563+
564+
bool pine_session_ispostmarket(const std::string& session,
565+
const std::string& tz,
566+
int64_t bar_ms,
567+
const std::string& chart_tf) {
568+
if (tf_is_daily_or_higher(chart_tf))
569+
return false;
570+
return pine_session_ispostmarket(session, tz, bar_ms);
571+
}
572+
544573
// ---------------------------------------------------------------------------
545574
// pine_time / pine_time_close (existing public API)
546575
// ---------------------------------------------------------------------------

‎tests/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ set(TEST_SOURCES
141141
test_exit_bracket_position_cycle_lifetime
142142
test_range_end_close
143143
test_close_id_retires_ledger
144+
test_session_predicates_daily_chart
144145
)
145146

146147
find_package(Threads REQUIRED)

0 commit comments

Comments
 (0)