Historical Hourly chart gives only first 25 records

Hello Everyone,

I am trying to pull hourly data using the below code . But I am noticing that it always gives me only first 25 records , thought I am trying to pull last 100 hrs it gives me only for first 25 hours

            startTime = Convert.ToDateTime("7/21/2021 9:00:00 AM");
            endTime = DateTime.UtcNow;
            barRequest = new HistoricalBarsRequest("TSLA", startTime, endTime, BarTimeFrame.Hour);
            bars = await alpacaDataClient.ListHistoricalBarsAsync(barRequest);
            bars1 = bars.Items;

I am expecting the above code to retrieve hourly bars from 07\21 to current (07\26) . but it gives me the result only for first 25 hours .

Not sure, if i am missing anything . Any help or insight on this is much appreciated .


This is expected (partially) behavior. See the Paging section of the documentation.

The SDK returns only first page of data and you have to re-request the rest using the NextPageToken property of response and original request parameters. You can do it manually in the 'whileloop or useGetHistoricalBarsAsAsyncEnumerableorGetHistoricalBarsPagesAsAsyncEnumerable` extension methods from the Alpaca.Markets.Extensions package.

I strongly recommend you the second approach but if you prefer to understand what happened under the hood of these methods see the next snippet:

var startTime = new DateTime(2021, 7, 21, 0, 0, 0, DateTimeKind.Utc);
var endTime = DateTime.UtcNow;

var bars = new List<IBar>();
var request = new HistoricalBarsRequest(
    "AAPL", startTime, endTime, BarTimeFrame.Hour);
    var page = await client.ListHistoricalBarsAsync(request);


} while (request.Pagination.Token is not null);

1 Like

Thank you oleg .

That worked like a charm :slight_smile: