def performance_metric(models_plotdates, avg_gain, sort_mode='sharpe', number_months=24): model_metric = [] model_dates = [] for ii in range(number_months,len(avg_gain)): if models_plotdates[ii].month <= 12: performance_history = np.array(avg_gain[ii-number_months:ii]) if sort_mode == 'sharpe': computed_stats = allstats(performance_history).sharpe(periods_per_year=12.) elif sort_mode == 'sortino': computed_stats = (allstats(performance_history).sortino()) elif sort_mode == 'sharpe_plus_sortino': computed_stats = allstats(performance_history).sharpe(periods_per_year=12.) + allstats(performance_history).sortino() model_dates.append(models_plotdates[ii]) if np.isinf(computed_stats): computed_stats = -1. model_metric.append(computed_stats) return model_dates, model_metric
''' print(" ... dynamic system = ", str(idate), recent_comparative_nstocks[best_comparative_index], recent_comparative_method[best_comparative_index], format(cumu_dynamic_system[-1], '10,.0f')) persistence_sorting_choices.append(inum_sort_choice) persistence_number_stocks_choices.append(inum_stocks_choices) persistence_values.append(persistence_months) final_values.append(cumu_dynamic_system[-1]) persistence_series_values.append(cumu_dynamic_system) persistence_DD.append(avg_DD(np.array(cumu_dynamic_system),len(cumu_dynamic_system))) persistence_mid_DD.append(avg_DD(np.array(cumu_dynamic_system),int(12*10.75))) persistence_recent_DD.append(avg_DD(np.array(cumu_dynamic_system),int(12*4.75))) try: persistence_sortinos.append(allstats(np.array(cumu_dynamic_system)).sortino()) except: persistence_sortinos.append(0.) try: persistence_mid_sortinos.append(allstats(np.array(cumu_dynamic_system[-int(12*10.75):])).sortino()) except: persistence_mid_sortinos.append(0.) try: persistence_recent_sortinos.append(allstats(np.array(cumu_dynamic_system[-int(12*4.75):])).sortino()) except: persistence_recent_sortinos.append(0.) try: persistence_sharpes.append(allstats(np.array(cumu_dynamic_system)).monthly_sharpe()) except: persistence_sharpes.append(0.) try:
def ensemble_prediction(models_list, idate, datearray, adjClose, num_stocks, sort_mode='sharpe'): #-------------------------------------------------------------- # loop through best models and pick companies from ensemble prediction #-------------------------------------------------------------- ensemble_symbols = [] ensemble_Ytrain = [] ensemble_sharpe = [] ensemble_recent_sharpe = [] ensemble_equal = [] ensemble_rank = [] for iii,imodel in enumerate(models_list): # -------------------------------------------------- # build DL model # -------------------------------------------------- config_filename = os.path.join(models_folder, imodel).replace('.hdf','.txt') #print(" ... config_filename = ", config_filename) print(".", end='') model = build_model(config_filename, verbose=False) # collect meta data for weighting ensemble_symbols params = get_params(config_filename) #num_stocks = params['num_stocks'] num_periods_history = params['num_periods_history'] increments = params['increments'] symbols_predict = symbols Xpredict, Ypredict, dates_predict, companies_predict = generateExamples3layerForDate(idate, datearray, adjClose, num_periods_history, increments, output_incr='monthly', verbose=False) dates_predict = np.array(dates_predict) companies_predict = np.array(companies_predict) # -------------------------------------------------- # make predictions monthly for backtesting # - there might be some bias since entire preiod # has data used for training # -------------------------------------------------- weights_filename = os.path.join(models_folder, imodel) try: model.load_weights(weights_filename) except: pass # show predictions for (single) last date _Xtrain = Xpredict[dates_predict == idate] _Ytrain = Ypredict[dates_predict == idate][:,0] _dates = np.array(dates_predict[dates_predict == idate]) _companies = np.array(companies_predict[dates_predict == idate]) _forecast = model.predict(_Xtrain)[:, 0] _symbols = np.array(symbols_predict)[_companies] del model K.clear_session() forecast_indices = _forecast.argsort()[-num_stocks:] sorted_Xtrain = _Xtrain[forecast_indices,:,:,:] sorted_Ytrain = _Ytrain[forecast_indices] sorted_companies = _companies[forecast_indices] sorted_forecast = _forecast[forecast_indices] sorted_symbols = _symbols[forecast_indices] ##print("\n ... sorted_symbols = ",sorted_symbols[-num_stocks:]) # ensemble_sharpe_weights = np.ones(np.array(sorted_symbols[-num_stocks:]).shape, 'float') * params['_sharpe_ratio_system'] # ensemble_recent_sharpe_weights = np.ones_like(ensemble_sharpe_weights) * params['_sharpe_ratio_recent_system'] ensemble_sharpe_weights = np.ones(sorted_companies.shape, 'float') ensemble_recent_sharpe_weights = np.ones_like(ensemble_sharpe_weights) #print("sorted_Xtrain.shape = ",sorted_Xtrain.shape, " sorted_companies.shape = ", sorted_companies.shape) for icompany in range(sorted_companies.shape[0]): #print("sorted_Xtrain[icompany,:,2,0].shape, sharpe = ",sorted_Xtrain[icompany,:,2,0].shape,allstats((sorted_Xtrain[icompany,:,0,0]+1.).cumprod()).sharpe(periods_per_year=252./increments[2])) if sort_mode == 'sharpe': ensemble_sharpe_weights[icompany] = allstats((sorted_Xtrain[icompany,:,-1,0]+1.).cumprod()).sharpe(periods_per_year=252./increments[-1]) ensemble_recent_sharpe_weights[icompany] = allstats((sorted_Xtrain[icompany,:,int(sorted_Xtrain.shape[2]/2),0]+1.).cumprod()).sharpe(periods_per_year=252./increments[0]) elif sort_mode == 'sharpe_plus_sortino': ensemble_sharpe_weights[icompany] = allstats((sorted_Xtrain[icompany,:,-1,0]+1.).cumprod()).sharpe(periods_per_year=252./increments[-1]) + \ allstats((sorted_Xtrain[icompany,:,-1,0]+1.).cumprod()).sortino() ensemble_recent_sharpe_weights[icompany] = allstats((sorted_Xtrain[icompany,:,int(sorted_Xtrain.shape[2]/2),0]+1.).cumprod()).sharpe(periods_per_year=252./increments[0]) + \ allstats((sorted_Xtrain[icompany,:,int(sorted_Xtrain.shape[2]/2),0]+1.).cumprod()).sortino() elif sort_mode == 'sortino': ensemble_sharpe_weights[icompany] = allstats((sorted_Xtrain[icompany,:,-1,0]+1.).cumprod()).sortino() ensemble_recent_sharpe_weights[icompany] = allstats((sorted_Xtrain[icompany,:,int(sorted_Xtrain.shape[2]/2),0]+1.).cumprod()).sortino() ensemble_equal_weights = np.ones_like(ensemble_sharpe_weights) ensemble_rank_weights = np.arange(np.array(sorted_symbols[-num_stocks:]).shape[0])[::-1] ensemble_symbols.append(sorted_symbols[-num_stocks:]) ensemble_Ytrain.append(sorted_Ytrain[-num_stocks:]) ensemble_sharpe.append(ensemble_sharpe_weights) ensemble_recent_sharpe.append(ensemble_recent_sharpe_weights) ensemble_equal.append(ensemble_recent_sharpe_weights) ensemble_rank.append(ensemble_rank_weights) #print(imodel,sorted_symbols[-num_stocks:]) #print(" ... ",ensemble_sharpe_weights) # sift through ensemble symbols ensemble_symbols = np.array(ensemble_symbols).flatten() ensemble_Ytrain = np.array(ensemble_Ytrain).flatten() ensemble_sharpe = np.array(ensemble_sharpe).flatten() ensemble_recent_sharpe = np.array(ensemble_recent_sharpe).flatten() ensemble_equal = np.array(ensemble_equal).flatten() ensemble_rank = np.array(ensemble_rank).flatten() #unique_symbols = list(set(np.array(ensemble_symbols))) unique_symbols = list(set(list(np.array(ensemble_symbols).flatten()))) unique_ensemble_symbols = [] unique_ensemble_Ytrain = [] unique_ensemble_sharpe = [] unique_ensemble_recent_sharpe = [] unique_ensemble_equal = [] unique_ensemble_rank = [] for k, ksymbol in enumerate(unique_symbols): unique_ensemble_symbols.append(np.array(ensemble_symbols)[ensemble_symbols == ksymbol][0]) unique_ensemble_Ytrain.append(ensemble_Ytrain[ensemble_symbols == ksymbol].mean()) unique_ensemble_sharpe.append(ensemble_sharpe[ensemble_symbols == ksymbol].sum()) unique_ensemble_recent_sharpe.append(ensemble_recent_sharpe[ensemble_symbols == ksymbol].sum()) unique_ensemble_equal.append(ensemble_equal[ensemble_symbols == ksymbol].sum()) unique_ensemble_rank.append(ensemble_rank[ensemble_symbols == ksymbol].sum()) #print("unique_ensemble_sharpe = ", np.sort(unique_ensemble_sharpe)[-num_stocks:]) indices_recent = np.argsort(unique_ensemble_recent_sharpe)[-num_stocks:] #print("indices = ",indices) sorted_recent_sharpe = np.array(unique_ensemble_recent_sharpe)[indices_recent] sorted_recent_sharpe = np.array(sorted_recent_sharpe) unique_ensemble_sharpe = np.array(unique_ensemble_sharpe) + np.array(unique_ensemble_recent_sharpe) indices = np.argsort(unique_ensemble_sharpe)[-num_stocks:] #print("indices = ",indices) sorted_sharpe = np.array(unique_ensemble_sharpe)[indices] sorted_sharpe = np.array(sorted_sharpe) #print(" ... sorted_sharpe[sorted_sharpe < 0.].shape = ", sorted_sharpe[sorted_sharpe < 0.].shape, sorted_recent_sharpe[sorted_recent_sharpe < 0.].shape) sorted_symbols = np.array(unique_ensemble_symbols)[indices] sorted_Ytrain = np.array(unique_ensemble_Ytrain)[indices] #company_indices = [list(unique_ensemble_symbols).index(isymbol) for isymbol in sorted_symbols] ##print("sorted_symbols = ", sorted_symbols) ##print("sorted_Ytrain = ", sorted_Ytrain) #print("_symbols[company_indices] = ", _symbols[company_indices][-num_stocks:]) #print("_Ytrain[company_indices] = ", _Ytrain[company_indices][-num_stocks:]) try: _Ytrain = _Ytrain[dates_predict == idate] sorted_Ytrain = sorted_Ytrain[-num_stocks:] BH_gain = _Ytrain.mean() except: BH_gain = 0.0 avg_gain = sorted_Ytrain.mean() return avg_gain, BH_gain, sorted_symbols
for im, imodel in enumerate(models_list): try: avg_gain, _, _, models_plotdates = one_model_prediction(os.path.abspath(os.path.join(models_folder,imodel)), first_history_index, datearray, adjClose, symbols, inum_stocks) plt.plot(models_plotdates, avg_gain,label=str(im)) cumu_models.append(avg_gain) system_final_values.append(avg_gain[-1]) print(" ... "+str(im)+" of "+str(len(models_list))) model_sharpe = allstats(np.array(avg_gain)).sharpe(periods_per_year=12.) model_sortino = allstats(np.array(avg_gain)).sortino() sharpe_list.append(model_sharpe) sortino_list.append(model_sortino) print(" ... sharpe = ", model_sharpe) print(" ... sortino = ", model_sortino) model_dates, model_metric_sharpe = performance_metric(models_plotdates, avg_gain, sort_mode='sharpe', number_months=i_num_months) model_dates, model_metric_sortino = performance_metric(models_plotdates, avg_gain, sort_mode='sortino', number_months=i_num_months) print(" ... sharpe and sortino ranks are the same ",(np.argsort(model_metric_sharpe)==np.argsort(model_metric_sortino)).all() ) print(" ... sharpe min/mean/median/max = ", np.min(model_metric_sharpe), np.mean(model_metric_sharpe), np.median(model_metric_sharpe), np.max(model_metric_sharpe)) print(" ... sortino min/mean/median/max = ", np.min(model_metric_sortino), np.mean(model_metric_sortino), np.median(model_metric_sortino), np.max(model_metric_sortino)) all_model_dates.append(model_dates) all_model_metrics_sharpe.append(model_metric_sharpe) all_model_metrics_sortino.append(model_metric_sortino) all_model_names.append(imodel)
persistence_sorting_choices.append(inum_sort_choice) persistence_number_stocks_choices.append(inum_stocks_choices) persistence_values.append(persistence_months) final_values.append(cumu_dynamic_system[-1]) persistence_series_values.append(cumu_dynamic_system) persistence_DD.append( avg_DD(np.array(cumu_dynamic_system), len(cumu_dynamic_system))) persistence_mid_DD.append( avg_DD(np.array(cumu_dynamic_system), int(12 * 11.75))) persistence_recent_DD.append( avg_DD(np.array(cumu_dynamic_system), int(12 * 4.75))) try: persistence_sortinos.append( allstats(np.array(cumu_dynamic_system)).sortino()) except: persistence_sortinos.append(0.) try: persistence_mid_sortinos.append( allstats(np.array( cumu_dynamic_system[-int(12 * 11.75):])).sortino()) except: persistence_mid_sortinos.append(0.) try: persistence_recent_sortinos.append( allstats(np.array( cumu_dynamic_system[-int(12 * 4.75):])).sortino()) except: persistence_recent_sortinos.append(0.) try:
persistence_sorting_choices.append(inum_sort_choice) persistence_number_stocks_choices.append(inum_stocks_choices) persistence_values.append(persistence_months) final_values.append(cumu_dynamic_system[-1]) persistence_series_values.append(cumu_dynamic_system) persistence_DD.append( avg_DD(np.array(cumu_dynamic_system), len(cumu_dynamic_system))) persistence_mid_DD.append( avg_DD(np.array(cumu_dynamic_system), int(12 * 10.75))) persistence_recent_DD.append( avg_DD(np.array(cumu_dynamic_system), int(12 * 4.75))) try: persistence_sortinos.append( allstats(np.array(cumu_dynamic_system)).sortino()) except: persistence_sortinos.append(0.) try: persistence_mid_sortinos.append( allstats(np.array( cumu_dynamic_system[-int(12 * 10.75):])).sortino()) except: persistence_mid_sortinos.append(0.) try: persistence_recent_sortinos.append( allstats(np.array( cumu_dynamic_system[-int(12 * 4.75):])).sortino()) except: persistence_recent_sortinos.append(0.) try: