Encoding Candlestick Patterns (Part 1): An Alphabetical System for Signal Detection
Introduction
Candlestick price action remains one of the most widely used approaches for interpreting market behavior because it focuses directly on price movement rather than relying solely on lagging technical indicators derived from historical data. Many traders favor this method for its ability to reveal market sentiment, momentum, rejection, and volatility in real time. However, despite its popularity, candlestick analysis suffers from a major limitation: most patterns are described using subjective terms such as "long body," "small wick," "pin bar," or "marubozu." These descriptions often vary from one trader to another, leading to inconsistent pattern recognition, conflicting implementations, and unreliable backtesting results.
This challenge becomes even more significant when developers attempt to automate candlestick analysis in trading platforms such as MetaTrader 5 using MQL5. Without clearly defined mathematical thresholds, two programmers can implement the same candlestick pattern differently and obtain entirely different trading signals. As a result, strategy testing, optimization, and statistical evaluation become difficult to reproduce consistently.
This article addresses that problem by introducing an objective alphabetical encoding system for candlesticks based on measurable body-to-wick ratios. Instead of relying on subjective visual interpretation, each candle is transformed into a deterministic symbol using predefined quantitative rules. Bullish and bearish candle structures are paired into symbolic forms such as A/a, H/h, E/e, G/g, and D, creating a formal classification framework that can be processed programmatically.
Building on this framework, the article develops a deterministic CandleType() classifier and implements two-candle pattern detection routines in MQL5. A configurable lookback parameter allows the indicator to scan historical bars, detect encoded candlestick combinations, and generate reproducible outputs through Print and Alert functions. By converting candlestick structures into standardized string codes, the system creates a stable signal layer that can be scanned, logged, filtered, alerted, and statistically tested with consistency.
While candlestick encoding improves objectivity in signal generation, trading success still depends on broader components such as risk-to-reward ratio, position sizing, capital preservation, and disciplined money management. The encoding system presented here is therefore not intended to replace trading strategy design, but rather to provide a reliable and reproducible foundation upon which robust analytical and automated trading systems can be built.
Historical Background of Candlestick Charts
Price action trading as we know it today owes a great deal to Munehisa Homma, an 18th‑century Japanese rice trader. At a time when most merchants recorded only closing prices with simple line or bar charts, Homma began plotting the open, high, low, and close of each trading session in a way that revealed the emotional ebb and flow of the market. His candlestick construction, later popularised in the West by Steve Nison, is now the charting style of choice for millions of traders worldwide.
Homma described each candlestick as comprised of a body (the range between the open and close) and wicks (also called shadows) extending above and below the body. The candlestick’s anatomy is as shown in Figure 1:
The mathematical representation of these components is defined as follows:
- Body size = Close price − Open price
- Upper wick = High price − max(Open price, Close price)
- Lower wick = min(Open price, Close price) − Low price
Where:
- max() returns the higher of the open and close prices
- min() returns the lower of the open and close prices

