Import Raw Packages
I. Import Data
API and DateTime package
Daily returns
df[pct_daily_return] = (1 + df[closing_price]).pct_change(1)
df[pct_daily_return] = (df[closing_price] / df[closing_price].shift(1) ) - 1
b. Daily cumulative return
The formulae for a daily cumulative return is the following:
$ ii = (1+r_t) * i{t-1} $
We can notice that we are only multiplying our previous investment i at t-1 by 1 + our percentage return. Pandas simplify the way to calculate with its cumprod () method. Using the following command:
df[daily_cumulative_return] = (1 + df[pct_daily_return]).cumprod()