a7dc
a7dc

Reputation: 3416

Using shift without pandas? Getting the previous close for stocks OHLC

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!

Upvotes: 0

Views: 126

Answers (0)

Related Questions