API not pulling historical bars

Hello,
When I attempt to use the API, I can’t get minute data past the last 1000 minutes. I am trying to get older data. Below is my python code. Can someone help me out on this? It’s just giving me data from 1000 minutes ago up to now (when the markets are open).

import alpaca_trade_api as tradeapi
api = tradeapi.REST()
barset = api.get_barset('AAPL', 'minute', limit=1000 ,start="2010-04-02")
a = barset['AAPL']
print(a[0])

Thanks!

A few issues. The get_barset API is quite particular about the formatting of the date. It’s a good practice to use the isoformat() method to do the formatting for you. If it doesn’t interpret the formatting correctly, it silently errors and uses the default date values. In this case the default is None for the start and the current date for end. So what the code above ends up like is this

barset = api.get_barset(‘AAPL’, ‘minute’, limit=1000)

That defaults to the current day and then counts backwards 1000 minutes.

The data doesn’t go back as far as 2010 (only 2015) but that’s fine. The following will fetch all the available minute data for AAPL

import pandas as pd
import pytz

NY_TIMEZONE = pytz.timezone('America/New_York')
fetch_ticker = 'AAPL'

fetch_start = pd.Timestamp('2010-04-02 00:00', tz=NY_TIMEZONE).isoformat()

bardata = api.get_barset(fetch_ticker, 'minute', start=fetch_start)

There is a post relating to a similar issue here. Good luck.

2 Likes

Thanks! I really appreciate that. This is exactly what I want!