I have the price history for Google stock in a Google Colab doc, like this:
df = pd.DataReader('GOOG', data_source='yahoo', start='08-01-2004')
These are open, high, low, close and adjusted close prices for each trading day in the price history. I can create a new column in the DataFrame for the rate of return of the stock over the trailing 12 months like this:
df['Trailing 12 month return'] = (df['Adj Close'] -
df['Adj Close'].shift(DAYS_TRADING_PER_YEAR)) /
df['Adj Close'].shift(DAYS_TRADING_PER_YEAR)
But what if what I actually want is one value for rate of the return per year, looking at the return over the previous calendar year? So, for 2015, just find the first trading day (more correctly, the first day for which we have data) in 2014 and the last trading day in 2014 and get the percentage change over that period?