def getFrontier(na_data): '''Function gets a 100 sample point frontier for given returns''' # Special Case with fTarget=None, just returns average rets. (na_avgrets, na_std, b_error) = tsu.OptPort(na_data, None) # Declaring bounds on the optimized portfolio na_lower = np.zeros(na_data.shape[1]) na_upper = np.ones(na_data.shape[1]) # Getting the range of possible returns with these bounds (f_min, f_max) = tsu.getRetRange(na_data, na_lower, na_upper, na_avgrets, s_type="long") # Getting the step size and list of returns to optimize for. f_step = (f_max - f_min) / 100.0 lf_returns = [f_min + x * f_step for x in range(101)] # Declaring empty lists lf_std = [] lna_portfolios = [] # Calling the optimization for all returns for f_target in lf_returns: (na_weights, f_std, b_error) = tsu.OptPort(na_data, f_target, na_lower, na_upper, s_type="long") lf_std.append(f_std) lna_portfolios.append(na_weights) return (lf_returns, lf_std, lna_portfolios, na_avgrets, na_std)
def getFrontier(startdate, enddate, ls_symbols, ref_symbol, ls_names = [], filename = "EquitiesvFrontier.pdf", target_return = 0.015): ''' @param ls_symbols candidates equities @param ls_names candidates names @parem ref_symbol reference ''' # Get the portfolio and reference data from yahoo ldf_data = web.DataReader(ls_symbols, 'yahoo', start=startdate, end=enddate) ref_data = web.DataReader(ref_symbol, 'yahoo', start=startdate, end=enddate) # Clean the NaN of the data key_source = 'Adj Close' for skey in ['Volume', key_source]: '''First forward fill then backward fill''' ldf_data[skey] = ldf_data[skey].fillna(method='ffill') ldf_data[skey] = ldf_data[skey].fillna(method='bfill') ldf_data[skey] = ldf_data[skey].fillna(1.0) ref_data[skey] = ref_data[skey].fillna(method='ffill') ref_data[skey] = ref_data[skey].fillna(method='bfill') ref_data[skey] = ref_data[skey].fillna(1.0) # Get the adjusted close price and the index of the data ls_price = ldf_data[key_source].values ls_date = ldf_data[key_source].index # Get the reference price ref_price = ref_data[key_source].values ref_date = ref_data[key_source].index # Normalizing the prices of the equity candidates and reference ls_normalized_price = ls_price / ls_price[0, :] ref_normalized_price = ref_price / ref_price[0] ## Optimize the efficient frontier na_data = ls_normalized_price.copy() tsu.returnize0(na_data) # Special Case with fTarget=None, just returns average rets. (na_avgrets, na_std, b_error) = tsu.OptPort(na_data, None) # Declaring bounds on the optimized portfolio na_lower = np.zeros(na_data.shape[1]) na_upper = np.ones(na_data.shape[1]) # Getting the range of possible returns with these bounds (f_min, f_max) = tsu.getRetRange(na_data, na_lower, na_upper, na_avgrets, s_type="long") # Getting the step size and list of returns to optimize for. f_step = (f_max - f_min) / 100.0 lf_returns = [f_min + x * f_step for x in range(101)] # Declaring empty lists lf_std = [] lna_portfolios = [] # Calling the optimization for all returns for f_target in lf_returns: (na_weights, f_std, b_error) = \ tsu.OptPort(na_data, f_target, na_lower, na_upper, s_type="long") lf_std.append(f_std) lna_portfolios.append(na_weights) f_target = target_return (na_weights, f_std, b_error) = \ tsu.OptPort(na_data, f_target, na_lower, na_upper, s_type="long") print 'Optimized portfolio for target return', f_target print 'Volatility is ', f_std for i in range(len(na_weights)): if abs(na_weights[i]) > 0.00001: print ls_names[i], ':', na_weights[i] plt.clf() fig = plt.figure(figsize=(8, 10), dpi=100) # Plot indivisual stock risk/return as green + for i in range(len(ls_symbols)): # plt.plot(na_std[i], f_ret, 'g+') # plt.text(na_std[i], f_ret, ls_names[i], fontsize = 10) ave = np.average(na_data[:,i]) std = np.std(na_data[:,i]) plt.plot(std, ave, 'g+') plt.text(std, ave, ls_names[i], fontsize = 5) ref_data = ref_normalized_price.copy() tsu.returnize0(ref_data) ave = np.average(ref_data) std = np.std(ref_data) plt.plot(std, ave, 'r+') plt.text(std, ave, 'CAC 40', fontsize = 6) plt.plot(lf_std, lf_returns, 'b') plt.title('Efficient Frontier For CAC 40') # plt.legend(['2013 Frontier'], loc = 'lower left') plt.ylabel('Expected Return') plt.xlabel('StDev') plt.savefig(filename, format='pdf') return na_weights
def get_frontier(basic_portfolio, ref_symbol, filename="EquitiesvFrontier.pdf", target_return=0.015): """ @param basic_portfolio @param ref_symbol reference symbol """ assert isinstance(basic_portfolio, BasicPortfolio) stock_close_price = basic_portfolio.get_stock_close_prices() stock_normalized_price = stock_close_price.values / stock_close_price.values[0, :] ref_close_price = load_stock_close_price(basic_portfolio.start_date, basic_portfolio.end_date, [ref_symbol]) ref_normalized_price = ref_close_price.values / ref_close_price.values[0, :] daily_return0 = get_daily_return0(stock_normalized_price) (na_avgrets, na_std, b_error) = tsu.OptPort(daily_return0, None) # Declaring bounds on the optimized portfolio na_lower = np.zeros(daily_return0.shape[1]) na_upper = np.ones(daily_return0.shape[1]) # Getting the range of possible returns with these bounds (f_min, f_max) = tsu.getRetRange(daily_return0, na_lower, na_upper, na_avgrets, s_type="long") # Getting the step size and list of returns to optimize for. f_step = (f_max - f_min) / 100.0 lf_returns = [f_min + x * f_step for x in range(101)] # Declaring empty lists lf_std = [] lna_portfolios = [] # Calling the optimization for all returns for f_target in lf_returns: (na_weights, f_std, b_error) = \ tsu.OptPort(daily_return0, f_target, na_lower, na_upper, s_type="long") lf_std.append(f_std) lna_portfolios.append(na_weights) f_target = target_return (na_weights, f_std, b_error) = \ tsu.OptPort(daily_return0, f_target, na_lower, na_upper, s_type="long") print 'Optimized portfolio for target return', f_target print 'Volatility is ', f_std for ticker_name, weight in zip(basic_portfolio.ticker_names, na_weights): if weight > 0.00001: print ticker_name, ':', weight plt.clf() plt.figure(figsize=(8, 10), dpi=100) # Plot individual stock risk/return as green + for i in range(len(basic_portfolio.ticker_names)): # plt.plot(na_std[i], f_ret, 'g+') # plt.text(na_std[i], f_ret, ls_names[i], fontsize = 10) ave = np.average(daily_return0[:, i]) std = np.std(daily_return0[:, i]) plt.plot(std, ave, 'g+') plt.text(std, ave, basic_portfolio.ticker_names[i], fontsize=5) ref_daily_return = get_daily_return0(ref_normalized_price) ave = np.average(ref_daily_return) std = np.std(ref_daily_return) plt.plot(std, ave, 'r+') plt.text(std, ave, 'CAC 40', fontsize=6) plt.plot(lf_std, lf_returns, 'b') plt.title('Efficient Frontier For CAC 40') # plt.legend(['2013 Frontier'], loc = 'lower left') plt.ylabel('Expected Return') plt.xlabel('StDev') if filename is None: plt.show() else: plt.savefig(filename, format='pdf') return na_weights
def get_frontier(basic_portfolio, ref_symbol, filename="EquitiesvFrontier.pdf", target_return=0.015): """ @param basic_portfolio @param ref_symbol reference symbol """ assert isinstance(basic_portfolio, BasicPortfolio) stock_close_price = basic_portfolio.get_stock_close_prices() stock_normalized_price = stock_close_price.values / stock_close_price.values[ 0, :] ref_close_price = load_stock_close_price(basic_portfolio.start_date, basic_portfolio.end_date, [ref_symbol]) ref_normalized_price = ref_close_price.values / ref_close_price.values[ 0, :] daily_return0 = get_daily_return0(stock_normalized_price) (na_avgrets, na_std, b_error) = tsu.OptPort(daily_return0, None) # Declaring bounds on the optimized portfolio na_lower = np.zeros(daily_return0.shape[1]) na_upper = np.ones(daily_return0.shape[1]) # Getting the range of possible returns with these bounds (f_min, f_max) = tsu.getRetRange(daily_return0, na_lower, na_upper, na_avgrets, s_type="long") # Getting the step size and list of returns to optimize for. f_step = (f_max - f_min) / 100.0 lf_returns = [f_min + x * f_step for x in range(101)] # Declaring empty lists lf_std = [] lna_portfolios = [] # Calling the optimization for all returns for f_target in lf_returns: (na_weights, f_std, b_error) = \ tsu.OptPort(daily_return0, f_target, na_lower, na_upper, s_type="long") lf_std.append(f_std) lna_portfolios.append(na_weights) f_target = target_return (na_weights, f_std, b_error) = \ tsu.OptPort(daily_return0, f_target, na_lower, na_upper, s_type="long") print 'Optimized portfolio for target return', f_target print 'Volatility is ', f_std for ticker_name, weight in zip(basic_portfolio.ticker_names, na_weights): if weight > 0.00001: print ticker_name, ':', weight plt.clf() plt.figure(figsize=(8, 10), dpi=100) # Plot individual stock risk/return as green + for i in range(len(basic_portfolio.ticker_names)): # plt.plot(na_std[i], f_ret, 'g+') # plt.text(na_std[i], f_ret, ls_names[i], fontsize = 10) ave = np.average(daily_return0[:, i]) std = np.std(daily_return0[:, i]) plt.plot(std, ave, 'g+') plt.text(std, ave, basic_portfolio.ticker_names[i], fontsize=5) ref_daily_return = get_daily_return0(ref_normalized_price) ave = np.average(ref_daily_return) std = np.std(ref_daily_return) plt.plot(std, ave, 'r+') plt.text(std, ave, 'CAC 40', fontsize=6) plt.plot(lf_std, lf_returns, 'b') plt.title('Efficient Frontier For CAC 40') # plt.legend(['2013 Frontier'], loc = 'lower left') plt.ylabel('Expected Return') plt.xlabel('StDev') if filename is None: plt.show() else: plt.savefig(filename, format='pdf') return na_weights
def PortfolioOptimizer(startdate, enddate, ls_symbols, ref_symbol, filename = "portfoliovCAC40.pdf", ls_names = []): ''' @param ls_symbols candidates equities @parem ref_symbol reference @return alloc allocation of equities ''' # Get the portfolio and reference data from yahoo ldf_data = web.DataReader(ls_symbols, 'yahoo', start=startdate, end=enddate) ref_data = web.DataReader(ref_symbol, 'yahoo', start=startdate, end=enddate) # Clean the NaN of the data key_source = 'Adj Close' for skey in ['Volume', key_source]: '''First forward fill then backward fill''' ldf_data[skey] = ldf_data[skey].fillna(method='ffill') ldf_data[skey] = ldf_data[skey].fillna(method='bfill') ldf_data[skey] = ldf_data[skey].fillna(1.0) ref_data[skey] = ref_data[skey].fillna(method='ffill') ref_data[skey] = ref_data[skey].fillna(method='bfill') ref_data[skey] = ref_data[skey].fillna(1.0) # Get the adjusted close price and the index of the data ls_price = ldf_data[key_source].values ls_date = ldf_data[key_source].index # Get the reference price ref_price = ref_data[key_source].values ref_date = ref_data[key_source].index # Normalizing the prices of the equity candidates and reference ls_normalized_price = ls_price / ls_price[0, :] ref_normalized_price = ref_price / ref_price[0] na_data = ls_normalized_price.copy() tsu.returnize0(na_data) # Special Case with fTarget=None, just returns average rets. (na_avgrets, na_std, b_error) = tsu.OptPort(na_data, None) # Declaring bounds on the optimized portfolio na_lower = np.zeros(na_data.shape[1]) na_upper = np.ones(na_data.shape[1]) # Getting the range of possible returns with these bounds (f_min, f_max) = tsu.getRetRange(na_data, na_lower, na_upper, na_avgrets, s_type="long") # Getting the step size and list of returns to optimize for. f_step = (f_max - f_min) / 100.0 lf_returns = [f_min + x * f_step for x in range(101)] # Declaring empty lists lf_std = [] lna_portfolios = [] # Calling the optimization for all returns for f_target in lf_returns: (na_weights, f_std, b_error) = \ tsu.OptPort(na_data, f_target, na_lower, na_upper, s_type="long") lf_std.append(f_std) lna_portfolios.append(na_weights) plt.clf() plt.plot(lf_std, lf_returns, 'b') # Plot indivisual stock risk/return as green + for i, f_ret in enumerate(na_avgrets): plt.plot(na_std[i], f_ret, 'g+') plt.text(na_std[i], f_ret, ls_names[i]) plt.title('Efficient Frontier For CAC 40') plt.legend(['2013 Frontier'], loc = 'lower left') plt.ylabel('Expected Return') plt.xlabel('StDev') plt.savefig('Frontier2013.pdf', format='pdf') '''
continue # Remove SPY index to avoid bias tickers.remove('SPY') # Use data from 2010 to 2012 to find optimal portfolios Adj_Close_Train = Adj_Close_2010_2012[tickers] opt_data = Adj_Close_Train.values.copy() #Create NumPy array tsu.returnize0(opt_data) # Normalize with respect to first value # Get average returns, upper and lower bounds (opt_avgrets, opt_std, b_error) = tsu.OptPort(opt_data, None) opt_lower = np.zeros(opt_data.shape[1]) opt_upper = np.ones(opt_data.shape[1]) (f_min, f_max) = tsu.getRetRange(opt_data, opt_lower, opt_upper,opt_avgrets, s_type="long") #%% Create optimal portfolios based on target return for all periods k = [0.5,0.6,0.7,0.8,0.9,1.0] portfolios = [] portfolios_std = [] optimal_tickers = [] for i in range(len(k)): target_return = k[i] * f_max (opt_weights, f_std, b_error) = tsu.OptPort(opt_data, target_return,opt_lower, opt_upper, s_type="long") opt_weights[opt_weights < 0.001] = 0.0 # Remove any tiny values portfolios.append(opt_weights) portfolios_std.append(f_std) t = list(np.array(opt_weights.nonzero()[0],dtype=int)) # Indexes of nonzero tickers for j in t: optimal_tickers.append(tickers[j]) #