Kelly Optimumum Leverages for Multiple Stock Porfolio
First, import libraries:
import pandas_datareader.data as pdr
import pandas as pd
import numpy as np
Get at least 3 different historical stock prices:
aapl = pdr.DataReader('AAPL', 'yahoo', start='2018-07-12', end='2022-06-30')
tsla = pdr.DataReader('TSLA', 'yahoo', start='2018-07-12', end='2022-06-30')
amzn = pdr.DataReader('AMZN', 'yahoo', start='2018-07-12', end='2022-06-30')
Combine prices into a single dataframe:
prices = pd.concat([aapl.Close, tsla.Close, amzn.Close],axis=1)
prices.columns = ['AAPL','TSLA','AMZN']
Calculate neccecary metrics:
dly_rets = prices.pct_change()
riskfree_r = 0.04
excess_ret = dly_rets - (riskfree_r / 252)
avg_excess_rets = excess_ret.mean() * 252
cov_ret = excess_ret.cov() * 252
Calculate Kelly and other metrics:
kelly = np.linalg.inv(cov_ret) @ avg_excess_rets
cagr_max = (np.dot(kelly.T, np.dot(cov_ret, kelly)) / 2) + riskfree_r
sharpe = np.sqrt(np.dot(kelly.T, np.dot(cov_ret,kelly)))
The result looks like this:
Kelly Optimum Leverages: [2.6, 1.6, -2.5] Maximum Compouned Annual Growth Rate: 0.97 Sharpe Ratio: 1.36