Cannot fetch bars

Hi,

This is my code:

ALPACA_BASE_URL = ‘https://paper-api.alpaca.markets/
ALPACA_MARKET_DATA_URL = “https://data.sandbox.alpaca.markets/

class AlpacaClient:
def init(self):
self.stockHistoricalDataClient = StockHistoricalDataClient(api_key=connectionsettings.ALPACA_API_KEY,
secret_key=connectionsettings.ALPACA_SECRET_KEY,
url_override=connectionsettings.ALPACA_BASE_URL,
)

def get_stock_bars(self, request_params):
    bars = self.stockHistoricalDataClient.get_stock_bars(request_params)

    return HttpResponse(bars, content_type="application/json")

symbol = “AAPL” # Example stock symbol

timeframee = TimeFrame(5, TimeFrameUnit.Minute)
start_date = datetime(2024, 5, 26)
end_date = datetime(2024, 5, 26)

alpacaClient = AlpacaClient()
request_params = StockBarsRequest(
    symbol_or_symbols=symbol,
    timeframe=timeframee,
    start=start_date,
    end=end_date
)
try:
    bars = alpacaClient.get_stock_bars(request_params)
    print(bars)
except Exception as e:
    print(f"An error occurred: {e}")

I get : An error occurred: Not Found
I have tried different variations such as:

  • timeframee = TimeFrame(1, TimeFrameUnit.Minute)
    

    start_date = datetime(2024, 5, 26, 18, 15, 0)
    end_date = datetime(2024, 5, 26, 18, 10, 0) (since basic subscription plan allows last 5 minutes) → I get : An error occurred: Not Found

  • If i use ALPACA_MARKET_DATA_URL instead then i get :
    An error occurred:

403 Forbidden

403 Forbidden


nginx

Any idea?

@jimcomccabe3 Basically, do not provide a url_override parameter when instantiating StockHistoricalDataClient. Let the defaults do their thing. It will default to
https://data.alpaca.markets/v2/ which is what you want.

Specifically, in the first instance, you overrode the default so the url was
https://paper-api.alpaca.markets/stocks/bars
which is not found because the bars endpoint doesn’t exist under that base url.

In the second instance, you overrode the default so the url was
https://data.sandbox.alpaca.markets/stocks/bars
which was found (it’s the market data for the broker API sandbox) but you received forbidden because the API key and secret key you provided are for a trading account and not a broker account.

So, just remove url_override=connectionsettings.ALPACA_BASE_URL and it should work.

Thank you but that default url is production . I want to run in test .

@jimcomccabe3 Not exactly sure what you mean when you say “I want to run in test”. There really is exactly one market data environment. There isn’t a ‘test’ market data and ‘production’ market data. Historical data endpoints all return the same values. The two different base URLs (ie https://data.alpaca.markets/v2/ and https://data.sandbox.alpaca.markets/stocks/) are simply provided so one can authenticate with either trading API keys or broker API keys. If one is an individual trader with a paper and/or live account, then you will be using your trading API keys to authenticate calls to endpoints with the base URL https://data.alpaca.markets/v2/.

Does that make sense? What is it you are trying to do which made you feel you want to “run in test”?

I am saying this because it says (production and sandbox) when i select base url in Historical bars. . so this makes me feel like sandbox is the opposite of production , ie test.

when i run below i get empty

why`?

@jimcomccabe3 It doesn’t look like you included the start and end dates (notice how those fields are gray and not black text). Enter the default dates and you should get this and then the associated response.

import requests

url = "https://data.alpaca.markets/v2/stocks/bars?symbols=TSLA&timeframe=1Min&start=2022-01-03T00%3A00%3A00Z&end=2022-01-04T00%3A00%3A00Z"

headers = {
    "accept": "application/json",
    "APCA-API-KEY-ID": "xxxxxx",
    "APCA-API-SECRET-KEY": "xxxxxxx"
}

response = requests.get(url, headers=headers)

print(response.text)

thank you !it worked!

1 Like