Python SDK
Market data
Last traded price, quotes with depth, candles and the option chain.
Prices, candles and the option chain, from the
Market data endpoints. Instruments are named
by instrument_id from the scrip master.
- No IP check, and a short
429is retried once. - For live prices use the tick stream:
get_ltpandget_quotesallow one call per second each. - Examples use
apiandinstrument_idfrom Getting started.
Last traded price#
api.get_ltp(instrument_ids: int | Sequence[int], *, timeout: float | None = None) -> dict
Calls GET /v1/marketdata/ltp · Scope: marketdata.read
Last price for up to 100 instruments.
Returns#
{"data": {"<id>": {"last_trade_price": ..., "last_trade_time": ...}}}, keyed
by the id as a string. An unknown id maps to None.
Raises#
SahiValidationException: more than 100 ids.SahiAPIException: far more ids, roughly 200, make the URL too long for the edge.ValueError: no ids, raised before sending.SahiRateLimitException: over 1/s.
Full quote with market depth#
api.get_quotes(instrument_ids: int | Sequence[int], *, timeout: float | None = None) -> dict
Calls GET /v1/marketdata/quotes · Scope: marketdata.read
Price, volume, price band, open interest and depth for up to 100 instruments.
Returns#
{"data": {"<id>": {...}}}; fields are on the endpoint.
Raises#
- As for
get_ltp.
Historical candles#
api.get_candles(instrument_id: int, size: str, from_ms: int, to_ms: int, *, include_partial: bool = False, oi: bool = False, timeout: float | None = None) -> dict
Calls GET /v1/marketdata/candles · Scope: marketdata.read
Up to 2500 candles for a window, most recent first if it holds more; then
has_more is set. For a longer range use
get_candle_history.
1m, 3m, 5m, 15m, 30m, 1h or 1d; use the CANDLE_* constants.Returns#
{"candles": [[ts, o, h, l, c, v], ...], "has_more": ...}. Prices in paise,
ts in epoch ms.
Raises#
ValueError: unknownsize, orto_msbeforefrom_ms.SahiRateLimitException: over 2/s, 250/min or 100,000/day.
Candle history#
api.get_candle_history(instrument_id: int, size: str, from_ms: int, to_ms: int, *, oi: bool = False, timeout: float | None = None) -> list[list]
Every candle in a range, oldest first: the SDK pages through get_candles for
you. The whole range is held in memory.
Returns#
A list of candles shaped as in get_candles.
Raises#
- As for
get_candles.
Option expiries#
api.get_option_expiries(underlying_id: int, *, timeout: float | None = None) -> dict
Calls GET /v1/marketdata/optionchain/expiries · Scope: marketdata.read
The expiries with an option chain for an underlying.
instrument_id.Returns#
{"expiries": ["YYYY-MM-DD", ...]}, nearest first.
Raises#
SahiRateLimitException: over 5/s, 250/min or 10,000/day.
Example#
nifty is from Find an index.
Option chain#
api.get_option_chain(underlying_id: int, expiry: str | None = None, *, timeout: float | None = None) -> dict
Calls GET /v1/marketdata/optionchain · Scope: marketdata.read
The chain for one underlying and expiry.
instrument_id.YYYY-MM-DD; default the nearest.Returns#
underlying_id, expiry, last_price and strikes (lowest first), each with
ce and pe legs. Prices are paise, implied_volatility is a percent, and
greeks are in rupees.
Raises#
SahiRateLimitException: over 2/s, 120/min or 20,000/day.