P
PipsGrowth
Back to Patterns

Pattern Detection Tools

Python packages, MT5 methods, and code examples for automated candlestick pattern recognition.

3
Python Libraries
4
MQL5 Examples
60+
Detectable Patterns
Auto
Detection

Python Pattern Detection Libraries

TA-Lib

The most comprehensive technical analysis library with 60+ candlestick pattern recognition functions. Industry standard for algorithmic trading.

pip install TA-Lib61 patterns

Key Features

  • Industry standard used by professionals worldwide
  • Blazing fast C-based engine for real-time detection
  • Comprehensive coverage of 61 candlestick patterns
  • Well documented with extensive community support
  • Battle-tested in production trading systems

Code Example

Python
import talib
import numpy as np
# Get OHLC data
open_prices = np.array([...])
high_prices = np.array([...])
low_prices = np.array([...])
close_prices = np.array([...])
# Detect Hammer pattern
hammer = talib.CDLHAMMER(open_prices, high_prices, low_prices, close_prices)
# Detect Engulfing pattern
engulfing = talib.CDLENGULFING(open_prices, high_prices, low_prices, close_prices)
# Detect Morning Star
morning_star = talib.CDLMORNINGSTAR(open_prices, high_prices, low_prices, close_prices)
# Value: 100 = bullish signal, -100 = bearish signal, 0 = no pattern

pandas-ta

Modern pandas-native technical analysis library with intuitive candlestick pattern recognition built for Python data workflows.

pip install pandas-ta30+ patterns

Key Features

  • Pure Python — install anywhere with zero dependencies
  • Seamless pandas DataFrame integration
  • Detect all patterns at once with a single function call
  • Actively maintained with regular updates
  • Clean, Pythonic API design

Code Example

Python
import pandas as pd
import pandas_ta as ta
# Load your OHLC data into a DataFrame
df = pd.DataFrame({
'open': [...],
'high': [...],
'low': [...],
'close': [...]
})
# Detect Doji pattern
df['doji'] = ta.cdl_doji(df['open'], df['high'], df['low'], df['close'])
# Detect all patterns at once (returns DataFrame with all patterns)
df.ta.cdl_pattern(name="all")
# Specific pattern
df.ta.cdl_pattern(name="engulfing")

candlestick-patterns

Lightweight Python library specifically designed for candlestick pattern recognition with minimal overhead.

pip install candlestick-patterns20+ patterns

Key Features

  • Lightweight with minimal memory footprint
  • Simple, intuitive API for quick integration
  • Pure Python — no external dependencies required
  • Focused specifically on pattern detection
  • Easy to extend with custom patterns

Code Example

Python
from candlestick import candlestick
# Detect patterns on DataFrame
# Returns DataFrame with boolean columns for each pattern
patterns = candlestick.hammer(df, target='signal')
patterns = candlestick.inverted_hammer(df, target='signal')
patterns = candlestick.doji(df, target='signal')
patterns = candlestick.engulfing(df, target='signal')

MT5 / MQL5 Pattern Detection

Hammer Pattern Detection

MQL5 function to detect hammer candlestick patterns programmatically in MetaTrader 5.

MQL5
// MQL5 - Hammer Pattern Detection
bool IsHammer(int bar) {
double open = iOpen(_Symbol, _Period, bar);
double close = iClose(_Symbol, _Period, bar);
double high = iHigh(_Symbol, _Period, bar);
double low = iLow(_Symbol, _Period, bar);
double body = MathAbs(close - open);
double upperShadow = high - MathMax(open, close);
double lowerShadow = MathMin(open, close) - low;
double totalRange = high - low;
// Hammer criteria:
// 1. Small body (< 30% of total range)
// 2. Long lower shadow (> 2x body)
// 3. Little or no upper shadow (< 10% of range)
if(totalRange == 0) return false;
bool smallBody = (body / totalRange) < 0.30;
bool longLowerShadow = lowerShadow >= (body * 2);
bool noUpperShadow = upperShadow < (totalRange * 0.10);
return smallBody && longLowerShadow && noUpperShadow;
}

Engulfing Pattern Detection

Detect bullish and bearish engulfing patterns in MQL5 for automated trading.

