Analyze Data and Trade Stocks Through Your Browser Using Google Colab and Alpaca API

_bars = api.get_bars(symbol=_SPY, timeframe=TimeFrame.Day, start=“2022-03-28”)

print(_bars)

File “”, line 1 _bars = api.get_bars(symbol=_SPY, timeframe=TimeFrame.Day, start=“2022-03-28”) ^ SyntaxError: invalid syntax

Like I said bra, a coding noob. Lol my bad I’m sure its a simple fix.

No worries. I started teaching myself Python back in November, specifically to develop a trading bot for Alpaca. It’s been a journey!

I think you are getting that error because you need to specify the symbol as a string.
symbol=_SPY

Either specify the string directly in the get_bars call, e.g.

_bars = api.get_bars(symbol= 'SPY', timeframe=TimeFrame.Day, start=“2022-03-28”)

or declare the symbol as a variable first:

_mySymbol = 'SPY' #  Whichever ticker you want
_bars = api.get_bars(symbol=_mySymbol, timeframe=TimeFrame.Day, start=“2022-03-28”)

Bear in mind, if you haven’t already, you will need to include/import some libraries at the top of your code:

import alpaca_trade_api as alpaca
from alpaca_trade_api.rest import TimeFrame

Thank you man. I will log on and try to start this. I am learning for the same reason pretty much. :100::fire:

1 Like

Modern style to graph bars

import pandas as pd
import mplfinance as mpf

bars = api.get_bars('AAPL','1T',start='2024-02-13T09:30:00-05:00')
data = {
    'timestamp': [bar.t for bar in bars],
    'open': [bar.o for bar in bars],
    'high': [bar.h for bar in bars],
    'low': [bar.l for bar in bars],
    'close': [bar.c for bar in bars],
    'volume': [bar.v for bar in bars],
}
df = pd.DataFrame(data).set_index('timestamp')
mpf.plot(
    df,
    type='candle',
    # addplot=plots,
    figscale=2.0,
    figratio=(20,6),
    style='charles',
    warn_too_much_data=100_000,
)

1 Like

@Igor_Ivoilov Looks nice! Thanks.