def _get(cls, df, positions, initial_capital): target = 'Adj Close' # Initialize the portfolio with value owned ret = positions.multiply(df[target], axis=0) # Store the difference in shares owned pos_diff = positions.diff() ret['holdings'] = (positions.multiply(df[target], axis=0)).sum(axis=1) ret['cash'] = initial_capital - (pos_diff.multiply( df[target], axis=0)).sum(axis=1).cumsum() ret['total'] = ret['cash'] + ret['holdings'] ret['returns'] = FINANCE.getDailyReturns(ret['total']) return ret
def by_quarter(cls, data): year_to_year = {} for month, year, dataSeries in cls.group_by_month(data): quarter = 1 if month in [4, 5, 6]: quarter = 2 elif month in [7, 8, 9]: quarter = 3 elif month in [10, 11, 12]: quarter = 4 percent = FINANCE.getDailyReturns(dataSeries) percent = percent.fillna(0) if len(percent) == 0: continue if quarter not in year_to_year: year_to_year[quarter] = pd.DataFrame(percent) else: init = len(year_to_year[quarter]) year_to_year[quarter] = year_to_year[quarter].append(percent) if init == len(year_to_year[quarter]): raise ValueError("dataframe did not append") return year_to_year