You can download market data every time you run a program. But you do not always need to.
Once you have a useful dataset, you can save it on your computer and load the same data again later.
In this lesson, we will use a simple file format called CSV.
What you will finish: you will download AAPL market data, save the DataFrame as a CSV file, and then load that CSV file back into Python.
1. The Simple Idea
FinanceDataReader
↓
DataFrame
↓
save
↓
CSV file
↓
reload
↓
DataFrame
The market data does not disappear when your Python program ends. It stays in the CSV file until you delete or replace it.
2. What Is a CSV File?
CSV stands for Comma-Separated Values. It is a simple text-based format for storing rows and columns.
Date,Open,High,Low,Close,Volume
2025-01-02,...
2025-01-03,...
2025-01-06,...
You do not need to edit this file by hand. Python can create it for you.
3. Create a New Python File
In your alphesta-lab folder, create:
save_reload_market_data.py
4. Download the Same AAPL Data
import FinanceDataReader as fdr
symbol = "AAPL"
start_date = "2025-01-02"
end_date = "2025-01-10"
df = fdr.DataReader(symbol, start_date, end_date)
At this point, df contains the market-data table.
5. Choose a CSV File Name
csv_file = "aapl_market_data.csv"
This is simply the name of the file we want Python to create.
6. Save the DataFrame
df.to_csv(csv_file)
Read it like this:
df
→ our market-data DataFrame
.to_csv(...)
→ save the DataFrame as a CSV file
csv_file
→ use this file name
After you run the program, you should see a new file in your project folder:
alphesta-lab/
├── save_reload_market_data.py
└── aapl_market_data.csv
7. Print a Simple Confirmation
print("Saved:", csv_file)
Your first version is now:
import FinanceDataReader as fdr
symbol = "AAPL"
start_date = "2025-01-02"
end_date = "2025-01-10"
df = fdr.DataReader(symbol, start_date, end_date)
csv_file = "aapl_market_data.csv"
df.to_csv(csv_file)
print("Saved:", csv_file)
8. Why Do We Need pandas Again?
FinanceDataReader helped us get the market data. Now we need a tool that can read the CSV file back into a DataFrame.
FinanceDataReader
= get financial data
pandas
= work with tables and files
Add:
import pandas as pd
pd is the common short name used for pandas.
9. Load the CSV Back into Python
loaded_df = pd.read_csv(
csv_file,
index_col="Date",
parse_dates=["Date"]
)
Read the parts separately:
pd.read_csv(...)
→ read a CSV file
index_col="Date"
→ use Date as the row index again
parse_dates=["Date"]
→ treat Date as date information
The result is stored in loaded_df.
df
→ DataFrame downloaded from the internet
loaded_df
→ DataFrame loaded from your CSV file
10. Print the Reloaded Data
print(loaded_df.head())
You should see the familiar rows again. The important point is the path the data followed:
internet
↓
df
↓
CSV file
↓
loaded_df
11. Your Complete File
import FinanceDataReader as fdr
import pandas as pd
symbol = "AAPL"
start_date = "2025-01-02"
end_date = "2025-01-10"
df = fdr.DataReader(symbol, start_date, end_date)
csv_file = "aapl_market_data.csv"
df.to_csv(csv_file)
print("Saved:", csv_file)
loaded_df = pd.read_csv(
csv_file,
index_col="Date",
parse_dates=["Date"]
)
print("\nReloaded data:")
print(loaded_df.head())
12. Why Saving Data Matters
If you want to test several indicator settings, you do not need to download the same historical dataset every time.
download once
↓
save one dataset
↓
test experiment 1
test experiment 2
test experiment 3
...
Reusing one saved dataset also helps keep the input data consistent while you compare experiments.
13. Change One Thing Yourself
Change:
symbol = "AAPL"
csv_file = "aapl_market_data.csv"
to:
symbol = "MSFT"
csv_file = "msft_market_data.csv"
Run the program again. You should now have two separate CSV files.
Check Your Understanding
- A DataFrame can be saved with
to_csv(). - A CSV file remains on your computer after the Python program ends.
- pandas can load a CSV file with
read_csv(). index_col="Date"restores Date as the row index.parse_dates=["Date"]tells pandas to treat Date as date information.- You can reuse a saved dataset instead of downloading it for every experiment.
What You Just Built
AAPL market data
↓
DataFrame
↓
to_csv()
↓
aapl_market_data.csv
↓
read_csv()
↓
DataFrame again
You can now download market data, keep it locally, and bring it back into Python later.
Where Do We Go Next?
The next useful step is checking whether market data is ready for analysis: dates, row order, duplicate rows, and missing values.
← Previous: How to Find One Stock in a Market Listing with Python
Next: How to Check Market Data Before Analysis with pandas →