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/ 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')

 stream = Stream(settings.alpaca_api_key,

 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