Candlestick Geometry And Pattern Primitives
Nearly every candlestick pattern indicator reimplements the same underlying measurements - how big is the body relative to the range, how long are the wicks, does one bar's body swallow the previous one's - each time slightly differently, with the thresholds for "small body" or "long wick" buried inside the pattern logic where they cannot be reused or tuned independently from the pattern that uses them. This header pulls those primitives out as plain, stateless functions, and puts every threshold in a default parameter rather than a hardcoded constant. The measurement functions - CandleBody, CandleRange, UpperWick, LowerWick, BodyToRangeRatio - are the building blocks everything else is defined in terms of. IsDoji and IsMarubozu are opposite ends of the body-to-range ratio: a doji's body is small relative to its range (open and close nearly equal regardless of where the wicks reach), a marubozu's body is nearly the whole range (negligible wicks on either end). IsHammer and IsShootingStar look for a small body positioned at one end of the range with a long wick on the opposite side - a hammer near the top with a long lower wick, a shooting star near the bottom with a long upper wick. Both are geometry only: this header has no visibility into the preceding trend, which is what actually determines whether a hammer-shaped bar means anything at a given point in a chart, so that judgement is left to the caller. IsBullishEngulfing and IsBearishEngulfing compare two bars' bodies using the classical definition - wicks are deliberately ignored, only whether the later body fully contains the earlier one, in the opposite direction. IsInsideBar and IsOutsideBar compare two bars' full ranges instead of their bodies, for consolidation and volatility-expansion setups. Every function takes raw open/high/low/close values, never an array or a bar index. The caller decides where those values come from - Open[i]/High[i]/Low[i]/Close[i] on a live chart, a custom candle structure, or backtesting data read from a file - which keeps this header usable from a script, an indicator, or an EA without assuming anything about how bars are stored. Notes and limitations - Every threshold (maxBodyRatio, minBodyRatio, minWickToBodyRatio) is a default parameter, not a fixed constant - what counts as "small" or "long" genuinely varies by instrument and timeframe, and the defaults are reasonable starting points, not calibrated values. - IsHammer and IsShootingStar answer a pure geometry question about a single bar. Neither considers where that bar sits relative to a trend, which is what conventionally separates a real reversal signal from a hammer-shaped bar in the middle of a range. Pair either function with your own trend context before acting on it. - IsBullishEngulfing and IsBearishEngulfing use the classical body-only definition. Some published variants require the engulfing bar's wicks to also exceed the previous bar's wicks - that stricter variant is not provided here. - BodyToRangeRatio, and therefore IsDoji and IsMarubozu, return 0.0 on a zero-range bar (high equals low) rather than dividing by zero. A zero-range bar is rare on real tick data but not impossible on a thin or synthetic feed.
Script · MT5 · v1.0 · Free
About Candlestick Geometry And Pattern Primitives
Candlestick Geometry And Pattern Primitives is a script for MetaTrader 5. It marks candlestick patterns. MQL source code included.
Files in the download: CandlePrimitives.mq5.mqh, CandlePrimitives.txt, CandlePrimitivesDemo.ex5, CandlePrimitivesDemo.mq5.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Candlestick Patterns · MT5 Script · MQL5 Source Code