Get_barset returning time data outside of range

Environment

Language
Python

Alpaca SDK Version
Version is 0.52.0

Problem

Summary
The times that .get_barset() is returning are not the respective times that we input. When we input timestamps for the start and end parameters we receive a response with a time that corresponds to a different date and time altogether.

Paper or Live Trading?
Currently not trading, just pulling historical data

Example Code

Input:
api = tradeapi.REST( alpacaApiKey, alpacaSecretKey )
startTime = str( datetime( 2021, 2, 2, 9, 30, 0 ).isoformat() )
endTime = str( datetime( 2021, 2, 2, 16, 0, 0 ).isoformat() )
print( startTime )
print( endTime )
price = api.get_barset( [“GOOG”], “5Min”, 126, startTime, endTime )
print( price )

Output:
2021-02-02T09:30:00
2021-02-02T16:00:00
{‘GOOG’: [Bar({ ‘c’: 2092.22, ‘h’: 2092.22, ‘l’: 2092.22, ‘o’: 2092.22, ‘t’: 1613139900, ‘v’: 269}), Bar({ ‘c’: 2088.05, ‘h’: 2091.67, ‘l’: 2086.38, ‘o’: 2088.82, ‘t’: 1613140200, ‘v’: 1659}), …]}

The t property (which should be epoch time in seconds, based on the documentation) in that first returned Bar object, t = 1613139900, corresponds to a date of Friday, February 12, 2021 8:25:00 AM GMT-06:00 (based on https://www.epochconverter.com/)

2 Likes

Try using the following snippet modified slightly from the barset example on the Python API github page.

import alpaca_trade_api as tradeapi
import pandas as pd

TZ = 'America/New_York'

api = tradeapi.REST()
api = tradeapi.REST( alpacaApiKey, alpacaSecretKey )
beg = pd.Timestamp('2021-02-02T09:30', tz=TZ).isoformat()
end = pd.Timestamp('2021-02-02T16:00', tz=TZ).isoformat()
bar = api.get_barset(['GOOG'], '5Min', start=beg, end=end)

print(bar.df.index.sort_values()[0])

This prints the first timestamp of the set as the value you’d expect – 2021-02-02 09:30:00-05:00.

As for why your example didn’t work, I’m not entirely sure. When I ran your example, I wasn’t able to reproduce your results. However I also didn’t get the expected results.

Some quick googling came up with this alpaca-trade-api-python ticket which is similar to yours. It seems that passing datetime objects into the get_barset function doesn’t work as expected. Instead of fixing the code or updating the documentation, the issue was closed by updating the barset example in the README.md.

1 Like

Got it, this worked for me when I reformatted and then specified the timezone, appreciate your help!

1 Like