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 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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest