Kronos icon indicating copy to clipboard operation
Kronos copied to clipboard

看起来长线预测不一定准确?使用prediction_example文件简单修改做未来预测,以01810.HK为例子

Open Kinghammer1 opened this issue 1 year ago • 7 comments

Image 如图,以2022到2025的长线预测为准,预测以后直接暴跌,看起来非常不正常。是模型不支持长线?还是代码问题? import pandas as pd import matplotlib.pyplot as plt import sys sys.path.append("./") from model import Kronos, KronosTokenizer, KronosPredictor import matplotlib as mpl # 设置字体 try: mpl.rcParams['font.sans-serif'] = ['Heiti TC'] mpl.rcParams['axes.unicode_minus'] = False except: print("设置中文字体失败,可能会显示乱码")

def plot_prediction(historical_df, pred_df): """ 绘制历史数据和预测数据

参数:
historical_df: 历史数据DataFrame (只包含实际存在的数据)
pred_df: 预测数据DataFrame (包含未来时间戳的预测值)
"""
# 创建图形
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8), sharex=True)

# 1. 绘制收盘价
# 历史收盘价
ax1.plot(historical_df['timestamps'], historical_df['close'], 
         label='历史数据', color='blue', linewidth=1.5)

# 预测收盘价
ax1.plot(pred_df.index, pred_df['close'], 
         label='预测数据', color='red', linestyle='--', linewidth=1.5)

# 标记预测起点
if len(historical_df) > 0:
    last_hist = historical_df.iloc[-1]
    ax1.scatter(last_hist['timestamps'], last_hist['close'], 
               color='green', s=50, zorder=5, label='预测起点')

ax1.set_ylabel('收盘价', fontsize=12)
ax1.legend(loc='upper left', fontsize=10)
ax1.grid(True, alpha=0.3)
ax1.set_title('股票价格历史与预测')

# 2. 绘制成交量
# 历史成交量
ax2.bar(historical_df['timestamps'], historical_df['volume'], 
        alpha=0.7, label='历史成交量', color='blue', width=0.8)

# 预测成交量
# 计算合适的条形宽度
if len(pred_df) > 1:
    time_diff = (pred_df.index[1] - pred_df.index[0]).days
    bar_width = max(0.1, min(1.0, time_diff * 0.8))
else:
    bar_width = 0.8
    
ax2.bar(pred_df.index, pred_df['volume'], 
        alpha=0.7, label='预测成交量', color='red', width=bar_width)

ax2.set_ylabel('成交量', fontsize=12)
ax2.legend(loc='upper left', fontsize=10)
ax2.grid(True, alpha=0.3)

# 设置x轴格式
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

1. Load Model and Tokenizer

tokenizer = KronosTokenizer.from_pretrained("NeoQuasar/Kronos-Tokenizer-base") model = Kronos.from_pretrained("NeoQuasar/Kronos-base")

2. Instantiate Predictor

predictor = KronosPredictor(model, tokenizer, device="mps", max_context=512)

3. Prepare Data

df = pd.read_csv("./examples/data/01810_20220201_20251019_Daily_K.csv") df['timestamps'] = pd.to_datetime(df['timestamps']) total_data_points = len(df) print(f"数据总条数: {total_data_points}")

lookback = 700 pred_len = 300

x_df = df.loc[:lookback-1, ['open', 'high', 'low', 'close', 'volume', 'amount']] x_timestamp = df.loc[:lookback-1, 'timestamps']

生成真正的未来时间戳

last_timestamp = x_timestamp.iloc[-1]

计算时间间隔(基于历史数据)

if len(x_timestamp) > 1: time_diffs = x_timestamp.diff().dropna() avg_interval = time_diffs.mean() else: avg_interval = pd.Timedelta(days=1)

生成未来时间戳

y_timestamp = pd.Series([last_timestamp + avg_interval * (i+1) for i in range(pred_len)])

print(f"历史数据长度: {len(x_df)}") print(f"生成的未来时间戳长度: {len(y_timestamp)}")

4. Make Prediction

pred_df = predictor.predict( df=x_df, x_timestamp=x_timestamp, y_timestamp=y_timestamp, pred_len=pred_len, T=1.0, top_p=0.9, sample_count=1, verbose=True )

5. Visualize Results

print("Forecasted Data Head:") print(pred_df.head(5))

使用修改后的绘图函数

plot_prediction(df, pred_df)

Kinghammer1 avatar Oct 19 '25 12:10 Kinghammer1

我觉得你预测的没有问题, 这个模型就是很容易在大涨之后预测出现大跌, 因为, 大涨之后出现大跌才是股票的常态。 另外, 长期的预测, 理论上肯定是没有短期准的

HLearning avatar Oct 19 '25 15:10 HLearning

我觉得你预测的没有问题, 这个模型就是很容易在大涨之后预测出现大跌, 因为, 大涨之后出现大跌才是股票的常态。 另外, 长期的预测, 理论上肯定是没有短期准的

我后面又试了几个一下,确实模型就会这样,尤其是对这只股票基本上都是看跌。还在摸索中,感觉这个项目还是更适合科研,实战可以不太够

Kinghammer1 avatar Oct 20 '25 03:10 Kinghammer1

我觉得你预测的没有问题, 这个模型就是很容易在大涨之后预测出现大跌, 因为, 大涨之后出现大跌才是股票的常态。 另外, 长期的预测, 理论上肯定是没有短期准的

我后面又试了几个一下,确实模型就会这样,尤其是对这只股票基本上都是看跌。还在摸索中,感觉这个项目还是更适合科研,实战可以不太够

仅靠OCHLV这5个数据上实盘, 几乎是不可能的。 股票的走势, 受多方因素影响

HLearning avatar Oct 20 '25 03:10 HLearning

股票受消息面的影响相对要比其他类型市场大的多

loyedison92 avatar Nov 05 '25 20:11 loyedison92

我觉得你预测的没有问题, 这个模型就是很容易在大涨之后预测出现大跌, 因为, 大涨之后出现大跌才是股票的常态。 另外, 长期的预测, 理论上肯定是没有短期准的

我后面又试了几个一下,确实模型就会这样,尤其是对这只股票基本上都是看跌。还在摸索中,感觉这个项目还是更适合科研,实战可以不太够

仅靠OCHLV这5个数据上实盘, 几乎是不可能的。 股票的走势, 受多方因素影响

请问一下,需要考虑哪些方面呢

world2025 avatar Apr 02 '26 09:04 world2025

预测长度不要过长 概率性的东西会把不准确率放大,蒙特卡洛+置信度+信号强度+一些阈值参数筛选过滤 可以提高信号准确性

loyedison92 avatar Apr 02 '26 09:04 loyedison92

信号强度指的是?

world2025 avatar Apr 02 '26 09:04 world2025