Financial market data arrives as a continuous stream of trades and quotes, with every execution, bid and ask timestamped to the microsecond. AI-based market forecasting requires converting historical price and volume observations into consistent inputs for model training and inference, according to NVIDIA.
A common preprocessing method aggregates transactions into market bars, with one row per financial instrument per time interval containing price, volume and other statistical features. Neural networks can use these historical sequences to identify patterns across instruments and forecast horizons.
NVIDIA Megatron Core is an open-source library of GPU-optimized components designed to train transformer models at scale. The Megatron-LM GitHub repository combines the library with preconfigured training scripts.
- NVIDIA Megatron-LM provides GPU-optimized components for training transformer models on financial time-series data across multiple instruments and forecast horizons.
- Market data preprocessing converts intraday price and volume observations into model-ready sequences without transforming numerical values into text.
- Chronological dataset separation, training-specific target statistics and configurable trading-session boundaries help prevent data leakage and forward-looking bias.
A preprocessing script converts intraday market bars into tensors, which a dataset provider subsequently feeds into Megatron’s distributed training process. The framework supports simultaneous forecasting of returns and trading volume across multiple horizons.
AI forecasting of market returns and trading volume
Price and volume are retained as continuous numerical features. A learned patch embedding converts groups of market observations into model inputs without representing financial values as text.
A standardized data interface allows researchers to train a shared forecasting model using historical observations from multiple financial instruments. Each training window contains data from a single instrument alongside its identifier, allowing the model to learn instrument-specific behavior within a shared set of parameters.
Researchers can use the same interface to evaluate longer historical contexts, alternative sampling frequencies and additional numerical features.
The model produces return and trading volume forecasts across several time horizons using the same historical context. Quantitative researchers can apply these outputs to studies of short-term price behavior, trading activity and execution timing. Point and interval forecasts support different research objectives without requiring separate input representations.
The prepared dataset remains reusable when researchers modify the model architecture, optimizer or training duration. During preprocessing, the system records patch size, context length and forecast horizons in the saved metadata, establishing an explicit data configuration for each experiment.
Standardized inputs also reduce the model’s dependence on individual market data providers. Data obtained from another vendor or generated using a different sampling method can follow the same row schema, with the dataset provider maintaining consistent batch fields.
Market bars undergo offline preprocessing before being stored as separate dataset splits. A memory-mapped loader supplies batches to Megatron’s training system, with data reads shared within each tensor-parallel group.
Converting market price and volume data into model-ready sequences
The input consists of a directory of Parquet files containing one row per market bar, with Symbol, Date, Timestamp, Close and Volume columns. Additional numerical feature columns are optional.
The framework accommodates data from different vendors and sampling methods, including one-minute and sub-minute aggregates of tick data. Observations for each instrument’s trading day must remain within a single file, with files arranged chronologically.
The preprocessing script sorts daily observations for each instrument by timestamp and independently calculates intraday logarithmic returns and logarithmic trading volumes.
Overnight price gaps are excluded from input returns. The preprocessor treats individual rows as observation steps without adjusting gaps between them. Researchers requiring a regular time grid must align observations before preprocessing. The preprocessing/py script groups consecutive market bars for a single instrument within one trading day into patches that serve as model tokens. Fixed-length context windows subsequently move across these patches to construct training sequences.
Each patch preserves the numerical features of its underlying observations. A configuration of eight bars per patch and 32 patches per context window, for example, represents 256 market observations across 32 model positions.
Preventing data leakage and forward-looking bias
The processed dataset is divided chronologically into training, validation and test sets. A context window is retained only if its complete historical sequence and all future observations required for the longest forecast horizon fall within the same dataset split. This restriction prevents observations allocated to validation or testing from entering training inputs or forecast targets.
An embargo period removes trading days from the end of the training dataset before the validation period begins. Validation and test periods remain adjacent.
The preprocessing script also verifies that context patches belonging to different dataset splits do not overlap. These controls reduce the risk of cross-split data leakage that could introduce forward-looking bias into financial forecasting experiments. Target statistics for each forecast horizon are calculated exclusively from the training dataset.
When target standardization is applied, researchers must use these statistics consistently across training, validation and testing. Observations from future evaluation periods therefore cannot influence the target scaling used during training. The Megatron-LM example trainer leaves forecast targets unscaled.
Configuring trading sessions and forecast horizons
Trading-session boundaries are configurable according to the forecasting task. Logarithmic returns used as model inputs reset at the beginning of each trading day.
Cumulative return forecasts can incorporate overnight price movements. For intraday-only forecasting, the system masks targets associated with horizons extending beyond the market close.
A separate configuration restricts historical input sequences to a single trading day when the research methodology requires observations from the same session.
The dataset loader reconstructs valid-target masks from stored values, allowing the training loss function to exclude unavailable forecast targets. These configuration parameters determine the historical observations available to the model, the length of each forecast horizon and whether predicted market returns cover a single trading session or extend across multiple sessions.