<jupyter_start><jupyter_text>读取沪深300行情数据，因为按教程的开展进度，我们目前还没有部署好QUANTAXIS，所以没有直接调用行情数据的方法。在这个教学例子中，只能用预先保存好的行情数据文件代替。行情数据文件的实际路径需要按照你的部署目录修改，请自行编辑下面的路径字符串。<jupyter_code># 如果加载不到这个例子数据文件，请自行下载数据文件保存到用户目录，并修改下面的存储路径的用户名
market_df = pd.read_pickle(u'C:\\Users\\azai\\OneDrive\\Documents\\kline_399300_60min_21-01-29_15_00.pickle')
# 日线
#market_df = pd.read_pickle(u'C:\\Users\\azai\\OneDrive\\Documents\\kline_399300_day_21-01-29_00_00.pickle')

# 兼容日线和小时线的处理。双重索引的行情数据结构来自QUANTAXIS:QADataStruct
if('type' in market_df.columns):
    market_df['date'] = pd.to_datetime(market_df.index.get_level_values(level=0))<jupyter_output><empty_output><jupyter_text>因为 Prophet 所需要的两列名称是 ‘ds’ 和 ‘y’，其中，’ds’ 表示时间戳，’y’ 表示时间序列的值，因此通常来说都需要修改 pd.dataframe 的列名字。如果原来的两列名字是 ‘timestamp’ 和 ‘value’ 的话，只需要这样写：<jupyter_code>df = market_df.reset_index().rename(columns={'date':'ds', 'close':'y'})
df['y'] = np.log(df['y'])

# 构造模型并且训练数据
model = Prophet()
model.fit(df);

# 预测未来的periods个数据
future = model.make_future_dataframe(periods=22) #forecasting for 1 year from now.
forecast = model.predict(future)

plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['figure.figsize']=(20,10)
plt.style.use('ggplot')

figure = model.plot(forecast)
for changepoint in model.changepoints:
    plt.axvline(changepoint,ls='--', lw=1)
two_years = market_df.copy()
code = market_df.index.get_level_values(level=1)[0]
forecast['code'] = code
forecast = forecast.rename(columns={'ds':'date'}).set_index(['date', 'code'])
two_years = two_years.reindex(columns=[*two_years.columns,
                                        *['yhat', 'yhat_upper', 'yhat_lower']])
two_years.loc[:, ['yhat', 'yhat_upper', 'yhat_lower']] = forecast.loc[two_years.index,
                                                                      ['yhat', 'yhat_upper', 'yhat_lower']]
two_years['yhat']=np.exp(two_years.yhat)
two_years['yhat_upper']=np.exp(two_years.yhat_upper)
two_years['yhat_lower']=np.exp(two_years.yhat_lower)
two_years[['close', 'yhat']].plot()
fig, ax1 = plt.subplots()
each_day = market_df.index.get_level_values(level=0)
ax1.plot(each_day, two_years.close)
ax1.plot(each_day, two_years.yhat)
ax1.plot(each_day, two_years.yhat_upper, color='black',  linestyle=':', alpha=0.5)
ax1.plot(each_day, two_years.yhat_lower, color='black',  linestyle=':', alpha=0.5)

ax1.set_title(u'沪深300时机走势(橙色)与沪深300预测波动置信区间(黑虚线)')
ax1.set_ylabel('Price')
ax1.set_xlabel('Date')
print(each_day[-1])
model.changepoints<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16914423463238248, 'fraction_numerical': 0.027320208919244676, 'mean_word_length': 6.238372093023256, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '284740', 'license': 'MIT', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1214, 'n_words': 109, 'path': '996Quant-master/chapter_02/l01_fbprophet_hs300.ipynb', 'repo_name': 'wangdecheng/QAStrategy', 'size': '2489'}