Exercise

Cumulative return on $1,000 invested in google vs apple II

Apple outperformed Google over the entire period, but this may have been different over various 1-year sub periods, so that switching between the two stocks might have yielded an even better result.

To analyze this, calculate that cumulative return for rolling 1-year periods, and then plot the returns to see when each stock was superior.

Instructions

100 XP

We have already imported pandas as pd and matplotlib.pyplot as plt. We have also loaded the GOOG and AAPL close prices from the last exercise into data.

  • Define a multi_period_return() function that returns the cumulative return from an array of period returns.
  • Calculate daily_returns by applying .pct_change() to data.
  • Create a '360D' .rolling() window on daily_returns, and .apply() multi_period_returns. Assign the result to rolling_annual_returns.
  • Plot rolling_annual_returns after multiplying it by 100.