Skip to content

Commit 11febbc

Browse files
luisleo526claude
andcommitted
fix: key calendar aux routing by the covered session
OANDA stamps daily FX/metal bars at the 17:00 ET break, one hour before the 1800-1700 session the bar carries. session_period_open_ms floors the stamp into the PREVIOUS session-day, so the last chart bar's content rows key beyond last_chart_key and the tail prefilter drops the whole slice: 'native chart bar has no matching auxiliary request.security bars' across every finer-tf request.security probe on the lane. Add session_covered_instant_ms — a stamp at/after its session-day close rolls forward to the next session-day open; in-session stamps (NSE Muhurat, CME open-stamped, equities, 24x7) are unchanged — and key chart route keys through it. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_011ncyC1v8C7ipSo7wikgdZU
1 parent 9e926f0 commit 11febbc

4 files changed

Lines changed: 83 additions & 3 deletions

File tree

‎include/pineforge/timeframe.hpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ int64_t session_period_open_ms(int64_t ms, const std::string& tz,
130130
const std::string& session,
131131
CalendarPeriod period);
132132

133+
/// 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+
/// ""/24x7 sessions have no gap and always return `ms` unchanged.
139+
int64_t session_covered_instant_ms(int64_t ms, const std::string& tz,
140+
const std::string& session);
141+
133142
/// Exclusive close (Unix ms) of the symbol's D/W/M bar that contains `ms`:
134143
/// DAY -> session-day open + session length (16:00 ET on equities, the next
135144
/// 17:00 ET on forex, next midnight on 24x7); WEEK / MONTH -> the open of

