Chapter 10
Working with Market Data
In order to backtest trading strategies properly, we need reliable historical market data. For this book, we will primarily use daily price data for the SPDR S&P 500 ETF Trust (ticker symbol: SPY), one of the most liquid and widely traded ETFs in the world.
10.1 Downloading SPY Data from Yahoo Finance
Yahoo Finance provides free historical price data for thousands of securities, including SPY. Anyone can download this data manually through their website.
- Visit finance.yahoo.com.
- Search for “SPY”.
- Navigate to the “Historical Data” tab.
- Set the desired time range, frequency (daily), and click “Download” to get a CSV file.
10.2 Automated Data Fetching via API
In this project, we will automate data collection using an API to download the latest SPY data programmatically. This allows for cleaner, repeatable workflows and removes manual steps.
Here is a simplified code snippet demonstrating how data can be fetched automatically:
// Placeholder for Rust code to fetch SPY data fn download_spy_data() { // Connect to Yahoo Finance API // Download historical data // Save to CSV }
(Placeholder: Insert real API fetching code.)
10.3 Overview of the SPY Dataset
The dataset we will work with covers daily SPY trading data from January 29, 1993 to April 25, 2025.
The CSV file contains the following columns:
- ticker — Stock ticker symbol (always “SPY”)
- date — Trading date (format: YYYY-MM-DD)
- price — Daily average price (specific meaning to be clarified later)
- volume — Number of shares traded
- open — Opening price of the day
- close — Closing price of the day (actual market close)
- high — Highest price of the day
- low — Lowest price of the day
- adjclose — Closing price adjusted for splits and dividends
Here is a glimpse of the first and last rows in the dataset:
ticker | date | price | volume | open | close | high | low | adjclose |
SPY | 1993-01-29 | 43.94 | 1,003,200 | 43.97 | 43.94 | 43.97 | 43.75 | 24.45 |
SPY | 2025-04-25 | 549.04 | 25,381,939 | 546.65 | 549.04 | 549.20 | 543.70 | 549.04 |
(Note: Values rounded to two decimal places for display.)
10.4 Close Price vs. Adjusted Close Price
When backtesting strategies that involve buying and selling actual shares, it’s important to use the adjusted close price. The adjusted close accounts for:
- Stock splits
- Dividend payouts
If we use the regular close price without adjustment, we would understate returns — especially over long periods where SPY paid substantial dividends.
For strategies involving options, however, we typically reference the raw close price, since option prices already reflect dividend expectations.
In this book, we will clearly specify which price we use based on the context.
10.5 Data Cleaning and Daily Alignment
Before running any backtests, we must ensure our data is properly cleaned and aligned:
- Verify all trading days are properly sequenced without missing gaps
- Handle missing or incomplete rows (although rare with SPY)
- Align any derived indicators (like moving averages) by trading date
Having clean, consistent data is crucial to avoid introducing artificial errors into backtest results.
Now that we have reliable data prepared, we are ready to move on to building a realistic trading simulation framework.