Skip to content

fix: catch up weekly and monthly schedules missed at a period boundary - #249

Open
Dev-next-gen wants to merge 1 commit into
OpenByteInc:mainfrom
Dev-next-gen:fix/strategy-v2-schedule-period-boundary
Open

Dev-next-gen wants to merge 1 commit into
OpenByteInc:mainfrom
Dev-next-gen:fix/strategy-v2-schedule-period-boundary

Conversation

@Dev-next-gen

Copy link
Copy Markdown
Contributor

Summary

I ran a strategy-v2 backtest on AAPL daily bars from 2026-01-20 to 2026-04-15, with run_weekly(rebalance, weekday=5) and run_monthly(month_end, monthday=31). It logged every Friday except the week of 2026-04-03 (Good Friday, no US session). The month-end callback ran only once, on 2026-03-31. January 31 and February 28 were Saturdays, so the January and February runs never happened.

The cause is in StrategyV2BacktestRunner._schedule_due: it only looks at the target day of the current week or month. If the target day has no bar, the next bar in the same period already catches it up (the existing test with monthday=15 fires on the 16th). But when the next bar falls in a new period, scheduled_at moves to that new period's target day, which is still in the future, so the missed run is dropped. The same happens to run_weekly(weekday=6) or weekday=7 on a stock market, which never runs at all. The live session calls the same helper with its own clock, so it has the same gap.

Changes

  • If the current bar is before this period's target, _schedule_due now also checks the previous week's or month's target and fires once if it falls between the previous bar and this one. That is the same catch-up rule that already applies inside a period. Daily schedules and the first bar (previous is None) behave as before.
  • The step back uses pd.DateOffset, not pd.Timedelta, so zone-aware live clocks don't slip into the week before when the gap spans a DST change. My first version used Timedelta(days=7) and missed a Saturday target across 2026-03-08 in New York; the new test covers that case.
  • Added test_scheduler_catches_up_target_day_missed_at_period_boundary: Good Friday, a month-end on a weekend, a Saturday weekly target, checks that nothing runs twice on the next bar, an intraday Monday bar before the scheduled time, and the DST case.

With the fix, the same backtest logs the weekly run on 2026-04-06 and the month-end runs on 2026-02-02, 2026-03-02 and 2026-03-31.

Test plan

  • Tested locally with docker compose up -d --build
  • Backend logs show no errors
  • Relevant pytest tests pass

The new test fails on main and passes with the change. The full backend suite passes: 2732 passed, 51 skipped. ruff check app scripts tests and scripts/backend_quality_check.py are clean. I did not run the Docker stack.

API documentation (if routes/schemas changed)

No routes or schemas changed.

  • Regenerated docs/api/openapi.yaml (cd backend_api_python && python scripts/export_openapi.py)
  • Updated docs/agent/agent-openapi.json if /api/agent/v1 routes changed
  • Breaking API changes called out below (oasdiff will fail CI otherwise)

Found by a defect-hunting pipeline I build and run (Dev-next-gen), using Claude Code with Anthropic's Claude Opus 5.

_schedule_due only looked at the target day of the current week or
month. When that day had no bar and the next bar opened a new period
(Good Friday with weekday=5, a month-end on a weekend with monthday=31,
or a Saturday target on a stock market), the run was silently dropped.
Inside a period the missed day was already caught up on the next bar;
apply the same rule to the previous period's target day.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant