计算最佳模型日期调整为上一交易日
This commit is contained in:
parent
f3971a94ec
commit
5ff6213717
44
lib/tools.py
44
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}')
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user