Figure 1: The structure of candlestick
A positive body (Close > Open) indicates a bullish candle – buyers were in control and pushed price higher. A negative body (Close < Open) indicates a bearish candle – sellers dominated.
The candlestick visually represents market psychology within a given time frame:
- The body reflects the net price movement.
- The wicks indicate price extremes and rejection levels.
Common Candlestick Types and Their Interpretations
Variations in body size and wick length produce many candlestick types. Traders commonly focus on a handful of recurring single-candle configurations with clear psychological meaning. These are the shapes that every price action trader learns to recognize immediately. Table 1 below summarizes the most important ones, along with a brief description of the market sentiment.
Table 1: Common Candlestick Types
| Candlestick Type | Appearance (Bullish / Bearish) | Description & Usual Interpretation |
|---|---|---|
![]() | Bullish: long green body, tiny or no wicks. | A candle with a large body and virtually no shadows, showing overwhelming dominance of one side. |
![]() | Body is relatively long with visible wicks. | A large green candle appears, it indicates aggressive buying pressure. Continuation Signal: If it appears during an existing markup phase, it confirms the strength of the move and suggests the upward trend will persist. A large red candle indicates heavy selling pressure and the retreat of buyers. Reversal Signal: When this follows a consolidated uptrend, it usually marks a "blow-off top" or a sudden influx of sellers, signaling a potential bearish reversal. |
![]() | Body is relatively short with visible wicks. | Candles with small bodies represent a lack of conviction from both buyers and sellers. When the real body is narrow, it indicates that the market is in a state of equilibrium or exhaustion, characterized by weak price movement. A small green candle suggests that buyers lack the momentum to drive a significant rally. It depicts a lack of aggressive interest from the bulls.
A small red candle indicates that sellers are not committed to pushing the price aggressively lower. It shows that supply is drying up or sellers are hesitant to commit at current levels. The descent is weak and sluggish, suggesting that the bearish momentum is fading. |
![]() | Small body near the top of the trading range, very long | When this shape occurs after a downtrend, it is called a Hammer – sellers pushed price lower but buyers stepped in and pushed it back near the open, suggesting a potential bullish reversal. After an uptrend the same shape is called a hanging man and warns of a possible bearish reversal. |
![]() | Small body near the bottom of the range, very long upper wick (≥2.5× body), little or no lower wick. | After a downtrend, it is named an inverted hammer: signals that buyers tried to push higher; although sellers pushed back, the buying pressure may resume, hinting at a bullish reversal. After an uptrend, it is referred to as a shooting star: shows that buyers failed to sustain higher prices and sellers forced a retreat – a bearish reversal warning. |
![]() | Small body (bullish or bearish) with both upper | A small real body, showing mild control by one side. Again, a sign of consolidation |
![]() | Open and close are virtually equal; | A state of indecision. Neither buyers nor sellers could gain control. The length of the wicks indicates the intensity of the struggle. Common variations: |
Encoding Candlestick Patterns: An Alphabetical Classification System
In traditional technical analysis, candlestick patterns are often described with subjective terms like "long bullish marubozu" or "bearish pin bar." This ambiguity makes systematic, rule-based trading difficult. To enable fully algorithmic market analysis, we propose shifting from verbal descriptions to a structured Alphabetical Classification System.
Each candlestick is defined by objective, measurable criteria—such as the ratio of body length to wick length and the candle’s relative range compared to recent price action. Based on these metrics, every predefined candle shape receives a unique letter code as shown in Figure 2:

Figure 2: Alphabet Classification System
Once encoded, a sequence of candlesticks becomes a simple text string (e.g., Ahe, GDAe, AaDH). This transformation allows traders and algorithms to:
- Eliminate ambiguity in pattern recognition – no more guessing whether a candle is "long enough."
- Standardize interpretation – any analyst or system using the same rules will identify the same pattern.
- Enable algorithmic detection – trading signals can be found with basic text-matching algorithms rather than complex visual heuristics.
Bullish and Bearish Pairs
To simplify reversal pattern detection, we assign uppercaseletters to bullish candles and the corresponding lowercase letters to their bearish opposites. This way, opposite pairs are instantly recognizable. For this article, we use a nine-type system that captures the essentials of direction, shape, and strength. Table 2 presents the encoding assignments as follows (see Figure 2):
Table 2: Encoding Candlestick Type
| Candlestick Type | Bullish | Bearish |
|---|---|---|
| Long, Short, or Marubozu | A | a |
| Pin bar | H | h |
| Inverted pin bar | E | e |
| Spinning Top | G | g |
| Doji (neutral – no bullish/bearish pair) | D | d |
With this alphabet, building and scanning for price action patterns becomes straightforward. For example, the string "Ahe" might represent a bullish long candle, followed by a bearish pin bar, then a bullish inverted pin bar. A substring like DH (a Doji followed by a bullish pin bar) may correspond to a classic reversal pattern. With simple text matching, an algorithm can scan thousands of candles in milliseconds.
This system does not replace judgment; it provides a clean, consistent foundation for automated and systematic trading strategies.
Code Structure: Candlestick Classification Function
The CandleType(int shift) function assigns a letter code (A/a, H/h, E/e, G/g, or D) to a candlestick based on its shape and direction. It indexes candles by shift (0 = current, 1 = previous, etc.).
//+------------------------------------------------------------------+ //| Candle Type labeling function | //+------------------------------------------------------------------+ string CandleType(int shift) { //--- Get price data for the specified candle double open = iOpen(NULL, 0, shift); // Price Open double close = iClose(NULL, 0, shift); // Price Close double high = iHigh(NULL, 0, shift); // Price High double low = iLow(NULL, 0, shift); // Price Low //--- Calculate candle components double body = MathAbs(close - open); double upperWick = high - MathMax(open, close); double lowerWick = MathMin(open, close) - low; //--- Handle bullish candles if(close > open) { if(body > 1.5 * upperWick && body > 1.5 * lowerWick) // long, short, marubozu return "A"; if(2 * body < upperWick && 2 * body < lowerWick) // spinning top return "G"; if(lowerWick > 2.5 * body && lowerWick > 2 * upperWick) // pinbar return "H"; if(upperWick > 2.5 * body && upperWick > 2 * lowerWick) // inverted pinbar return "E"; } //--- Handle bearish candles else if(close < open) { if(body > 1.5 * upperWick && body > 1.5 * lowerWick) return "a"; if(2 * body < upperWick && 2 * body < lowerWick) return "g"; if(lowerWick > 2.5 * body && lowerWick > 2 * upperWick) return "h"; if(upperWick > 2.5 * body && upperWick > 2 * lowerWick) return "e"; } else if(close == open) return "D"; // doji //--- Return empty string if no conditions met return ""; }
The function computes key candle metrics (body, upper and lower wicks), identifies bullish or bearish direction, then applies shape-based rules to return the correct letter code.
Permutation: Mathematical Foundation for Constructing Candlestick Patterns
In price action analysis, price patterns can be viewed as ordered sequences of individual candlesticks. Once each candlestick is encoded into a symbolic form (e.g., letters), these sequences can be modelled using the mathematical concept of Permutation.
Permutationrefers to the arrangement of elements in a specific order. By treating each encoded candlestick as a distinct element, we can systematically construct a wide range of possible candlestick patterns. This approach shifts pattern recognition from subjective visual interpretation to a structured, quantitative framework.
Traditionally, traders focus on a limited set of well-known patterns such as Engulfing or Morning Star formations. While these patterns are widely accepted, restricting analysis to a small predefined group may lead to the omission of numerous other sequences that could possess strong statistical significance. The permutation framework addresses this limitation by enabling the exploration of all possible ordered combinations, thereby uncovering patterns that are not immediately obvious through manual chart observation.
Mathematical Representation
The number of possible ordered patterns can be determined using the permutation formula:

