def plot_performance(dt_start, dt_end, arr_stock_symbols, nd_weights): ''''Plot the performance of each individual equity together with the portfolio.''' dic_df = dic_df_market_data(dt_start, dt_end, arr_stock_symbols) nd_prices = dic_df["close"].values nd_prices_normalized = nd_prices / nd_prices[0, :] nd_weights.shape = (1, nd_prices_normalized.shape[1]) nd_prices_weighted = nd_weights * nd_prices_normalized nd_portfolio_value = nd_prices_weighted.sum(axis=1) nd_portfolio_value = nd_portfolio_value / nd_portfolio_value[0] arr_stock_symbols.append("portfolio") dt_timeofday = dt.timedelta(hours=16) ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday) plt.clf() plt.xticks(rotation=70) plt.plot(ldt_timestamps, nd_prices_normalized) plt.plot(ldt_timestamps, nd_portfolio_value) plt.legend(arr_stock_symbols) plt.ylabel('Normalized Close') plt.xlabel('Date') plt.show()
def optimizer(date_start, date_end, arr_stock_symbols, weight_increment): '''Returns an optimal equity allocation over the equities in arr_stock_symbols w.r.t. the sharpe ratio.''' dic_df = dic_df_market_data(date_start, date_end, arr_stock_symbols) nd_prices = dic_df["close"].values nd_prices_normalized = nd_prices / nd_prices[0, :] # Get best allocation w.r.t. the shape ratio num_equities = len(arr_stock_symbols) best_metrics = [0 for i in xrange(num_equities)] best_sharpe = 0 best_allocation = [0 for i in xrange(num_equities)] generator_allocations = gen_possible_allocations(num_equities, weight_increment) for nd_allocation in generator_allocations: nd_allocation.shape = (1, nd_prices_normalized.shape[1]) nd_prices_weighted = nd_allocation * nd_prices_normalized nd_portfolio_value = nd_prices_weighted.sum(axis=1) portfolio_metrics = arr_portfolio_metrics(nd_portfolio_value) curr_sharpe = portfolio_metrics[-1] if curr_sharpe > best_sharpe: best_metrics = portfolio_metrics best_sharpe = curr_sharpe best_allocation = nd_allocation return (best_allocation, best_metrics)
def write_event_study_to_disk(date_start, date_end, arr_equity_symbols, str_market_symbol, pdf_to_disk_name): '''Writes a plot onto disk illustrating the market relative mean return for occuring events in between the dates date_start and date_end for all equities in arr_equity_symbols relative to the market str_market_symbol.''' arr_equity_symbols.append(str_market_symbol) dic_df = dic_df_market_data(date_start, date_end, arr_equity_symbols) eventHandler = EventHandler(dic_df, str_market_symbol, date_start, date_end) eventHandler.assign_events() df_events = eventHandler.get_df_events() #df_events = get_event_df(dic_df, str_market_symbol, date_start, date_end) print "Creating Study" ep.eventprofiler(df_events, dic_df, i_lookback=20, i_lookforward=20, s_filename=pdf_to_disk_name, b_market_neutral=True, b_errorbars=True, s_market_sym=str_market_symbol)