Skip to content

fix(web): derive expiry-day markers from trades.expiry, not hardcoded Tue/Thu#7

Merged
SwathiMystery merged 1 commit intomainfrom
fix/expiry-from-data
Apr 21, 2026
Merged

fix(web): derive expiry-day markers from trades.expiry, not hardcoded Tue/Thu#7
SwathiMystery merged 1 commit intomainfrom
fix/expiry-from-data

Conversation

@SwathiMystery
Copy link
Copy Markdown
Contributor

What

Calendar expiry marker was hardcoded to Tue/Thu. Now reads the actual trades.expiry values from the user's own DB.

Why

  • Hardcoded Tue/Thu captured BSE Sensex (Tue) + NSE Nifty (Thu) weekly expiries in mid-2024. SEBI has since shuffled weekly expiry days more than once; any hardcoded day goes stale within 12 months.
  • It marked every Tue/Thu even on days the user didn't hold a derivative contract — pure noise.
  • The DB already has the truth: every derivative execution carries drvExpiryDate, normalised into trades.expiry.

Changes

  • khata/web/queries.py::expiry_days_in_range(user, start, end_excl) → frozenset[date] — distinct expiry dates from the user's trades inside a half-open window.
  • khata/web/main.py — calendar view passes per-month set; day view membership-tests its single date.
  • khata/web/helpers.py::is_expiry_day — kept as a thin compatibility shim; returns False unless an explicit set is passed.
  • khata/web/templates/calendar.html — uses d in expiry_days directly; legend updated to "expiry of a contract you traded".

Verification

Against a live account:

Month Marked days (derived) Matches SELECT DISTINCT expiry FROM trades
April 2026 07, 13, 21
March 2026 24, 30
February 2026 none ✅ (no trades)

Checklist

  • uv run ruff check khata tests clean
  • uv run ruff format khata tests applied
  • Tests 34/34 green
  • No schema change

… Tue/Thu

The previous check marked every Tuesday (BSE) and Thursday (NSE) regardless
of whether anything actually expired on that day for this user — noisy, and
wrong the moment SEBI shifts weekly expiry days again (it already has, more
than once). The DB knows the real answer: every execution row carries the
derivative expiry from the broker.

- New query: expiry_days_in_range(user, start, end_excl) → frozenset[date]
- Calendar view passes a per-month expiry set to the template
- Day view computes is_expiry as membership in a single-day range
- helpers.is_expiry_day kept as a thin compatibility shim that returns False
  unless an explicit set is passed (no more global Tue/Thu opinion)
- Legend copy: 'weekly expiry (Tue/Thu)' → 'expiry of a contract you traded'
@SwathiMystery SwathiMystery merged commit 6b5f2af into main Apr 21, 2026
7 checks passed
@SwathiMystery SwathiMystery deleted the fix/expiry-from-data branch April 21, 2026 03:49
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