import numpy as np import pandas as pd import pandas_datareader.data as web import matplotlib.pyplot as plt #list of stocks in profilo stocks=['BTC-USD','SPY'] #download daily price data for each of the stocks in the portfolio data = web.DataReader(stocks,data_source='yahoo',start='07/16/2010')['Adj Close'] data.sort_index(inplace=True) #convert daily stock prices into daily returns returns = data.pct_change() #calculate mean daily return and covariance of daily returns mean_daily_returns = returns.mean() cov_matrix = returns.cov() #set number of runs of random portfolio weights num_portfolios = 25000 #set up array to hold results #We have increased the size of the array to hold the weight values for each stock results = np.zeros((4+len(stocks)-1,num_portfolios)) for i in range(num_portfolios): #select random weights for portfolio holdings weights = np.array(np.random.random(2)) #rebalance weights to sum to 1 weights /= np.sum(weights) #calculate portfolio return and volatility portfolio_return = np.sum(mean_daily_returns * weights) * XXXXXXXXXXportfolio_std_dev = np.sqrt(np.dot(weights.T,np.dot(cov_matrix, weights))) * np.sqrt(252) #store results in results array results[0,i] = portfolio_return results[1,i] = portfolio_std_dev #store Sharpe Ratio (return / volatility) - risk free rate element excluded for simplicity results[2,i] = results[0,i] / results[1,i] #iterate through the weight vector and add data to results array for j in range(len(weights)): XXXXXXXXXXresults[j+3,i] = weights[j] #convert results array to Pandas DataFrame results_frame = pd.DataFrame(results.T,columns=['ret','stdev','sharpe',stocks[0],stocks[1]]) #locate position of portfolio with highest Sharpe Ratio max_sharpe_port = results_frame.iloc[results_frame['sharpe'].idxmax()] #locate positon of portfolio with minimum standard deviation min_vol_port = results_frame.iloc[results_frame['stdev'].idxmin()] #create scatter plot coloured by Sharpe Ratio plt.scatter(results_frame.stdev,results_frame.ret,c=results_frame.sharpe,cmap='RdYlBu') plt.xlabel('Volatility') plt.ylabel('Returns') plt.colorbar() #plot red star to highlight position of portfolio with highest Sharpe Ratio plt.scatter(max_sharpe_port[1],max_sharpe_port[0],marker=(5,1,0),color='r',s=1000) #plot green star to highlight position of minimum variance portfolio plt.scatter(min_vol_port[1],min_vol_port[0],marker=(5,1,0),color='g',s=1000)
-------------------------------------------------------- XXXXXXXXXXValueError XXXXXXXXXXTraceback (most recent call last) in XXXXXXXXXXstocks=['BTC-USD','SPY'] XXXXXXXXXX #download daily price data for each of the stocks in the portfolio ---> 10 data = web.DataReader(stocks,data_source='yahoo',start='07/16/2010')['Adj Close'] XXXXXXXXXXdata.sort_index(inplace=True) ~\Anaconda3\lib\site-packages\pandas_datareader\data.py in DataReader(name, data_source, start, end, retry_count, pause, session, access_key XXXXXXXXXX XXXXXXXXXXadjust_price=False, chunksize=25, XXXXXXXXXX XXXXXXXXXXretry_count=retry_count, pause=pause, --> 310 XXXXXXXXXXsession=session).read XXXXXXXXXX elif data_source == "google": ~\Anaconda3\lib\site-packages\pandas_datareader\base.py in read(self XXXXXXXXXX XXXXXXXXXXdf = self._dl_mult_symbols(self.symbols.index XXXXXXXXXXelse: --> XXXXXXXXXXdf = self._dl_mult_symbols(self.symbols XXXXXXXXXXreturn df XXXXXXXXXX ~\Anaconda3\lib\site-packages\pandas_datareader\base.py in _dl_mult_symbols(self, symbols XXXXXXXXXX XXXXXXXXXXfor sym in failed: XXXXXXXXXX XXXXXXXXXXstocks[sym] = df_na --> XXXXXXXXXXresult = concat(stocks).unstack(level= XXXXXXXXXX XXXXXXXXXXresult.columns.names = ['Attributes', 'Symbols'] XXXXXXXXXX XXXXXXXXXXreturn result ~\Anaconda3\lib\site-packages\pandas\core\frame.py in unstack(self, level, fill_value XXXXXXXXXX """ XXXXXXXXXXfrom pandas.core.reshape.reshape import unstack -> XXXXXXXXXXreturn unstack(self, level, fill_value XXXXXXXXXX _shared_docs['melt'] = (""" ~\Anaconda3\lib\site-packages\pandas\core\reshape\reshape.py in unstack(obj, level, fill_value XXXXXXXXXXif isinstance(obj, DataFrame): XXXXXXXXXXif isinstance(obj.index, MultiIndex): --> XXXXXXXXXXreturn _unstack_frame(obj, level, fill_value=fill_value XXXXXXXXXXelse: XXXXXXXXXX XXXXXXXXXXreturn obj.T.stack(dropna=False) ~\Anaconda3\lib\site-packages\pandas\core\reshape\reshape.py in _unstack_frame(obj, level, fill_value XXXXXXXXXX XXXXXXXXXXvalue_columns=obj.columns, XXXXXXXXXX XXXXXXXXXXfill_value=fill_value, --> 515 XXXXXXXXXXconstructor=obj._constructor XXXXXXXXXXreturn unstacker.get_result XXXXXXXXXX ~\Anaconda3\lib\site-packages\pandas\core\reshape\reshape.py in __init__(self, values, index, level, value_columns, fill_value, constructor XXXXXXXXXX XXXXXXXXXXself._make_sorted_values_labels() --> XXXXXXXXXXself._make_selectors XXXXXXXXXX def _make_sorted_values_labels(self): ~\Anaconda3\lib\site-packages\pandas\core\reshape\reshape.py in _make_selectors(self XXXXXXXXXX XXXXXXXXXXif mask.sum() len(self.index): --> XXXXXXXXXXraise ValueError('Index contains duplicate entries, ' XXXXXXXXXX 'cannot reshape' XXXXXXXXXXValueError: Index contains duplicate entries, cannot reshape