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
- Type ten made-up daily bars into a spreadsheet, with the header date, open, high, low, close, and save it as CSV.
- Load it with pd.read_csv and run the four checks.
Quick check
1. What does index_col="date" do?
2. df.dtypes says the close column is object. What does that suggest?
3. Why check the time zone of intraday bars?
0 of 3 answered.
Finished this lesson?
Tick it off and keep your place
Read the full notes
A free add-on for Python that works with tables of data, such as a file of price bars, a bit like a spreadsheet you control with code.More in the glossary reads a A plain text file of a table: one row per line, with commas between the columns. CSV stands for comma separated values.More in the glossary 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 A plain text file of a table: one row per line, with commas between the columns. CSV stands for comma separated values.More in the glossary is plain text: a header row naming the columns, then one bar per row, with commas between the columns (Open, high, low and close: the four prices a candle or bar shows for its stretch of time.More in the glossary 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’ name for a table of data, with labelled rows and named columns, such as one row per price bar and a column for each of open, high, low and close.More in the glossary, 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 numbersIs the row count about right?
Made-up numbers · daily bars, 2022 to 2025 · about 261 weekdays a year
- 4 × 261 = about 1,044 rows
- The file has 1,043 rows: about right.
- Real futures data would have a few fewer, for exchange holidays.
- 900 rows would mean months are missing.
A quick sum tells you whether the file holds what you expected.
Your turn
DrillMade-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.