Skip to content

Commit 49c09bc

Browse files
committed
merge subscription
1 parent 0a01f13 commit 49c09bc

File tree

6 files changed

+44
-51
lines changed

6 files changed

+44
-51
lines changed

.github/workflows/publish-to-pypi.yml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,12 @@ jobs:
1212
uses: actions/setup-python@v1
1313
with:
1414
python-version: 3.7
15-
- name: Install pep517
16-
run: >-
17-
python -m
18-
pip install
19-
pep517
20-
--user
2115
- name: Build a binary wheel and a source tarball
2216
run: >-
2317
python -m
24-
pep517.build
25-
--source
26-
--binary
27-
--out-dir dist/
28-
.
18+
setup.py
19+
sdist
20+
bdist_wheel
2921
- name: Publish package to Test PyPI
3022
uses: pypa/gh-action-pypi-publish@master
3123
with:

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

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

5-
![PyPI and TestPyPI](https://github.com/openfeed-org/sdk-python/workflows/PyPI%20and%20TestPyPI/badge.svg)
5+
![Build](https://github.com/openfeed-org/sdk-python/workflows/PyPI%20and%20TestPyPI/badge.svg)
6+
7+
![PyPI](https://img.shields.io/pypi/v/openfeed?label=PyPI%20)
68

79
## Installation
810

openfeed/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
from .openfeed_client import OpenfeedClient
1010

11-
VERSION = '1.0.3'
11+
VERSION = '1.0.5'

openfeed/openfeed_client.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import traceback
12
import sys
23
import inspect
34
from generated import openfeed_api_pb2
@@ -52,7 +53,8 @@ def add_symbol_subscription(self, symbol, callback):
5253
self.symbol_handlers[symbol] = []
5354

5455
if self.token is not None:
55-
self._send_message(self.__create_symbol_request([symbol]))
56+
self._send_message(
57+
self.__create_subscription_request(None, [symbol]))
5658

5759
self.symbol_handlers[symbol].append(callback)
5860

@@ -61,17 +63,23 @@ def add_exchange_subscription(self, exchange, callback):
6163
self.exchange_handlers[exchange] = []
6264

6365
if self.token is not None:
64-
self._send_message(self.__create_exchange_request([exchange]))
66+
self._send_message(
67+
self.__create_subscription_request([exchange], None))
6568

6669
self.exchange_handlers[exchange].append(callback)
6770

71+
def get_instrument_definitions(self):
72+
return self.instrument_definitions
73+
6874
def get_instrument_definition(self, id):
6975
return self.instrument_definitions[id]
7076

7177
def get_instrument_definition_by_symbol(self, symbol):
7278
return self.instruments_by_symbol[symbol]
7379

7480
def _send_message(self, msg):
81+
if self.debug:
82+
print("Sending:", msg)
7583
self.ws.send(msg.SerializeToString(), websocket.ABNF.OPCODE_BINARY)
7684

7785
def __reset(self):
@@ -89,13 +97,9 @@ def handleLogin(msg):
8997
self.token = msg.loginResponse.token
9098

9199
# sub to all existing interest
92-
self._send_message(self.__create_symbol_request(
93-
self.symbol_handlers.keys()))
94-
95-
# sub to all existing exchanges
96-
self._send_message(self.__create_exchange_request(
97-
self.exchange_handlers.keys()))
98-
100+
self._send_message(self.__create_subscription_request(
101+
self.exchange_handlers.keys(), self.symbol_handlers.keys()))
102+
99103
return msg
100104

101105
def handleHeartbeat(msg):
@@ -134,7 +138,10 @@ def handleMarketSnapshot(msg):
134138
inst = self.instrument_definitions[msg.marketSnapshot.marketId].instrumentDefinition
135139

136140
self.__notify_exchange_listeners(inst.barchartExchangeCode, msg)
137-
self.__notify_symbol_listeners(inst.symbol, msg)
141+
142+
# TODO review symbology handling, subbing by one and keying off the other can create unexpected results
143+
# for example subscribing to "ZCYAIA40.CM" will come back with OF symbol (less the suffix) in `instrument.symbol`
144+
self.__notify_symbol_listeners(inst.symbols[0].symbol, msg)
138145

139146
return msg
140147

@@ -167,7 +174,7 @@ def on_message(ws, message):
167174
def on_error(ws, error):
168175
if self.debug:
169176
print("WS Error: ", error)
170-
177+
traceback.print_exc()
171178
self.__callback(self.on_error, error)
172179

173180
def on_close(ws):
@@ -225,30 +232,22 @@ def __notify_heartbeat_listeners(self, msg):
225232
print("Failed to notify `heartbeat` callback", e)
226233
self.__callback(self.on_error, e)
227234

228-
def __create_symbol_request(self, symbols):
235+
def __create_subscription_request(self, exchanges, symbols):
229236
requests = []
230237

231-
for sym in symbols:
232-
requests.append(openfeed_api_pb2.SubscriptionRequest.Request(
233-
symbol=sym,
234-
))
238+
if len(exchanges) > 0:
239+
for exch in exchanges:
240+
requests.append(openfeed_api_pb2.SubscriptionRequest.Request(
241+
exchange=exch,
242+
subscriptionType=[
243+
openfeed_api_pb2.SubscriptionType.Value("QUOTE")]
244+
))
235245

236-
of_req = openfeed_api_pb2.OpenfeedGatewayRequest(
237-
subscriptionRequest=openfeed_api_pb2.SubscriptionRequest(
238-
token=self.token,
239-
service=openfeed_pb2.Service.Value("REAL_TIME"),
240-
requests=requests
241-
))
242-
243-
return of_req
244-
245-
def __create_exchange_request(self, exchanges):
246-
requests = []
247-
248-
for exch in exchanges:
249-
requests.append(openfeed_api_pb2.SubscriptionRequest.Request(
250-
exchange=exch,
251-
))
246+
if len(symbols) > 0:
247+
for sym in symbols:
248+
requests.append(openfeed_api_pb2.SubscriptionRequest.Request(
249+
symbol=sym,
250+
))
252251

253252
of_req = openfeed_api_pb2.OpenfeedGatewayRequest(
254253
subscriptionRequest=openfeed_api_pb2.SubscriptionRequest(
@@ -282,7 +281,6 @@ def handle_heartbeat(msg):
282281

283282
of_client = OpenfeedClient("username", "password", debug=False)
284283

285-
of_client.add_symbol_subscription(symbol="AAPL", callback=handle_message)
286284
of_client.add_exchange_subscription(
287285
exchange="FOREX", callback=handle_message)
288286
of_client.add_heartbeat_subscription(callback=handle_heartbeat)
@@ -292,7 +290,8 @@ def handle_heartbeat(msg):
292290
of_client.on_connected = lambda x: print("of-client: connected")
293291

294292
# blocking mode
295-
of_client.start(blocking=True)
293+
of_client.start(blocking=False)
296294

297-
# while True:
298-
# time.sleep(3)
295+
while True:
296+
print("Number of Instruments:", len(of_client.get_instrument_definitions()))
297+
time.sleep(10)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "openfeed"
3-
version = "1.0.3"
3+
version = "1.0.5"
44
description = "Python SDK for Openfeed"
55
authors = ["Barchart <[email protected]>"]
66
license = "MIT"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
setup(
1919
name='openfeed',
20-
version='1.0.3',
20+
version='1.0.5',
2121
author='Barchart',
2222
author_email='[email protected]',
2323
license='MIT',

0 commit comments

Comments
 (0)