Using websocket to save daily open prices to variable, then exit

It’s more or less in the title: I’m looking only to get the daily open prices of the constituent stocks in the S&P 500. I can subscribe to daily bars via the below, but I’m not quite sure how to save the results to a variable and then terminate the loop.

I should mention that I’m running this in a Jupyter notebook.

from alpaca_trade_api.stream import Stream
from alpaca_trade_api.common import URL

import nest_asyncio
nest_asyncio.apply()


async def bar_callback(t):
    print('bar', t)
    
    
stream = Stream(alpaca_key,
                alpaca_secret,
                raw_data=True,
                data_feed='iex') 

stream.subscribe_daily_bars(bar_callback, 'AAPL')

stream.run()

Why do you need streaming for such a simple task? Just request a snapshot for a single symbol (or for several symbols at once) and check/use current/previous daily bars available instantly.

That’s much more sensible, yes. I was under the mistaken impression that real-time data was accessible only through the websocket. Thank you!