How accurate is get_stock_latest_bar?

Hi all,

How accurate get_stock_latest_bar?

I compared the result of the same one minute bar with get_stock_bars 's one minute bar.
There is difference. Especially in volume.

Can you help me understand how to use get_stock_latest_bar?


@huseyin The data returned from get_stock_latest_bar and get_stock_bars will be identical if requested at the same time. There may be differences however if get_stock_bars is requested some time later. I’ll get into that, but first I’ll mention the use case for get_stock_latest_bar.

One issue with bars is they are only created if there are ‘valid’ trades during the bar. (Check out this article for a description on how, and when, bars are calculated.) As the name implies get_stock_latest_bar will search backwards from the current time and return the first bar it finds (ie the latest). This may be the previous minute, but it also could be from several minutes previous or even the previous day (for very thinly traded symbols). It’s good practice to check the bar timestamp and use that in ones validation logic. get_stock_latest_bar will always return a bar. get_stock_bars however, simply fetches all bars between a start and end time. There is the potential it won’t return any bars. Herein lies the problem. Fetching the latest bar using get_stock_bars becomes a challenge because one doesn’t know how far back to start. get_stock_latest_bar solves that issue.

Now about timing. There are two primary considerations to be aware of. First, bars can get updated and, if one is using the FreeMarket Data, the most current 15 minutes of data is restricted to only trades which occurred on the IEX exchange (ie its not full market data).

Since trading venues have 10 seconds to report their trades, there is the potential for valid trades to arrive 10 seconds after the close of a bar. Rather than waiting 10 seconds to calculate every bar, Alpaca creates a bar within about a second after the close of the bar, but then updates the bar 30 seconds later if there were any ‘missed’ trades. Additionally, the SIPs often correct trades throughout the day. Alpaca waits until the end of the day (ie after extended hours trading closes at 20:00 ET) to process any of these corrections and likewise updates any affected bars. So, because of these updates, a bar fetched immediately at the close of a bar could be updated (and be different) from the same bar fetched 30 seconds later or after markets close.

The issue with the Free Market Data being restricted from fetching the most recent 15 minutes means a bar fetched immediately after a bar closes can be different from a bar fetched 15 (or more) minutes later. This may be the issue here? If one sees a big change in volume, it’s probably due to the initial (ie ‘latest’) bar being only volume on the IEX exchange, but then the same bar 15 minutes later will be full market volume. Note this issue only applies to users of the Free Market Data. Paid Market Data plans can fetch real time full market data and bars with no restriction.

Does that help? Does it explain any difference you are seeing?

1 Like

Thank you for explaining.

I understood that the reason of the difference is that I am trying to get the latest 15 mins data.
1000 dollars a year a lot for me to get the paid account.

Thanks anyway.