# barchart-desktop-sdk

> Live market state from an Openfeed connection, for Python. The SDK subscribes, applies snapshots and updates, and keeps quotes, order books and cumulative volume per instrument, calling you when they change. It connects to the Barchart Desktop API (ws://localhost:9000, with Barchart Desktop's credentials) or to an Openfeed gateway. It's the Python port of `@barchart/desktop-sdk-js`.

- Early release. Python 3.11 or later. Install it from PyPI: `pip install barchart-desktop-sdk` or `uv add barchart-desktop-sdk`. It brings the `openfeed` client it needs with it.
- Create one `DesktopSdk` for the application's lifetime, or use `with DesktopSdk() as sdk:`, and `login(Credentials(url=..., username=..., password=...))`. `logout()` keeps subscriptions; `dispose()` ends everything.
- `subscribe_quote`, `subscribe_depth` and `subscribe_cumulative_volume` take a symbol and a `Listener(on_event=..., on_error=..., on_message=...)`, and return a `Subscription`. Call `close()` on it when done.
- Always pass `on_error`: nothing else calls you when the server refuses a subscription, such as one without permission. It gets a `SubscriptionError`: log it, and decide in code on its `result`.
- Listeners run one at a time, in order, on the SDK's own delivery thread, never inside a subscribe call: the first one, an `initial` event with the current state, comes after it returns. Protect anything they share with your other threads.
- Every method is safe to call from any thread, listeners included. A listener mustn't wait on a future the SDK settles, such as `sdk.when_connected().result()`: that deadlocks. Use `add_done_callback`.
- `when_connected`, `raw.request_snapshot` and `raw.lookup_instruments` return a `concurrent.futures.Future`. In `asyncio`, use `await asyncio.wrap_future(...)`.
- Keep the process alive yourself: the SDK's threads are daemon threads.
- Records are frozen dataclasses and may be kept. Prices are floats scaled to real values; `None` means the feed hasn't sent a field. Times are UTC `datetime`s and trade dates are `date`s. Protocol messages on `raw` are shared, so read them and don't change them.
- `formatted` on a quote event, and `sdk.get_formatted_quote(symbol)`, give prices in the instrument's own notation (such as 32nds).
- Not every Openfeed server sends volume at price: on one that doesn't, a cumulative volume subscription goes `active`, but no events come.
- `raw.lookup_instruments` fails with an `InstrumentLookupError` when the server refuses a lookup or finds nothing.
- Protocol enums (`Service`, `SubscriptionType`, `Result`) and message types come from `openfeed.generated`.
- Names are the TypeScript SDK's in snake_case: its reference below describes the same methods and behavior.
- API reference: every public class, method, field and type, generated from the docstrings, one page each. It's in the installed package, not in the repository: `barchart_desktop_sdk/docs/api/README.md` in its `site-packages`. The same docstrings are in the package's sources, and in `help(...)` on any of them.

## Docs

- [README](https://raw.githubusercontent.com/barchart/desktop-sdk/main/python/README.md): the introduction, names, and threads (asyncio included).
- [Reference](https://raw.githubusercontent.com/barchart/desktop-sdk/main/js/docs/reference.md): every public method and type, and the delivery guarantees, in the TypeScript SDK's names.

## Examples

- [Quotes](https://raw.githubusercontent.com/barchart/desktop-sdk/main/python/examples/quotes.py): a quote's last price and best bid and offer, as they change.
- [Order book](https://raw.githubusercontent.com/barchart/desktop-sdk/main/python/examples/order_book.py): the order book, rendered on each change.
- [Coalescing](https://raw.githubusercontent.com/barchart/desktop-sdk/main/python/examples/coalescing.py): at most one event per subscription per interval, on a busy feed.
- [Cumulative volume](https://raw.githubusercontent.com/barchart/desktop-sdk/main/python/examples/cumulative_volume.py): the volume-at-price levels that changed.
- [Connection](https://raw.githubusercontent.com/barchart/desktop-sdk/main/python/examples/connection.py): logging in and out, rejected credentials, heartbeats.
- [Raw access](https://raw.githubusercontent.com/barchart/desktop-sdk/main/python/examples/raw_access.py): raw subscriptions, one-off snapshots and instrument lookups.

## Optional

- [Design](https://raw.githubusercontent.com/barchart/desktop-sdk/main/js/docs/design.md): how the TypeScript SDK works inside; `CLAUDE.md` in this repo says how the threads map onto it.
- [Openfeed](https://openfeed.com/): the protocol, its services and messages.
- [Openfeed client](https://github.com/openfeed-org/sdk-python): `openfeed`, which the SDK runs on.
