Sep-24-2022, 11:11 AM
If you want the percent change for each day I would do this:
import pandas_datareader as web import pandas as pd #Enter the stocks you want to use as variables portfolio = { "BRK-B":0.2, "AMZN":0.3, "AAPL":0.1, "NFLX":0.2, "GOOG":0.2, } #select start date for correlation window as well as list of tickers def percent_change(portfolio, start, end): pct_change = {} for ticker in portfolio: stock = web.get_data_yahoo(ticker, start=start, end=end) pct_change[ticker] = stock['Adj Close'].pct_change()[1:] return pd.DataFrame(pct_change) pct_change = percent_change(portfolio, "2013-01-01", "2013-01-10") weighted_change = pct_change * portfolio.values() weighted_change['Total'] = weighted_change.sum(axis = 1) countsum = [] cs = 100 for total in weighted_change["Total"]: cs *= (1 + total) countsum.append(cs) weighted_change["Countsum"] = countsum print(weighted_change)
Output: BRK-B AMZN AAPL NFLX GOOG Total Countsum
Date
2013-01-03 0.000901 0.001364 -0.001262 0.009955 0.000116 0.011075 101.107477
2013-01-04 0.000491 0.000778 -0.002785 -0.001263 0.003952 0.001173 101.226027
2013-01-07 -0.000852 0.010778 -0.000588 0.006710 -0.000873 0.015174 102.762025
2013-01-08 0.000770 -0.002324 0.000269 -0.004113 -0.000395 -0.005792 102.166791
2013-01-09 -0.001045 -0.000034 -0.001563 -0.002573 0.001315 -0.003900 101.768360
2013-01-10 0.002850 -0.001138 0.001240 0.004358 0.000910 0.008221 102.605005