Where:
- n: Total number of distinct candlestick types (encoded symbols)
- r: Number of candlesticks in a sequence (pattern length), with 0≤r≤n
- ! (factorial): The product of all positive integers from 1 up to a given number
- P(n, r): Total number of possible ordered arrangements
If you define a set of encoded candlestick types (e.g., A, H, E, G), then n = 4.
If you are analyzing patterns of length 3 (e.g., three consecutive candles), then r = 3.
Constructing Two-Candlestick Patterns Using Permutation
With the mathematical foundation of permutation established, we can now develop simple price action patterns using sequences of two candlesticks. In our encoding framework, candlesticks are classified as:
- Bullish set: A, H, E, G
- Bearish set: a, h, e, g
Each letter represents a distinct candlestick structure based on body size and wick characteristics.
Step 1: Determining the Number of Possible Patterns
To construct two-candle patterns, we calculate the number of possible ordered arrangements from four distinct elements. This is given by the permutation formula:
P(4,2)=4!/(4−2)!=12
This result indicates that there are 12 unique ordered combinations of two candlesticks that can be formed from four distinct types. Importantly, this applies separately to:
- The bullish set (A, H, E, G)
- The bearish set (a, h, e, g)
Thus, each directional group produces its own 12 distinct two-candle patterns.
Step 2: Constructing Ordered Patterns
Using the bullish set as an example, the 12 possible two-candle sequences are:
Bullish patterns: AH, AE, AG, HA, HE, HG, EA, EH, EG, GA, GH, GE
Bearish patterns: ah, ae, ag, ha, he, hg, ea, eh, eg, ga, gh, ge
Each sequence represents a specific transition from one candle type to another, capturing short-term market behavior.
Step 3: Application in Trading Environment
To implement pattern detection, we design functions that track, combine, and evaluate encoded candlestick sequences. These functions identify two-candle patterns and print them once a valid structure is detected.
The CheckBullishPattern function is designed to scan the chart and identify any occurrence of valid two-candle bullish patterns based on the predefined encoded combinations.
//+------------------------------------------------------------------+ //| Check for bullish two-candle patterns and print if found | //+------------------------------------------------------------------+ void CheckBullishPattern(int shift) { string candle2 = CandleType(shift + 1); // older candle string candle1 = CandleType(shift); // newer candle string pattern = candle2 + candle1; //--- Bullish patterns (uppercase pairs) if(pattern == "AH" || pattern == "AE" || pattern == "AG" || pattern == "HA" || pattern == "HE" || pattern == "HG" || pattern == "EA" || pattern == "EH" || pattern == "EG" || pattern == "GA" || pattern == "GH" || pattern == "GE") { Print("Bullish pattern detected: ", pattern); Alert("Bullish pattern detected: ", pattern); } }
In contrast, the CheckBearishPattern function scans the chart to detect two-candle bearish patterns that satisfy the specified encoding criteria.
//+------------------------------------------------------------------+ //| Check for bearish two-candle patterns and print if found | //+------------------------------------------------------------------+ void CheckBearishPattern(int shift) { string candle2 = CandleType(shift + 1); string candle1 = CandleType(shift); string pattern = candle2 + candle1; //--- Bearish patterns (lowercase pairs) if(pattern == "ah" || pattern == "ae" || pattern == "ag" || pattern == "ha" || pattern == "he" || pattern == "hg" || pattern == "ea" || pattern == "eh" || pattern == "eg" || pattern == "ga" || pattern == "gh" || pattern == "ge") { Print("Bearish pattern detected: ", pattern); Alert("Bearish pattern detected: ", pattern); } }
These functions are responsible for:
- Extracting encoded candlestick types
- Combining two consecutive candles into a pattern string
- Checking whether the pattern matches predefined valid sequences
- Printing the detected pattern
They serve as the pattern recognition layer in your trading system.
Demonstration: Signal Detection
The indicator accepts a configurable lookback parameter that defines the number of historical candles to analyze. With lookback = 500, the indicator scans the previous 500 bars. It loops through them and calls CheckBullishPattern and CheckBearishPattern to output any detected two-candle patterns. To optimize performance and conserve computational resources, the loop is executed only once per new bar.
//+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int32_t rates_total, const int32_t prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int32_t &spread[]) { //--- set arrays as series ArraySetAsSeries(time, true); static datetime lastTime; int Lmt = (prev_calculated == 0) ? MathMin(rates_total - 3, lookback - 3) : 3; if(lastTime == time[0]) return 0; lastTime = time[0]; for(int i = Lmt; i >= 0; i--) { CheckBullishPattern(i); CheckBearishPattern(i); } //--- return value of prev_calculated for next call return(rates_total); }
In the current implementation, patterns are reported via Print() and Alert(). The system can be extended with SendNotification() to deliver real-time notifications when the indicator runs on a server.

