Getting Live Data

I am currently working on a senior project and I have decided to make a stock trading algorithm using python and Alpaca. I was wondering if there is an elegant way of getting stock prices to update as they are happening? I have yet to find a solution to this.

Hi there. One approach would be to use Alpaca’s StreamConn class, which is included in their python SDK, to consume real-time tick data (or one-second aggregates). See the following links for implementation details:


1 Like

Thank you for telling me about this! Yet I have been looking at this all day and have yet to figure out how to use streamconn. Is there an example that you could provide on how this works? The ones that have found do not example it in any way and I am having a hard time applying it to my list of tickers.

1 Like

It would be great to see functioning examples of streamconn, because the API docs seem to only include theoretical examples, with a lot of missing parts.

Can we see a dead-simple example of retrieving minute bar data for a single stock, where we can literally copy and paste the code and just replace our API keys?

So I believe that I can actually provide some assistance now after some trial and error.
As you should you should already have your api keys and be connected to it similar to this:

api = tradeapi.REST(
base_url=base_url,
key_id=api_key_id,
secret_key=api_secret,
api_version='v2')

For using the Streamconn it is the same concept. If you are using python you need to import StreamConn from the api something like this:

from alpaca_trade_api import StreamConn

Then comes the fun parts. You now need to connect to it similar to connecting with the API in general and should look something like this:

conn = StreamConn(base_url=base_url,key_id=api_key_id,secret_key=api_secret)

Then comes the what I had trouble with and I think most people who ask this question as well is what to do next after connecting to it.

@conn.on(r'^account_updates$')
async def on_account_updates(conn, channel, account):
    print('account', account)

@conn.on(r'^status$')
async def on_status(conn, channel, data):
    print('polygon status update', data)

@conn.on(r'^AM$')
async def on_minute_bars(conn, channel, bars):
    print('bars', new_bar)

conn.run(['account_updates', 'AM.*'])

The conn.run is were the magic happens and in this example once the code gets to the conn.run it will continue to run till you stop it so more still needs to be done about setting it up for running at certain times. However if you run this every minute all the stocks that the API can see will update and print out to your terminal but it should be updating the stock as intended.

If you or someone else needs more help I will be more than glad to help.

1 Like

Just wondering…!!
Have been saving the bar data somewhere for later use…?
if you are how are you doing that…?

Just curious,
What will be doing with the live data…??

I am actually using yfinance to get live data update, which I believe is more accurate and easy to use. You can do a infinite loop with time.sleep() to get data per minute. I am doing a inday-trading algorithm base on KD indicator which is yielding about 5%~15% with 4x margin daily.