Market data: prices as rows you can loop over
Before any strategy, you need data. Market data is just rows of prices over time — and to code, each row is a dictionary you can loop over. (Everything here is for learning, not financial advice.)
The big idea
Market data is a time-ordered series of price bars, each with values like open, high, low, and close, that you read row by row.
See it in code
The simplest market data is just a list of closing prices, oldest first. A loop reads them in time order — which is the whole point, because markets are sequences:
# Closing prices, oldest first - for study, not advice.
closes = [100.0, 102.5, 101.0, 104.0]
for close in closes:
print(close)100.0 102.5 101.0 104.0
That's market data at its barest: numbers in time order. But a bare price doesn't say when it happened — so next we attach a day to each one.
enumerate hands you a counter alongside each value, so we can label every close with its day number — the price and its place in time:
closes = [100.0, 102.5, 101.0, 104.0]
for day, close in enumerate(closes, start=1):
print(f"Day {day}: {close}")Day 1: 100.0 Day 2: 102.5 Day 3: 101.0 Day 4: 104.0
A bar carries two facts now — a day and a close. Real bars carry more, so it helps to bundle each row's values together instead of tracking parallel lists.
Now bundle each day and its price into one row — a dictionary — so a bar can hold many values at once. In the lab we use frozen sample bars (fixed historical data, reproducible and purely educational); a loop walks them in time order, exactly as a real analysis would:
# Frozen sample bars - for study, not advice.
bars = [
{"day": 1, "close": 100.0},
{"day": 2, "close": 102.5},
{"day": 3, "close": 101.0},
{"day": 4, "close": 104.0},
]
for bar in bars:
print(f"Day {bar['day']}: close {bar['close']}")
print("Bars loaded:", len(bars))Day 1: close 100.0 Day 2: close 102.5 Day 3: close 101.0 Day 4: close 104.0 Bars loaded: 4
The data is just a list of dictionaries, and the loop reads it in time order — which matters, because markets are sequences. Real datasets add open, high, low, and volume per bar, but the shape is the same: rows over time.
Time-series data is everywhere: temperatures by day, heart rate by second, website visits by hour. The skills are shared — load rows, iterate in order, compute over a window. A price series is one example of a pattern you'll meet in science, health, and operations.
Try it yourself
Add high and low to each bar and print the daily range (high - low). Then find the highest close by looping and tracking a running maximum.
The common mistake
Ignoring order, or peeking at the future. Market data is a sequence — shuffling it destroys its meaning. And a strategy must only use data available up to the current bar; using later bars is 'lookahead bias', a classic mistake that makes a backtest look better than reality.
What it unlocks
Reading bars is the base for moving averages, trading signals, and backtesting, all built on lists and dictionaries.