Workflow:TA Lib Ta lib python Candlestick Pattern Recognition
| Knowledge Sources | |
|---|---|
| Domains | Technical_Analysis, Financial_Data, Pattern_Recognition |
| Last Updated | 2026-02-09 22:00 GMT |
Overview
End-to-end process for detecting candlestick chart patterns in OHLC price data using TA-Lib's 61 built-in pattern recognition functions.
Description
This workflow covers the use of TA-Lib's candlestick pattern recognition functions, which analyze open, high, low, and close price arrays to detect well-known Japanese candlestick patterns. The library provides 61 pattern functions (all prefixed with CDL), including both single-candle patterns (Doji, Hammer, Marubozu) and multi-candle patterns (Engulfing, Three Black Crows, Morning Star). Each function returns an integer array where non-zero values indicate pattern detection: positive values signal bullish patterns, negative values signal bearish patterns, and zero means no pattern detected. The magnitude (typically 100 or 200) can indicate pattern strength.
Usage
Execute this workflow when you have OHLC price data and want to identify candlestick chart patterns for trading signal generation, technical analysis research, or pattern-based screening across multiple securities. This is particularly useful for building automated pattern scanners or augmenting indicator-based strategies with price action signals.
Execution Steps
Step 1: Prepare OHLC Price Data
Load or construct four aligned arrays representing open, high, low, and close prices. All four arrays are required for every pattern recognition function. Arrays must be of equal length, contain float64 values, and be sorted chronologically. Data can be provided as numpy arrays, pandas Series, or polars Series.
Key considerations:
- All four OHLC arrays are mandatory — pattern functions do not accept close-only data
- Ensure high >= max(open, close) and low <= min(open, close) for valid candles
- Data quality directly affects pattern detection accuracy
Step 2: Call Pattern Recognition Functions
Invoke one or more of the 61 CDL-prefixed pattern functions from the talib namespace, passing open, high, low, and close arrays as positional arguments. Some patterns accept an optional penetration parameter that adjusts the sensitivity threshold. Each function returns an integer array of the same length as the input.
Pattern categories include:
- Single-candle: CDLDOJI, CDLHAMMER, CDLSHOOTINGSTAR, CDLMARUBOZU, CDLSPINNINGTOP, etc.
- Two-candle: CDLENGULFING, CDLHARAMI, CDLDARKCLOUDCOVER, CDLPIERCING, etc.
- Three-candle: CDLMORNINGSTAR, CDLEVENINGSTAR, CDL3BLACKCROWS, CDL3WHITESOLDIERS, etc.
- Complex: CDLRISEFALL3METHODS, CDLBREAKAWAY, CDLMATHOLD, etc.
Step 3: Interpret Pattern Signals
Parse the integer output arrays to identify pattern occurrences. A value of zero indicates no pattern at that position. Positive values (typically +100) indicate a bullish pattern, while negative values (typically -100) indicate a bearish pattern. Some patterns can be both bullish and bearish depending on context (e.g., Engulfing).
Key considerations:
- Values are typically -100, 0, or +100
- Some patterns return -200/+200 for stronger signals
- Multi-candle patterns report the signal at the position of the last candle in the formation
- The lookback period varies by pattern complexity (1-candle patterns have minimal lookback)
Step 4: Scan Across All Patterns
For comprehensive pattern screening, iterate over all pattern function names (filterable from talib.get_function_groups() under the Pattern Recognition group) and apply each to the same OHLC data. Aggregate the results to build a pattern summary matrix showing which patterns were detected at each time step.
Key considerations:
- The Pattern Recognition group contains 61 functions
- Use talib.get_function_groups()['Pattern Recognition'] to get all CDL function names
- The Abstract API can also be used for dynamic iteration over pattern functions
Step 5: Integrate Pattern Signals
Combine detected patterns with other technical indicators or trading rules to generate composite signals. Pattern signals can be used as entry/exit triggers, confirmation filters, or features in a machine learning model. When using pandas or polars input, outputs preserve the original index for seamless alignment with other data.
Key considerations:
- Patterns alone are not trading signals — combine with trend or momentum indicators
- Multiple patterns may fire on the same candle
- Use the Abstract API's info property to retrieve human-readable pattern descriptions