def cal_fhs_garch(wind_code, simulate_count, nav_acc_s): """ 获取子基金净值,生成csv文件,调用FHSGACH3Py.R文件计算,返回 csv文件 :param wind_code: 基金代码 :return: 压力测试数据 DataFrame, index为日期,column每一组压力测试数据 """ nav_date_latest = nav_acc_s.index[-1] input_file_name = '%s_%s.csv' % (wind_code, nav_date_latest.strftime(DATE_STR_FORMAT)) input_file_path = get_cache_file_path(ANALYSIS_CACHE_FILE_NAME, input_file_name) date_index = get_return_rate_csv(nav_acc_s, input_file_path) if date_index is None: return None output_file_name = '%s_simulate_%s.csv' % ( wind_code, nav_date_latest.strftime(DATE_STR_FORMAT)) output_file_path = get_cache_file_path(ANALYSIS_CACHE_FILE_NAME, output_file_name) if not ENABLE_CACHE or not path.exists(output_file_path): # 测试使用,临时将 input_file_name 替换为 样例csv文件 # input_file_name = 'rr_example.csv' print('invoke fhs_garch.R for', wind_code, simulate_count) robjects.r.source("fhs_garch.R") robjects.r['FHSGACH3Py'](input_file_path, output_file_path, simulate_count) # 读取 csv文件,重置索引 simulate_df = pd.DataFrame.from_csv( output_file_path).reset_index().set_index(date_index) # print('simulate_df.shape', simulate_df.shape) return simulate_df
def plot_fof_copula(wind_code_list, weighted_list, startdate, enddate, simulate_count): start_time = datetime.now() st = stress_test('Clayton') dd = st.get_max_drawdown(wind_code_list, startdate, enddate, weighted_list, simulate_count) figure_time = datetime.now() file_name = '%s_%s.png' % ('fof', figure_time.strftime('%Y_%m_%d %H_%M_%S')) file_path = get_cache_file_path(ANALYSIS_CACHE_FILE_NAME, file_name) plt.hist(dd) # plt.show() plt.savefig(file_path) finished_time = datetime.now() print("time estimate:", finished_time - start_time) return file_path
def get_cache_file_path(file_name): return fh_utils.get_cache_file_path(ANALYSIS_CACHE_FILE_NAME, file_name)
def savefig_df(df, file_name): file_path = get_cache_file_path(ANALYSIS_CACHE_FILE_NAME, file_name) df.plot(legend=False) plt.savefig(file_path) return file_path