cuda 11.8 python 3.10 - 3.12 组件 最低要求 推荐配置 GPU型号 NVIDIA GPU with CUDA 11.8+ RTX 3080, RTX 4080, A100 显存(VRAM) 6GB 8GB+ CUDA版本 11.8 12.1+ 驱动版本 515.65+ 最新驱动
Kronos 量化模型训练手册 (Sim-to-Real 版) 核心理念: 不要等待市场给你机会,自己创造 10 万次机会喂给 AI,再用真实市场做校验。 目标形态: 均线压制下的假突破(震荡 -> 诱多阳线 -> 猎杀阴线)。
📂 第一阶段:军火库储备(数据生成) 目标: 生产 1 万 - 10 万条高质量的“合成数据”,覆盖各种变异形态。
- 手工打造“黄金样本” (必须做) 你是教官,你要先定义标准。
程序: kline_surgeon.py (K 线外科医生)
操作:
运行 streamlit run kline_surgeon.py。
正样本 (Pos): 捏造 20-50 个完美的“诱多杀跌”形态。
硬负样本 (Hard Neg): 捏造 20-50 个“看起来像突破,结果真的飞了”的形态(教 AI 别乱空)。
用途: 这些数据既可以混入训练集,也可以留作最后的“考卷”。
- 批量生产“常规弹药” (主力军) 利用规则生成器,瞬间生成海量数据。
程序: generator_v6.py (或 v4/v5,建议用 v6 真实分布版)
操作:
打开脚本,设置生成数量(比如循环 10,000 次)。
它会自动按 15% 强趋势 / 45% 宽幅震荡 / 40% 箱体的比例生成。
输出: 保存为 .npy 或 .csv 格式的训练集(需微调代码对接 Dataset)。
- (进阶) 伪造“高保真噪音” 如果模型在实盘总被噪音骗,用这个加餐。
程序: gan_kline_forger.py (GAN 伪造大师)
操作:
喂给它真实的 doupo.csv 或你筛选出的假突破片段。
训练 200 轮后,让它生成几千张带有真实市场“毛刺感”的 K 线。
🏋️ 第二阶段:封闭特训(预训练) 目标: 让 Kronos 在合成数据上把“假突破”刻入 DNA,达到 99% 准确率。
程序: train_silly_money.py (终极魔改版)
关键配置 (Config):
Python
MODEL_PATH = "NeoQuasar/Kronos-base" SYNTHETIC_WEIGHTS = None # 不加载旧权重 DATA_DIR = "generated_data" # 指向你的合成数据文件夹 AUGMENT = True # 开启数据增强 (随机缩放、加噪) EPOCHS = 20 ~ 50 # 合成数据多跑几轮 LR = 2e-5 # 全量微调标准学习率 执行命令: python train_silly_money.py
产出: best_full_finetune.pth (这是模型的“出师证明”)。
🎯 第三阶段:实战演习(迁移学习 & 微调) 目标: 让模型适应真实数据的“手感”(滑点、跳空、非理性波动)。
数据准备: 你的 89 条(或更多)真实标注数据。
程序: train_silly_money.py (同一脚本,不同配置)
关键配置 (Config):
Python
MODEL_PATH = "NeoQuasar/Kronos-base" SYNTHETIC_WEIGHTS = "best_full_finetune.pth" # 🔥 加载刚才训练好的脑子 DATA_DIR = "real_data" # 指向真实数据文件夹
FREEZE_BACKBONE = True # 锁死底座,只练眼睛 LR = 1e-4 # 只练头,学习率可以大一点 EPOCHS = 50 ~ 100 # 数据少,多跑几轮保证收敛 执行命令: python train_silly_money.py
产出: silly_money_final_weapon.pth (最终实盘模型)。
🔍 第四阶段:视觉验收(盲测) 目标: 只有你亲眼认可的信号,才能开实盘。
程序: verify_visual.py
操作:
加载 silly_money_final_weapon.pth。
让它跑一遍你没见过的 2024 年数据。
它会画出 K 线图,并在图上标出 PRED: BUY (Conf: 98%)。
通过标准: 随机抽查 10 张图,至少有 8-9 张是你觉得“这单能做”的。
🛠️ 附录:核心技术检查清单 (Checklist) 在运行任何训练之前,请确保代码里包含以下 3 大护法,否则必报错:
✅ 6 列数据补全: 必须包含 Amount (成交额)。如果 CSV 里没有,代码里必须有 df['amount'] = close * volume。
✅ 取模大法 (Modulo Hack): Tokenizer 输出的 ID 必须 % 1024。这是解决 CUDA error: device-side assert triggered 的唯一解。
✅ 归一化 (Log + Z-Score): Volume 和 Amount 必须做 np.log1p,价格必须做 (p - mean) / std。 安装流程 sudo apt update sudo apt install git
git clone https://github.com/wyhgood/Kronos.git cd Kronos
ls -la
python3 -m venv kronos_env
source kronos_env/bin/activate
cd Kronos
pip install -r requirements.txt
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
test pytorch python3 test_gpu_pytorch.py
test kronos 安装是否成功 python3 test_kronos.py
作者:Henry的量化策略小作坊 链接:https://juejin.cn/post/7568710909314334758 来源:稀土掘金 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Kronos is the first open-source foundation model for financial candlesticks (K-lines), trained on data from over 45 global exchanges.
- 🚩 [2025.11.10] Kronos has been accpeted by AAAI 2026.
- 🚩 [2025.08.17] We have released the scripts for fine-tuning! Check them out to adapt Kronos to your own tasks.
- 🚩 [2025.08.02] Our paper is now available on arXiv!
Kronos is a family of decoder-only foundation models, pre-trained specifically for the "language" of financial markets—K-line sequences. Unlike general-purpose TSFMs, Kronos is designed to handle the unique, high-noise characteristics of financial data. It leverages a novel two-stage framework:
- A specialized tokenizer first quantizes continuous, multi-dimensional K-line data (OHLCV) into hierarchical discrete tokens.
- A large, autoregressive Transformer is then pre-trained on these tokens, enabling it to serve as a unified model for diverse quantitative tasks.
We have set up a live demo to visualize Kronos's forecasting results. The webpage showcases a forecast for the BTC/USDT trading pair over the next 24 hours.
We release a family of pre-trained models with varying capacities to suit different computational and application needs. All models are readily accessible from the Hugging Face Hub.
| Model | Tokenizer | Context length | Params | Open-source |
|---|---|---|---|---|
| Kronos-mini | Kronos-Tokenizer-2k | 2048 | 4.1M | ✅ NeoQuasar/Kronos-mini |
| Kronos-small | Kronos-Tokenizer-base | 512 | 24.7M | ✅ NeoQuasar/Kronos-small |
| Kronos-base | Kronos-Tokenizer-base | 512 | 102.3M | ✅ NeoQuasar/Kronos-base |
| Kronos-large | Kronos-Tokenizer-base | 512 | 499.2M | ❌ |
- Install Python 3.10+, and then install the dependencies:
pip install -r requirements.txtForecasting with Kronos is straightforward using the KronosPredictor class. It handles data preprocessing, normalization, prediction, and inverse normalization, allowing you to get from raw data to forecasts in just a few lines of code.
Important Note: The max_context for Kronos-small and Kronos-base is 512. This is the maximum sequence length the model can process. For optimal performance, it is recommended that your input data length (i.e., lookback) does not exceed this limit. The KronosPredictor will automatically handle truncation for longer contexts.
Here is a step-by-step guide to making your first forecast.
First, load a pre-trained Kronos model and its corresponding tokenizer from the Hugging Face Hub.
from model import Kronos, KronosTokenizer, KronosPredictor
# Load from Hugging Face Hub
tokenizer = KronosTokenizer.from_pretrained("NeoQuasar/Kronos-Tokenizer-base")
model = Kronos.from_pretrained("NeoQuasar/Kronos-small")Create an instance of KronosPredictor, passing the model, tokenizer, and desired device.
# Initialize the predictor
predictor = KronosPredictor(model, tokenizer, device="cuda:0", max_context=512)The predict method requires three main inputs:
df: A pandas DataFrame containing the historical K-line data. It must include columns['open', 'high', 'low', 'close'].volumeandamountare optional.x_timestamp: A pandas Series of timestamps corresponding to the historical data indf.y_timestamp: A pandas Series of timestamps for the future periods you want to predict.
import pandas as pd
# Load your data
df = pd.read_csv("./data/XSHG_5min_600977.csv")
df['timestamps'] = pd.to_datetime(df['timestamps'])
# Define context window and prediction length
lookback = 400
pred_len = 120
# Prepare inputs for the predictor
x_df = df.loc[:lookback-1, ['open', 'high', 'low', 'close', 'volume', 'amount']]
x_timestamp = df.loc[:lookback-1, 'timestamps']
y_timestamp = df.loc[lookback:lookback+pred_len-1, 'timestamps']Call the predict method to generate forecasts. You can control the sampling process with parameters like T, top_p, and sample_count for probabilistic forecasting.
# Generate predictions
pred_df = predictor.predict(
df=x_df,
x_timestamp=x_timestamp,
y_timestamp=y_timestamp,
pred_len=pred_len,
T=1.0, # Temperature for sampling
top_p=0.9, # Nucleus sampling probability
sample_count=1 # Number of forecast paths to generate and average
)
print("Forecasted Data Head:")
print(pred_df.head())The predict method returns a pandas DataFrame containing the forecasted values for open, high, low, close, volume, and amount, indexed by the y_timestamp you provided.
For efficient processing of multiple time series, Kronos provides a predict_batch method that enables parallel prediction on multiple datasets simultaneously. This is particularly useful when you need to forecast multiple assets or time periods at once.
# Prepare multiple datasets for batch prediction
df_list = [df1, df2, df3] # List of DataFrames
x_timestamp_list = [x_ts1, x_ts2, x_ts3] # List of historical timestamps
y_timestamp_list = [y_ts1, y_ts2, y_ts3] # List of future timestamps
# Generate batch predictions
pred_df_list = predictor.predict_batch(
df_list=df_list,
x_timestamp_list=x_timestamp_list,
y_timestamp_list=y_timestamp_list,
pred_len=pred_len,
T=1.0,
top_p=0.9,
sample_count=1,
verbose=True
)
# pred_df_list contains prediction results in the same order as input
for i, pred_df in enumerate(pred_df_list):
print(f"Predictions for series {i}:")
print(pred_df.head())Important Requirements for Batch Prediction:
- All series must have the same historical length (lookback window)
- All series must have the same prediction length (
pred_len) - Each DataFrame must contain the required columns:
['open', 'high', 'low', 'close'] volumeandamountcolumns are optional and will be filled with zeros if missing
The predict_batch method leverages GPU parallelism for efficient processing and automatically handles normalization and denormalization for each series independently.
For a complete, runnable script that includes data loading, prediction, and plotting, please see examples/prediction_example.py.
Running this script will generate a plot comparing the ground truth data against the model's forecast, similar to the one shown below:
Additionally, we provide a script that makes predictions without Volume and Amount data, which can be found in examples/prediction_wo_vol_example.py.
We provide a complete pipeline for finetuning Kronos on your own datasets. As an example, we demonstrate how to use Qlib to prepare data from the Chinese A-share market and conduct a simple backtest.
Disclaimer: This pipeline is intended as a demonstration to illustrate the finetuning process. It is a simplified example and not a production-ready quantitative trading system. A robust quantitative strategy requires more sophisticated techniques, such as portfolio optimization and risk factor neutralization, to achieve stable alpha.
The finetuning process is divided into four main steps:
- Configuration: Set up paths and hyperparameters.
- Data Preparation: Process and split your data using Qlib.
- Model Finetuning: Finetune the Tokenizer and the Predictor models.
- Backtesting: Evaluate the finetuned model's performance.
- First, ensure you have all dependencies from
requirements.txtinstalled. - This pipeline relies on
qlib. Please install it:pip install pyqlib
- You will need to prepare your Qlib data. Follow the official Qlib guide to download and set up your data locally. The example scripts assume you are using daily frequency data.
All settings for data, training, and model paths are centralized in finetune/config.py. Before running any scripts, please modify the following paths according to your environment:
qlib_data_path: Path to your local Qlib data directory.dataset_path: Directory where the processed train/validation/test pickle files will be saved.save_path: Base directory for saving model checkpoints.backtest_result_path: Directory for saving backtesting results.pretrained_tokenizer_pathandpretrained_predictor_path: Paths to the pre-trained models you want to start from (can be local paths or Hugging Face model names).
You can also adjust other parameters like instrument, train_time_range, epochs, and batch_size to fit your specific task. If you don't use Comet.ml, set use_comet = False.
Run the data preprocessing script. This script will load raw market data from your Qlib directory, process it, split it into training, validation, and test sets, and save them as pickle files.
python finetune/qlib_data_preprocess.pyAfter running, you will find train_data.pkl, val_data.pkl, and test_data.pkl in the directory specified by dataset_path in your config.
The finetuning process consists of two stages: finetuning the tokenizer and then the predictor. Both training scripts are designed for multi-GPU training using torchrun.
This step adjusts the tokenizer to the data distribution of your specific domain.
# Replace NUM_GPUS with the number of GPUs you want to use (e.g., 2)
torchrun --standalone --nproc_per_node=NUM_GPUS finetune/train_tokenizer.pyThe best tokenizer checkpoint will be saved to the path configured in config.py (derived from save_path and tokenizer_save_folder_name).
This step finetunes the main Kronos model for the forecasting task.
# Replace NUM_GPUS with the number of GPUs you want to use (e.g., 2)
torchrun --standalone --nproc_per_node=NUM_GPUS finetune/train_predictor.pyThe best predictor checkpoint will be saved to the path configured in config.py.
Finally, run the backtesting script to evaluate your finetuned model. This script loads the models, performs inference on the test set, generates prediction signals (e.g., forecasted price change), and runs a simple top-K strategy backtest.
# Specify the GPU for inference
python finetune/qlib_test.py --device cuda:0The script will output a detailed performance analysis in your console and generate a plot showing the cumulative return curves of your strategy against the benchmark, similar to the one below:
- Raw Signals vs. Pure Alpha: The signals generated by the model in this demo are raw predictions. In a real-world quantitative workflow, these signals would typically be fed into a portfolio optimization model. This model would apply constraints to neutralize exposure to common risk factors (e.g., market beta, style factors like size and value), thereby isolating the "pure alpha" and improving the strategy's robustness.
- Data Handling: The provided
QlibDatasetis an example. For different data sources or formats, you will need to adapt the data loading and preprocessing logic. - Strategy and Backtesting Complexity: The simple top-K strategy used here is a basic starting point. Production-level strategies often incorporate more complex logic for portfolio construction, dynamic position sizing, and risk management (e.g., stop-loss/take-profit rules). Furthermore, a high-fidelity backtest should meticulously model transaction costs, slippage, and market impact to provide a more accurate estimate of real-world performance.
📝 AI-Generated Comments: Please note that many of the code comments within the
finetune/directory were generated by an AI assistant (Gemini 2.5 Pro) for explanatory purposes. While they aim to be helpful, they may contain inaccuracies. We recommend treating the code itself as the definitive source of logic.
If you use Kronos in your research, we would appreciate a citation to our paper:
@misc{shi2025kronos,
title={Kronos: A Foundation Model for the Language of Financial Markets},
author={Yu Shi and Zongliang Fu and Shuo Chen and Bohan Zhao and Wei Xu and Changshui Zhang and Jian Li},
year={2025},
eprint={2508.02739},
archivePrefix={arXiv},
primaryClass={q-fin.ST},
url={https://arxiv.org/abs/2508.02739},
}
This project is licensed under the MIT License.