0

I'm using the Alpaca API to pull in daily OHLC data:

chunk_size = 200
for i in range(0, len(symbols), chunk_size):
    symbol_chunk = symbols[i:i+chunk_size]
        
    barsets = api.get_bars_iter(symbol_chunk, TimeFrame.Day, "2022-07-25", "2022-08-01")

    for bar in barsets:
        symbol = bar.S
        stock_id = stock_dict[symbol]
        print(f'processing symbol', bar)

        previous_close = bar.c.shift()
        print(previous_close)

With the line previous_close = bar.c.shift() I would expect to be able to get the previous close, but instead I get the error:

Traceback (most recent call last):
  File "populate_prices.py", line 36, in <module>
    previous_close = bar.c.shift()
AttributeError: 'float' object has no attribute 'shift'

I'm guessing this is because shift is something you can only use with pandas...

So here's my question: how do I look back and get the previous_close? Do I have to use pandas to use shift() or is there another way?

P.s I dunno if this matters, but inside my for bar in barsets loop I am inserting into a sql database:

    # cursor.execute("""
    #     INSERT INTO stock_price (stock_id, date, open, high, low, close, volume, number_of_trades, volume_weighted_average_price)
    #     VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
    # """, (stock_id, bar.t.date(), bar.o, bar.h, bar.l, bar.c, bar.v, bar.n, bar.vw))
    # connection.commit()

Thanks and sorry for the noob question!

a7dc
  • 3,323
  • 7
  • 32
  • 50
  • You can write your own function to do what `shift` does in pandas, or you can use pandas. You can't just expect functions from a module like pandas to work everywhere... – BeRT2me Aug 02 '22 at 04:21
  • Then how can I do it without pandas? – a7dc Aug 02 '22 at 05:10

0 Answers0