psx-dps GitHub

psx-dps

Pakistan Stock Exchange market data in Python β€” no API key, no dependencies, no scraping headaches.

Live quotes, KSE-100 constituents, 5 years of end-of-day history, intraday ticks,
corporate announcements and sector data from the official PSX data portal.

πŸ“– Documentation site

tests Python 3.8+ Zero dependencies MIT licence No API key required


pip install git+https://github.com/umairsandhu/psx-dps
from psx_dps import Client

with Client() as psx:
    psx.quote("MARI")                      # live quote: OHLC, volume, order book
    psx.eod("LUCK", since="2026-01-01")    # daily bars β€” 5 years in one request
    psx.index_constituents("KSE100")       # all 100, with index weights
    psx.snapshot()                         # the whole market, one request
psx-dps quote MARI OGDC HBL
psx-dps market --index KSE100 --sort change --limit 10
psx-dps eod LUCK --since 2026-01-01 --csv > luck.csv

That's the whole setup. No account, no token, no requests, no pandas. Full method reference: docs/API.md.


Why another PSX library?

Because the hard part isn't calling the endpoints β€” it's that calling them naively fails most of the time, in a way that looks permanent.

dps.psx.com.pk is served by several nodes, and some of them return HTTP 404 for every data route while serving the ordinary website with 200. The zone publishes a single rotating A record, so DNS will hand you a broken node and you'll keep landing on it for the TTL.

52.128.23.6    /data/symbol-position β†’ 404      /company/MARI β†’ 200
52.128.23.16   /data/symbol-position β†’ 200      /company/MARI β†’ 200

Measured live, with dig, 8.8.8.8 and 1.1.1.1 all returning only the broken address. A plain requests.get("https://dps.psx.com.pk/market-watch") is a coin flip, and when it loses it looks exactly like "PSX removed the API".

It didn't. psx-dps keeps a pool of candidate nodes, probes for one that genuinely serves data, pins it, and re-probes instead of believing a 404 β€” while still validating TLS against the real hostname.

The full investigation, including the dead ends, is in DISCOVERY.md.

What you get

Live quotes OHLC, LDCP, volume and full order book for ~496 trading symbols
Whole market in 1 request market_watch() / snapshot() β€” never loop over symbols
End-of-day history ~5 years of daily bars per symbol, one request
Intraday ticks Today's full tick series
Indices KSE-100, KSE-30, KMI-30, ALLSHR and more β€” levels and constituents with weights
Sector data Advance/decline, turnover and market cap for all 38 sectors
Announcements 224k+ corporate and regulator filings, each with its PDF URL
Historical tables Every symbol on any trading day, or one symbol's month with high/low
Market breadth Advancers, decliners, unchanged

Built to be left running

Most of this library is the boring part that scrapers skip β€” because it's reading a free public service that PSX runs for investors, not an API we pay for.

πŸ” Node failover Survives the 404 problem above, automatically
πŸ’Ύ Shared cache Ten projects on one machine cost PSX one request
πŸ•°οΈ Market-aware TTLs 60s while trading; cached until the next open once shut β€” off-hours polling is free
🚦 Cross-process rate limit 1 req/s enforced machine-wide via a lock file, not per-process
πŸ›‘ Backs off when pushed A 429 stops instantly and starts a shared, escalating cooldown. No retry storms.
πŸ”Œ Zero dependencies Python 3.8+ and the standard library
🩺 Self-diagnosis psx-dps diagnose --deep names what broke and fixes what it can
βœ… 80 offline tests The suite never touches PSX

5-minute polling of the entire market costs ~432 requests/day β€” about one request every 50 seconds during the session, less than a single person with the portal open on auto-refresh. The arithmetic.

Command line

psx-dps quote MARI OGDC          # live quotes
psx-dps market --index KSE100    # the whole board, filtered
psx-dps eod LUCK --since 2026-01-01 --csv
psx-dps announcements --symbol ENGROH
psx-dps movers                   # volume leaders + breadth
psx-dps diagnose --deep          # what's broken, and fix it
psx-dps nodes                    # which PSX address works, and since when

Every command takes --json or --csv, so it pipes into anything.

