Skip to content
Wildflower
Join free
Backtesting in Python (optional)

Free Trading School · Level 7 · Course 7.5

Loading price data with pandas

Lesson 4 of 15 in Backtesting in Python (optional)Module 1: Getting started

Advanced · Backtesting in Python

Loading price data with pandas

Wildflower Trading School · Lesson 7.5.1.4

Wildflower

Last time you kept four prices in a list. A real test has thousands of bars, each with an open, high, low and close. By the end of this lesson, you'll know what a price file looks like, and how to load it into a table in one line. You'll also know four checks to run before you trust it.

1 of 9

What you'll learn

  • Read a price file. pd.read_csv turns a CSV file of bars into a table in one line.
  • Pick out what you need. Take one column, or the rows between two dates.
  • Run four checks first. Look at rows, first and last dates, number types and time zone.
  • Count the rows. A quick sum tells you if the file holds what you expected.
  • Match the time zone. A file in world time (UTC) shifts bars from New York chart hours.

Exercise

  1. Type ten made-up daily bars into a spreadsheet, with the header date, open, high, low, close, and save it as CSV.
  2. Load it with pd.read_csv and run the four checks.

Next: Where test data comes from.

Quick check

  1. 1. What does index_col="date" do?

  2. 2. df.dtypes says the close column is object. What does that suggest?

  3. 3. Why check the time zone of intraday bars?

0 of 3 answered.

Finished this lesson?

Tick it off and keep your place

Make a free account and we'll remember where you are, so “Where test data comes from” is waiting next time.

Read the full notes

reads a of bars into a table in one line, pd.read_csv. Before trusting it, check the row count, the first and last dates, that prices are numbers, and the time zone.

  • A is plain text: a header row naming the columns, then one bar per row, with commas between the columns ( and volume).
  • Load it: import pandas as pd, then df = pd.read_csv("made_up_daily.csv", parse_dates=["date"], index_col="date"). parse_dates turns text into dates; index_col makes the date each row's label. df is a , pandas' word for a table. df.head(3) shows the first rows.
  • One column: df["close"]. Rows between dates: df.loc["2024-03-04":"2024-03-06"], both ends included.
  • Four checks: rows (len(df)), first and last dates, types (df.dtypes: float or int, never object for prices), and for intraday bars the time zone. A file in UTC and a chart in New York time put the same bar at different hours.
  • Made-up file: 1,043 rows, 2022 to 2025. 4 × 261 ≈ 1,044, so about right; real futures data also skips exchange holidays (Holidays and early closes).

Worked example

Made-up numbers

Is the row count about right?

Made-up numbers · daily bars, 2022 to 2025 · about 261 weekdays a year

  1. 4 × 261 = about 1,044 rows
  2. The file has 1,043 rows: about right.
  3. Real futures data would have a few fewer, for exchange holidays.
  4. 900 rows would mean months are missing.

A quick sum tells you whether the file holds what you expected.

Your turn

Drill

Made-up numbers · 3 years of daily bars · about 261 weekdays a year

Trading is risky. You can lose money. Test results are hypothetical, and a test can't tell you what will happen next. This lesson explains how testing works. It isn't advice.