Skip to content

Fall back to old bhavcopy format (with ISIN) when udiff unavailable - #135

Merged
sevakram merged 1 commit into
masterfrom
fix/bhavcopy-fallback-old-format-isin
Aug 25, 2026
Merged

Fall back to old bhavcopy format (with ISIN) when udiff unavailable#135
sevakram merged 1 commit into
masterfrom
fix/bhavcopy-fallback-old-format-isin

Conversation

@sevakram

Copy link
Copy Markdown
Contributor

Summary

  • When UDiff bhavcopy is not available, bhavcopy_raw now falls back to the old bhavcopy format (legacy cm{dd}{MMM}{yyyy}bhav.csv.zip, which includes the ISIN field) instead of the full bhavcopy (sec_bhavdata_full, which has no ISIN).
  • Added NSEArchives.bhavcopy_old_raw() (also exported as module-level bhavcopy_old_raw) which fetches the file through the NSE reports API:
https://www.nseindia.com/api/reports?archives=[{"name":"CM - Bhavcopy(csv)","type":"daily-reports","category":"capital-market","section":"equities"}]&date=01-Jan-2020&type=equities&mode=single
  • The method bootstraps cookies via a homepage hit on HTTP 403 and raises requests.RequestException when NSE returns 404 (file unavailable, e.g. UDiff-era dates).

Fallback order now

  1. UDiFF historical archive (dates >= Jul 8, 2024)
  2. Daily-reports API (current/previous trading day)
  3. Old-format CM bhavcopy with ISIN via reports API (previously: full bhavcopy without ISIN)

Tests

  • Updated test_bhavcopy (live): historical date 2020-01-01 must return the old format with ISIN header and must NOT contain DELIV_QTY columns.
  • Added live tests: direct bhavcopy_old_raw download incl. ISIN value check; 404 for post-Jul-2024 dates.
  • Added mocked offline tests (run in CI): fallback goes to old format and never to full_bhavcopy_raw; pre-UDiff dates skip udiff entirely; correct reports-API URL/params + unzip; 404 raises.
  • Marked all network-dependent tests in tests/test_bhav.py as @pytest.mark.live per repo convention, keeping CI green without network.

Misc

  • Bumped version to 0.35.5 (+ CHANGELOG entry)
  • requirements.dev.txt: pinned pytest>=8.0 (pytest 7.1.2 is incompatible with Python 3.14 — ast.Str removed)

Verified live: full suite passes (73 passed), and bhavcopy_save(date(2020,1,1)) saves cm01Jan2020bhav.csv with ISIN column populated.

Supersedes #134 (re-created under correct account with re-authored commit).

bhavcopy_raw previously fell back to the full bhavcopy (sec_bhavdata_full,
no ISIN column) when UDiff sources failed. Now it falls back to the legacy
cm{dd}{MMM}{yyyy}bhav.csv.zip file, fetched through the NSE reports API:

  /api/reports?archives=[{"name": "CM - Bhavcopy(csv)", "type":
  "daily-reports", "category": "capital-market", "section": "equities"}]
  &date={DD-MMM-YYYY}&type=equities&mode=single

- Add NSEArchives.bhavcopy_old_raw (exported as module-level bhavcopy_old_raw)
- Handles 403 cookie bootstrap and raises for missing files (404)
- Update test_bhavcopy to expect old format with ISIN for historical dates
- Mark network tests in tests/test_bhav.py as live; add mocked offline
  fallback tests for CI
- Bump version to 0.35.5
@sevakram
sevakram merged commit 7ae415e into master Aug 25, 2026
4 checks passed
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