Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:TA Lib Ta lib python CDL Function API

From Leeroopedia


Knowledge Sources
Domains Technical_Analysis, Pattern_Recognition
Last Updated 2026-02-09 22:00 GMT

Overview

Concrete tool for detecting candlestick patterns via 61 CDL* functions (CDLDOJI, CDLENGULFING, CDLMORNINGSTAR, etc.) provided by the TA-Lib Cython bindings.

Description

The 61 CDL (candlestick) functions are Cython wrappers around the TA-Lib C library's pattern recognition routines. Each function takes four OHLC arrays and returns an integer array where non-zero values indicate pattern detection.

Representative functions:

  • CDLDOJI — Doji pattern (single candle, no parameters)
  • CDLENGULFING — Engulfing pattern (two candles, no parameters)
  • CDLMORNINGSTAR — Morning Star (three candles, penetration parameter)

Usage

Call any CDL function with four OHLC arrays. Check output for non-zero values to find pattern occurrences.

Code Reference

Source Location

  • Repository: ta-lib-python
  • File: talib/_func.pxi
  • Lines: L1252-1280 (CDLDOJI), L1348-1376 (CDLENGULFING), L2156-2186 (CDLMORNINGSTAR)

Signature

def CDLDOJI(open: np.ndarray, high: np.ndarray,
            low: np.ndarray, close: np.ndarray) -> np.ndarray:
    """Doji pattern. Returns int32 array: -100, 0, or +100."""

def CDLENGULFING(open: np.ndarray, high: np.ndarray,
                 low: np.ndarray, close: np.ndarray) -> np.ndarray:
    """Engulfing pattern. Returns int32 array: -100, 0, or +100."""

def CDLMORNINGSTAR(open: np.ndarray, high: np.ndarray,
                   low: np.ndarray, close: np.ndarray,
                   penetration: float = 0.3) -> np.ndarray:
    """Morning Star pattern with configurable penetration threshold."""

Import

import talib

I/O Contract

Inputs

Name Type Required Description
open np.ndarray (float64) Yes Opening prices
high np.ndarray (float64) Yes High prices
low np.ndarray (float64) Yes Low prices
close np.ndarray (float64) Yes Closing prices
penetration float No Threshold for some patterns (e.g., CDLMORNINGSTAR default 0.3)

Outputs

Name Type Description
pattern_signal np.ndarray (int32) Array of same length as input: -100 (bearish), 0 (no pattern), +100 (bullish)

Usage Examples

Detect Doji Patterns

import numpy as np
import talib

# Generate sample OHLC data
n = 100
close = np.cumsum(np.random.randn(n) * 0.5) + 100
open_p = close + np.random.randn(n) * 0.01  # Near-equal open/close for doji
high = np.maximum(open_p, close) + np.abs(np.random.randn(n) * 0.5)
low = np.minimum(open_p, close) - np.abs(np.random.randn(n) * 0.5)

doji = talib.CDLDOJI(open_p, high, low, close)
doji_indices = np.where(doji != 0)[0]
print(f"Doji patterns found at indices: {doji_indices}")

Detect Engulfing Patterns

engulfing = talib.CDLENGULFING(open_p, high, low, close)
bullish = np.where(engulfing == 100)[0]
bearish = np.where(engulfing == -100)[0]
print(f"Bullish engulfing at: {bullish}")
print(f"Bearish engulfing at: {bearish}")

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment