Commit 5210b5e6 authored by 吕先亚's avatar 吕先亚

AI Forecast Improve

parent a4c020e0
......@@ -20,12 +20,17 @@ toForecast = False # False means test, True means forecast
syncData = False # 开启会同步数据库指数及基金数据
uploadData = False # 开启会上传预测结果
doReport = True # 开启会生成Excel报告
# JIFU_SPX_OPEPS_CURRQ_TTM
# JIFU_SPX_OPEPS_YMAE1
# JIFU_SPX_OPEPS_YMAE2
# JIFU_SPX_OPEPS_YMAE3
eps_eco = 'JIFU_SPX_OPEPS_CURRQ_TTM' # 选取的eps指标名称
# 待预测指数
PREDICT_LIST = [67]
# PREDICT_LIST = [67, 121, 122, 123, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
# 164, 165, 166, 167, 168, 169, 170, 171, 174, 175, 177, 178]
eco = [65, 66, 74, 134, 191]
eco = [65, 66, 74, 134, 191, 192, 193, 194]
index = [67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 105, 106, 116, 117, 138, 139, 142, 143, 140, 141, 144, 145, 146]
# fund = [121, 122, 123, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165,
# 166, 167, 168, 169, 170, 171, 174, 175, 177, 178]
......
import datetime
import json
import math
import random
import pandas as pd
......@@ -58,11 +59,13 @@ def do_reporter2(records=None, excel_name=None):
datas.append(data)
RMSE = get_RMSE(datas)
RefRMSE = get_RefRMSE(datas)
RevisedReferenceRMSE = get_RevisedReferenceRMSE(datas)
symbol_index_dict = {symbol: index for index, symbol in enumerate(symbols)}
sorted_data = sorted(datas, key=lambda x: symbol_index_dict[x['Ticker'].split(' ')[0]])
pf = pd.DataFrame(sorted_data)
pf['RMSE'] = RMSE
pf['RefRMSE'] = RefRMSE
pf['RevisedReferenceRMSE'] = RevisedReferenceRMSE
pf.to_excel(excel_name if excel_name else "Forcast_Report_chu.xlsx", index=False)
......@@ -98,6 +101,36 @@ def get_RefRMSE(datas):
datas]) / len(datas)) ** 0.5
def get_RevisedReferenceRMSE(datas):
"""
Revised Reference RMSE
@param data: 预测值
@return:
"""
datas = [data for data in datas if data.get('real outcome label') is not None]
tags = {v: k for k, v in LABEL_TAG.items()}
# Initialize the sum of squared differences
total_sum = 0
# Iterate over each data point
for data in datas:
# Get the true label
true_label = tags.get(data['real outcome label'])
# Sum up the squared differences for all possible deviations (-2 to 2)
for deviation in range(-2, 3):
total_sum += (true_label - deviation) ** 2
# Calculate the average by dividing by 5N
average = total_sum / (5 * len(datas))
# Take the square root to get the final Rev. Ref. RMSE
revised_ref_rmse = math.sqrt(average)
return revised_ref_rmse
def is_right(id, type, start, predict):
predict_term = 21
navs = []
......
......@@ -112,7 +112,7 @@ def save_sp500():
wb.close()
def sync_sp500(day):
def sync_sp500(ticker, day):
file = Path(__file__).parent / 'resources/sp-500-eps-est_USA20241014.xlsx'
if day:
files = list_files_sorted_by_name(Path(__file__).parent / 'resources', day)
......@@ -127,6 +127,7 @@ def sync_sp500(day):
actuals = "ACTUALS"
actuals_row = 0
datas = []
estimates_datas = []
# 遍历A列
for row in range(100, 300):
cell_value = ws[f'A{row}'].value
......@@ -160,6 +161,13 @@ def sync_sp500(day):
'eps': ws[f'C{i}'].value,
'releaseDate': datetime.strptime(str(file)[-13:-5], "%Y%m%d")}
datas.append(data)
else:
# 未来数据作为备用
data = {'date': date_value,
'eps': ws[f'C{i}'].value}
data["releaseDate"] = data['date'] + timedelta(days=1)
data["date"] = data['releaseDate']
estimates_datas.append(data)
for i in range(actuals_row + 1, ws.max_row):
if ws[f'A{i}'].value is None:
break
......@@ -171,8 +179,22 @@ def sync_sp500(day):
datas.append(data)
wb.close()
datas = pd.DataFrame(datas[::-1])
datas['eps'] = datas['eps'].shift(-1)
datas['close'] = datas['eps'].rolling(window=4).sum().round(2)
if ticker == 'JIFU SPX OPEPS CURRQ TTM':
datas['eps'] = datas['eps'].shift(-1)
datas['close'] = datas['eps'].rolling(window=4).sum().round(2)
elif ticker == 'JIFU SPX OPEPS YMAE1':
datas = datas.append(estimates_datas[:-2:-1], ignore_index=True)
datas['close'] = datas['eps'].rolling(window=4).sum().round(2)
datas['close'] = datas['close'].shift(-2)
elif ticker == 'JIFU SPX OPEPS YMAE2':
datas = datas.append(estimates_datas[:-3:-1], ignore_index=True)
datas['close'] = datas['eps'].rolling(window=4).sum().round(2)
datas['close'] = datas['close'].shift(-3)
elif ticker == 'JIFU SPX OPEPS YMAE3':
datas = datas.append(estimates_datas[:-4:-1], ignore_index=True)
datas['close'] = datas['eps'].rolling(window=4).sum().round(2)
datas['close'] = datas['close'].shift(-4)
datas = datas[datas['date'] <= report_day]
datas.ffill(inplace=True)
datas.dropna(inplace=True)
return datas.to_dict(orient="records")[-1::] if day else datas.to_dict(orient="records")
......@@ -192,4 +214,4 @@ def send_sp500(content, attach_paths):
if __name__ == '__main__':
# print(list_files_sorted_by_name(Path(__file__).parent / 'resources'))
# save_sp500()
sync_sp500(day=None)
sync_sp500(ticker='JIFU SPX OPEPS YMAE3', day=None)
......@@ -248,7 +248,7 @@ class EcoSync(EcoSync):
if datum.get("source") == "calculating":
logger.debug(f'start sync ticker[{datum["bloombergTicker"]}]')
while True:
datas = sync_sp500(self.datum_start_date(datum['id']))
datas = sync_sp500(datum.get("bloombergTicker"), self.datum_start_date(datum['id']))
if datas:
self.store_date(datum['id'], datas)
else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment