Discussing the article: "Feature Engineering for ML (Part 8): Entropy Features in MQL5"

 

Check out the new article: Feature Engineering for ML (Part 8): Entropy Features in MQL5.

An MQL5 port of four entropy estimators — Shannon, Plug-In, Lempel-Ziv, and Kontoyiannis — operating on the intrabar tick-rule sequence. CopyTicksRange() limits data to the broker's cached tick window, so features apply to recent bars only. The implementation encodes bid-direction ticks from MqlTick, replaces NumPy-dependent steps with array-based methods, and ships CEntropyFeatures.mqh and EntropyViewer.mq5 for EA and indicator use.

This article ports four entropy estimators—Shannon, Plug-In, Lempel-Ziv and Kontoyiannis—that were implemented in Python (Part 7) to MQL5 and explains the design decisions required to make them practical inside MetaTrader 5. The Python reference relied on NumPy sliding windows, Python dictionaries and Numba acceleration; none of those primitives are available in MQL5. More importantly, MQL5 access to intrabar ticks is limited to the broker‑cached window returned by CopyTicksRange(), so historical tick sequences beyond that cache are simply unavailable. The port therefore addresses three central constraints at once:

  • how to fetch and encode intrabar tick rules using CopyTicksRange() while marking bars with insufficient ticks via the sentinel ENT_EMPTY;
  • how to count overlapping w‑grams without NumPy (a base‑3 integer hash into a fixed counter array replaces sliding window_view and unique);
  • how to bound the Kontoyiannis match search (a configurable look‑back window caps the O(n²) inner loop).

The goal is practical: reproduce the Python reference numerically, expose safe EA/inidicator integration points, and make the feature set usable in live trading despite the tick-cache limitation.

Author: Patrick Murimi Njoroge