Alpaca Api Simple 5 Min Historical Bar Request Returning Null
I Must Be Making Very Simple Mistake Which I Haven't Been Able to Figure out for Hours. I Am Referencing Alpaca Api Doc and Trying to Follow. the Code Is Shown...
I must be making very simple mistake which I haven't been able to figure out for hours. I am referencing Alpaca API Doc and trying to follow. The code is shown below trying to get the 5 min historical bar on the TSLA as the test purposes which I failed. See below code.
import config, requests, json
# QUOTE_URL = '
# LATESTBAR_URL = '
BARS_URL = '
timeframe = '?timeframe=5Min'
BARS_URL = BARS_URL+timeframe
# r = requests.get(QUOTE_URL, headers=config.HEADERS)
# r2 = requests.get(LATESTBAR_URL, headers=config.HEADERS)
r3 = requests.get(BARS_URL, headers=config.HEADERS)
print(json.dumps(r3.json(), indent=4))
The result I get is shown below:
{
"bars": null,
"symbol": "TSLA",
"next_page_token": null
}
1 Answer
Here is my solution which helped me to get the OHLC data about any symbol from Alpaca API:
# imports
import alpaca_trade_api as tradeapi
from alpaca.data import TimeFrame
# create Alpaca API object with the given credentials
api = tradeapi.REST(key_id="YOUR_API_KEY",
secret_key="YOUR_SECRET_KEY",
base_url=')
# Call the API to get OHLC TSLA data adn store it in a dataframe
data = api.get_bars(
symbol='TSLA',
timeframe=TimeFrame.Minute
).df
# Get some historical data for TSLA
historical_data = api.get_bars(
symbol='TSLA', #any symbol is acceptable if it can be found in Alpaca API
timeframe=TimeFrame.Minute,
start="2018-01-01T00:00:00-00:00",
end="2018-02-01T00:00:00-00:00"
).df
print(data)
print(historical_data)
The data will be stored in a Dataframe, of course, if you want to get the results in a raw format just leave the ".df"
I hope it will help!