Skip to content

Commit 5c2d74a

Browse files
authored
Fix streaming news article event dispatching. (#691)
News article events are different from other types of events (e.g. stock trades) because a single news article could be applicable to multiple symbols. For that reason, the streamed event does not have the usual `S` field that contains a single symbol; instead, the event has a `symbols` field that contains a list of symbols. #555 mistakenly assumes that the event has an `S` field. This commit changes the dispatching logic to instead use the `symbols` field. (There is a similar [commit](alpacahq/alpaca-trade-api-csharp@be67b62) for the C# library.)
1 parent dc3cf7a commit 5c2d74a

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

alpaca_trade_api/stream.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,12 +565,20 @@ def _cast(self, msg_type, msg):
565565

566566
async def _dispatch(self, msg):
567567
msg_type = msg.get('T')
568-
symbol = msg.get('S')
569568
if msg_type == 'n':
570-
handler = self._handlers['news'].get(
571-
symbol, self._handlers['news'].get('*', None))
572-
if handler:
573-
await handler(self._cast(msg_type, msg))
569+
symbols = msg.get('symbols', [])
570+
# A news article could be unrelated to any symbols, resulting in an empty symbols list. Those news articles
571+
# should still be dispatched to the wildcard event handler.
572+
if not symbols:
573+
symbols.append('*')
574+
575+
for symbol in symbols:
576+
handler = self._handlers['news'].get(symbol)
577+
if handler is None:
578+
handler = self._handlers['news'].get('*')
579+
580+
if handler is not None:
581+
await handler(self._cast(msg_type, msg))
574582
else:
575583
await super()._dispatch(msg)
576584

0 commit comments

Comments
 (0)