聚烯烃次日预测,本周预测最佳模型计算
This commit is contained in:
		
							parent
							
								
									5d5b2ae251
								
							
						
					
					
						commit
						f11a81a4ff
					
				| @ -86,6 +86,11 @@ bdwdname = [ | |||||||
|     '次日' |     '次日' | ||||||
| ] | ] | ||||||
| 
 | 
 | ||||||
|  | # 数据库预测结果表八大维度列名 | ||||||
|  | price_columns = [ | ||||||
|  |     'day_price', 'week_price', 'second_week_price', 'next_week_price', | ||||||
|  |     'next_month_price', 'next_february_price', 'next_march_price', 'next_april_price' | ||||||
|  | ] | ||||||
| 
 | 
 | ||||||
| modelsindex = [{ | modelsindex = [{ | ||||||
|     'NHITS': 'SELF0000077', |     'NHITS': 'SELF0000077', | ||||||
|  | |||||||
| @ -51,6 +51,7 @@ global_config = { | |||||||
|     'baicangidnamedict': None,  # 百川id名称映射 |     'baicangidnamedict': None,  # 百川id名称映射 | ||||||
|     'modelsindex': None,  # 模型索引 |     'modelsindex': None,  # 模型索引 | ||||||
|     'bdwdname': None, |     'bdwdname': None, | ||||||
|  |     'price_columns': None, | ||||||
| 
 | 
 | ||||||
|     # 模型参数 |     # 模型参数 | ||||||
|     'data_set': None,                  # 数据集名称 |     'data_set': None,                  # 数据集名称 | ||||||
| @ -746,6 +747,25 @@ def calculate_kdj(data, n=9): | |||||||
|     # data = data.dropna() |     # data = data.dropna() | ||||||
|     return data |     return data | ||||||
| 
 | 
 | ||||||
|  | # 计算因子与预测目标的相关性 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def calculate_correlation(df): | ||||||
|  |     try: | ||||||
|  |         yy = df['y'] | ||||||
|  |         # 去掉ds y | ||||||
|  |         df = df.drop(columns=['ds', 'y']) | ||||||
|  |         # 计算相关系数 | ||||||
|  |         corr = df.corrwith(yy) | ||||||
|  |         # 输出相关系数 | ||||||
|  |         print(corr) | ||||||
|  |         # 保存结果 | ||||||
|  |         corr.to_csv(os.path.join(config.dataset, '相关系数.csv')) | ||||||
|  |     except Exception as e: | ||||||
|  |         config.logger.info('计算相关系数错误:', e) | ||||||
|  |     finally: | ||||||
|  |         config.logger.info('计算相关系数完成') | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| def check_column(df, col_name, two_months_ago): | def check_column(df, col_name, two_months_ago): | ||||||
|     ''' |     ''' | ||||||
| @ -1080,6 +1100,7 @@ def datachuli_juxiting(df_zhibiaoshuju, df_zhibiaoliebiao, datecol='date', end_t | |||||||
|     if add_kdj: |     if add_kdj: | ||||||
|         df = calculate_kdj(df) |         df = calculate_kdj(df) | ||||||
| 
 | 
 | ||||||
|  |     calculate_correlation(df=df) | ||||||
|     featureAnalysis(df, dataset=dataset, y=y) |     featureAnalysis(df, dataset=dataset, y=y) | ||||||
|     return df |     return df | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -40,3 +40,39 @@ class PredictionResult(BaseModel): | |||||||
|     create_date: Optional[datetime] = None |     create_date: Optional[datetime] = None | ||||||
|     update_user: Optional[str] = None |     update_user: Optional[str] = None | ||||||
|     update_date: Optional[datetime] = None |     update_date: Optional[datetime] = None | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class PpPredictionResult(BaseModel): | ||||||
|  |     feature_factor_frequency: str | ||||||
|  |     strategy_id: int | ||||||
|  |     oil_code: Optional[str] = 'PP' | ||||||
|  |     oil_name: Optional[str] = 'PP期货' | ||||||
|  |     data_date: Optional[datetime] = None | ||||||
|  |     market_price: Optional[Decimal] = None | ||||||
|  |     day_price: Optional[Decimal] = None | ||||||
|  |     week_price: Optional[Decimal] = None | ||||||
|  |     second_week_price: Optional[Decimal] = None | ||||||
|  |     next_week_price: Optional[Decimal] = None | ||||||
|  |     next_month_price: Optional[Decimal] = None | ||||||
|  |     next_february_price: Optional[Decimal] = None | ||||||
|  |     next_march_price: Optional[Decimal] = None | ||||||
|  |     next_april_price: Optional[Decimal] = None | ||||||
|  |     model_evaluation_id: int | ||||||
|  |     model_id: int | ||||||
|  |     tenant_code: Optional[str] = None | ||||||
|  |     reserved_str1: Optional[str] = None | ||||||
|  |     reserved_str2: Optional[str] = None | ||||||
|  |     reserved_str3: Optional[str] = None | ||||||
|  |     reserved_str4: Optional[str] = None | ||||||
|  |     reserved_str5: Optional[str] = None | ||||||
|  |     reserved_num1: Optional[Decimal] = None | ||||||
|  |     reserved_num2: Optional[Decimal] = None | ||||||
|  |     reserved_num3: Optional[Decimal] = None | ||||||
|  |     reserved_num4: Optional[Decimal] = None | ||||||
|  |     reserved_num5: Optional[Decimal] = None | ||||||
|  |     version_num: Decimal = Decimal(1) | ||||||
|  |     delete_flag: str = '0' | ||||||
|  |     create_user: Optional[str] = None | ||||||
|  |     create_date: Optional[datetime] = None | ||||||
|  |     update_user: Optional[str] = None | ||||||
|  |     update_date: Optional[datetime] = None | ||||||
|  | |||||||
							
								
								
									
										357
									
								
								lib/tools.py
									
									
									
									
									
								
							
							
						
						
									
										357
									
								
								lib/tools.py
									
									
									
									
									
								
							| @ -37,7 +37,7 @@ import time | |||||||
| import logging | import logging | ||||||
| from dotenv import load_dotenv | from dotenv import load_dotenv | ||||||
| 
 | 
 | ||||||
| from lib.pydantic_models import PredictionResult | from lib.pydantic_models import PredictionResult, PpPredictionResult | ||||||
| load_dotenv() | load_dotenv() | ||||||
| 
 | 
 | ||||||
| global logger | global logger | ||||||
| @ -642,6 +642,105 @@ def get_week_date(end_time): | |||||||
|     return create_dates, ds_dates |     return create_dates, ds_dates | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def get_bdwd_date(date=''): | ||||||
|  |     ''' | ||||||
|  |     计算当前日期date对应的明天,五班后,下周日,下下周日,下月最后一天,下两月最后一天,下三月最后一天,下四月最后一天的日期 | ||||||
|  | 
 | ||||||
|  |     参数: | ||||||
|  |         date (str): 输入的日期,格式为 '%Y-%m-%d',默认为空字符串,表示当前日期 | ||||||
|  | 
 | ||||||
|  |     返回: | ||||||
|  |         dict: 包含所需日期的字典,键分别为 'tomorrow', 'five_working_days_later', 'next_sunday',  | ||||||
|  |               'next_next_sunday', 'next_month_last_day', 'next_two_months_last_day',  | ||||||
|  |               'next_three_months_last_day', 'next_four_months_last_day' | ||||||
|  |     ''' | ||||||
|  |     import datetime | ||||||
|  |     if not date: | ||||||
|  |         current_date = datetime.date.today() | ||||||
|  |     else: | ||||||
|  |         current_date = datetime.datetime.strptime(date, '%Y-%m-%d').date() | ||||||
|  | 
 | ||||||
|  |     # 计算明天的日期 | ||||||
|  |     tomorrow = current_date + datetime.timedelta(days=1) | ||||||
|  | 
 | ||||||
|  |     # 计算五班后的日期 | ||||||
|  |     five_working_days_later = current_date | ||||||
|  |     working_days_count = 0 | ||||||
|  |     while working_days_count < 5: | ||||||
|  |         five_working_days_later += datetime.timedelta(days=1) | ||||||
|  |         if five_working_days_later.weekday() < 5:  # 周一到周五是工作日 | ||||||
|  |             working_days_count += 1 | ||||||
|  | 
 | ||||||
|  |      # 计算下周日的日期 | ||||||
|  |     days_to_next_sunday = (6 - current_date.weekday()) % 7 | ||||||
|  |     if days_to_next_sunday == 0: | ||||||
|  |         days_to_next_sunday = 7 | ||||||
|  |     next_sunday = current_date + datetime.timedelta(days=days_to_next_sunday) | ||||||
|  | 
 | ||||||
|  |     # 计算下下周周日的日期 | ||||||
|  |     next_next_sunday = next_sunday + datetime.timedelta(days=7) | ||||||
|  | 
 | ||||||
|  |     # 计算下下下周周日的日期 | ||||||
|  |     next_next_next_sunday = next_next_sunday + datetime.timedelta(days=7) | ||||||
|  | 
 | ||||||
|  |     # 计算下月最后一天的日期 | ||||||
|  |     next_month = current_date.replace(day=28) + datetime.timedelta(days=4) | ||||||
|  |     next_month_last_day = next_month.replace( | ||||||
|  |         day=1) - datetime.timedelta(days=1) | ||||||
|  | 
 | ||||||
|  |     # 计算下两月最后一天的日期 | ||||||
|  |     next_two_months = next_month.replace(day=28) + datetime.timedelta(days=4) | ||||||
|  |     next_two_months_last_day = next_two_months.replace( | ||||||
|  |         day=1) - datetime.timedelta(days=1) | ||||||
|  | 
 | ||||||
|  |     # 计算下三月最后一天的日期 | ||||||
|  |     next_three_months = next_two_months.replace( | ||||||
|  |         day=28) + datetime.timedelta(days=4) | ||||||
|  |     next_three_months_last_day = next_three_months.replace( | ||||||
|  |         day=1) - datetime.timedelta(days=1) | ||||||
|  | 
 | ||||||
|  |     # 计算下四月最后一天的日期 | ||||||
|  |     next_four_months = next_three_months.replace( | ||||||
|  |         day=28) + datetime.timedelta(days=4) | ||||||
|  |     next_four_months_last_day = next_four_months.replace( | ||||||
|  |         day=1) - datetime.timedelta(days=1) | ||||||
|  | 
 | ||||||
|  |     # 计算下五月最后一天的日期 | ||||||
|  |     next_five_months = next_four_months.replace( | ||||||
|  |         day=28) + datetime.timedelta(days=4) | ||||||
|  |     next_five_months_last_day = next_five_months.replace( | ||||||
|  |         day=1) - datetime.timedelta(days=1) | ||||||
|  | 
 | ||||||
|  |     return { | ||||||
|  |         'tomorrow': tomorrow.strftime('%Y-%m-%d'), | ||||||
|  |         'five_working_days_later': five_working_days_later.strftime('%Y-%m-%d'), | ||||||
|  |         # 'next_sunday': next_sunday.strftime('%Y-%m-%d'), | ||||||
|  |         'next_next_sunday': next_next_sunday.strftime('%Y-%m-%d'), | ||||||
|  |         'next_next_next_sunday': next_next_next_sunday.strftime('%Y-%m-%d'), | ||||||
|  |         # 'next_month_last_day': next_month_last_day.strftime('%Y-%m-%d'), | ||||||
|  |         'next_two_months_last_day': next_two_months_last_day.strftime('%Y-%m-%d'), | ||||||
|  |         'next_three_months_last_day': next_three_months_last_day.strftime('%Y-%m-%d'), | ||||||
|  |         'next_four_months_last_day': next_four_months_last_day.strftime('%Y-%m-%d'), | ||||||
|  |         'next_five_months_last_day': next_five_months_last_day.strftime('%Y-%m-%d'), | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def get_bdwd_price(date, true_price, global_config): | ||||||
|  |     ''' | ||||||
|  |     计算当前日期date对应的明天,五班后,下周日,下下周日,下月最后一天,下两月最后一天,下三月最后一天,下四月最后一天的价格 | ||||||
|  |     ''' | ||||||
|  |     bdwd_price = {} | ||||||
|  |     for wd in global_config['price_columns']: | ||||||
|  |         if wd == 'day_price': | ||||||
|  |             bdwd_price[wd] = true_price[true_price['ds'] == date][wd].values[0] | ||||||
|  |         if wd == 'week_price': | ||||||
|  | 
 | ||||||
|  |             bdwd_price[wd] = true_price[true_price['ds'] == date][wd].values[0] | ||||||
|  |         true_price[wd] = pd.to_numeric(true_price[wd]) | ||||||
|  | 
 | ||||||
|  |     return | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| class DeepSeek(): | class DeepSeek(): | ||||||
|     def __init__(self): |     def __init__(self): | ||||||
|         pass |         pass | ||||||
| @ -674,6 +773,17 @@ class DeepSeek(): | |||||||
|         return summary |         return summary | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def get_model_id_name_dict(global_config): | ||||||
|  |     ''' | ||||||
|  |     预测结果和模型表求子集得到模型名称 | ||||||
|  |     ''' | ||||||
|  |     tb = 'v_tbl_predict_models' | ||||||
|  |     sql = f'select model_name,id from {tb} ' | ||||||
|  |     modelsname = global_config['db_mysql'].execute_query(sql) | ||||||
|  |     model_id_name_dict = {row['id']: row['model_name'] for row in modelsname} | ||||||
|  |     return model_id_name_dict | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| def get_modelsname(df, global_config): | def get_modelsname(df, global_config): | ||||||
|     ''' |     ''' | ||||||
|     预测结果和模型表求子集得到模型名称 |     预测结果和模型表求子集得到模型名称 | ||||||
| @ -682,7 +792,7 @@ def get_modelsname(df, global_config): | |||||||
|     tb = 'v_tbl_predict_models' |     tb = 'v_tbl_predict_models' | ||||||
|     sql = f'select model_name,id from {tb} ' |     sql = f'select model_name,id from {tb} ' | ||||||
|     modelsname = global_config['db_mysql'].execute_query(sql) |     modelsname = global_config['db_mysql'].execute_query(sql) | ||||||
|     model_id_name_dict = {row['id']: row['model_name'] for row in modelsname} |     model_id_name_dict = get_model_id_name_dict(global_config=global_config) | ||||||
|     model_name_list = [row['model_name'] for row in modelsname] |     model_name_list = [row['model_name'] for row in modelsname] | ||||||
|     model_name_list = set(columns) & set(model_name_list) |     model_name_list = set(columns) & set(model_name_list) | ||||||
|     model_name_list = list(model_name_list) |     model_name_list = list(model_name_list) | ||||||
| @ -709,5 +819,248 @@ def convert_df_to_pydantic(df_predict, model_id_name_dict, global_config): | |||||||
|     return results |     return results | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def convert_df_to_pydantic_pp(df_predict, model_id_name_dict, global_config): | ||||||
|  |     reverse_model_id_name_dict = { | ||||||
|  |         value: key for key, value in model_id_name_dict.items()} | ||||||
|  |     results = [] | ||||||
|  |     data = global_config['DEFAULT_CONFIG'].copy() | ||||||
|  |     data['data_date'] = df_predict['created_dt'].values[0] | ||||||
|  |     if isinstance(data['data_date'], np.datetime64): | ||||||
|  |         data['data_date'] = pd.Timestamp( | ||||||
|  |             data['data_date']).to_pydatetime() | ||||||
|  |     for c in df_predict.columns: | ||||||
|  |         if c not in ['ds', 'created_dt']: | ||||||
|  |             data['model_id'] = reverse_model_id_name_dict[c] | ||||||
|  |             data['predicted_price'] = Decimal( | ||||||
|  |                 round(df_predict[c].values[0], 2)) | ||||||
|  |             result = PpPredictionResult(**data) | ||||||
|  |             results.append(result) | ||||||
|  |     return results | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | # 查找上一交易日各维度的最佳模型 | ||||||
|  | def find_best_models(date='', global_config=None): | ||||||
|  | 
 | ||||||
|  |     best_models = {} | ||||||
|  |     model_id_name_dict = get_model_id_name_dict(global_config=global_config) | ||||||
|  | 
 | ||||||
|  |     import datetime | ||||||
|  |     if date == '': | ||||||
|  |         date = datetime.datetime.now().strftime('%Y-%m-%d') | ||||||
|  |     else: | ||||||
|  |         date = datetime.datetime.strptime( | ||||||
|  |             date, '%Y-%m-%d').strftime('%Y-%m-%d') | ||||||
|  | 
 | ||||||
|  |     # 获取真实价格的八个维度价格 | ||||||
|  |     true_price = pd.read_csv(os.path.join( | ||||||
|  |         global_config['dataset'], '指标数据.csv')) | ||||||
|  |     true_price = true_price[['ds', 'y']] | ||||||
|  | 
 | ||||||
|  |     year = int(date.split('-')[0]) | ||||||
|  |     month = int(date.split('-')[1]) | ||||||
|  |     day = int(date.split('-')[2]) | ||||||
|  | 
 | ||||||
|  |     # 计算六月前的年月 | ||||||
|  |     if month <= 6: | ||||||
|  |         year = int(year) - 1 | ||||||
|  |         month = 12 | ||||||
|  |     else: | ||||||
|  |         month = month - 6 | ||||||
|  | 
 | ||||||
|  |     tb = 'v_tbl_predict_pp_prediction_results' | ||||||
|  |     sql = f'select * from {tb} where data_date >= \'{year}-{month}-01\'' | ||||||
|  |     # 数据库查询对应日期的预测值 | ||||||
|  |     predictresult = global_config['db_mysql'].execute_query(sql) | ||||||
|  |     if len(predictresult) == 0: | ||||||
|  |         print('没有预测结果') | ||||||
|  |         return | ||||||
|  |     df = pd.DataFrame(predictresult) | ||||||
|  |     df = df[['data_date', 'model_id']+global_config['price_columns']] | ||||||
|  |     print('预测结果数量:', df.shape) | ||||||
|  |     print('预测结果日期范围:', df['data_date'].min(), '到', df['data_date'].max()) | ||||||
|  | 
 | ||||||
|  |     # 遍历全局配置中的价格列 | ||||||
|  |     for i, wd in enumerate(global_config['price_columns']): | ||||||
|  |         # 为每个价格列初始化一个空字典,用于存储最佳模型信息 | ||||||
|  |         best_models[wd] = {} | ||||||
|  |         # 处理第一个价格列,计算次日的最佳模型 | ||||||
|  |         if i == 0: | ||||||
|  |             # 计算当前日期的前一天日期 | ||||||
|  |             ciridate = (pd.Timestamp(date) - pd.Timedelta(days=1) | ||||||
|  |                         ).strftime('%Y-%m-%d') | ||||||
|  |             # 记录日志,提示开始计算次日的最佳模型 | ||||||
|  |             global_config['logger'].info(f'计算预测{date}的次日{ciridate}最佳模型') | ||||||
|  |             # 记录日志,输出当前日期的真实价格 | ||||||
|  |             global_config['logger'].info( | ||||||
|  |                 f'{date}真实价格:{true_price[true_price["ds"] == date]["y"].values[0]}') | ||||||
|  |             # 从数据框中选取需要的列 | ||||||
|  |             price = df[['data_date', wd, 'model_id']] | ||||||
|  |             # 筛选出数据日期在 ciridate 到 date 之间的数据 | ||||||
|  |             price = price[(price['data_date'] == ciridate) | ||||||
|  |                           | (price['data_date'] == date)] | ||||||
|  |             # 将价格列的数据类型转换为 float | ||||||
|  |             price[wd] = price[wd].astype(float) | ||||||
|  |             # 删除价格列中包含缺失值的行 | ||||||
|  |             price = price.dropna(subset=[wd]) | ||||||
|  |             # 判断价格趋势,若当前日期价格大于前一天价格,趋势为 1,否则为 -1 | ||||||
|  |             trend = 1 if true_price[true_price['ds'] == date]['y'].values[0] - \ | ||||||
|  |                 true_price[true_price['ds'] == ciridate]['y'].values[0] > 0 else -1 | ||||||
|  |             # 为数据框添加真实价格列 | ||||||
|  |             price['trueprice'] = true_price[true_price['ds'] | ||||||
|  |                                             == date]['y'].values[0] | ||||||
|  |             # 根据预测价格与真实价格的差值判断趋势,大于 0 为 1,否则为 -1 | ||||||
|  |             price['trend'] = np.where( | ||||||
|  |                 price['trueprice'] - price[wd] > 0, 1, -1) | ||||||
|  |             # 计算预测价格与真实价格差值的绝对值 | ||||||
|  |             price['abs'] = (price['trueprice'] - price[wd]).abs() | ||||||
|  |             # 筛选出趋势与整体趋势一致的数据 | ||||||
|  |             price = price[price['trend'] == | ||||||
|  |                           trend] | ||||||
|  |             # 筛选出预测价格与真实价格差值绝对值最小的数据 | ||||||
|  |             price = price[price['abs'] == price['abs'].min()] | ||||||
|  |             # 记录日志,输出筛选后的价格数据 | ||||||
|  |             global_config['logger'].info(price) | ||||||
|  |             # 获取最佳模型的 ID | ||||||
|  |             best_model_id = price.iloc[0]['model_id'] | ||||||
|  |             # 记录日志,输出次日预测最准确的模型 ID | ||||||
|  |             global_config['logger'].info(f'{ciridate}预测最准确的模型:{best_model_id}') | ||||||
|  |             # 将最佳模型的 ID 存入字典 | ||||||
|  |             best_models[wd]['model_id'] = best_model_id | ||||||
|  |             # 根据模型 ID 获取模型名称并存入字典 | ||||||
|  |             best_models[wd]['model_name'] = model_id_name_dict[best_model_id] | ||||||
|  |             # 记录日志,输出次日预测最准确的模型名称 | ||||||
|  |             global_config['logger'].info(f'{ciridate}预测最准确的模型名称:{best_models}') | ||||||
|  | 
 | ||||||
|  |         if i == 1: | ||||||
|  |             # 计算五个工作日之前的日期 | ||||||
|  |             benzhoudate = (pd.Timestamp(date) - pd.Timedelta(days=7) | ||||||
|  |                            ).strftime('%Y-%m-%d') | ||||||
|  | 
 | ||||||
|  |             # 记录日志,提示开始计算五天前的最佳模型 | ||||||
|  |             global_config['logger'].info(f'计算预测{date}的五天前{benzhoudate}最佳模型') | ||||||
|  |             # 记录日志,输出当前日期的真实价格 | ||||||
|  |             global_config['logger'].info( | ||||||
|  |                 f'{date}真实价格:{true_price[true_price["ds"] == date]["y"].values[0]}') | ||||||
|  |             # 从数据框中选取需要的列 | ||||||
|  |             price = df[['data_date', wd, 'model_id']] | ||||||
|  |             # 筛选出数据日期在 benzhoudate 到 date 之间的数据 | ||||||
|  |             price = price[(price['data_date'] == benzhoudate) | ||||||
|  |                           | (price['data_date'] == date)] | ||||||
|  |             # 将价格列的数据类型转换为 float | ||||||
|  |             price[wd] = price[wd].astype(float) | ||||||
|  |             # 删除价格列中包含缺失值的行 | ||||||
|  |             price = price.dropna(subset=[wd]) | ||||||
|  |             # 判断价格趋势,若当前日期价格大于前一天价格,趋势为 1,否则为 -1 | ||||||
|  |             trend = 1 if true_price[true_price['ds'] == date]['y'].values[0] - \ | ||||||
|  |                 true_price[true_price['ds'] == benzhoudate]['y'].values[0] > 0 else -1 | ||||||
|  |             # 记录日志,输出五天前预测最准确的模型名称 | ||||||
|  |             global_config['logger'].info(f'实际趋势是:{trend}') | ||||||
|  |             # 为数据框添加真实价格列 | ||||||
|  |             price['trueprice'] = true_price[true_price['ds'] | ||||||
|  |                                             == date]['y'].values[0] | ||||||
|  |             # 根据预测价格与真实价格的差值判断趋势,大于 0 为 1,否则为 -1 | ||||||
|  |             price['trend'] = np.where( | ||||||
|  |                 price['trueprice'] - price[wd] > 0, 1, -1) | ||||||
|  |             # 计算预测价格与真实价格差值的绝对值 | ||||||
|  |             price['abs'] = (price['trueprice'] - price[wd]).abs() | ||||||
|  |             # 筛选出趋势与整体趋势一致的数据 | ||||||
|  |             price = price[price['trend'] == | ||||||
|  |                           trend] | ||||||
|  |             # 筛选出预测价格与真实价格差值绝对值最小的数据 | ||||||
|  |             price = price[price['abs'] == price['abs'].min()] | ||||||
|  |             # 记录日志,输出筛选后的价格数据 | ||||||
|  |             global_config['logger'].info(price) | ||||||
|  |             # 获取最佳模型的 ID | ||||||
|  |             best_model_id = price.iloc[0]['model_id'] | ||||||
|  |             # 记录日志,输出五天前预测最准确的模型 ID | ||||||
|  |             global_config['logger'].info( | ||||||
|  |                 f'{benzhoudate}预测最准确的模型:{best_model_id}') | ||||||
|  |             # 将最佳模型的 ID 存入字典 | ||||||
|  |             best_models[wd]['model_id'] = best_model_id | ||||||
|  |             # 根据模型 ID 获取模型名称并存入字典 | ||||||
|  |             best_models[wd]['model_name'] = model_id_name_dict[best_model_id] | ||||||
|  |             # 记录日志,输出五天前预测最准确的模型名称 | ||||||
|  |             global_config['logger'].info( | ||||||
|  |                 f'{benzhoudate}预测最准确的模型名称:{best_models}') | ||||||
|  | 
 | ||||||
|  |         if i == 2: | ||||||
|  |             # 计算当前周的前两周的周一和周日的日期 | ||||||
|  |             current_date = datetime.datetime.strptime(date, '%Y-%m-%d') | ||||||
|  |             # 计算前两一周周一 | ||||||
|  |             one_weeks_ago_monday = current_date - \ | ||||||
|  |                 datetime.timedelta(days=current_date.weekday() + 7) | ||||||
|  |             # 计算前一周周日 | ||||||
|  |             one_weeks_ago_sunday = one_weeks_ago_monday + \ | ||||||
|  |                 datetime.timedelta(days=6) | ||||||
|  |             cizhoudate = f"{one_weeks_ago_monday.strftime('%Y-%m-%d')} - {one_weeks_ago_sunday.strftime('%Y-%m-%d')}" | ||||||
|  |             print(f'计算预测{date}次周最佳模型,前一周日期区间: {cizhoudate}') | ||||||
|  |         if i == 3: | ||||||
|  |             # 计算当前周的前两周的周一和周日的日期 | ||||||
|  |             current_date = datetime.datetime.strptime(date, '%Y-%m-%d') | ||||||
|  |             # 计算前两周周一 | ||||||
|  |             two_weeks_ago_monday = current_date - \ | ||||||
|  |                 datetime.timedelta(days=current_date.weekday() + 14) | ||||||
|  |             # 计算前两周周日 | ||||||
|  |             two_weeks_ago_sunday = two_weeks_ago_monday + \ | ||||||
|  |                 datetime.timedelta(days=6) | ||||||
|  |             gezhoudate = f"{two_weeks_ago_monday.strftime('%Y-%m-%d')} - {two_weeks_ago_sunday.strftime('%Y-%m-%d')}" | ||||||
|  |             print(f'计算预测{date}隔周最佳模型,前两周日期区间: {gezhoudate}') | ||||||
|  |         if i == 4: | ||||||
|  |             # 计算当上月的1日及最后一日 | ||||||
|  |             current_date = pd.Timestamp(date) | ||||||
|  |             # 获取上月第一天 | ||||||
|  |             last_month_first_day = ( | ||||||
|  |                 current_date - pd.offsets.MonthBegin(2)).strftime('%Y-%m-%d') | ||||||
|  |             # 获取上月最后一天 | ||||||
|  |             last_month_last_day = (pd.Timestamp( | ||||||
|  |                 last_month_first_day) + pd.offsets.MonthEnd(0)).strftime('%Y-%m-%d') | ||||||
|  |             print( | ||||||
|  |                 f'计算预测{date}次月最佳模型,上月日期区间: {last_month_first_day} - {last_month_last_day}') | ||||||
|  |         if i == 5: | ||||||
|  |             # 计算两月前的1日及最后一日 | ||||||
|  |             current_date = pd.Timestamp(date) | ||||||
|  |             # 获取上上月第一天 | ||||||
|  |             last_month_first_day = ( | ||||||
|  |                 current_date - pd.offsets.MonthBegin(3)).strftime('%Y-%m-%d') | ||||||
|  |             # 获取上上月最后一天 | ||||||
|  |             last_month_last_day = (pd.Timestamp( | ||||||
|  |                 last_month_first_day) + pd.offsets.MonthEnd(0)).strftime('%Y-%m-%d') | ||||||
|  |             print( | ||||||
|  |                 f'计算预测{date}次二月最佳模型,两月前日期区间: {last_month_first_day} - {last_month_last_day}') | ||||||
|  |         if i == 6: | ||||||
|  |             # 计算三月前的1日及最后一日 | ||||||
|  |             current_date = pd.Timestamp(date) | ||||||
|  |             # 获取前三月第一天 | ||||||
|  |             last_month_first_day = ( | ||||||
|  |                 current_date - pd.offsets.MonthBegin(4)).strftime('%Y-%m-%d') | ||||||
|  |             # 获取前三月最后一天 | ||||||
|  |             last_month_last_day = (pd.Timestamp( | ||||||
|  |                 last_month_first_day) + pd.offsets.MonthEnd(0)).strftime('%Y-%m-%d') | ||||||
|  |             print( | ||||||
|  |                 f'计算预测{date}次三月最佳模型,三月前日期区间: {last_month_first_day} - {last_month_last_day}') | ||||||
|  |         if i == 7: | ||||||
|  |             # 计算四月前的1日及最后一日 | ||||||
|  |             current_date = pd.Timestamp(date) | ||||||
|  |             # 获取前四月第一天 | ||||||
|  |             last_month_first_day = ( | ||||||
|  |                 current_date - pd.offsets.MonthBegin(5)).strftime('%Y-%m-%d') | ||||||
|  |             # 获取前四月最后一天 | ||||||
|  |             last_month_last_day = (pd.Timestamp( | ||||||
|  |                 last_month_first_day) + pd.offsets.MonthEnd(0)).strftime('%Y-%m-%d') | ||||||
|  |             print( | ||||||
|  |                 f'计算预测{date}次四月最佳模型,四月前日期区间: {last_month_first_day} - {last_month_last_day}') | ||||||
|  | 
 | ||||||
|  |     # # 获取真实价格的八个维度价格 | ||||||
|  |     # true_price = pd.read_csv(os.path.join( | ||||||
|  |     #     global_config['dataset'], '指标数据.csv')) | ||||||
|  |     # true_price = true_price[['ds', 'y']] | ||||||
|  |     # print(true_price.head()) | ||||||
|  | 
 | ||||||
|  |     # # 根据当前日期date,计算对应八个维度的价格 | ||||||
|  |     # bdwd_price = get_bdwd_price(date, true_price) | ||||||
|  | 
 | ||||||
|  |     return predictresult | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     print('This is a tool, not a script.') |     print('This is a tool, not a script.') | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
| 
 | 
 | ||||||
| from lib.dataread import * | from lib.dataread import * | ||||||
| from config_juxiting import * | from config_juxiting import * | ||||||
| from lib.tools import SendMail, exception_logger, convert_df_to_pydantic, exception_logger, get_modelsname | from lib.tools import SendMail, exception_logger, convert_df_to_pydantic_pp, exception_logger, get_modelsname | ||||||
| from models.nerulforcastmodels import ex_Model_Juxiting, model_losss_juxiting, pp_export_pdf | from models.nerulforcastmodels import ex_Model_Juxiting, model_losss_juxiting, pp_export_pdf | ||||||
| import datetime | import datetime | ||||||
| import torch | import torch | ||||||
| @ -24,6 +24,7 @@ global_config.update({ | |||||||
|     'settings': settings, |     'settings': settings, | ||||||
| 
 | 
 | ||||||
|     'bdwdname': bdwdname, |     'bdwdname': bdwdname, | ||||||
|  |     'price_columns': price_columns, | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     # 模型参数 |     # 模型参数 | ||||||
| @ -169,10 +170,7 @@ def sql_inset_predict(global_config): | |||||||
|     wd = ['day_price', 'week_price'] |     wd = ['day_price', 'week_price'] | ||||||
|     model_name_list, model_id_name_dict = get_modelsname(df, global_config) |     model_name_list, model_id_name_dict = get_modelsname(df, global_config) | ||||||
| 
 | 
 | ||||||
|     PRICE_COLUMNS = [ |     PRICE_COLUMNS = global_config['price_columns'] | ||||||
|         'day_price', 'week_price', 'second_week_price', 'next_week_price', |  | ||||||
|         'next_month_price', 'next_february_price', 'next_march_price', 'next_april_price' |  | ||||||
|     ] |  | ||||||
| 
 | 
 | ||||||
|     params_list = [] |     params_list = [] | ||||||
|     for df, price_type in zip([next_day_df, this_week_df], wd): |     for df, price_type in zip([next_day_df, this_week_df], wd): | ||||||
| @ -208,7 +206,7 @@ def sql_inset_predict(global_config): | |||||||
| 
 | 
 | ||||||
|         next_day_df = df[['ds', 'created_dt'] + model_name_list] |         next_day_df = df[['ds', 'created_dt'] + model_name_list] | ||||||
| 
 | 
 | ||||||
|         pydantic_results = convert_df_to_pydantic( |         pydantic_results = convert_df_to_pydantic_pp( | ||||||
|             next_day_df, model_id_name_dict, global_config) |             next_day_df, model_id_name_dict, global_config) | ||||||
|         if pydantic_results: |         if pydantic_results: | ||||||
| 
 | 
 | ||||||
| @ -556,7 +554,9 @@ if __name__ == '__main__': | |||||||
|     #         logger.info(f'预测失败:{e}') |     #         logger.info(f'预测失败:{e}') | ||||||
|     #         continue |     #         continue | ||||||
| 
 | 
 | ||||||
|     predict_main() |     # predict_main() | ||||||
| 
 | 
 | ||||||
|     # push_market_value() |     # push_market_value() | ||||||
|     # sql_inset_predict(global_config) |     # sql_inset_predict(global_config) | ||||||
|  |     from lib.tools import find_best_models | ||||||
|  |     find_best_models(date='2025-07-18', global_config=global_config) | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
| 
 | 
 | ||||||
| from lib.dataread import * | from lib.dataread import * | ||||||
| from config_juxiting_yuedu import * | from config_juxiting_yuedu import * | ||||||
| from lib.tools import SendMail, convert_df_to_pydantic, exception_logger, get_modelsname | from lib.tools import SendMail, convert_df_to_pydantic_pp, exception_logger, get_modelsname | ||||||
| from models.nerulforcastmodels import ex_Model, model_losss_juxiting, pp_export_pdf | from models.nerulforcastmodels import ex_Model, model_losss_juxiting, pp_export_pdf | ||||||
| import datetime | import datetime | ||||||
| import torch | import torch | ||||||
| @ -218,7 +218,7 @@ def sql_inset_predict(global_config): | |||||||
| 
 | 
 | ||||||
|         next_day_df = df[['ds', 'created_dt'] + model_name_list] |         next_day_df = df[['ds', 'created_dt'] + model_name_list] | ||||||
| 
 | 
 | ||||||
|         pydantic_results = convert_df_to_pydantic( |         pydantic_results = convert_df_to_pydantic_pp( | ||||||
|             next_day_df, model_id_name_dict, global_config) |             next_day_df, model_id_name_dict, global_config) | ||||||
|         if pydantic_results: |         if pydantic_results: | ||||||
| 
 | 
 | ||||||
| @ -252,7 +252,6 @@ def sql_inset_predict(global_config): | |||||||
|     config.db_mysql.close() |     config.db_mysql.close() | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| def predict_main(): | def predict_main(): | ||||||
|     """ |     """ | ||||||
|     主预测函数,用于从 ETA 获取数据、处理数据、训练模型并进行预测。 |     主预测函数,用于从 ETA 获取数据、处理数据、训练模型并进行预测。 | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
| 
 | 
 | ||||||
| from lib.dataread import * | from lib.dataread import * | ||||||
| from config_juxiting_zhoudu import * | from config_juxiting_zhoudu import * | ||||||
| from lib.tools import SendMail, exception_logger, convert_df_to_pydantic, exception_logger, get_modelsname | from lib.tools import SendMail, exception_logger, convert_df_to_pydantic_pp, exception_logger, get_modelsname | ||||||
| from models.nerulforcastmodels import ex_Model_Juxiting, model_losss_juxiting, pp_export_pdf | from models.nerulforcastmodels import ex_Model_Juxiting, model_losss_juxiting, pp_export_pdf | ||||||
| import datetime | import datetime | ||||||
| import torch | import torch | ||||||
| @ -205,7 +205,7 @@ def sql_inset_predict(global_config): | |||||||
| 
 | 
 | ||||||
|         next_day_df = df[['ds', 'created_dt'] + model_name_list] |         next_day_df = df[['ds', 'created_dt'] + model_name_list] | ||||||
| 
 | 
 | ||||||
|         pydantic_results = convert_df_to_pydantic( |         pydantic_results = convert_df_to_pydantic_pp( | ||||||
|             next_day_df, model_id_name_dict, global_config) |             next_day_df, model_id_name_dict, global_config) | ||||||
|         if pydantic_results: |         if pydantic_results: | ||||||
| 
 | 
 | ||||||
| @ -238,6 +238,7 @@ def sql_inset_predict(global_config): | |||||||
|         config.logger.info(f"成功插入或更新 {affected_rows} 条记录") |         config.logger.info(f"成功插入或更新 {affected_rows} 条记录") | ||||||
|     config.db_mysql.close() |     config.db_mysql.close() | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| def predict_main(): | def predict_main(): | ||||||
|     """ |     """ | ||||||
|     主预测函数,用于从 ETA 获取数据、处理数据、训练模型并进行预测。 |     主预测函数,用于从 ETA 获取数据、处理数据、训练模型并进行预测。 | ||||||
| @ -318,7 +319,7 @@ def predict_main(): | |||||||
| 
 | 
 | ||||||
|         # 数据处理 |         # 数据处理 | ||||||
|         df = zhoududatachuli(df_zhibiaoshuju, df_zhibiaoliebiao, y=global_config['y'], dataset=dataset, add_kdj=add_kdj, is_timefurture=is_timefurture, |         df = zhoududatachuli(df_zhibiaoshuju, df_zhibiaoliebiao, y=global_config['y'], dataset=dataset, add_kdj=add_kdj, is_timefurture=is_timefurture, | ||||||
|                                 end_time=end_time) |                              end_time=end_time) | ||||||
| 
 | 
 | ||||||
|     else: |     else: | ||||||
|         # 读取数据 |         # 读取数据 | ||||||
|  | |||||||
| @ -1,17 +0,0 @@ | |||||||
| ds,NHITS,Informer,LSTM,iTransformer,TSMixer,TSMixerx,PatchTST,RNN,GRU,TCN,BiTCN,DilatedRNN,MLP,DLinear,NLinear,TFT,StemGNN,MLPMultivariate,TiDE,DeepNPTS,y,min_within_quantile,max_within_quantile,min_price,max_price,id,CREAT_DATE,序号,LOW_PRICE,HIGH_PRICE,ACCURACY |  | ||||||
| 2024-12-09,71.63069,72.14122,70.19943,69.644196,71.80898,70.52471,71.53064,67.38501,70.75815,73.546684,71.92033,70.327484,71.95594,71.18323,71.882935,71.90951,73.94965,72.28691,71.25138,73.34018,72.13999938964844,67.38501,73.94965,67.38501,73.94965,51,2024-12-06,5.0,70.92,72.65,1.0 |  | ||||||
| 2024-12-10,71.14343,71.9462,70.405106,69.48242,71.70601,70.66241,71.57484,67.23587,70.46323,73.37324,71.720894,70.45846,71.88132,72.34705,71.75997,72.41326,73.74943,72.31887,71.47953,72.78831,72.19000244140625,67.23587,73.74943,67.23587,73.74943,52,2024-12-06,4.0,70.73,71.77,1.0 |  | ||||||
| 2024-12-11,71.71588,72.31544,70.175125,69.58213,71.59609,70.91783,71.54794,67.433334,70.518196,73.76477,71.84062,70.746284,72.27111,71.85789,70.77939,72.912704,73.91716,72.42111,71.47695,72.61624,73.5199966430664,67.433334,73.91716,67.433334,73.91716,53,2024-12-06,3.0,72.15,73.75,1.0 |  | ||||||
| 2024-12-12,72.46348,71.87648,70.26041,69.922165,71.65103,70.689384,71.72716,67.54506,70.99872,73.52567,71.78495,70.777115,72.34328,72.756325,70.9607,73.391495,73.944244,72.465836,71.445244,71.69109,73.88999938964844,67.54506,73.944244,67.54506,73.944244,54,2024-12-06,2.0,72.42,74.0,0.9647113924050618 |  | ||||||
| 2024-12-13,72.85073,72.36679,70.489136,69.759766,71.78641,70.69935,71.60861,67.47295,70.81146,73.85618,71.966835,70.923485,72.63866,72.29209,71.11011,73.777534,73.82516,72.58803,71.807915,72.6083,,67.47295,73.85618,67.47295,73.85618,55,2024-12-06,1.0,73.3,74.59,0.43114728682170156 |  | ||||||
| 2024-12-10,71.97169,73.11586,71.41545,70.59598,71.81998,71.541016,72.397606,68.78458,71.30409,73.65467,71.78349,71.03456,71.8188,71.85696,72.014534,72.33824,73.77812,72.2061,71.36985,72.46154,72.19000244140625,68.78458,73.77812,68.78458,73.77812,56,2024-12-09,4.0,70.73,71.77,1.0 |  | ||||||
| 2024-12-11,72.35509,73.087166,71.63927,70.430176,71.82658,71.76624,72.57018,68.59579,71.03884,73.45442,71.75791,71.15297,72.066956,72.28318,72.36303,72.75233,73.594864,72.24322,71.16944,72.39418,73.5199966430664,68.59579,73.594864,68.59579,73.594864,57,2024-12-09,3.0,72.15,73.75,0.9030400000000004 |  | ||||||
| 2024-12-12,73.268654,73.31714,71.39498,70.65145,71.76794,71.6176,73.038315,68.85988,71.12751,73.84763,71.82094,71.41621,72.99199,72.42496,71.474464,73.17588,73.74838,72.31574,71.692444,72.48112,73.88999938964844,68.85988,73.84763,68.85988,73.84763,58,2024-12-09,2.0,72.42,74.0,0.9035632911392374 |  | ||||||
| 2024-12-13,73.11824,73.70929,71.442184,70.72518,71.878975,71.82435,73.13541,68.760376,71.51967,73.62109,71.86448,71.44768,72.952484,72.107254,72.57947,73.61362,73.77317,72.3857,71.873566,72.40313,,68.760376,73.77317,68.760376,73.77317,59,2024-12-09,1.0,73.3,74.59,0.36679844961239827 |  | ||||||
| 2024-12-11,72.372795,72.6546,71.37516,71.41169,71.977135,72.13211,72.774284,69.25488,71.62906,73.70365,72.27147,71.489716,72.77891,71.61932,72.090294,72.166916,73.72794,72.78662,71.960884,72.473854,73.5199966430664,69.25488,73.72794,69.25488,73.72794,61,2024-12-10,3.0,72.15,73.75,0.9862125000000024 |  | ||||||
| 2024-12-12,72.91286,72.80857,71.612946,71.193184,72.05397,72.48625,72.85768,69.06833,71.35641,73.5444,72.19988,71.585815,73.47307,72.22734,72.219765,72.48997,73.544106,72.96339,71.66363,72.122116,73.88999938964844,69.06833,73.5444,69.06833,73.5444,62,2024-12-10,2.0,72.42,74.0,0.7116455696202503 |  | ||||||
| 2024-12-13,73.01643,72.78796,71.3701,71.516174,72.21711,72.4036,73.66599,69.338615,71.467476,73.9247,72.2582,71.84913,73.643265,71.70761,72.41268,72.85244,73.69811,72.91225,71.75731,71.76489,,69.338615,73.9247,69.338615,73.9247,63,2024-12-10,1.0,73.3,74.59,0.4842635658914738 |  | ||||||
| 2024-12-12,73.82699,73.58274,72.81162,73.18572,72.8827,72.958374,74.36039,71.09452,72.673904,74.025635,73.17438,72.72577,73.668365,72.48243,73.224655,74.286606,73.70824,73.668076,72.934685,73.16352,73.88999938964844,71.09452,74.36039,71.09452,74.36039,66,2024-12-11,2.0,72.42,74.0,1.0 |  | ||||||
| 2024-12-13,74.025696,73.275696,73.0588,72.85848,72.719444,73.238945,74.05083,71.060295,72.455666,73.90173,72.966385,72.81345,73.77382,72.488365,73.78919,74.5482,73.52537,73.58226,72.8307,73.26384,,71.060295,74.5482,71.060295,74.5482,67,2024-12-11,1.0,73.3,74.59,0.9675968992247993 |  | ||||||
| 2024-12-13,73.624176,73.15132,73.069374,73.517944,73.56706,74.08185,73.880775,71.83479,73.23418,74.28763,73.372154,73.431366,73.51813,73.90266,73.87494,74.43155,73.691505,73.46715,73.56533,73.50562,,71.83479,74.43155,71.83479,74.43155,71,2024-12-12,1.0,73.3,74.59,0.877170542635658 |  | ||||||
| 2024-12-13,73.624176,73.15132,73.069374,73.517944,73.56706,74.08185,73.880775,71.83479,73.23418,74.28763,73.372154,73.431366,73.51813,73.90266,73.87494,74.43155,73.691505,73.46715,73.56533,73.50562,,71.83479,74.43155,71.83479,74.43155,76,2024-12-13,1.0,73.3,74.59,0.877170542635658 |  | ||||||
| 
 | 
		Loading…
	
		Reference in New Issue
	
	Block a user