Documentation

Guide What's in it
API.md Full Python reference β€” every method, option and exception
ENDPOINTS.md Every PSX endpoint, payload shapes and quirks
SYMBOLS.md What a ticker tells you β€” maturity dates, rights issues, silent renames
DISCOVERY.md How the endpoints were reverse-engineered, dead ends included
FAIR-USE.md Polling budgets and keeping the footprint small
STAYING-UNBLOCKED.md What happens when PSX pushes back
TROUBLESHOOTING.md How PSX has broken before, and the runbook
EVALUATION-PROMPT.md Already have a PSX integration? Audit it against this one

FAQ

Does the Pakistan Stock Exchange have an official API? No. PSX publishes no public API and no documentation for one. The data portal at `dps.psx.com.pk` has undocumented JSON endpoints that its own front end uses, which is what this library reads. They can change without notice β€” see [ENDPOINTS.md](docs/ENDPOINTS.md).
Why do I get 404 from dps.psx.com.pk when the website works? You're almost certainly on a node that doesn't serve the data routes. It's not you and the API wasn't removed β€” see [Why another PSX library?](#why-another-psx-library) above. `psx-dps diagnose` confirms it in seconds.
Why did I get a 403? Am I blocked? No. There's **no WAF** on this service. A 403 on a data route means you're on a node that doesn't serve them β€” the same condition that gives 404 without an `X-Requested-With` header. Don't respond by rotating User-Agents or proxies; that's the one thing likely to get you *genuinely* blocked. [Details](docs/TROUBLESHOOTING.md#it-is-not-a-waf-and-you-are-not-blocked).
Do I need an API key or account? No. Every endpoint this library uses is unauthenticated.
How do I get KSE-100 index data?
psx.indices()                        # levels for KSE100, KSE30, KMI30, ALLSHR…
psx.index_constituents("KSE100")     # all 100 members with index weights
How far back does historical data go? About five years of daily bars per symbol via `eod()`, in a single request. `history_by_date()` gives every symbol on a given trading day, and `history_by_month()` adds high/low.
Can I use this for a real-time tracker or dashboard? Yes β€” it's designed for it. Poll `snapshot()` on a schedule and store the result; that's one request for all ~496 symbols and gives you your own intraday series. Read [FAIR-USE.md](docs/FAIR-USE.md) first for the budgets and the one pattern that would turn it abusive.
Is scraping PSX data legal? The data is published free on a public website with no login, no paywall and no click-through terms. `dps.psx.com.pk` serves no `robots.txt`; `www.psx.com.pk`'s is permissive with no crawl delay. So nothing is being circumvented. That is **not** a licence to redistribute β€” market data is PSX's to license, and reselling or republishing a live feed is a conversation to have with them. Not legal advice.
Why does it import as psx_dps and not psx? The top-level `psx` name already belongs to the unrelated `psx-data-reader` package on PyPI. Colliding with it would break anyone using both.
A symbol disappeared. Where did it go? Either it isn't trading today (the market watch carries ~496 of ~1,029 listed instruments), or a corporate action renamed it β€” `ENGRO` became `ENGROH` in 2024, silently. `psx.search("engro")` finds the successor. [SYMBOLS.md](docs/SYMBOLS.md) covers both.

Contributing

Issues and PRs welcome β€” particularly new PSX node addresses if you find one (SEED_NODES in src/psx_dps/transport.py), and parser fixes when PSX changes a payload.

git clone https://github.com/umairsandhu/psx-dps && cd psx-dps
pip install -e . pytest && python -m pytest     # 80 tests, all offline

Disclaimer

Unofficial and unaffiliated. Not endorsed by the Pakistan Stock Exchange. The endpoints are undocumented and can change without notice β€” don't put this on a critical path without a fallback. Nothing here is investment advice, and there's no warranty of accuracy or timeliness. MIT licensed.


Keywords: Pakistan Stock Exchange API Β· PSX API Python Β· KSE-100 data Β· Karachi Stock Exchange Β· PSX market data Β· dps.psx.com.pk Β· Pakistan stock market data Β· PSX historical data Β· KSE-100 constituents Β· PSX scraper Β· Pakistani equities