shiyu-coder/Kronos
Kronos: A Foundation Model for the Language of Financial Markets
Kronos is the first open-source foundation model for financial candlesticks (K-lines), trained on data from over 45 global exchanges.
Kronos is the first open-source foundation model for financial candlesticks (K-lines), trained on data from over 45 global exchanges.
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:
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 | ❌ |
|
|
Forecasting 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.
|
|
Create an instance of KronosPredictor, passing the model, tokenizer, and desired device.
|
|
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']. volume and amount are optional.x_timestamp: A pandas Series of timestamps corresponding to the historical data in df.y_timestamp: A pandas Series of timestamps for the future periods you want to predict.
|
|
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.
|
|
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.
|
|
Important Requirements for Batch Prediction:
pred_len)['open', 'high', 'low', 'close']volume and amount columns are optional and will be filled with zeros if missingThe 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:
requirements.txt installed.qlib. Please install it:
|
|
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_path and pretrained_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.
|
|
After 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.
|
|
The 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.
|
|
The 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.
|
|
The 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:
QlibDataset is an example. For different data sources or formats, you will need to adapt the data loading and preprocessing logic.📝 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:
|
|
This project is licensed under the MIT License.