Skip to content

Commit 88cd0c8

Browse files
luisleo526claude
andcommitted
fix: anchor OANDA's 1800-1700 intraday grid at the 17:00 ET day stamp
TradingView aligns the intraday request.security grid ("45", "240") at the symbol's DAY STAMP -- the instant its daily bar starts -- and on OANDA that instant is the 17:00 ET forex roll for every instrument. EURUSD (1700-1700) opens on it, so its grid was already right; XAUUSD (1800-1700) trades from one hour after it, and the engine anchored its grid at the 18:00 ET session open (intraday_clock_ms subtracting session_open_offset_minutes). Pinned with `lab tv` on OANDA:XAUUSD 15, 2025-04-01..04-15 (pin-xau-grid-240 / -45 / -D: entry on ta.change(time("<tf>")), close next bar, so every entry stamp is a boundary + 15 min): "240": boundaries 01:00Z / 05:00Z / 09:00Z / 13:00Z / 17:00Z and the day's first traded bar 22:00Z -- a 4h grid anchored at 21:00Z = 17:00 EDT, whose 17:00-21:00 ET bucket holds only the 18:00-20:45 trading; "45": boundaries 22:30Z / 23:15Z / 00:00Z / 00:45Z ... -- 21:00Z + k*45m (an 18:00 ET anchor puts them at 22:45Z / 23:30Z / 00:15Z); "D": the daily bucket starts at the 17:00 ET stamp; the day's first bar is 22:00Z. Family evidence: all 7 TV base entries of every 3commas-* XAUUSD DCA probe (request.security(syminfo.tickerid, "240", ta.rsi(...)) gate) sit on the 17:00-ET 4h grid (21/01/05/09/13/17 ET) and all 7 engine ones on an 18:00-ET grid; the 3 matched legs agree to 1e-6, so the DCA math is exact once the grid agrees. The rule lives in one place: session_day_stamp_offset_minutes (src/timeframe.cpp) returns the session open for every session except the 1800-1700 window, whose stamp is its 17:00 close -- the same hour engine_aux_security.cpp already routes OANDA's native daily bars by. intraday_clock_ms subtracts the stamp instead of the open, so the request.security ratio buckets, bucket_open_ms / bar_label_ms (the grid label, finding 473), tf_change and the range-start bucket cut all move together. The session-day ordinal runs on the same clock; since no 1800-1700 bar trades in the 17:00-18:00 hour the two clocks attribute differently, every D/W/M anchor of that session is unchanged: the D bar opens 18:00 ET and closes 17:00 ET, the week opens Sunday 18:00 ET, and OANDA's 17:00-ET-stamped daily bars still route to the session they cover (session_covered_instant_ms now rolls a pre-open stamp forward to its session's open, keeping its documented contract bit-for-bit). One consequence is a fix of its own: the "D" bucket on 1800-1700 now completes on the session's last chart bar (16:45 ET, whose close is the stamp) as it does on 1700-1700, instead of on the next session's first bar. Sessions whose open is the stamp keep their grid bit-for-bit: OANDA 1700-1700 (now the very grid XAUUSD uses), CME 1700-1600 CT, NYSE 0930-1600, NSE 0915-1530, 24x7 UTC. tests/test_oanda_day_stamp_grid pins the XAUUSD "240" / "45" grids in EDT and EST, the unchanged session-day model and D completion, and those regression grids; compiled against the previous library its XAUUSD checks fail and every regression pin passes. Not touched, reported as follow-ups: time("<intraday tf>") (session_time.cpp compute_tf_open_ms) still keys intraday tfs on the UTC epoch grid for every symbol -- the pin strategies would expose that separately -- and time("D") on 1800-1700 still reports the 18:00 ET session open. ctest 153/153 (152 + the new binary, 683 checks); scripts unittest 102 OK; check_c_abi_runtime.py clean. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_014vizwdJMsDkHsjmS4StvZY Claude-Session: https://claude.ai/code/session_01BvPydDgRoDYDWrjz5JtGE1
1 parent 11e61d4 commit 88cd0c8

