"""Read-only Dhan market data. Credentials never leave the backend.""" from __future__ import annotations import asyncio import atexit import csv import io import json import math import os import re import sqlite3 import threading import time import uuid from contextlib import closing from datetime import datetime, timedelta, timezone import pandas as pd import requests from dhan_auth import DhanAuthError, DhanTokenManager, token_manager_from_env IST = timezone(timedelta(hours=5, minutes=30)) _READ_ONLY = {("GET", "/profile"), ("POST", "/marketfeed/quote"), ("POST", "/charts/historical"), ("POST", "/charts/intraday"), ("POST", "/optionchain"), ("POST", "/margincalculator")} _ERRORS = { "disabled": "Dhan data is disabled. Configure access in the server environment.", "not_configured": "Dhan client ID and access token are required.", "token_expired": "Dhan token is invalid or expired. Replace the server access token.", "subscription_required": "Dhan Data API access is not active. Check your account entitlement.", "access_denied": "Dhan denied data access. Check token permissions and Data API entitlement.", "rate_limited": "Dhan rate limit reached. Retrying after cooldown.", "unavailable": "Dhan is temporarily unavailable.", "invalid_response": "Dhan returned invalid market data.", "instrument_unavailable": "Dhan equity instrument mapping is unavailable for this ticker.", "read_only": "Only read-only Dhan market-data endpoints are allowed.", "stream_limit": "Dhan stream subscription limit reached.", "sdk_unavailable": "The Dhan Python SDK is unavailable. Install project dependencies.", "yahoo_fallback": "Dhan failed after two retries. Using delayed Yahoo Finance for the rest of this IST day.", "auth_setup_required": "Configure Dhan client ID, PIN, TOTP secret and token-encryption key privately once to enable automatic authentication.", "auth_rejected": "Dhan rejected automatic authentication. Check server credentials and TOTP setup.", "auth_unavailable": "Dhan automatic authentication is temporarily unavailable.", "auth_cooldown": "Dhan authentication is cooling down; no new login was attempted.", "auth_busy": "Dhan authentication is already running; using the data fallback temporarily.", "auth_storage_error": "Dhan encrypted token storage is unavailable. Check the persistent disk.", } class DhanError(ValueError): def __init__(self, code: str): self.code = code super().__init__(_ERRORS.get(code, _ERRORS["unavailable"])) def parse_instruments(text: str) -> dict[str, str]: result, ambiguous = {}, set() for raw in csv.DictReader(io.StringIO(text)): row = {(key or "").strip(): (value or "").strip() for key, value in raw.items()} exchange = row.get("SEM_EXM_EXCH_ID", row.get("EXCH_ID")) segment = row.get("SEM_SEGMENT", row.get("SEGMENT")) instrument = row.get("SEM_INSTRUMENT_NAME", row.get("INSTRUMENT")) series = row.get("SEM_SERIES", row.get("SERIES")) symbol = row.get("SEM_TRADING_SYMBOL") or row.get("UNDERLYING_SYMBOL") or row.get("SYMBOL_NAME", "") security_id = row.get("SEM_SMST_SECURITY_ID", row.get("SECURITY_ID", "")) if (exchange != "NSE" or segment != "E" or instrument != "EQUITY" or series != "EQ" or not re.fullmatch(r"[A-Z0-9&_.-]+", symbol) or not security_id.isdigit()): continue ticker = symbol + ".NS" if ticker in result and result[ticker] != security_id: ambiguous.add(ticker) result[ticker] = security_id return {ticker: security_id for ticker, security_id in result.items() if ticker not in ambiguous} def normalize_candles(payload: dict) -> pd.DataFrame: columns = {"timestamp": "Timestamp", "open": "Open", "high": "High", "low": "Low", "close": "Close", "volume": "Volume"} arrays = [payload.get(key) for key in columns] if not all(isinstance(values, list) for values in arrays) or len({len(values) for values in arrays}) != 1: raise DhanError("invalid_response") frame = pd.DataFrame({name: payload[key] for key, name in columns.items()}) for column in frame.columns: frame[column] = pd.to_numeric(frame[column], errors="coerce") valid = frame.notna().all(axis=1) valid &= frame.map(lambda value: math.isfinite(value)).all(axis=1) valid &= (frame[["Open", "High", "Low", "Close", "Timestamp"]] > 0).all(axis=1) valid &= (frame["Volume"] >= 0) & (frame["Low"] <= frame[["Open", "Close"]].min(axis=1)) valid &= frame["High"] >= frame[["Open", "Close"]].max(axis=1) frame = frame.loc[valid] frame.index = pd.to_datetime(frame.pop("Timestamp"), unit="s", utc=True).dt.tz_convert(IST) frame = frame.sort_index() frame = frame[~frame.index.duplicated(keep="last")] frame.attrs["source"] = "dhan" return frame class DhanDataClient: def __init__(self, client_id: str = "", access_token: str = "", enabled: bool = False, session=None, cache_path: str | None = None, auth_manager: DhanTokenManager | None = None): self.client_id = client_id self.access_token = access_token self.auth_manager = auth_manager self.enabled = enabled self.history_adjusted = os.getenv("DHAN_HISTORY_ADJUSTED", "0") == "1" self.session = session if session is not None else requests.Session() data_dir = "/data" if os.path.isdir("/data") and os.access("/data", os.W_OK) else os.path.dirname(__file__) self.cache_path = cache_path or os.path.join(data_dir, "dhan_cache.db") self._lock = threading.RLock() self._http_lock = threading.Lock() self._instrument_lock = threading.Lock() self._quote_lock = threading.Lock() self._changed = threading.Condition(self._lock) self._stop = threading.Event() self._thread = None self._consumers: dict[str, set[str]] = {} self._instruments: dict[str, str] = {} self._instruments_at = 0.0 self._instruments_retry_at = 0.0 self._quotes: dict[str, dict] = {} self._profile: dict = {} self._profile_at = 0.0 self._last_request: dict[str, float] = {} self._cooldown_until = 0.0 self._error = None self._stream_state = "idle" self._retry_day = None self._failed_attempts = 0 self._daily_paused = False @property def configured(self) -> bool: return bool(self.client_id and (self.auth_manager.configured if self.auth_manager is not None else self.access_token)) def _ensure_access_token(self, timeout: float = 8) -> str: if self.auth_manager is None: return self.access_token if self.yahoo_fallback_active: raise DhanError("yahoo_fallback") try: token = self.auth_manager.get_token(timeout=timeout) except DhanAuthError as exc: error = DhanError(exc.code) error.count_failure = exc.attempted raise error from None if token != self.access_token: self.access_token = token self._profile = {} self._profile_at = 0 return token def _refresh_daily_budget(self) -> None: day = datetime.fromtimestamp(time.time(), IST).date().isoformat() with self._lock: if self._retry_day == day: return cached = self._cache_get("dhan_daily_retry_budget") raw = cached[0] if cached and isinstance(cached[0], dict) else {} failures = raw.get("failures", 0) if raw.get("day") == day else 0 self._failed_attempts = min(3, max(0, failures)) if type(failures) is int else 0 self._daily_paused = bool(raw.get("paused")) if raw.get("day") == day else False self._daily_paused = self._daily_paused or self._failed_attempts >= 3 self._retry_day = day self._cooldown_until = 0 if not self._daily_paused: self._error = None if self._stream_state == "yahoo_fallback": self._stream_state = "idle" @property def yahoo_fallback_active(self) -> bool: if not self.enabled or not self.configured: return False self._refresh_daily_budget() with self._lock: return self._daily_paused def _record_failure(self, code: str) -> None: self._refresh_daily_budget() with self._lock: self._failed_attempts = min(3, self._failed_attempts + 1) self._daily_paused = self._daily_paused or self._failed_attempts >= 3 self._error = code if self._daily_paused: self._quotes.clear() self._cache_put("dhan_daily_retry_budget", {"day": self._retry_day, "failures": self._failed_attempts, "paused": self._daily_paused}) self._changed.notify_all() def _record_success(self) -> None: self._refresh_daily_budget() with self._lock: if self._daily_paused: return self._error = None if self._failed_attempts: self._failed_attempts = 0 self._cache_put("dhan_daily_retry_budget", {"day": self._retry_day, "failures": 0, "paused": False}) def _cache_get(self, key: str): try: with closing(sqlite3.connect(self.cache_path, timeout=2)) as connection: row = connection.execute("SELECT payload, saved_at FROM dhan_data_cache WHERE cache_key=?", (key,)).fetchone() return (json.loads(row[0]), row[1]) if row else None except (sqlite3.Error, ValueError, OSError): return None def _cache_put(self, key: str, payload) -> None: try: with closing(sqlite3.connect(self.cache_path, timeout=2)) as connection: connection.execute("PRAGMA journal_mode=WAL") connection.execute("CREATE TABLE IF NOT EXISTS dhan_data_cache (cache_key TEXT PRIMARY KEY, payload TEXT NOT NULL, saved_at REAL NOT NULL)") connection.execute("INSERT OR REPLACE INTO dhan_data_cache VALUES (?, ?, ?)", (key, json.dumps(payload, allow_nan=False), time.time())) connection.commit() except (sqlite3.Error, ValueError, OSError): pass def _request(self, method: str, path: str, payload=None, timeout: float = 8) -> dict: if (method, path) not in _READ_ONLY: raise DhanError("read_only") if not self.configured: raise DhanError("not_configured") market_data = path != "/profile" if market_data and self.yahoo_fallback_active: raise DhanError("yahoo_fallback") if time.monotonic() < self._cooldown_until: raise DhanError(self._error or "unavailable") deadline = time.monotonic() + timeout if not self._http_lock.acquire(timeout=max(0, timeout)): raise DhanError("rate_limited") try: if market_data and self.yahoo_fallback_active: raise DhanError("yahoo_fallback") token = self._ensure_access_token(timeout=max(0, deadline - time.monotonic())) bucket = "quotes" if path.startswith("/marketfeed") else "data" interval = 1.05 if bucket == "quotes" else 0.25 if path == "/optionchain": bucket, interval = "optionchain", 3.05 delay = max(0, self._last_request.get(bucket, 0) + interval - time.monotonic()) if delay >= deadline - time.monotonic() or self._stop.wait(delay): raise DhanError("rate_limited") self._last_request[bucket] = time.monotonic() remaining = max(0.1, deadline - time.monotonic()) response = self.session.request(method, "https://api.dhan.co/v2" + path, headers={"access-token": token, "client-id": self.client_id}, json=payload, timeout=remaining) if response.status_code >= 400: code = {401: "token_expired", 403: "access_denied", 429: "rate_limited"}.get(response.status_code, "unavailable") if response.status_code == 401 and self.auth_manager is not None: self.auth_manager.invalidate(token) raise DhanError(code) result = response.json() if isinstance(result, dict) and result.get("errorCode") == "DH-901": if self.auth_manager is not None: self.auth_manager.invalidate(token) raise DhanError("token_expired") if (not isinstance(result, dict) or result.get("status") in ("failure", "failed", "error") or result.get("errorCode") or result.get("errorType")): raise DhanError("invalid_response") return result except (requests.RequestException, ValueError) as exc: error = exc if isinstance(exc, DhanError) else DhanError("unavailable") if market_data and error.code != "yahoo_fallback" and getattr(error, "count_failure", True): self._record_failure(error.code) self._error = error.code self._cooldown_until = time.monotonic() + (60 if error.code in ("token_expired", "access_denied") else 10) raise error from None finally: self._http_lock.release() def status(self, probe: bool = False) -> dict: paused = self.yahoo_fallback_active if probe and self.configured and not paused and (self.auth_manager is None or self.enabled) and time.time() - self._profile_at >= 60: try: raw = self._request("GET", "/profile") expiry = raw.get("tokenValidity") expired = False if expiry: try: expired = datetime.strptime(expiry, "%d/%m/%Y %H:%M").replace(tzinfo=IST).timestamp() <= time.time() except (TypeError, ValueError): pass self._profile = {"token_valid": not expired, "token_expiry": expiry, "data_plan": raw.get("dataPlan", "Unknown"), "data_expiry": raw.get("dataValidity")} self._error = "token_expired" if expired else None except DhanError as exc: self._error = exc.code if exc.code == "token_expired": self._profile["token_valid"] = False self._profile_at = time.time() with self._lock: state = "disabled" if not self.enabled else "not_configured" if not self.configured else "yahoo_fallback" if paused else self._error or self._stream_state if self.enabled and self.auth_manager is not None and not self.auth_manager.configured: state = "auth_setup_required" auth_status = self.auth_manager.status() if self.auth_manager is not None else { "automatic_auth": False, "auth_ready": self.configured, "requires_daily_login": bool(self.configured), } midnight = (datetime.fromtimestamp(time.time(), IST) + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0) return {"configured": self.configured, "enabled": self.enabled, "state": state, "message": _ERRORS.get(state), "stream": self._stream_state, "subscriptions": len(self.stream_tickers()), "read_only": True, "history_adjusted": self.history_adjusted, "active_provider": "dhan" if self.enabled and self.configured and not paused else "yahoo", "fallback_active": paused, "fallback_until": midnight.isoformat() if paused else None, "failed_attempts": self._failed_attempts, "retries_remaining": max(0, 2 - max(0, self._failed_attempts - 1)), **self._profile, **auth_status} def resolve(self, ticker: str, timeout: float = 6) -> str: if not re.fullmatch(r"[A-Z0-9&_.-]+\.NS", ticker): raise DhanError("instrument_unavailable") deadline = time.monotonic() + timeout if not self._instrument_lock.acquire(timeout=max(0, timeout)): raise DhanError("instrument_unavailable") try: if time.time() < self._instruments_retry_at: if time.time() - self._instruments_at > 7 * 86400 or ticker not in self._instruments: raise DhanError("instrument_unavailable") return self._instruments[ticker] if time.time() - self._instruments_at >= 86400: cached = self._cache_get("instruments") if cached and (not isinstance(cached[0], dict) or not cached[0] or any( not isinstance(security_id, str) or not security_id.isdigit() for security_id in cached[0].values() )): cached = None if cached and time.time() - cached[1] < 86400: self._instruments, self._instruments_at = cached else: try: remaining = deadline - time.monotonic() if remaining <= 0: raise DhanError("instrument_unavailable") response = self.session.get("https://api.dhan.co/v2/instrument/NSE_EQ", timeout=remaining) response.raise_for_status() instruments = parse_instruments(response.text) if not instruments: raise DhanError("instrument_unavailable") self._instruments, self._instruments_at = instruments, time.time() self._cache_put("instruments", instruments) except (requests.RequestException, DhanError): self._instruments_retry_at = time.time() + 60 if cached and time.time() - cached[1] <= 7 * 86400: self._instruments, self._instruments_at = cached elif not self._instruments or time.time() - self._instruments_at > 7 * 86400: raise DhanError("instrument_unavailable") from None if ticker not in self._instruments: raise DhanError("instrument_unavailable") return self._instruments[ticker] finally: self._instrument_lock.release() def record_tick(self, packet: dict) -> None: if not isinstance(packet, dict) or packet.get("exchange_segment") != 1: return try: if isinstance(packet.get("LTP"), bool): return price, traded = float(packet["LTP"]), float(packet["LTT"]) now = time.time() if not math.isfinite(price) or price <= 0 or not math.isfinite(traded) or not -5 <= now - traded <= 90: return ticker = next((symbol for symbol, security_id in self._instruments.items() if security_id == str(packet.get("security_id"))), None) if not ticker: return if self.yahoo_fallback_active: return quote = {"ticker": ticker, "price": price, "source": "dhan", "trade_time": traded, "observed_at": now, "stale": False} for source, target in (("high", "day_high"), ("low", "day_low"), ("volume", "volume")): value = float(packet.get(source) or 0) if math.isfinite(value) and value > 0: quote[target] = value with self._changed: if traded < self._quotes.get(ticker, {}).get("trade_time", 0): return self._quotes[ticker] = quote self._record_success() self._changed.notify_all() except (TypeError, ValueError, KeyError): return def cached_quotes(self, tickers, include_stale: bool = False) -> dict[str, dict]: if self.yahoo_fallback_active: return {} result = {} with self._lock: for ticker in tickers: quote = self._quotes.get(ticker) if not quote: continue stale = time.time() - quote["observed_at"] > 15 or time.time() - quote["trade_time"] > 90 if include_stale or not stale: result[ticker] = {**quote, "stale": stale} return result def quotes(self, tickers, timeout: float = 8) -> dict[str, dict]: if not self.enabled or not self.configured or self.yahoo_fallback_active: return {} tickers = list(dict.fromkeys(tickers))[:1000] deadline = time.monotonic() + timeout if not self._quote_lock.acquire(timeout=max(0, timeout)): raise DhanError("rate_limited") try: cached = self.cached_quotes(tickers) missing = [ticker for ticker in tickers if ticker not in cached] if not missing: return cached ids = {} for ticker in missing: try: remaining = deadline - time.monotonic() if remaining <= 0: raise DhanError("rate_limited") ids[self.resolve(ticker, timeout=remaining)] = ticker except DhanError: continue if not ids: return cached remaining = deadline - time.monotonic() if remaining <= 0: raise DhanError("rate_limited") raw = self._request("POST", "/marketfeed/quote", {"NSE_EQ": [int(security_id) for security_id in ids]}, timeout=remaining) data = raw.get("data") or {} quote_data = data.get("NSE_EQ", {}) if isinstance(data, dict) else None if not isinstance(quote_data, dict): self._record_failure("invalid_response") raise DhanError("invalid_response") for security_id, quote in quote_data.items(): if str(security_id) not in ids or not isinstance(quote, dict): continue try: traded = datetime.strptime(quote["last_trade_time"], "%d/%m/%Y %H:%M:%S").replace(tzinfo=IST).timestamp() except (KeyError, ValueError, TypeError): continue self.record_tick({"exchange_segment": 1, "security_id": security_id, "LTP": quote.get("last_price"), "LTT": traded, "high": quote.get("ohlc", {}).get("high"), "volume": quote.get("volume")}) result = self.cached_quotes(tickers) if not result: from market_calendar import market_status if market_status().get("status") == "OPEN": self._record_failure("invalid_response") return result finally: self._quote_lock.release() def history(self, ticker: str, period: str, interval: str, timeout: float = 8, refresh: bool = False) -> pd.DataFrame | None: if not self.enabled or not self.configured or not ticker.endswith(".NS") or self.yahoo_fallback_active: return None deadline = time.monotonic() + timeout match = re.fullmatch(r"(\d+)(d|mo|y)", period) if not match or interval not in {"1d", "1m", "5m", "15m", "25m", "30m", "60m"}: return None days = min(7300, int(match[1]) * {"d": 1, "mo": 31, "y": 366}[match[2]]) key = f"history:{ticker}:{period}:{interval}" cached = self._cache_get(key) if cached and not refresh and time.time() - cached[1] <= (3600 if interval == "1d" else 60): return normalize_candles(cached[0]) end = datetime.now(IST) start = end - timedelta(days=days) if interval == "1d": end = (end + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0) remaining = deadline - time.monotonic() if remaining <= 0: raise DhanError("unavailable") payload = {"securityId": self.resolve(ticker, timeout=remaining), "exchangeSegment": "NSE_EQ", "instrument": "EQUITY", "oi": False} path = "/charts/historical" if interval == "1d" else "/charts/intraday" if interval != "1d": payload["interval"] = "15" if interval == "30m" else interval[:-1] date_format = "%Y-%m-%d" if interval == "1d" else "%Y-%m-%d %H:%M:%S" chunks = [] while start < end: stop = end if interval == "1d" else min(end, start + timedelta(days=90)) remaining = deadline - time.monotonic() if remaining <= 0: raise DhanError("unavailable") raw = self._request("POST", path, {**payload, "fromDate": start.strftime(date_format), "toDate": stop.strftime(date_format)}, timeout=remaining) try: chunk = normalize_candles(raw) except (ValueError, TypeError, OverflowError): self._record_failure("invalid_response") raise DhanError("invalid_response") from None if chunk.empty: self._record_failure("invalid_response") raise DhanError("invalid_response") chunks.append(chunk) start = stop frame = pd.concat(chunks).sort_index() frame = frame[~frame.index.duplicated(keep="last")] if frame.empty: return None if interval == "30m": frame = frame.resample("30min", origin="start_day", offset="9h15min").agg( {"Open": "first", "High": "max", "Low": "min", "Close": "last", "Volume": "sum"}).dropna() frame.attrs["source"] = "dhan" self._record_success() stored = {column.lower(): frame[column].tolist() for column in frame.columns} stored["timestamp"] = [int(stamp.timestamp()) for stamp in frame.index] self._cache_put(key, stored) return frame def stream_tickers(self) -> set[str]: with self._lock: return set().union(*self._consumers.values()) if self._consumers else set() def subscribe(self, tickers) -> str: if not self.enabled or not self.configured: raise DhanError("disabled" if not self.enabled else "not_configured") if not tickers or len(tickers) > 100 or any(not re.fullmatch(r"[A-Z0-9&_.-]+\.NS", ticker) for ticker in tickers): raise DhanError("instrument_unavailable") with self._lock: if len(self._consumers) >= 10: raise DhanError("stream_limit") consumer = uuid.uuid4().hex self._consumers[consumer] = set(tickers) if not self.yahoo_fallback_active and (self._thread is None or not self._thread.is_alive()): self._stop.clear() self._thread = threading.Thread(target=self._run_feed, name="dhan-feed", daemon=True) self._thread.start() return consumer def unsubscribe(self, consumer: str) -> None: with self._changed: self._consumers.pop(consumer, None) self._changed.notify_all() def wait_for_update(self, timeout: float = 1) -> None: deadline = time.monotonic() + timeout with self._changed: while not self._stop.is_set(): remaining = deadline - time.monotonic() if remaining <= 0: break self._changed.wait(timeout=remaining) async def _consume(self, feed, initial_tickers: set[str] | None = None) -> None: token_at_connect = self.access_token try: await asyncio.wait_for(feed.connect(), timeout=10) self._stream_state = "connected" self._error = None subscribed = self.stream_tickers() if initial_tickers is None else initial_tickers while not self._stop.is_set() and self.stream_tickers() and not self.yahoo_fallback_active: if self.auth_manager is not None and (self.auth_manager.needs_refresh is True or self.access_token != token_at_connect): break desired = self.stream_tickers() if desired != subscribed: feed.instruments = [(1, self.resolve(ticker), 17) for ticker in desired] await asyncio.wait_for(feed.subscribe_instruments(), timeout=5) removed = subscribed - desired if removed: removed = list(removed) for offset in range(0, len(removed), 100): batch = removed[offset:offset + 100] message = {"RequestCode": 18, "InstrumentCount": len(batch), "InstrumentList": [ {"ExchangeSegment": "NSE_EQ", "SecurityId": self.resolve(ticker)} for ticker in batch]} await asyncio.wait_for(feed.ws.send(json.dumps(message)), timeout=5) subscribed = desired try: self.record_tick(await asyncio.wait_for(feed.get_instrument_data(), timeout=2)) except asyncio.TimeoutError: continue finally: self._stream_state = "disconnected" await asyncio.wait_for(feed.disconnect(), timeout=3) def _run_feed(self) -> None: backoff = 1 while not self._stop.is_set(): with self._lock: if not self._consumers: self._thread = None self._stream_state = "idle" return if self.yahoo_fallback_active: self._thread = None self._stream_state = "yahoo_fallback" return feed = None feed_token = None try: from dhanhq import DhanContext, MarketFeed class TimestampedFeed(MarketFeed): def utc_time(self, epoch_time): return float(epoch_time) def server_disconnection(self, data): code = int.from_bytes(data[8:10], "little") raise DhanError({806: "subscription_required", 807: "token_expired", 808: "access_denied", 809: "token_expired"}.get(code, "unavailable")) initial_tickers = self.stream_tickers() instruments = [(1, self.resolve(ticker), 17) for ticker in initial_tickers] self._stream_state = "connecting" feed_token = self._ensure_access_token(timeout=8) feed = TimestampedFeed(DhanContext(self.client_id, feed_token), instruments, version="v2") feed.loop.run_until_complete(self._consume(feed, initial_tickers)) backoff = 1 except Exception as exc: self._error = exc.code if isinstance(exc, DhanError) else "sdk_unavailable" if isinstance(exc, ImportError) else "unavailable" if self._error == "token_expired" and self.auth_manager is not None and feed_token: self.auth_manager.invalidate(feed_token) if getattr(exc, "count_failure", True): self._record_failure(self._error) self._stream_state = "reconnecting" if self._error in {"token_expired", "access_denied", "subscription_required", "sdk_unavailable", "auth_rejected", "auth_cooldown", "auth_busy", "auth_storage_error"}: backoff = 60 finally: if feed is not None: feed.loop.close() if self._stop.wait(backoff): break backoff = min(60, backoff * 2) def stop(self) -> None: self._stop.set() with self._changed: self._consumers.clear() self._changed.notify_all() _CLIENT = None _CLIENT_LOCK = threading.Lock() def get_dhan_client() -> DhanDataClient: global _CLIENT with _CLIENT_LOCK: if _CLIENT is None: client_id = os.getenv("DHAN_CLIENT_ID", "") _CLIENT = DhanDataClient(client_id, os.getenv("DHAN_ACCESS_TOKEN", ""), enabled=os.getenv("DHAN_ENABLED", "0") == "1", auth_manager=token_manager_from_env(client_id)) atexit.register(_CLIENT.stop) return _CLIENT