Learning Scientific Programming with Python (2nd edition)
Chapter 9: Data Analysis with pandas
/
Problems
/
P9.4.4
P9.4.4: Investments over 10 years
Question P9.4.4
The data set at investment.tar lists the following quantities, in US dollars over time: (a) the price of gold; (b) the S&P 500 US stock market index; and (c) the price of the cryptocurrency Bitcoin. Compare the performance of these indexes over the period 2010–2020 with respect to the regular investment of $100 per month.
Show Solution
Solution P9.4.4
import pandas as pd
gold = pd.read_csv(
"gold.txt",
delim_whitespace=True,
index_col=0,
usecols=[0, 1],
parse_dates=True,
)
snp = pd.read_csv("SNP.csv", parse_dates=True, index_col=0)
btc = pd.read_csv("BTC.csv", parse_dates=True, index_col=0)
gold["holding"] = 100 / gold["Price"]
snp["holding"] = 100 / snp["Open"]
btc["holding"] = 100 / btc["BTC-USD"]
final_date = "2020-07-01"
total_btc = btc["holding"][:final_date].sum()
total_gold = gold["holding"][:final_date].sum()
total_snp = snp["holding"][:final_date].sum()
gold = total_gold * gold.loc[final_date]["Price"]
print(f"Gold investment: USD {gold:,.2f}")
sandp = total_snp * snp.loc[final_date]["Open"]
print(f"S&P investment: USD {sandp:,.2f}")
btc = total_btc * btc.loc[final_date]["BTC-USD"]
print(f"BTC investment: USD {btc:,.2f}")
Should have bought Bitcoin back in 2010:
Gold investment: USD 17,433.58
S&P investment: USD 21,739.46
BTC investment: USD 529,701,861.66
Learning Scientific Programming with Python © 2025 Christian Hill
The content on this page is licensed under CC-BY 4.0 unless otherwise stated.