Get live market data with python SDK

Hi,

I am new to Alpaca, but I must confess that I am pretty frustrated at this moment. I have spent many many hours across several weeks trying to do what I consider an elemental feature of any self-respecting trading platform: get live market data.

I run my code in python, therefore I have been trying Alpaca’s official python SDK. By the way, their support and documentation are simply horrible.

For now, I use the free iex stream, which means I must use a websocket connection to get real-time data-- the REST API, which works great, is 15min delayed.

I have followed a bunch of examples and guidelines, to no avail. BTW, a big problem is that a lot of what is out there is written for the now defunct polygon data stream.

Anyway, let’s start with something simple. Could someone explain to me how to retrieve the latest quote, trade, and 1 min bar for ‘AAPL’? I need a code that will retrieve the data and allow the remaining of my code to run. That is to say, I don’t want code that requires a keyboard interrupt. I don’t think this is too much to ask!

Thank you

Hi @buysell
Thanks for flagging us on this, we are on working to create a better docs for SDK.

However, as your request, here i created google colab to show you the code to retrieve the latest data:

if you want to find a more detail, you can read it on the lib classes:
Bar: alpaca-trade-api-python/rest.py at master · alpacahq/alpaca-trade-api-python · GitHub
Trade: alpaca-trade-api-python/rest.py at master · alpacahq/alpaca-trade-api-python · GitHub
Quote: alpaca-trade-api-python/rest.py at master · alpacahq/alpaca-trade-api-python · GitHub

Thank you

2 Likes

Thank you so much @Radzi_Purba_LPCA

This works great!

Comments:
I am (pleasantly) surprised that this works with the REST API and iex subscription, since other functions like get_quotes throw the error: “your subscription does not permit querying data from the past 15 minutes”
get_snapshot (s) also work, in case someone else is interested

Follow-up question:
Is it possible to retrieve Level II data for stocks? I am under the impression that there are functions for cryptos (for example subscribe_crypto_orderbooks), but I am not sure about stocks.

Thank you!

This worked perfectly for getting live stock data, but when I attempt to query crypto data it comes back telling me that the last trade occurred in 2021 and all values are 0.

Is there some syntax change I should be using to query crypto data?