MQL5
// MQL5 - Engulfing Pattern Detection
int DetectEngulfing(int bar) {
double open1 = iOpen(_Symbol, _Period, bar);
double close1 = iClose(_Symbol, _Period, bar);
double open2 = iOpen(_Symbol, _Period, bar + 1);
double close2 = iClose(_Symbol, _Period, bar + 1);
bool prevBearish = close2 < open2;
bool prevBullish = close2 > open2;
bool currBullish = close1 > open1;
bool currBearish = close1 < open1;
// Bullish Engulfing: Previous bearish, current bullish
// Current candle body completely covers previous
if(prevBearish && currBullish) {
if(open1 <= close2 && close1 >= open2) {
return 1; // Bullish Engulfing
}
}
// Bearish Engulfing
if(prevBullish && currBearish) {
if(open1 >= close2 && close1 <= open2) {
return -1; // Bearish Engulfing
}
}
return 0; // No pattern
}

Morning/Evening Star Detection

Detect three-candle reversal patterns for high-probability trade entries.

MQL5
// MQL5 - Morning Star Detection
bool IsMorningStar(int bar) {
// First candle (oldest) - Long bearish
double o1 = iOpen(_Symbol, _Period, bar + 2);
double c1 = iClose(_Symbol, _Period, bar + 2);
// Second candle (star) - Small body
double o2 = iOpen(_Symbol, _Period, bar + 1);
double c2 = iClose(_Symbol, _Period, bar + 1);
double h2 = iHigh(_Symbol, _Period, bar + 1);
double l2 = iLow(_Symbol, _Period, bar + 1);
// Third candle - Long bullish
double o3 = iOpen(_Symbol, _Period, bar);
double c3 = iClose(_Symbol, _Period, bar);
double body1 = MathAbs(c1 - o1);
double body2 = MathAbs(c2 - o2);
double body3 = MathAbs(c3 - o3);
bool firstBearish = c1 < o1 && body1 > body2 * 2;
bool smallStar = body2 < body1 * 0.5;
bool thirdBullish = c3 > o3 && body3 > body2 * 2;
bool closesIntoFirst = c3 > (o1 + c1) / 2;
return firstBearish && smallStar && thirdBullish && closesIntoFirst;
}

Pattern Scanner EA Framework

Complete Expert Advisor framework for scanning multiple candlestick patterns simultaneously.

MQL5
// MQL5 - Pattern Scanner Framework
enum SIGNAL { NONE, BULLISH, BEARISH };
SIGNAL ScanPatterns(int bar) {
// Check reversal patterns
if(IsHammer(bar)) return BULLISH;
if(IsShootingStar(bar)) return BEARISH;
int engulf = DetectEngulfing(bar);
if(engulf == 1) return BULLISH;
if(engulf == -1) return BEARISH;
if(IsMorningStar(bar)) return BULLISH;
if(IsEveningStar(bar)) return BEARISH;
// Add more patterns...
return NONE;
}
void OnTick() {
// Scan for patterns on the last completed candle
SIGNAL signal = ScanPatterns(1);
if(signal == BULLISH && PositionsTotal() == 0) {
// Open buy position
double sl = iLow(_Symbol, _Period, 1) - 10 * _Point;
double tp = Ask + 2 * (Ask - sl);
trade.Buy(0.1, _Symbol, Ask, sl, tp);
}
}

📋 TA-Lib Pattern Functions Reference

Complete list of candlestick pattern functions available in TA-Lib:

Reversal Patterns
CDLHAMMER
CDLINVERTEDHAMMER
CDLSHOOTINGSTAR
CDLHANGINGMAN
CDLENGULFING
CDLMORNINGSTAR
CDLEVENINGSTAR
CDLPIERCING
CDLDARKCLOUDCOVER
CDLHARAMI
CDLHARAMICROSS
Continuation
CDLRISEFALL3METHODS
CDLTASUKIGAP
CDLSEPARATINGLINES
CDLMATHOLD
CDL3LINESTRIKE
Doji Patterns
CDLDOJI
CDLDOJISTAR
CDLDRAGONFLYDOJI
CDLGRAVESTONEDOJI
CDLLONGLEGGEDDOJI
Multi-Candle
CDL3WHITESOLDIERS
CDL3BLACKCROWS
CDL3INSIDE
CDL3OUTSIDE
CDLABANDONEDBABY
CDLKICKING
Other
CDLSPINNINGTOP
CDLHIGHWAVE
CDLBELTHOLD
CDLCOUNTERATTACK

💡 Pattern Detection Best Practices

For Python / Backtesting

  • Use TA-Lib for production — fastest and most reliable
  • Combine multiple patterns for stronger signals
  • Filter patterns by trend direction
  • Add support/resistance context
  • Backtest before live trading

For MT5 Expert Advisors

  • Always check bar 1 (completed candle), not bar 0
  • Use tolerances for pattern detection
  • Add trend filters (moving averages)
  • Consider volume confirmation
  • Test on Strategy Tester first
PipsGrowth - Expert Broker Reviews, Trading Strategies & Tools