ok I was using an outdated version of the alpaca_trade_api and this resolved my issue. Thanks!
Sorry for this silly question but I have the same error, I tried to update alpaca_trade_api but it’s already updated. How did you fix this problem?
Traceback (most recent call last): File “”, line 1, in TypeError: get_bars() missing 2 required positional arguments: ‘start’ and ‘end’
This is what i have had success with but only after updating/reinstalling the alpaca module in my terminal.
the useful info for V2 is on the git page not the main website for alpaca.
base_url = ‘https://data.alpaca.markets/v2/stocks’
api_key = ‘key’
secret_key = ‘Skey’
api = REST(api_key, secret_key, base_url)
barset = api.get_bars(“IBM”, TimeFrame.Day, “2021-11-04”,“2022-03-16”, adjustment=‘raw’)
The lack of an Alpaca team member watching the forum after ending V1 is kinda crazy right?
Dan, check which version you are running. Run the following command:
bash# pip3 list --outdated
…
Package Version Latest Type
alpaca-trade-api 1.0.1 1.5.1 wheel
…
This is what I saw so you can see I was running a very old version. If this is your case do the following:
bash# pip3 install --upgrade alpaca-trade-api
Now when you list the outdated packages you won’t see alpaca-trade-api and you should be able to do the following:
bash-3.2$ python3
Python 3.7.7 (default, Mar 10 2020, 15:43:33)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import alpaca_trade_api as tradeapi
>>> APCA_API_KEY_ID=<your key here>
>>> APCA_API_SECRET_KEY=<your key here>
>>> api_v2 = tradeapi.REST(APCA_API_KEY_ID, APCA_API_SECRET_KEY, api_version=‘v2’)
>>> barset = api_v2.get_bars([‘AAPL’], “1Min”, limit=1)
>>> print(barset)
[Bar({ ‘S’: ‘AAPL’,
‘c’: 159.87,
‘h’: 159.89,
‘l’: 159.87,
‘n’: 58,
‘o’: 159.89,
‘t’: ‘2022-03-18T08:00:00Z’,
‘v’: 1203,
‘vw’: 159.739792})]
I was able to fix the error with your answer. Thank you!
Has this been fixed?
I’m getting the same error but it only occurs for some symbol_chunks. I’m not sure how to resolve it. If anyone knows that would be a great help!
from warnings import catch_warnings
import config, sqlite3
import alpaca_trade_api as tradeapi
from alpaca_trade_api import TimeFrame, TimeFrameUnit
connection = sqlite3.connect(config.DB_FILE)
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
cursor.execute("""SELECT id, symbol, name FROM stock""")
rows = cursor.fetchall()
symbols = [row['symbol'] for row in rows]
stock_dict = {}
for row in rows:
symbol = row['symbol']
symbols.append(symbol)
stock_dict[symbol] = row['id']
api = tradeapi.REST(config.API_KEY, config.SECRET_KEY, base_url=config.API_URL)
chunk_size = 200
for i in range(0, len(symbols), chunk_size):
print(i)
print(i + chunk_size)
symbol_chunk = symbols[i:i+chunk_size]
try:
bars = api.get_bars(symbol_chunk, TimeFrame(1,TimeFrameUnit.Day), "2023-12-14", "2023-12-15", adjustment='raw')
except Exception as e:
print(f"Error fetching bars: {e}")
continue # Skip to the next chunk if there's an error
#for bar in bars:
# print(f"processing symbol {bar.S}")
#for bar in bars:
# stock_id = stock_dict[bar.S]
# cursor.execute("""
# INSERT INTO stock_price (stock_id, date, open, high, low, close, volume)
# VALUES(?,?,?,?,?,?, ?)""", (stock_id, bar.t.date(), bar.o, bar.h, bar.l, bar.c, bar.v))
# print(bar.t, bar.o, bar.h, bar.l, bar.c, bar.v)