SAHI DOCS

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.

Last traded price#

method api.get_ltp(...)
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.

instrument_ids int or list[int] Required
One id, or up to 100.
timeout float Optional
Seconds.

Returns#

{"data": {"<id>": {"last_trade_price": ..., "last_trade_time": ...}}}, keyed by the id as a string. An unknown id maps to None.

Raises#

Full quote with market depth#

method api.get_quotes(...)
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#

method api.get_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.

instrument_id int Required
The instrument.
size str Required
1m, 3m, 5m, 15m, 30m, 1h or 1d; use the CANDLE_* constants.
from_ms int Required
Start, epoch ms, inclusive.
to_ms int Required
End, epoch ms, inclusive.
include_partial bool Optional
Add the in-progress candle while the market is open.
oi bool Optional
Add open interest as a seventh element.
timeout float Optional
Seconds.

Returns#

{"candles": [[ts, o, h, l, c, v], ...], "has_more": ...}. Prices in paise, ts in epoch ms.

Raises#

  • ValueError: unknown size, or to_ms before from_ms.
  • SahiRateLimitException: over 2/s, 250/min or 100,000/day.

Candle history#

method api.get_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#

method api.get_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.

underlying_id int Required
The index's or stock's instrument_id.
timeout float Optional
Seconds.

Returns#

{"expiries": ["YYYY-MM-DD", ...]}, nearest first.

Raises#

Example#

nifty is from Find an index.

Option chain#

method api.get_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.

underlying_id int Required
The underlying's instrument_id.
expiry str Optional
YYYY-MM-DD; default the nearest.
timeout float Optional
Seconds.

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#