‎src/engine_aux_security.cpp‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,17 @@ void BacktestEngine::prepare_aux_security_chart_ranges(
111111
std::vector<int64_t> chart_route_keys;
112112
chart_route_keys.reserve(static_cast<std::size_t>(n_chart));
113113
for (int i = 0; i < n_chart; ++i) {
114+
// Key each native bar by the session it COVERS: OANDA stamps daily
115+
// bars at the 17:00 ET break, one hour before the 1800-1700 session
116+
// the bar actually carries, and the raw stamp would key one session
117+
// too early -- the tail prefilter below would then drop the last
118+
// bar's entire slice as beyond last_chart_key.
114119
const int64_t key = calendar_chart
115-
? session_period_open_ms(chart_bars[i].timestamp,
116-
syminfo_.timezone, syminfo_.session,
117-
chart_period)
120+
? session_period_open_ms(
121+
session_covered_instant_ms(chart_bars[i].timestamp,
122+
syminfo_.timezone,
123+
syminfo_.session),
124+
syminfo_.timezone, syminfo_.session, chart_period)
118125
: chart_bars[i].timestamp;
119126
if (!chart_route_keys.empty() && key <= chart_route_keys.back()) {
120127
throw std::runtime_error(

‎src/timeframe.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,17 @@ static int64_t session_day_close_real_ms(int64_t d, const std::string& tz,
417417
+ static_cast<int64_t>(session_length_minutes(session)) * 60000;
418418
}
419419

420+
int64_t session_covered_instant_ms(int64_t ms, const std::string& tz,
421+
const std::string& session) {
422+
const int64_t d = session_day_index(ms, tz, session);
423+
// Inside the session-day (before its exclusive close): covered as-is.
424+
// ""/24x7 sessions close exactly at the next open, so the gap is empty
425+
// and every timestamp takes this branch.
426+
if (ms < session_day_close_real_ms(d, tz, session)) return ms;
427+
// In the gap after the close: the stamp covers the next session.
428+
return session_day_open_real_ms(d + 1, tz, session);
429+
}
430+
420431
int64_t session_period_close_ms(int64_t ms, const std::string& tz,
421432
const std::string& session,
422433
CalendarPeriod period) {

‎tests/test_aux_security_feed.cpp‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,58 @@ void test_nq_labor_day_sessions_coalesce_into_native_interval() {
270270
}
271271

272272

273+
void test_oanda_break_stamped_daily_bars_route_by_covered_session() {
274+
// OANDA XAUUSD: session 1800-1700 ET, but the immutable daily tape stamps
275+
// every bar at 17:00 ET -- inside the inter-session break, one hour BEFORE
276+
// the session the bar covers. Keying the stamp by session-day floor maps
277+
// it to the PREVIOUS session, so the last chart bar's content rows read as
278+
// beyond last_chart_key and the tail prefilter empties the final bar.
279+
constexpr int64_t minute = 60000;
280+
constexpr int64_t stamp_a = 1704751200000; // Mon 2024-01-08 17:00 EST
281+
constexpr int64_t open_a = 1704754800000; // Mon 18:00 EST
282+
constexpr int64_t stamp_b = 1704837600000; // Tue 17:00 EST
283+
constexpr int64_t open_b = 1704841200000; // Tue 18:00 EST
284+
constexpr int64_t stamp_c = 1704924000000; // Wed 17:00 EST
285+
constexpr int64_t open_c = 1704927600000; // Wed 18:00 EST
286+
constexpr int64_t lead = 1704733200000; // Mon 12:00 EST (prior session)
287+
constexpr int64_t trail = 1705014000000; // Thu 18:00 EST (next session)
288+
const Bar chart[] = {
289+
{100.0, 160.0, 90.0, 150.0, 1000.0, stamp_a},
290+
{200.0, 260.0, 190.0, 250.0, 2000.0, stamp_b},
291+
{300.0, 360.0, 290.0, 350.0, 3000.0, stamp_c},
292+
};
293+
const Bar aux[] = {
294+
{90.0, 90.0, 90.0, 90.0, 1.0, lead},
295+
{10.0, 11.5, 9.5, 11.0, 10.0, open_a},
296+
{11.0, 12.5, 10.5, 12.0, 11.0, open_a + minute},
297+
{20.0, 21.5, 19.5, 21.0, 20.0, open_b},
298+
{21.0, 22.5, 20.5, 22.0, 21.0, open_b + minute},
299+
{30.0, 31.5, 29.5, 31.0, 30.0, open_c},
300+
{31.0, 32.5, 30.5, 32.0, 31.0, open_c + minute},
301+
{80.0, 80.0, 80.0, 80.0, 1.0, trail},
302+
};
303+
304+
SplitFeedProbe probe;
305+
strategy_set_syminfo_timezone(
306+
static_cast<pf_strategy_t>(&probe), "America/New_York");
307+
strategy_set_syminfo_session(
308+
static_cast<pf_strategy_t>(&probe), "1800-1700");
309+
assert(strategy_set_aux_security_feed(
310+
static_cast<pf_strategy_t>(&probe),
311+
reinterpret_cast<const pf_bar_t*>(aux), 8, "1") == 0);
312+
313+
probe.run(chart, 3, "1D", "1D", false, 4,
314+
MagnifierDistribution::ENDPOINTS);
315+
assert(probe.last_error().empty());
316+
assert((probe.chart_closes == std::vector<double>{150.0, 250.0, 350.0}));
317+
assert((probe.security_at_chart_close
318+
== std::vector<double>{12.0, 22.0, 32.0}));
319+
assert((probe.lower_tf_at_chart_close
320+
== std::vector<std::vector<double>>{
321+
{11.0, 12.0}, {21.0, 22.0}, {31.0, 32.0}}));
322+
}
323+
324+
273325
void test_overnight_daily_lower_tf_array_does_not_split_at_utc_midnight() {
274326
constexpr int64_t session_open = 1704232800000; // 2024-01-02 17:00 NY
275327
constexpr int64_t minute = 60000;
@@ -305,6 +357,7 @@ int main() {
305357
test_intraday_aux_label_inside_native_span_without_chart_bar_fails();
306358
test_nifty_muhurat_shifted_open_maps_by_trading_date();
307359
test_nq_labor_day_sessions_coalesce_into_native_interval();
360+
test_oanda_break_stamped_daily_bars_route_by_covered_session();
308361
test_overnight_daily_lower_tf_array_does_not_split_at_utc_midnight();
309362
return 0;
310363
}

0 commit comments

Comments
 (0)