def generate_optimized_portfolio(symbols, start_date, end_date, investment, rerun=False): # get new optimized portfolio and get its value for the date tickers = get_tickers_for_symbols(symbols, start_date, end_date) weights = [] for symbol in symbols: weights.append(1 / float(len(symbols))) portfolio = Portfolio(tickers, weights, start_date, end_date, investment) optimized_portfolio = optimize_portfolio(portfolio) # only keep symbols with 2% or higher weight and only run it once to make sure we dont create an inifinite loop if not rerun: # create dict of weights and symbols symb_weight_dict = dict( zip(optimized_portfolio.symbols, optimized_portfolio.weights)) symbols_to_keep = [] for item in symb_weight_dict: if symb_weight_dict[item] > 0.02: symbols_to_keep.append(item) else: pass if len(symbols_to_keep) < len(symbols): print 'running optimizer one more time' optimized_portfolio = generate_optimized_portfolio( symbols_to_keep, start_date, end_date, investment, True) return optimized_portfolio
def get_portfolio(symbols, weights, start_date, end_date, investment, no_spy=True): prices_df, prices_df_with_spy = get_prices(symbols, start_date, end_date, no_spy) tickers = get_tickers_for_symbols(symbols, start_date, end_date, no_spy) portfolio = Portfolio(tickers, weights, start_date, end_date, investment) return portfolio
def optimize_portfolio(portfolio): symbol_alloc, is_reduced = get_top_optimal_symbols(portfolio.ticker_prices) if is_reduced: symbols_used = symbol_alloc.index.values symbol_alloc, is_reduced = get_top_optimal_symbols( portfolio.ticker_prices) symbols_used = symbol_alloc.index.values.tolist() allocations_used = symbol_alloc['Allocations'].values.tolist() tickers = get_tickers_for_symbols(symbols_used, portfolio.start_date, portfolio.end_date) return Portfolio(tickers, allocations_used, portfolio.start_date, portfolio.end_date, portfolio.investment)
def run(): start_date = '2016-12-23' end_date = '2017-01-05' investment = 100000 # $100,000.00 as starting investment symbols = ['PGR', 'CCI', 'STZ', 'WYNN', 'DPS'] print 'Data for ' + symbols[0], get_data_for_symbol(symbols[0]).head() weights = [0.2, 0.2, 0.2, 0.2, 0.2] optimize = False # analyse_portfolio(symbols, weights, start_date, end_date, investment, optimize) # print calculated features and label prices_df, prices_df_with_spy = get_prices([symbols[0]], start_date, end_date) ticker_analysed = TickerAnalysed(symbol=symbols[0], data_df=prices_df[[symbols[0]]]) print 'Stats for: ', ticker_analysed.symbol # Original data store_ticker_analysis(ticker_analysed.df, ticker_analysed.symbol + '_original') ticker_analysed.print_df() # after filling missing data store_ticker_analysis(ticker_analysed.get_features_df(), ticker_analysed.symbol + '_fill') print ticker_analysed.get_features_df() tickers = get_tickers_for_symbols(symbols, start_date, end_date) portfolio = Portfolio(tickers, weights, start_date, end_date, investment) print 'Before Optimization' print '---------------------------' print 'Comparing portfoltio to S&P' print '---------------------------' compare_to_SP(portfolio) optimized_portfolio = optimize_portfolio(portfolio) print '---------------------------' print 'After Optimization: ' print '---------------------------' optimized_portfolio.describe() print '---------------------------' print 'Comparing portfoltio to S&P' print '---------------------------' compare_to_SP(optimized_portfolio)
def display_ticker(symbol, start_date, end_date): tickers = get_tickers_for_symbols([symbol], start_date, end_date) ticker = tickers[0] plot_data(ticker.df, symbol + ' Adj Close Price')