0

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 = 'https://data.alpaca.markets/v2/stocks/TSLA/quotes'
# LATESTBAR_URL = 'https://data.alpaca.markets/v2/stocks/tsla/bars/latest'
BARS_URL = 'https://data.alpaca.markets/v2/stocks/tsla/bars'

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
}
Dongyob
  • 87
  • 2
  • 8

1 Answers1

0

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='https://paper-api.alpaca.markets')

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