Financial Data Science Financial Performance Analysis: Difference between revisions
Jump to navigation
Jump to search
Line 10: | Line 10: | ||
import matplotlib.ticker as ptick | import matplotlib.ticker as ptick | ||
from fredapi import Fred | from fredapi import Fred | ||
fred_api_key = '...' | |||
start_date = '2023-07-10' | |||
# load the DataFrame | # load the DataFrame | ||
Line 25: | Line 27: | ||
# extract a specific time series ('Fidelity Self', 'Fidelity Managed', etc.) | # extract a specific time series ('Fidelity Self', 'Fidelity Managed', etc.) | ||
ts = df['Fidelity Self'].apply(dollar_to_int) | |||
# resample and interpolate | # resample and interpolate | ||
ts = ts.resample('D').interpolate() | |||
# get the SP&500 | # get the SP&500 | ||
fred = Fred(api_key= | fred = Fred(api_key=fred_api_key) | ||
sp500 = fred.get_series(series_id="SP500") | sp500 = fred.get_series(series_id="SP500") | ||
sp500 = sp500.resample('D').interpolate() | sp500 = sp500.resample('D').interpolate() | ||
Line 38: | Line 39: | ||
# apply a window, normalize and compute the percentage difference | # apply a window, normalize and compute the percentage difference | ||
# | # | ||
ts = ts.loc[start_date:] | |||
sp500 = sp500.loc[start_date:] | sp500 = sp500.loc[start_date:] | ||
sp500_perf = sp500.apply(lambda x: x * | sp500_perf = sp500.apply(lambda x: x * ts.loc[start_date] / sp500.loc[start_date]) | ||
sp500_perc_diff = | sp500_perc_diff = ts.sub(sp500_perf).div(sp500_perf).mul(100) | ||
# graph | # graph | ||
Line 53: | Line 53: | ||
ax.yaxis.set_major_formatter(ptick.FormatStrFormatter('% 1.0f')) | ax.yaxis.set_major_formatter(ptick.FormatStrFormatter('% 1.0f')) | ||
ax.plot(sp500_perf, lw=0.5, color='black') | ax.plot(sp500_perf, lw=0.5, color='black') | ||
ax.plot( | ax.plot(ts, lw=0.5, color='red') | ||
fig2, ax2 = plt.subplots() | fig2, ax2 = plt.subplots() |
Revision as of 04:46, 21 October 2023
Internal
Overview
import math
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ptick
from fredapi import Fred
fred_api_key = '...'
start_date = '2023-07-10'
# load the DataFrame
df = pd.read_csv("./finances.csv", parse_dates=["Date"])
# make it a time series DataFrame
df = df.set_index('Date')
# declare the function that converts the dollar amount
def dollar_to_int(s):
if isinstance(s, str):
return int(s[1:].replace(',',''))
elif math.isnan(s):
return s # we will interpolate later
# extract a specific time series ('Fidelity Self', 'Fidelity Managed', etc.)
ts = df['Fidelity Self'].apply(dollar_to_int)
# resample and interpolate
ts = ts.resample('D').interpolate()
# get the SP&500
fred = Fred(api_key=fred_api_key)
sp500 = fred.get_series(series_id="SP500")
sp500 = sp500.resample('D').interpolate()
#
# apply a window, normalize and compute the percentage difference
#
ts = ts.loc[start_date:]
sp500 = sp500.loc[start_date:]
sp500_perf = sp500.apply(lambda x: x * ts.loc[start_date] / sp500.loc[start_date])
sp500_perc_diff = ts.sub(sp500_perf).div(sp500_perf).mul(100)
# graph
plt.style.use('seaborn-v0_8-whitegrid')
fig, ax = plt.subplots()
fig.autofmt_xdate()
fig.set_figwidth(20)
ax.set_ylabel("amount")
ax.yaxis.set_major_formatter(ptick.FormatStrFormatter('% 1.0f'))
ax.plot(sp500_perf, lw=0.5, color='black')
ax.plot(ts, lw=0.5, color='red')
fig2, ax2 = plt.subplots()
fig2.autofmt_xdate()
fig2.set_figwidth(25)
ax2.set_ylabel("percentage")
ax2.yaxis.set_major_formatter(ptick.PercentFormatter())
ax2.plot(sp500_perc_diff, lw=0.5, color='indigo')
plt.show()