Figure 3: Encoded Signal Detection
Conclusion
In this article, we transformed traditional candlestick analysis from a subjective visual practice into a structured and programmatic framework suitable for systematic market analysis. By defining objective measurements for candle body and wick ratios, we encoded candlestick structures into an alphabetical classification system such as A/a, H/h, E/e, G/g, and D. Using this foundation, we implemented the core MQL5 components including CandleType(), bullish and bearish pattern validation routines, and an OnCalculate() loop with a configurable lookback parameter to generate reproducible candlestick pattern strings such as "AH" or "ge".
This encoding approach creates a clean signal layer that can be scanned, logged, alerted, and statistically tested with consistency. By converting candlestick formations into symbolic sequences, traders and developers can move beyond subjective chart reading and systematically investigate which candle arrangements may possess statistical significance or trading edge.
With the framework now established, the next stage of the series will focus on deeper permutation and statistical analysis, expansion of the coded pattern library, and integration of contextual filters and money-management techniques. However, no encoded pattern guarantees success under all market conditions. These signals should therefore be used as one disciplined component within a broader trading framework built around proper risk management and market context analysis.
Warning: All rights to these materials are reserved by MetaQuotes Ltd. Copying or reprinting of these materials in whole or in part is prohibited.
This article was written by a user of the site and reflects their personal views. MetaQuotes Ltd is not responsible for the accuracy of the information presented, nor for any consequences resulting from the use of the solutions, strategies or recommendations described.
Creating an EMA Crossover Forward Simulation Indicator in MQL5
Exploring Conformal Forecasting of Financial Time Series
Feature Engineering for ML (Part 3): Session-Aware Time Features for Forex Machine Learning
Adaptive Malaysian Engulfing Indicator (Part 1): Pattern Detection and Retest Validation
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use






