From 5ff62137170c907ba32561a7ebe7b34ffb467c1a Mon Sep 17 00:00:00 2001 From: jingboyitiji Date: Tue, 5 Aug 2025 14:42:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A1=E7=AE=97=E6=9C=80=E4=BD=B3=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E6=97=A5=E6=9C=9F=E8=B0=83=E6=95=B4=E4=B8=BA=E4=B8=8A?= =?UTF-8?q?=E4=B8=80=E4=BA=A4=E6=98=93=E6=97=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tools.py | 44 +++++++++++++++++++++++++------------------- main_juxiting.py | 4 +++- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/lib/tools.py b/lib/tools.py index 7f043cb..1b6ee20 100644 --- a/lib/tools.py +++ b/lib/tools.py @@ -843,6 +843,8 @@ def convert_df_to_pydantic_pp(df_predict, model_id_name_dict, global_config): def find_best_models(date='', global_config=None): best_models = {} model_id_name_dict = get_model_id_name_dict(global_config=global_config) + + # 处理日期输入 if not date: @@ -855,8 +857,13 @@ def find_best_models(date='', global_config=None): global_config['logger'].error( f"日期格式错误,期望格式为 '%Y-%m-%d',实际输入: {date}") return best_models - current_date = datetime.datetime.strptime(date, '%Y-%m-%d') + + + # 上一交易日日期 Last trading day + last_trading_day = pd.Timestamp(date) - pd.tseries.offsets.BusinessDay(1) + last_trading_day_str = last_trading_day.strftime('%Y-%m-%d') + # 计算date对应月的一日 first_day_of_month = current_date.replace(day=1) # 计算date对应周的周一 @@ -943,14 +950,13 @@ def find_best_models(date='', global_config=None): # 遍历全局配置中的价格列 for i, wd in enumerate(global_config['price_columns']): global_config['logger'].info( - f'*********************************************************************************************************计算预测{date}的{wd}最佳模型') + f'*********************************************************************************************************计算预测{last_trading_day_str}的{wd}最佳模型') best_models[wd] = {} if i == 0: # 计算当前日期的前一工作日日期 - ciridate = (pd.Timestamp(date) - - pd.tseries.offsets.BusinessDay(1)).strftime('%Y-%m-%d') - global_config['logger'].info(f'计算预测{date}的次日{ciridate}最佳模型') + ciridate = last_trading_day_str + global_config['logger'].info(f'计算预测{last_trading_day}的次日{last_trading_day}最佳模型') global_config['logger'].info( f'{date}真实价格:{true_price[true_price["ds"] == date]["y"].values[0]}') price = df[['data_date', wd, 'model_id']] @@ -965,7 +971,7 @@ def find_best_models(date='', global_config=None): global_config['logger'].info( f'{ciridate}预测最准确的模型名称:{best_models[wd]}') predictresult = query_predict_result( - date, best_model_id, global_config, wd) + last_trading_day, best_model_id, global_config, wd) if predictresult: global_config['logger'].info( f'最佳模型{best_models[wd]}在{date}预测结果:{predictresult}') @@ -976,9 +982,9 @@ def find_best_models(date='', global_config=None): elif i == 1: # 计算五个工作日之前的日期 - benzhoudate = (pd.Timestamp(date) - + benzhoudate = (pd.Timestamp(last_trading_day) - pd.Timedelta(days=7)).strftime('%Y-%m-%d') - global_config['logger'].info(f'计算预测{date}的五天前{benzhoudate}最佳模型') + global_config['logger'].info(f'计算预测{last_trading_day}的五天前{benzhoudate}最佳模型') global_config['logger'].info( f'{date}真实价格:{true_price[true_price["ds"] == date]["y"].values[0]}') price = df[['data_date', wd, 'model_id']] @@ -992,7 +998,7 @@ def find_best_models(date='', global_config=None): global_config['logger'].info( f'{benzhoudate}预测最准确的模型名称:{best_models[wd]}') predictresult = query_predict_result( - date, best_model_id, global_config, wd) + last_trading_day, best_model_id, global_config, wd) if predictresult: global_config['logger'].info( f'最佳模型{best_models[wd]}在{date}预测结果:{predictresult}') @@ -1004,16 +1010,16 @@ def find_best_models(date='', global_config=None): elif i in [2, 3]: weeks_ago = 1 if i == 2 else 2 - ago_monday = current_date - \ - datetime.timedelta(days=current_date.weekday() + 7 * weeks_ago) + ago_monday = last_trading_day - \ + datetime.timedelta(days=last_trading_day.weekday() + 7 * weeks_ago) ago_sunday = ago_monday + datetime.timedelta(days=6) ago_date_str = f"{ago_monday.strftime('%Y-%m-%d')} - {ago_sunday.strftime('%Y-%m-%d')}" global_config['logger'].info( f'计算预测{date}的前{weeks_ago}周{ago_date_str}最佳模型') - weektrueprice = true_price[(true_price['ds'] >= date_monday.strftime( - '%Y-%m-%d')) & (true_price['ds'] <= date)]['y'].mean() + weektrueprice = true_price[(true_price['ds'] >= ago_monday.strftime('%Y-%m-%d')) & (true_price['ds'] <= ago_sunday.strftime('%Y-%m-%d'))]['y'].mean() global_config['logger'].info( - f'当周{date_monday.strftime("%Y-%m-%d")}---{date}真实价格的周均价:{weektrueprice}') + f'当周{date_monday.strftime("%Y-%m-%d")}---{last_trading_day_str}真实价格的周均价:{weektrueprice}') + price = df[['data_date', wd, 'model_id']] price = price[(price['data_date'] >= ago_monday) & (price['data_date'] <= ago_sunday)] @@ -1025,7 +1031,8 @@ def find_best_models(date='', global_config=None): global_config['logger'].info( f'{ago_date_str}预测最准确的模型名称:{best_models[wd]}') predictresult = query_predict_result( - date, best_model_id, global_config, wd) + last_trading_day_str, best_model_id, global_config, wd) + if predictresult: global_config['logger'].info( f'最佳模型{best_models[wd]}在{date}预测结果:{predictresult}') @@ -1039,9 +1046,8 @@ def find_best_models(date='', global_config=None): elif i in [4, 5, 6, 7]: months_ago = i - 3 - current_date_ts = pd.Timestamp(date) last_month_first_day = ( - current_date_ts - pd.offsets.MonthBegin(months_ago)).strftime('%Y-%m-%d') + last_trading_day - pd.offsets.MonthBegin(months_ago)).strftime('%Y-%m-%d') last_month_last_day = (pd.Timestamp( last_month_first_day) + pd.offsets.MonthEnd(0)).strftime('%Y-%m-%d') global_config['logger'].info( @@ -1049,7 +1055,7 @@ def find_best_models(date='', global_config=None): monthtrueprice = true_price[(true_price['ds'] >= first_day_of_month.strftime( '%Y-%m-%d')) & (true_price['ds'] <= date)]['y'].mean() global_config['logger'].info( - f'当月{first_day_of_month.strftime("%Y-%m-%d")}-{date}真实价格的月均价:{monthtrueprice}') + f'当月{first_day_of_month.strftime("%Y-%m-%d")}-{last_trading_day}真实价格的月均价:{monthtrueprice}') price = df[['data_date', wd, 'model_id']] price = price[(price['data_date'] >= last_month_first_day) & ( price['data_date'] <= last_month_last_day)] @@ -1061,7 +1067,7 @@ def find_best_models(date='', global_config=None): global_config['logger'].info( f'{last_month_first_day}-{last_month_last_day}预测最准确的模型名称:{best_models[wd]}') predictresult = query_predict_result( - date, best_model_id, global_config, wd) + last_trading_day, best_model_id, global_config, wd) if predictresult: global_config['logger'].info( f'最佳模型{best_models[wd]}在{date}预测结果:{predictresult}') diff --git a/main_juxiting.py b/main_juxiting.py index c0d0268..296a051 100644 --- a/main_juxiting.py +++ b/main_juxiting.py @@ -557,8 +557,10 @@ if __name__ == '__main__': # except Exception as e: # logger.info(f'预测失败:{e}') # continue - global_config['end_time'] = '2025-08-04' + global_config['end_time'] = '2025-08-01' + global_config['db_mysql'].connect() predict_main() + # global_config['end_time'] = '2025-08-01' # push_market_value() # sql_inset_predict(global_config)