Calculate the daily percentage change of both the stocks
df_change = df.pct_change()
Drop the first row containing the null values
df_change.dropna(inplace=True)
Calculate the cumulative percentage returns of each stock
df_cumulative_returns = (df_change+1).cumprod()*100
Save the number of stocks in a variable
no_of_stocks = df_change.shape[1]
no_of_stocks
weights = cp.Variable(no_of_stocks)
weights.shape
Save the portfolio returns in a variable
portfolio_returns = (np.array(df_change)@weights)
final_portfolio_value = cp.sum(cp.log(1+portfolio_returns))
objective = cp.Maximize(final_portfolio_value)
constraints = [0.0 <= weights, cp.sum(weights) == 1]
problem = cp.Problem(objective, constraints)
The optimal objective value is returned by prob.solve()
.
problem.solve()
The optimal value for w is stored in w.value
.
print(weights.value)
Whats the rational for this line?
Calculate the cumulative percentage returns of each stock
df_cumulative_returns = (df_change+1).cumprod()*100