iranquant¶
Async-first TSETMC + Codal market-data client for the Iranian capital market.
iranquant uses the same JSON endpoints as the TSETMC web terminal and
reconstructs Codal's embedded var datasource tables into DataFrames. It
provides a synchronous API for scripts and an async API for production
services, with Pandas or Polars output, a quant analytics engine and a market
watcher.
Capabilities¶
| Area | Description |
|---|---|
| Async and sync | One pooled httpx transport with retries, a concurrency semaphore and TLS fallbacks. The sync wrapper works inside Jupyter/Colab event loops. |
| Codal parsing | Tables are read from the raw var datasource = {...} JSON with a brace-balanced parser; no HTML selectors are involved. |
| Pydantic V2 | Wire payloads are validated by models that tolerate fields added by TSETMC. |
| Pandas and Polars | Every public function accepts engine="pandas" (default) or engine="polars" and returns that engine type. |
| Price adjustment | Backward gap-ratio adjustment (adjust="all") and ex-rights capital-increase adjustment (adjust="capital_only"). |
| Quant analytics | Buyer power, per-capita values, smart-money entry/exit, multi-timeframe Kaufman efficiency, ZigZag swing legs and a vectorized screener. |
| Monthly sales | Extracts current-month, YTD and prior-year sales from the standard تولید و فروش table and computes MoM/YoY growth. |
| CLI | iranquant search, history, screener, indices and codal --parse-latest, rendered with Rich tables. |
| Watcher | MarketWatch polling with BUY_QUEUE_LOCKED, BUY_QUEUE_BROKEN, SELL_QUEUE_LOCKED, SELL_QUEUE_BROKEN and VOLUME_SPIKE events. |
| Networking | Environment proxies are ignored by default. TLS is verified first for TSETMC and Codal; certificate failures fall back once to an unverified transport only when allow_tls_fallback=True, with a one-time warning. |
Architecture¶
flowchart LR
subgraph Client
A[AsyncIranQuant]
B[IranQuant / sync functions]
end
A --> C[AsyncHTTPClient<br/>retries · semaphore · TLS fallbacks]
B --> A
C --> D[TSETMC CDN]
C --> E[Codal]
D --> F[Pydantic V2 models]
E --> F
F --> G[market / codal parsers]
G --> H{engine}
H --> I[Pandas]
H --> J[Polars]
G --> K[Quant engine]
K --> I
K --> J
C --> L[MarketWatcher]
L --> M[MarketEvent callbacks]
I --> N[(Local Parquet cache)]
J --> N
Data conventions¶
- Units. TSETMC prices and traded values are in IRR (Rials); volume is share count. Codal financial and monthly figures are in Million IRR unless the table header states otherwise.
- Calendars. Historical and client-type frames include Gregorian
dateand Jalalijdate(YYYY-MM-DD) inAsia/Tehran. - Price adjustment. Factors compound backwards from the newest bar. Ratios are
pDrCotVal / pcforall,(pc - dps) / pcforcash, andnumberOfShareOld / numberOfShareNewforcapital. The public API exposesall,capital_onlyandnone. - Quant heuristics.
smart_money_entry,smart_money_exitandmovement_power_scoreare deterministic screening rules (vol_ratio >= 1.8,buyer_power_ratio >= 1.5,net_real_value > 0), not backtested alpha or financial advice. - Polling watcher.
MarketWatcherdiffs periodic HTTP MarketWatch snapshots; it is not a WebSocket tick feed. - TLS and terms. Verification is attempted first. Fail closed with
IranQuantConfig(allow_tls_fallback=False); environment overrides areIRANQUANT_VERIFY_SSLandIRANQUANT_ALLOW_TLS_FALLBACK. Code is MIT-licensed; market data remains subject to TSETMC and Codal terms.
Installation¶
From a checkout:
Requirements: Python 3.10+, httpx, pydantic>=2.7, tenacity,
jdatetime, pandas, polars and pyarrow.
Quick example¶
import iranquant as iq
code = iq.get_inscode("فولاد") # "46348559193224090"
daily = iq.history(code, adjust="all") # 11 clean OHLCV columns
full = iq.full_history("فولاد") # + real/legal client data
market = iq.market_watch() # one request, whole market
flow = iq.money_flow("فولاد") # smart-money columns
Use Polars anywhere with engine="polars":
daily = iq.history("فولاد", engine="polars")
print(type(daily)) # <class 'polars.dataframe.frame.DataFrame'>
Next steps¶
- Quickstart — a five-minute sync and async tutorial, including proxy and split-tunneling tips for Iran.
- Market Data — symbols, adjusted history, MarketWatch, order books, intraday bars and indices.
- Codal Reports — announcement search and table reconstruction.
- Quant Analytics — exact formulas for money flow, movement power and swing legs.
- Streaming — the
MarketWatcherevent loop. - Caching & Polars — incremental Parquet caching.
- API Reference — every public symbol.
- راهنمای فارسی — راهنمای کامل فارسی.