SAHI DOCS

Python SDK

Live streams

SahiFeed: the tick stream and order updates, with reconnect and a local tick cache.

SahiFeed connects to the tick stream and the order-update stream.

Examples use api and instrument_id from Getting started, and feed from below.

Build a feed#

class SahiFeed(...)
SahiFeed(api: SahiAPI)

Takes a SahiAPI and copies its token. Nothing connects until the first subscribe.

Subscribe for last price#

method feed.subscribe_ltp(...)
feed.subscribe_ltp(instrument_ids: list[int], on_data_received: Callable[[dict, dict], None] | None = None) -> None

Subscribes instruments at LTP mode. If any id is refused, the whole batch is refused and your callback gets an error event.

instrument_ids list[int] Required
Up to 1000 per connection.
on_data_received callable Optional
callback(data, meta). meta["type"] is ltp_tick, quote_tick, depth_tick, ack or error. One callback serves all three modes.

Subscribe for quotes#

method feed.subscribe_quote(...)
feed.subscribe_quote(instrument_ids: list[int], on_data_received: Callable[[dict, dict], None] | None = None) -> None

As subscribe_ltp, at QUOTE mode: adds volume_today, the price band and open interest.

Subscribe for depth#

method feed.subscribe_depth(...)
feed.subscribe_depth(instrument_ids: list[int], on_data_received: Callable[[dict, dict], None] | None = None) -> None

As subscribe_ltp, at DEPTH mode: adds best_buys and best_sells.

Change mode#

method feed.change_mode(...)
feed.change_mode(instrument_ids: list[int], mode: str) -> None

Moves instruments to another mode.

instrument_ids list[int] Required
Ids to move.
mode str Required
SahiAPI.MODE_LTP, MODE_QUOTE or MODE_DEPTH.

Raises#

  • ValueError: unknown mode.

Unsubscribe#

method feed.unsubscribe(...)
feed.unsubscribe(instrument_ids: list[int]) -> None

Stops ticks for these instruments and clears their cached values.

Latest cached tick#

method feed.last_ltp(...)
feed.last_ltp(instrument_id: int) -> dict | None

The newest LTP tick held for an instrument, or None until one arrives. The cache fills even without consume(), so you can poll it.

Raises#

Latest cached quote#

method feed.last_quote(...)
feed.last_quote(instrument_id: int) -> dict | None

As feed.last_ltp, for QUOTE ticks.

Latest cached depth#

method feed.last_depth(...)
feed.last_depth(instrument_id: int) -> dict | None

As feed.last_ltp, for DEPTH ticks.

Order updates#

method feed.subscribe_order_updates(...)
feed.subscribe_order_updates(on_data_received: Callable[[dict, dict], None], *, on_connect: Callable[[], None] | None = None) -> None

Streams every order event on the account. Updates can repeat and are not replayed after a gap, so take a snapshot with get_order_list() in on_connect and keep the newer last_modified_at per order: see Stream + Snapshot.

on_data_received callable Required
callback(order, meta), one order object per update.
on_connect callable Optional
Called after every connect and reconnect.

Run the callbacks#

method feed.consume(...)
feed.consume() -> None

Blocks and runs your callbacks until stop() is called. However it ends, both streams are stopped.

Raises#

Stop both streams#

method feed.stop(...)
feed.stop() -> None

Closes both connections and unblocks consume(). Safe from any thread.