Skip to content
All library documents

Porting Kronos Autoregressive Forecasting to MQL5 with an Exact KV Cache

Article MQL5 articles

Summary

This installment describes the decoder and autoregressive prediction half of a native MQL5 implementation of Kronos, a transformer model for financial candles. It reconstructs normalized OHLCVA bars from hierarchical tokens, embeds token pairs alongside temporal features, predicts successive tokens, and converts the generated sequence back to prices. The article details implementation pitfalls in cross-attention, rotary embeddings, embedding scaling, and the model’s differing head structure. It also explains a key/value cache that reuses earlier computations during sequential generation, plus a transpose correction identified through profiling.

Verification compares intermediate decoder and predictor outputs with a PyTorch reference, using maximum absolute error and checking predicted token choices. The article reports close numerical agreement for those stages and a substantial per-step speedup after the cache and transpose fixes. This is evidence of implementation fidelity and runtime improvement, not evidence of forecast accuracy or profitability. The text focuses on reproducing model computation; it does not establish how forecasts perform out of sample, how sampling settings should be chosen, or whether the approach generalizes across assets and market conditions.

Key ideas

  • The decoder maps hierarchical token pairs back to normalized candle features, which must then be denormalized using context statistics.
  • The predictor combines learned embeddings for two token levels with time features before generating the next token autoregressively.
  • A key/value cache avoids repeating prior transformer computations during sequential generation.
  • Golden-reference checks compare continuous outputs by numerical error and also verify predicted token choices.
  • The reported checks establish implementation agreement and speed improvements, not profitable forecasting performance.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.