alpacahq / alpaca-trade-api-python

Python client for Alpaca's trade API
https://pypi.org/project/alpaca-trade-api/
Apache License 2.0
1.73k stars 535 forks source link

Exception in thread for long-short example program (forbidden URL) #381

Open 0w8States opened 3 years ago

0w8States commented 3 years ago

Hi!

I'm getting started with the API this morning, and I ran into a bit of an issue. I've added my API keys and ran the program, but I get a thrown exception. Am I missing something that needs to be configured?

Python Version 3.8.6

(Output) Waiting for market to open... Market opened. Exception in thread Thread-5: Traceback (most recent call last): File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner self.run() File "/usr/lib/python3.8/threading.py", line 870, in run self._target(*self._args, self._kwargs) File "long-short.py", line 312, in getPercentChanges bars = self.alpaca.get_barset(stock[0], 'minute', length) File "/home/john/GitHub Repo/Long-short/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 473, in get_barset resp = self.data_get('/bars/{}'.format(timeframe), params) File "/home/john/GitHub Repo/Long-short/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 178, in data_get return self._request( File "/home/john/GitHub Repo/Long-short/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 125, in _request return self._one_request(method, url, opts, retry) File "/home/john/GitHub Repo/Long-short/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 146, in _one_request resp.raise_for_status() File "/app/lib/python3.8/site-packages/requests/models.py", line 941, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://data.alpaca.markets/v1/bars/minute?symbols=SWN&limit=10 Exception in thread Thread-6: Traceback (most recent call last): File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner self.run() File "/usr/lib/python3.8/threading.py", line 870, in run self._target(*self._args, *self._kwargs) File "long-short.py", line 272, in getTotalPrice bars = self.alpaca.get_barset(stock, "minute", 1) File "/home/john/GitHub Repo/Long-short/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 473, in get_barset resp = self.data_get('/bars/{}'.format(timeframe), params) File "/home/john/GitHub Repo/Long-short/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 178, in data_get return self._request( File "/home/john/GitHub Repo/Long-short/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 125, in _request return self._one_request(method, url, opts, retry) File "/home/john/GitHub Repo/Long-short/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 146, in _one_request resp.raise_for_status() File "/app/lib/python3.8/site-packages/requests/models.py", line 941, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://data.alpaca.markets/v1/bars/minute?symbols=VG&limit=1 Exception in thread Thread-7: Traceback (most recent call last): File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner self.run() File "/usr/lib/python3.8/threading.py", line 870, in run self._target(self._args, self._kwargs) File "long-short.py", line 272, in getTotalPrice bars = self.alpaca.get_barset(stock, "minute", 1) File "/home/john/GitHub Repo/Long-short/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 473, in get_barset resp = self.data_get('/bars/{}'.format(timeframe), params) File "/home/john/GitHub Repo/Long-short/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 178, in data_get return self._request( File "/home/john/GitHub Repo/Long-short/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 125, in _request return self._one_request(method, url, opts, retry) File "/home/john/GitHub Repo/Long-short/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 146, in _one_request resp.raise_for_status() File "/app/lib/python3.8/site-packages/requests/models.py", line 941, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://data.alpaca.markets/v1/bars/minute?symbols=SWN&limit=1 Exception in thread Thread-3: Traceback (most recent call last): File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner self.run() File "/usr/lib/python3.8/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "long-short.py", line 265, in rerank self.qLong = int(self.longAmount // respGetTPLong[0])

0w8States commented 3 years ago

I just noticed that the Client Error URL above is stating it's using 'v1'

However, the tradeapi in the example program is using 'v2'

self.alpaca = tradeapi.REST(API_KEY, API_SECRET, APCA_API_BASE_URL, 'v2')