Alpaca Data v2 - Multi-symbol market data request

I’m using the Alpaca Data API V2. I imported this example from the github repo: alpaca-trade-api-python/websocket_example.py at master · alpacahq/alpaca-trade-api-python · GitHub

I’m fairly new to python development. When I hard-code multiple ticker symbols to stream.subscribe_trades(print_trade, 'AAPL', 'IBM', 'ASML', 'TSLA')
the logger returns: subscribed to trades: ['IBM', 'ASML', 'TSLA', 'AAPL'], quotes: [] and bars: []

However, if I attempt to apply a LIST or a TUPLE as a variable (stock_tuple in this case) to that function, it fails:
stream.subscribe_trades(print_trade, stock_tuple)

I’ve simplified the creation of the tuple by defining it this way:
stock_tuple = ('AAPL', 'IBM', 'ASML', 'TSLA')

That variable is then passed to the stream.subscribe_trades function, and it fails.

 stock_tuple = ('AAPL', 'IBM', 'ASML', 'TSLA')
 print(stock_tuple)

 stream = Stream(settings.alpaca_api_key,
                 settings.alpaca_secret,
                 base_url=URL(settings.alpaca_base_url),
                 data_feed=settings.alpaca_feed,
                 raw_data=True)

 stream.subscribe_trades(print_trade, stock_tuple)

This fails with the message: error: invalid syntax (400)

How might I dynamically send the list of stocks I want to watch to this function?

Try unpacking the data structure with an asterisk.

stream.subscribe_trades(print_trade, *stock_tuple)
1 Like

Hi @dbetts, did this work for you?

I am having the same issue but with multiple symbols. The StockDataStream - subscribe_bars document states that the subcribe_bars can take tuple but when I use test it I get a error: invalid syntax (400) . It only works with single symbol even if I unpacked it. Is there another way of doing it? Any help would be much appreciated.

Hi @sean, do you have any other suggestions? Thanks

image

Did it work ? I am getting the same error