def find_best_str_name(base_df, method_type, drawdown_limit=None): sub_base_df = base_df[base_df.index >= datetime.datetime(2016, 2, 1)] if drawdown_limit is not None: base_df_max_draw_down = sub_base_df.apply(get_max_draw_down, axis=0) sub_keys = base_df_max_draw_down[ base_df_max_draw_down <= drawdown_limit].keys() sub_base_df = sub_base_df[sub_keys] # annualized return if method_type == 'ar': sta_df = get_annualized_return(sub_base_df, const.WEALTH_DATAFRAME) else: sta_df = get_sharpe_ratio(sub_base_df, const.WEALTH_DATAFRAME) return sta_df.idxmax()
def get_line_not_alpha(data_series, prefix_info): if date_tuple[0] is not None: sub_data_series = data_series[ data_series.index > date_tuple[0]] else: sub_data_series = data_series if date_tuple[1] is not None: sub_data_series = sub_data_series[ sub_data_series.index < date_tuple[1]] sharpe_ratio = get_sharpe_ratio(sub_data_series, df_type=const.WEALTH_DATAFRAME) ann_return = get_annualized_return( sub_data_series, df_type=const.WEALTH_DATAFRAME) * 100 max_draw_down = get_max_draw_down(sub_data_series) * 100 current_line = '{}: Sharpe Ratio {:.3f}, Annualized Return {:.2f}%, Max Drawdown rate {:.2f}%'.format( prefix_info, sharpe_ratio, ann_return, max_draw_down) return current_line
pool = pathos.multiprocessing.ProcessingPool(multiprocessing.cpu_count() - 2) parameter_list = [] for hday in hday_list: for sr in sr_list: for p in portfolio_range: parameter_list.append({'hday': hday, 'sr': sr, 'p': p}) pool.map(multi_process_wealth_series, parameter_list) file_list = os.listdir(raw_wealth_path) df = pd.read_pickle(os.path.join(raw_wealth_path, file_list[0])) result_df = pd.DataFrame(index=df.index) for f in file_list: df = pd.read_pickle(os.path.join(raw_wealth_path, f)) col_name = f[:-2] result_df[col_name] = df result_df.to_excel(os.path.join(const.TEMP_PATH, file_path, '20170418_insider_reblance_test_result.xlsx')) sta_df = pd.DataFrame(columns=result_df.keys()) sta_df.loc[const.ANNUALIZED_RETURN] = get_annualized_return(result_df, const.WEALTH_DATAFRAME) sta_df.loc[const.SHARPE_RATIO] = get_sharpe_ratio(result_df, const.WEALTH_DATAFRAME) sta_df.loc['max_drawdown'] = result_df.apply(get_max_draw_down) sta_df.to_excel(os.path.join(const.TEMP_PATH, file_path, '20170418_insider_reblance_test_statistics.xlsx')) print(time.time() - start_time)
def generate_review_strategies(alpha_data, raw_data, base_df_type='alpha', review=6, forward=6, max_method='daily_return'): daily_alpha_return = alpha_data / alpha_data.shift(1) - 1 daily_raw_return = raw_data / raw_data.shift(1) - 1 date_index = alpha_data.index # start_date = datetime.datetime(year=date_index[0].year, month=date_index[0].month, day=1) start_date = datetime.datetime(year=date_index[0].year, month=date_index[0].month, day=1) middle_date = start_date + relativedelta(months=review) end_date = start_date + relativedelta(months=(review + forward)) if end_date > date_index[-1]: end_date = date_index[-1] + relativedelta(days=1) if base_df_type == 'alpha': base_df = daily_alpha_return else: base_df = daily_raw_return learning_alpha_series = pd.Series(index=date_index) learning_raw_series = pd.Series(index=date_index) while middle_date < date_index[-1]: trading_index = date_index[date_index >= start_date] trading_index = trading_index[trading_index < middle_date] filling_index = date_index[date_index < end_date] filling_index = filling_index[filling_index >= middle_date] sub_base_df = base_df.loc[trading_index] if max_method == 'daily_return': sub_learning_strategy_name = sub_base_df.mean().idxmax() elif max_method == 'sharpe_ratio': sub_learning_strategy_name = get_sharpe_ratio( sub_base_df, const.RETURN_DATAFRAME).idxmax() else: sub_learning_strategy_name = get_annualized_return( sub_base_df, const.RETURN_DATAFRAME).idxmax() learning_alpha_series.loc[filling_index] = daily_alpha_return.loc[ filling_index, sub_learning_strategy_name] learning_raw_series.loc[filling_index] = daily_raw_return.loc[ filling_index, sub_learning_strategy_name] middle_date = end_date start_date = middle_date - relativedelta(months=review) end_date = middle_date + relativedelta(months=forward) if end_date > date_index[-1]: end_date = date_index[-1] + relativedelta(days=1) learning_alpha_wealth = (learning_alpha_series.fillna(0) + 1).cumprod() * 10000 learning_raw_wealth = (learning_raw_series.fillna(0) + 1).cumprod() * 10000 return learning_alpha_wealth, learning_raw_wealth