I have been testing out the API. I have been trying to understand the asof query parameter on the v2/bars API. But I do not really see how it should be used. When using it I have noticed that I at times only get older data even though I asked for newer data in the start and end date times. For instance, if I ask for 5 years of data up until today for CZR and BEAT I only get data up until 2020 for CZR and up to 2021 for BEAT if I use the asof with today’s date.
What exactly does the asof query parameter do and what are it’s use-cases?
@Discorida You bring up two separate issues
- …at times only get older data even though I asked for newer data in the start and end date times
- What exactly does the asof query parameter do and what are it’s use-cases?
The first issue is probably because you didn’t specify a
limit. By default, if one requests bar data, only the first 1000 bars are returned and they are returned in ascending order. So, if the requested window is longer than 1000 bars (eg 1000 trading days if requesting 1Day bars) the results will be truncated and appear to only return old data. Set
limit=10000 (ie the max) and for 1Day bars you will get all of them (because 10000 trading days is about 40 years). However, if using some other bar size you may still not get all the bars. In that case you will need to ‘page’ the results. There is a bit in the docs how to do this under paging here.
The second question about use cases for the
asof parameter… The description of that parameter is in the docs here. Basically, it addresses the problem introduced by stock symbols being reused. Take for example the symbol S which was used by The Sprint Corporation up until 2020 when Sprint was acquired by T-Mobile. Then, in 2021 SentinelOne went public and began trading under the symbol S. By default, specifying S will fetch data for SentinelOne (the company using that symbol as of the current date) and return data from 2021-current. However, what if you wanted historical data for Sprint? By specifying an asof date anytime while Sprint was still using that symbol, you can do that. Specifying, for example
asof=2020-01-01 will fetch Sprint data from 2016-2020 (ie the earliest date Alpaca has for historical data through the time when Sprint last traded).
So, the use case for the
asof parameter is to fetch data for companies where their stock symbol has been reused. This is a pretty specific use case, so generally do not even specify an
asof date and you will get the expected results. But keep it in mind if you do ever want to do any historical research or backtesting.
Hi, thanks for your detailed answer @Dan_Whitnable_Alpaca.
I looked at my previous code and I can see it was not the limit that was wrong, I had that set to 2000 and asked for 5 years of daily bars, so it was plenty. But I could spot the bug and that was that I used the asof query parameter and I was setting it to the start which then was in 2018. I did mean to set it to the end date which had been today. But I have now removed the asof query parameter and it is working as expected.