fix: catch up weekly and monthly schedules missed at a period boundary - #249
Open
Dev-next-gen wants to merge 1 commit into
Open
Dev-next-gen wants to merge 1 commit into
Dev-next-gen wants to merge 1 commit into
Conversation
_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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)andrun_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 withmonthday=15fires on the 16th). But when the next bar falls in a new period,scheduled_atmoves to that new period's target day, which is still in the future, so the missed run is dropped. The same happens torun_weekly(weekday=6)orweekday=7on 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
_schedule_duenow 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.pd.DateOffset, notpd.Timedelta, so zone-aware live clocks don't slip into the week before when the gap spans a DST change. My first version usedTimedelta(days=7)and missed a Saturday target across 2026-03-08 in New York; the new test covers that case.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
docker compose up -d --buildThe new test fails on
mainand passes with the change. The full backend suite passes: 2732 passed, 51 skipped.ruff check app scripts testsandscripts/backend_quality_check.pyare clean. I did not run the Docker stack.API documentation (if routes/schemas changed)
No routes or schemas changed.
docs/api/openapi.yaml(cd backend_api_python && python scripts/export_openapi.py)docs/agent/agent-openapi.jsonif/api/agent/v1routes changedFound by a defect-hunting pipeline I build and run (Dev-next-gen), using Claude Code with Anthropic's Claude Opus 5.