Loading…

Bar Pattern Encoder

A pattern dictionary study - "what usually happens after this exact bar shape" - needs the same three pieces every time: a consistent way to turn a bar into a state, a way to pack a run of states into one lookup key, and a pass that counts how often each key occurred together with what followed it. This library provides all three as plain, reusable functions instead of leaving each script to reinvent them. Each bar is classified Down, Doji or Up by comparing its body to its OWN high-low range rather than a fixed pip threshold - a fixed threshold classifies every bar as Up/Down on a quiet instrument and every bar as Doji on a volatile one, so the ratio is the correct scale-free measure and stays a caller-supplied input rather than a hardcoded constant. A window of N states packs into one base-3 integer (3^N possible codes), with BpeDecodeWindow and BpeCodeLabel as exact inverses of the encoding - useful both for printing readable labels and for verifying the codec itself. Window size is a parameter, not fixed at three bars the way a one-off script typically hardcodes it. BpeBuildDictionary walks a symbol's history once, encoding every complete window and accumulating both an occurrence count and a summed forward return (measured a caller-chosen number of bars after each window's ending bar) into caller-supplied arrays - sized once via BpePatternCount(windowSize) and reusable across repeated calls if a caller wants to accumulate several symbols into one shared table. Notes and limitations - windowSize is capped at 12 (3^12 = 531,441 codes) purely to keep the packed code inside a plain MQL4 int with headroom; nothing in the header needs windows that long, and per-code sample sizes thin out quickly well before that cap in practice. - The forward return is expressed in points, not pips - multiply by 10 for a 3 or 5 digit symbol if pips are wanted, matching this catalogue's usual pip/point distinction. - BpeBuildDictionary does not zero the caller's arrays itself, so a caller wanting several symbols folded into one shared table can call it repeatedly on the same arrays; a caller wanting a fresh table per call must ArrayInitialize before each call, as the demo does. - This encodes the SHAPE of a bar run only (direction plus Doji), not its size - two 3-bar runs with wildly different range still encode to the same code if their bodies fall on the same side of the Doji threshold.

Script · MT4 · v1.0 · Free

About Bar Pattern Encoder

Bar Pattern Encoder is a script for MetaTrader 4. It marks candlestick patterns. It has 4 settings, such as Window size, Max bars and Doji body ratio. MQL source code included.

Files in the download: BarPatternEncoder.mqh, BarPatternEncoder.txt, BarPatternEncoderDemo.mq4.

Settings

SettingDefaultWhat it does
Window size3Bars per encoded window (1..12)
Max bars2000Most recent bars to include in the dictionary
Doji body ratio0.15Body/range fraction at or below which a bar is a Doji
Forward bars5Bars after the sequence completes measured for the forward return

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Candlestick Patterns · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria