Metadata-Version: 2.4
Name: openfeed
Version: 1.5.0
Summary: Barchart Openfeed Example Client for Python
Author-email: Barchart <openfeed@barchart.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/openfeed-org/sdk-python
Project-URL: Download, https://github.com/openfeed-org/sdk-python/archive/master.zip
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: websocket-client>=0.40.0
Requires-Dist: protobuf>=6.33.3
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: python-dateutil>=2.7.5; extra == "test"
Dynamic: license-file

# Openfeed SDK for Python

This SDK encapuslates the openfeed proto objects and faciliates client connections to openfeed servers.

![Build](https://github.com/openfeed-org/sdk-python/workflows/PyPI%20and%20TestPyPI/badge.svg)

![PyPI](https://img.shields.io/pypi/v/openfeed?label=PyPI%20)

## Installation

```
pip install openfeed
```

Requires Python 3.9 or newer.

## Example Usage

```python
import openfeed

# new client with credentials
of_client = openfeed.OpenfeedClient("username", "password")

# optional state handlers
of_client.on_error = lambda x: print("Error:", x)
of_client.on_connected = lambda x: print("Connected")
of_client.on_disconnected = lambda x: print("Disconnected")

# attach a global message handler
of_client.on_message = lambda x: print("Global Message:", x)

# sub to markets by symbol
def on_message(msg):
    print("Market Data: ", msg)

of_client.add_symbol_subscription("AAPL", callback=on_message)

# sub to markets by exchange
of_client.add_exchange_subscription("NYSE", callback=on_message)

of_client.start()
```

### Expected Output

```log
of-client: Connected
of-client: Market Data:  subscriptionResponse
of-client: Market Data:  marketSnapshot
of-client: Market Data:  marketUpdate
of-client: Market Data:  marketUpdate
```

### Server

By default the client connects to `openfeed.aws.barchart.com`. Pass a host name to connect to another server, or a full `ws://` or `wss://` URL to set the scheme and path (for example, for TLS or a proxy):

```python
of_client = openfeed.OpenfeedClient("username", "password", server="demo.openfeed.barchart.com")
of_client = openfeed.OpenfeedClient("username", "password", server="wss://host:port/path")
```

### Openfeed Documentation

* [https://docs.barchart.com/openfeed](https://docs.barchart.com/openfeed)

## Subscription Types

Openfeed supports many levels of [subscription types](https://docs.barchart.com/openfeed/#/proto?id=subscriptiontype).

### OHLC

```python
of_client.add_exchange_subscription(["NYSE"], callback=on_message, subscription_type=["OHLC"])
```

```
{
  marketId: 5389879102616877808
  symbol: "AAPL"
  open {
    price: 1205600
  }
  high {
    price: 1205600
  }
  low {
    price: 1205247
  }
  close {
    price: 1205490
  }
  volume: 43635
  priceVolume: 5259897.1422
  numberTrades: 224
  tradeDate: 20201116
  transactionTime: 1605547921000000000
  openStartTime: 1605547885850000000
  closeEndTime: 1605547920964000000
}
```

## Development

Requires [uv](https://docs.astral.sh/uv/) and `protoc` (`brew install protobuf`).

```sh
uv sync                                          # create .venv and install deps
uv run python example.py -u USER -p PASS         # run the example
./build.sh                                       # regenerate protos and build dist/
```