4 files changed

Lines changed: 450 additions & 10 deletions

File tree

‎include/pineforge/timeframe.hpp‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ bool crosses_boundary(int64_t prev_ms, int64_t curr_ms, CalendarPeriod period);
9090
/// behavior for 24x7 symbols (the corpus regime). Session symbols
9191
/// (equities RTH, forex) anchor HTF buckets on the exchange clock instead:
9292
/// daily boundaries at symbol-local midnight, intraday buckets offset by the
93-
/// session-open minutes. With tz="UTC" and session ""/"24x7" these are
94-
/// bit-identical to the UTC forms, so existing callers are unaffected.
93+
/// symbol's day stamp -- the session open, except OANDA's 1800-1700 metals
94+
/// session whose day (and TradingView's "45"/"240" grid) rolls at the 17:00
95+
/// ET forex roll one hour before it opens (session_day_stamp_offset_minutes
96+
/// in timeframe.cpp cites the pin). With tz="UTC" and session ""/"24x7"
97+
/// these are bit-identical to the UTC forms, so existing callers are
98+
/// unaffected.
9599
bool crosses_boundary(int64_t prev_ms, int64_t curr_ms, CalendarPeriod period,
96100
const std::string& tz, const std::string& session);
97101
bool tf_change(int64_t prev_ms, int64_t curr_ms, const std::string& tf,
@@ -131,10 +135,11 @@ int64_t session_period_open_ms(int64_t ms, const std::string& tz,
131135
CalendarPeriod period);
132136

133137
/// The session instant a native CALENDAR chart stamp covers. A stamp inside
134-
/// its session-day returns unchanged. A stamp in the inter-session gap (at
135-
/// or after its session-day's close) covers the session about to open --
136-
/// OANDA stamps daily FX/metal bars at the 17:00 ET break under an
137-
/// 1800-1700 session -- and rolls forward to the next session-day's open.
138+
/// its session (open to exclusive close) returns unchanged. A stamp in the
139+
/// inter-session gap -- before its session-day's open (the 1800-1700 day
140+
/// stamp hour) or at / after its close -- covers the session about to open
141+
/// -- OANDA stamps daily FX/metal bars at the 17:00 ET break under an
142+
/// 1800-1700 session -- and rolls forward to that session's open.
138143
/// ""/24x7 sessions have no gap and always return `ms` unchanged.
139144
int64_t session_covered_instant_ms(int64_t ms, const std::string& tz,
140145
const std::string& session);

‎src/timeframe.cpp‎

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,50 @@ static int64_t calendar_clock_ms(int64_t ts, const std::string& tz) {
216216
return ts - tz_offset_ms(ts, tz);
217217
}
218218

219-
/// Intraday grid clock: exchange-tz seconds since (local-midnight + session
220-
/// open), so integer division by the bucket width reproduces TV's
221-
/// session-open-anchored grouping (09:30/13:30 on equities).
219+
static int session_length_minutes(const std::string& session);
220+
221+
/// Minutes from symbol-local midnight to the symbol's DAY STAMP: the instant
222+
/// its daily bar starts, which is where TradingView anchors the intraday HTF
223+
/// grid ("45", "240", ...) and where the session-day ordinal below rolls.
224+
/// For every declared session that is the session open -- 09:30 ET on NYSE
225+
/// RTH, 09:15 IST on NSE, 17:00 CT on CME 1700-1600, 17:00 ET on OANDA forex
226+
/// 1700-1700, midnight on 24x7 -- with ONE exception the engine has to know
227+
/// by its session string: OANDA's 1800-1700 metals / CFD session (XAUUSD,
228+
/// XAGUSD, ...). It trades from 18:00 ET, but OANDA aligns every
229+
/// instrument's day at the 17:00 ET forex roll -- the stamp its native daily
230+
/// bars carry (engine_aux_security.cpp routes them by the session they
231+
/// COVER for that reason) -- and TradingView inherits that alignment.
232+
/// Pinned with `lab tv` on OANDA:XAUUSD 15, 2025-04-01..04-15
233+
/// (pin-xau-grid-240 / -45: entries on ta.change(time("<tf>"))): "240"
234+
/// buckets open 21:00Z / 01:00Z / 05:00Z / 09:00Z / 13:00Z / 17:00Z -- 17:00
235+
/// EDT, the 17:00-21:00 bucket holding only the 18:00-20:45 bars -- and "45"
236+
/// buckets 21:00Z / 21:45Z / 22:30Z / 23:15Z; the 18:00 ET session-open
237+
/// anchor put them at 22:00Z / 02:00Z / ... and 22:00Z / 22:45Z / 23:30Z.
238+
/// Every 3commas-* XAUUSD DCA probe (request.security "240" RSI gate) shows
239+
/// the same 17:00-ET grid on TV against the engine's 18:00-ET one, the DCA
240+
/// legs agreeing to 1e-6 once the grid does. The session OPEN stays the
241+
/// session-day's nominal open (the D bar's label and its close arithmetic,
242+
/// session_day_open_nominal_ms); only the clock moves, and no 1800-1700 bar
243+
/// trades in the 17:00-18:00 hour the two clocks attribute differently.
244+
static int session_day_stamp_offset_minutes(const std::string& session) {
245+
const int open = session_open_offset_minutes(session);
246+
if (open == 18 * 60
247+
&& (open + session_length_minutes(session)) % 1440 == 17 * 60) {
248+
return 17 * 60;
249+
}
250+
return open;
251+
}
252+
253+
/// Intraday grid clock: exchange-tz seconds since (local-midnight + the
254+
/// symbol's day stamp), so integer division by the bucket width reproduces
255+
/// TV's day-anchored grouping (09:30/13:30 on equities, 17:00 ET on OANDA).
256+
/// The session-day ordinal (session_day_index) runs on the same clock: the
257+
/// day rolls at the stamp, and the nominal session open is added back per
258+
/// day by session_day_open_nominal_ms.
222259
static int64_t intraday_clock_ms(int64_t ts, const std::string& tz,
223260
const std::string& session) {
224261
return calendar_clock_ms(ts, tz)
225-
- static_cast<int64_t>(session_open_offset_minutes(session)) * 60000;
262+
- static_cast<int64_t>(session_day_stamp_offset_minutes(session)) * 60000;
226263
}
227264

228265
/// Minutes from symbol-local midnight to the first session window's CLOSE
@@ -420,6 +457,11 @@ static int64_t session_day_close_real_ms(int64_t d, const std::string& tz,
420457
int64_t session_covered_instant_ms(int64_t ms, const std::string& tz,
421458
const std::string& session) {
422459
const int64_t d = session_day_index(ms, tz, session);
460+
// Before the session-day's open -- the 17:00-18:00 ET hour between the
461+
// 1800-1700 day stamp and its session open, where OANDA stamps the daily
462+
// bar: the stamp covers the session about to open.
463+
const int64_t open = session_day_open_real_ms(d, tz, session);
464+
if (ms < open) return open;
423465
// Inside the session-day (before its exclusive close): covered as-is.
424466
// ""/24x7 sessions close exactly at the next open, so the gap is empty
425467
// and every timestamp takes this branch.

‎tests/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ set(TEST_SOURCES
7272
test_htf_session_close_completion
7373
test_chart_bar_bucket_label
7474
test_htf_bucket_real_end
75+
test_oanda_day_stamp_grid
7576
test_adversarial_ohlcv
7677
test_report_trace
7778
test_path_resolve_extra

0 commit comments

Comments
 (0)