heyunfei commited on
Commit
fe94c1c
·
verified ·
1 Parent(s): 85653bc

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +7 -333
README.md CHANGED
@@ -1,333 +1,7 @@
1
- <div align="center">
2
- <h2><b>Kronos: A Foundation Model for the Language of Financial Markets </b></h2>
3
- </div>
4
-
5
-
6
- <div align="center">
7
-
8
- </a>
9
- <a href="https://huggingface.co/NeoQuasar">
10
- <img src="https://img.shields.io/badge/🤗-Hugging_Face-yellow" alt="Hugging Face">
11
- </a>
12
- <a href="https://shiyu-coder.github.io/Kronos-demo/"> <img src="https://img.shields.io/badge/🚀-Live_Demo-brightgreen" alt="Live Demo"> </a>
13
- <a href="https://github.com/shiyu-coder/Kronos/graphs/commit-activity">
14
- <img src="https://img.shields.io/github/last-commit/shiyu-coder/Kronos?color=blue" alt="Last Commit">
15
- </a>
16
- <a href="https://github.com/shiyu-coder/Kronos/stargazers">
17
- <img src="https://img.shields.io/github/stars/shiyu-coder/Kronos?color=lightblue" alt="GitHub Stars">
18
- </a>
19
- <a href="https://github.com/shiyu-coder/Kronos/network/members">
20
- <img src="https://img.shields.io/github/forks/shiyu-coder/Kronos?color=yellow" alt="GitHub Forks">
21
- </a>
22
- <a href="./LICENSE">
23
- <img src="https://img.shields.io/github/license/shiyu-coder/Kronos?color=green" alt="License">
24
- </a>
25
-
26
- </div>
27
-
28
- <div align="center">
29
- <!-- Keep these links. Translations will automatically update with the README. -->
30
- <a href="https://zdoc.app/de/shiyu-coder/Kronos">Deutsch</a> |
31
- <a href="https://zdoc.app/es/shiyu-coder/Kronos">Español</a> |
32
- <a href="https://zdoc.app/fr/shiyu-coder/Kronos">français</a> |
33
- <a href="https://zdoc.app/ja/shiyu-coder/Kronos">日本語</a> |
34
- <a href="https://zdoc.app/ko/shiyu-coder/Kronos">한국어</a> |
35
- <a href="https://zdoc.app/pt/shiyu-coder/Kronos">Português</a> |
36
- <a href="https://zdoc.app/ru/shiyu-coder/Kronos">Русский</a> |
37
- <a href="https://zdoc.app/zh/shiyu-coder/Kronos">中文</a>
38
- </div>
39
-
40
- <p align="center">
41
-
42
- <img src="./figures/logo.png" width="100">
43
-
44
- </p>
45
-
46
- > Kronos is the **first open-source foundation model** for financial candlesticks (K-lines),
47
- > trained on data from over **45 global exchanges**.
48
-
49
-
50
- </div>
51
-
52
- ## 📰 News
53
- * 🚩 **[2025.08.17]** We have released the scripts for fine-tuning! Check them out to adapt Kronos to your own tasks.
54
- * 🚩 **[2025.08.02]** Our paper is now available on [arXiv](https://arxiv.org/abs/2508.02739)!
55
-
56
- <p align="center">
57
-
58
- ## 📜 Introduction
59
-
60
- **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:
61
- 1. A specialized tokenizer first quantizes continuous, multi-dimensional K-line data (OHLCV) into **hierarchical discrete tokens**.
62
- 2. A large, autoregressive Transformer is then pre-trained on these tokens, enabling it to serve as a unified model for diverse quantitative tasks.
63
-
64
- <p align="center">
65
- <img src="figures/overview.png" alt="" align="center" width="700px" />
66
- </p>
67
-
68
- ## ✨ Live Demo
69
- 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.
70
-
71
- **👉 [Access the Live Demo Here](https://shiyu-coder.github.io/Kronos-demo/)**
72
-
73
- ## 📦 Model Zoo
74
- 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.
75
-
76
- | Model | Tokenizer | Context length | Param | Open-source |
77
- |--------------|---------------------------------------------------------------------------------| -------------- | ------ |---------------------------------------------------------------------------|
78
- | Kronos-mini | [Kronos-Tokenizer-2k](https://huggingface.co/NeoQuasar/Kronos-Tokenizer-2k) | 2048 | 4.1M | ✅ [NeoQuasar/Kronos-mini](https://huggingface.co/NeoQuasar/Kronos-mini) |
79
- | Kronos-small | [Kronos-Tokenizer-base](https://huggingface.co/NeoQuasar/Kronos-Tokenizer-base) | 512 | 24.7M | ✅ [NeoQuasar/Kronos-small](https://huggingface.co/NeoQuasar/Kronos-small) |
80
- | Kronos-base | [Kronos-Tokenizer-base](https://huggingface.co/NeoQuasar/Kronos-Tokenizer-base) | 512 | 102.3M | ✅ [NeoQuasar/Kronos-base](https://huggingface.co/NeoQuasar/Kronos-base) |
81
- | Kronos-large | [Kronos-Tokenizer-base](https://huggingface.co/NeoQuasar/Kronos-Tokenizer-base) | 512 | 499.2M | ❌ |
82
-
83
-
84
- ## 🚀 Getting Started
85
-
86
- ### Installation
87
-
88
- 1. Install Python 3.10+, and then install the dependencies:
89
-
90
- ```shell
91
- pip install -r requirements.txt
92
- ```
93
-
94
- ### 📈 Making Forecasts
95
-
96
- 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.
97
-
98
- **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.
99
-
100
- Here is a step-by-step guide to making your first forecast.
101
-
102
- #### 1. Load the Tokenizer and Model
103
-
104
- First, load a pre-trained Kronos model and its corresponding tokenizer from the Hugging Face Hub.
105
-
106
- ```python
107
- from model import Kronos, KronosTokenizer, KronosPredictor
108
-
109
- # Load from Hugging Face Hub
110
- tokenizer = KronosTokenizer.from_pretrained("NeoQuasar/Kronos-Tokenizer-base")
111
- model = Kronos.from_pretrained("NeoQuasar/Kronos-small")
112
- ```
113
-
114
- #### 2. Instantiate the Predictor
115
-
116
- Create an instance of `KronosPredictor`, passing the model, tokenizer, and desired device.
117
-
118
- ```python
119
- # Initialize the predictor
120
- predictor = KronosPredictor(model, tokenizer, device="cuda:0", max_context=512)
121
- ```
122
-
123
- #### 3. Prepare Input Data
124
-
125
- The `predict` method requires three main inputs:
126
- - `df`: A pandas DataFrame containing the historical K-line data. It must include columns `['open', 'high', 'low', 'close']`. `volume` and `amount` are optional.
127
- - `x_timestamp`: A pandas Series of timestamps corresponding to the historical data in `df`.
128
- - `y_timestamp`: A pandas Series of timestamps for the future periods you want to predict.
129
-
130
- ```python
131
- import pandas as pd
132
-
133
- # Load your data
134
- df = pd.read_csv("./data/XSHG_5min_600977.csv")
135
- df['timestamps'] = pd.to_datetime(df['timestamps'])
136
-
137
- # Define context window and prediction length
138
- lookback = 400
139
- pred_len = 120
140
-
141
- # Prepare inputs for the predictor
142
- x_df = df.loc[:lookback-1, ['open', 'high', 'low', 'close', 'volume', 'amount']]
143
- x_timestamp = df.loc[:lookback-1, 'timestamps']
144
- y_timestamp = df.loc[lookback:lookback+pred_len-1, 'timestamps']
145
- ```
146
-
147
- #### 4. Generate Forecasts
148
-
149
- 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.
150
-
151
- ```python
152
- # Generate predictions
153
- pred_df = predictor.predict(
154
- df=x_df,
155
- x_timestamp=x_timestamp,
156
- y_timestamp=y_timestamp,
157
- pred_len=pred_len,
158
- T=1.0, # Temperature for sampling
159
- top_p=0.9, # Nucleus sampling probability
160
- sample_count=1 # Number of forecast paths to generate and average
161
- )
162
-
163
- print("Forecasted Data Head:")
164
- print(pred_df.head())
165
- ```
166
-
167
- 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.
168
-
169
- 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.
170
-
171
- ```python
172
- # Prepare multiple datasets for batch prediction
173
- df_list = [df1, df2, df3] # List of DataFrames
174
- x_timestamp_list = [x_ts1, x_ts2, x_ts3] # List of historical timestamps
175
- y_timestamp_list = [y_ts1, y_ts2, y_ts3] # List of future timestamps
176
-
177
- # Generate batch predictions
178
- pred_df_list = predictor.predict_batch(
179
- df_list=df_list,
180
- x_timestamp_list=x_timestamp_list,
181
- y_timestamp_list=y_timestamp_list,
182
- pred_len=pred_len,
183
- T=1.0,
184
- top_p=0.9,
185
- sample_count=1,
186
- verbose=True
187
- )
188
-
189
- # pred_df_list contains prediction results in the same order as input
190
- for i, pred_df in enumerate(pred_df_list):
191
- print(f"Predictions for series {i}:")
192
- print(pred_df.head())
193
- ```
194
-
195
- **Important Requirements for Batch Prediction:**
196
- - All series must have the same historical length (lookback window)
197
- - All series must have the same prediction length (`pred_len`)
198
- - Each DataFrame must contain the required columns: `['open', 'high', 'low', 'close']`
199
- - `volume` and `amount` columns are optional and will be filled with zeros if missing
200
-
201
- The `predict_batch` method leverages GPU parallelism for efficient processing and automatically handles normalization and denormalization for each series independently.
202
-
203
- #### 5. Example and Visualization
204
-
205
- For a complete, runnable script that includes data loading, prediction, and plotting, please see [`examples/prediction_example.py`](examples/prediction_example.py).
206
-
207
- Running this script will generate a plot comparing the ground truth data against the model's forecast, similar to the one shown below:
208
-
209
- <p align="center">
210
- <img src="figures/prediction_example.png" alt="Forecast Example" align="center" width="600px" />
211
- </p>
212
-
213
- Additionally, we also provide a script that makes predictions without Volume and Amount data, which can be found in [`examples/prediction_wo_vol_example.py`](examples/prediction_wo_vol_example.py).
214
-
215
-
216
- ## 🔧 Finetuning on Your Own Data (A-Share Market Example)
217
-
218
- We provide a complete pipeline for finetuning Kronos on your own datasets. As an example, we demonstrate how to use [Qlib](https://github.com/microsoft/qlib) to prepare data from the Chinese A-share market and conduct a simple backtest.
219
-
220
- > **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.
221
-
222
- The finetuning process is divided into four main steps:
223
-
224
- 1. **Configuration**: Set up paths and hyperparameters.
225
- 2. **Data Preparation**: Process and split your data using Qlib.
226
- 3. **Model Finetuning**: Finetune the Tokenizer and the Predictor models.
227
- 4. **Backtesting**: Evaluate the finetuned model's performance.
228
-
229
- ### Prerequisites
230
-
231
- 1. First, ensure you have all dependencies from `requirements.txt` installed.
232
- 2. This pipeline relies on `qlib`. Please install it:
233
- ```shell
234
- pip install pyqlib
235
- ```
236
- 3. You will need to prepare your Qlib data. Follow the [official Qlib guide](https://github.com/microsoft/qlib) to download and set up your data locally. The example scripts assume you are using daily frequency data.
237
-
238
- ### Step 1: Configure Your Experiment
239
-
240
- 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:
241
-
242
- * `qlib_data_path`: Path to your local Qlib data directory.
243
- * `dataset_path`: Directory where the processed train/validation/test pickle files will be saved.
244
- * `save_path`: Base directory for saving model checkpoints.
245
- * `backtest_result_path`: Directory for saving backtesting results.
246
- * `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).
247
-
248
- 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](https://www.comet.com/), set `use_comet = False`.
249
-
250
- ### Step 2: Prepare the Dataset
251
-
252
- 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.
253
-
254
- ```shell
255
- python finetune/qlib_data_preprocess.py
256
- ```
257
-
258
- 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.
259
-
260
- ### Step 3: Run the Finetuning
261
-
262
- 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`.
263
-
264
- #### 3.1 Finetune the Tokenizer
265
-
266
- This step adjusts the tokenizer to the data distribution of your specific domain.
267
-
268
- ```shell
269
- # Replace NUM_GPUS with the number of GPUs you want to use (e.g., 2)
270
- torchrun --standalone --nproc_per_node=NUM_GPUS finetune/train_tokenizer.py
271
- ```
272
-
273
- The best tokenizer checkpoint will be saved to the path configured in `config.py` (derived from `save_path` and `tokenizer_save_folder_name`).
274
-
275
- #### 3.2 Finetune the Predictor
276
-
277
- This step finetunes the main Kronos model for the forecasting task.
278
-
279
- ```shell
280
- # Replace NUM_GPUS with the number of GPUs you want to use (e.g., 2)
281
- torchrun --standalone --nproc_per_node=NUM_GPUS finetune/train_predictor.py
282
- ```
283
-
284
- The best predictor checkpoint will be saved to the path configured in `config.py`.
285
-
286
- ### Step 4: Evaluate with Backtesting
287
-
288
- 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.
289
-
290
- ```shell
291
- # Specify the GPU for inference
292
- python finetune/qlib_test.py --device cuda:0
293
- ```
294
-
295
- 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:
296
-
297
- <p align="center">
298
- <img src="figures/backtest_result_example.png" alt="Backtest Example" align="center" width="700px" />
299
- </p>
300
-
301
- ### 💡 From Demo to Production: Important Considerations
302
-
303
- * **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.
304
- * **Data Handling**: The provided `QlibDataset` is an example. For different data sources or formats, you will need to adapt the data loading and preprocessing logic.
305
- * **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.
306
-
307
- > **📝 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.
308
-
309
- ## 📖 Citation
310
-
311
- If you use Kronos in your research, we would appreciate a citation to our [paper](https://arxiv.org/abs/2508.02739):
312
-
313
- ```
314
- @misc{shi2025kronos,
315
- title={Kronos: A Foundation Model for the Language of Financial Markets},
316
- author={Yu Shi and Zongliang Fu and Shuo Chen and Bohan Zhao and Wei Xu and Changshui Zhang and Jian Li},
317
- year={2025},
318
- eprint={2508.02739},
319
- archivePrefix={arXiv},
320
- primaryClass={q-fin.ST},
321
- url={https://arxiv.org/abs/2508.02739},
322
- }
323
- ```
324
-
325
- ## 📜 License
326
- This project is licensed under the [MIT License](./LICENSE).
327
-
328
-
329
-
330
-
331
-
332
-
333
-
 
1
+ title: crypt
2
+ emoji: 🏢
3
+ colorFrom: gray
4
+ colorTo: pink
5
+ sdk: docker
6
+ pinned: false
7
+ license: apache-2.0