New for 2027The MonsterDesk course and run-it-yourself software. Founding prices can be reserved now.Course and run-it-yourself software.See what is on offer
MonsterDesk

Free lesson · Module 1: Setting up · about 12 min

1.4 Getting market data: prices, bars and timeframes

A short video intro to the lesson. The full lesson videos come with the course in early 2027.

Agents learn from bars. A bar is one slice of time: the open, high, low and close price, plus the volume traded. The desk mostly uses hourly bars, and daily bars for its slower agents.

Download 60 days of hourly bars

Save this as get_data.py and run it:

import os
from datetime import datetime, timedelta, timezone
from dotenv import load_dotenv
from alpaca.data.enums import DataFeed
from alpaca.data.historical import StockHistoricalDataClient
from alpaca.data.requests import StockBarsRequest
from alpaca.data.timeframe import TimeFrame

load_dotenv()
data = StockHistoricalDataClient(os.environ["APCA_API_KEY_ID"], os.environ["APCA_API_SECRET_KEY"])
now = datetime.now(timezone.utc)
req = StockBarsRequest(symbol_or_symbols="SPY", timeframe=TimeFrame.Hour,
                       start=now - timedelta(days=60), end=now, feed=DataFeed.IEX)
bars = data.get_stock_bars(req).df.loc["SPY"]

# keep only bars that have finished: the current hour's bar is still moving
this_hour = now.replace(minute=0, second=0, microsecond=0)
bars = bars[bars.index < this_hour]

print(bars.tail())
bars.to_csv("spy_1h.csv")
print(f"saved {len(bars)} hourly bars")

SPY is a fund that tracks the S&P 500. feed=DataFeed.IEX uses the free data feed every Alpaca account can read.

What you get back

A table where each row is one bar: open, high, low, close, volume and a few extras. Most of what follows uses close.

Three traps to know now

  • Market hours. US stocks only trade in market hours, so there are gaps overnight and at weekends.
  • Crypto never closes. It trades 24/7, so its bars have no gaps.
  • Unfinished bars. Always drop the bar that is still forming, or your agent makes decisions on a price that's still moving. The MonsterDesk desk had a bug just like that.

Checklist

  • get_data.py prints the last five bars
  • spy_1h.csv is saved in your folder
Add MonsterDesk to your home screenOpen the live desk like an app.
Follow the desk free