Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Shiyu coder Kronos Candlestick Data Preparation Pattern

From Leeroopedia


Field Value
Implementation Name Candlestick_Data_Preparation_Pattern
Repository Shiyu_coder_Kronos
Repository URL https://github.com/shiyu-coder/Kronos
Type Pattern Doc
Source File examples/prediction_example.py
Lines L48-57 (canonical pattern)
Implements Principle Principle:Shiyu_coder_Kronos_Candlestick_Data_Preparation
Dependencies pandas
Last Updated 2026-02-09 14:00 GMT

Overview

This is a user-defined pattern (not a library API) that demonstrates the canonical approach for loading and structuring OHLCV financial time series data from a CSV file into the three inputs required by KronosPredictor.predict().

Pattern Interface

import pandas as pd

# 1. Load CSV
df = pd.read_csv("path/to/data.csv")
df['timestamps'] = pd.to_datetime(df['timestamps'])

# 2. Define windows
lookback = 400
pred_len = 120

# 3. Slice input data
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']

Input

A CSV file with the following required columns:

Column Type Description
timestamps datetime-parseable string Timestamp for each data point
open float Opening price
high float Highest price in period
low float Lowest price in period
close float Closing price
volume float Trading volume
amount float Trading amount (monetary value)

Output

Variable Type Shape Description
x_df pd.DataFrame (lookback, 6) Historical OHLCV + amount features for the input context window
x_timestamp pd.Series (lookback,) DatetimeIndex for the historical window
y_timestamp pd.Series (pred_len,) DatetimeIndex for the future prediction horizon

Full Example from Source

The complete pattern in context (from examples/prediction_example.py):

import pandas as pd
from model import Kronos, KronosTokenizer, KronosPredictor

# 1. Load Model and Tokenizer
tokenizer = KronosTokenizer.from_pretrained("NeoQuasar/Kronos-Tokenizer-base")
model = Kronos.from_pretrained("NeoQuasar/Kronos-small")

# 2. Instantiate Predictor
predictor = KronosPredictor(model, tokenizer, max_context=512)

# 3. Prepare Data
df = pd.read_csv("./data/XSHG_5min_600977.csv")
df['timestamps'] = pd.to_datetime(df['timestamps'])

lookback = 400
pred_len = 120

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']

# 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
)

Alternative Slicing Pattern

For scenarios where you want to hold out ground truth for evaluation, you can slice from the end of the DataFrame:

# Slice so that the last pred_len rows are held out as ground truth
x_df = df.iloc[-lookback - pred_len:-pred_len][['open', 'high', 'low', 'close', 'volume', 'amount']]
x_timestamp = pd.to_datetime(df.iloc[-lookback - pred_len:-pred_len]['timestamps'])
y_timestamp = pd.to_datetime(df.iloc[-pred_len:]['timestamps'])

This approach is useful when the last pred_len rows serve as ground truth for evaluating prediction accuracy.

Key Notes

  • The timestamps column must be converted to datetime before slicing.
  • The feature column order in x_df should be ['open', 'high', 'low', 'close', 'volume', 'amount'] to match the model's expected input format.
  • y_timestamp tells the predictor the temporal positions of the future time steps, enabling time-aware forecasting.
  • The KronosPredictor handles internal normalization, tokenization, and autoregressive generation -- no manual normalization of x_df is required.

See Also

Environment & Heuristic Links

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment