Extending Candlestick Patterns
Relevant source files
The following files were used as context for generating this wiki page:
Stolgo provides a specialized framework for Price Action Trading analysis, allowing developers to automate the detection of specific candlestick patterns, trends, and market breakouts. The library is designed to facilitate backtesting of price-based rules by providing programmatic access to common technical analysis structures.
The core of the pattern recognition system lies in the CandleStick class, which evaluates OHLC (Open, High, Low, Close) data to identify formations such as Bullish Engulfing, Hammer, and Doji candles. This system works in conjunction with trend detection and breakout analysis modules to provide a holistic view of market behavior.
Sources: README.md:5-15, lib/stolgo/candlestick.py:3-5
Candlestick Analysis Architecture
The CandleStick module utilizes a logic-based approach to analyze pandas.DataFrame objects containing price data. Each method evaluates specific geometric relationships between the candle components (body, upper wick, and lower wick) relative to the total candle length.
Fundamental Candle Evaluation
Before identifying complex patterns, the system determines the basic nature of a single candle through price comparison.
- Bearish Candle: Identified when the Close price is less than the Open price.
- Bullish Candle: Identified when the Close price is greater than the Open price.
Sources: lib/stolgo/candlestick.py:5-9
Geometric Pattern Recognition logic
For specialized candles like Hammers or Dojis, the library uses configurable thresholds for wicks and body ratios.
The diagram above illustrates the internal decision flow for ratio-based candle patterns like Hammer and Doji.
Sources: lib/stolgo/candlestick.py:23-86
Pattern Detection Methods
The following table summarizes the key detection methods available in the CandleStick class:
| Pattern | Method | Key Logic / Thresholds |
|---|---|---|
| Bullish Engulfing | is_bullish_engulfing | Current candle engulfs previous bearish candle's body. |
| Hammer | is_hammer_candle | Default: Lower Wick (60%), Body (20%), Upper Wick (20%). |
| Inverted Hammer | is_inverse_hammer_candle | Default: Lower Wick (20%), Body (20%), Upper Wick (60%). |
| Doji | is_doji_candle | Default: Lower Wick (40%), Body (2%), Upper Wick (40%). |
Sources: lib/stolgo/candlestick.py:11-86
Multi-Candle Patterns
Patterns like the Bullish Engulfing require a sequence of at least two candles. The system validates the data length and then compares the open/close prices of the current candle (at pos) against the previous candle (at pos-1).
Sources: lib/stolgo/candlestick.py:11-21
Integrating Trends and Breakouts
Candlestick patterns are often most effective when combined with market context. Stolgo provides Trend and Breakout modules to extend the utility of basic pattern detection.
Trend Context
The Trend module evaluates a series of candles to identify sustained movements. is_giant_uptrend validates that a specified number of consecutive candles are bullish and making higher closes.
This graph represents the modular relationship between raw data and the high-level analysis components.
Sources: lib/stolgo/trend.py:9-35
Breakout Identification
The Breakout class allows for detecting when price moves out of a consolidation range. This is achieved by first identifying a period of consolidation where the price remains within a specific percentage range.
| Parameter | Type | Description | Default |
|---|---|---|---|
periods | integer | Number of candles to analyze for consolidation | 13 |
percentage | float | Range of consolidation in percentage | 2.0 |
Sources: lib/stolgo/breakout.py:4-15
Error Handling and Validation
The library implements custom exceptions to handle data integrity issues during pattern analysis. The BadDataError is raised if the input data does not meet the minimum requirement for a specific pattern (e.g., needing two candles for an engulfing pattern).
# Example of validation in candlestick.py
if candles.shape[0] < 2:
raise BadDataError("Minimun two candles require")
Sources: lib/stolgo/exception.py:1-3, lib/stolgo/candlestick.py:12-13
Data Preparation and Resampling
Patterns can be analyzed on different timeframes. The helper.py module provides get_data_resample to transform lower timeframe data into higher timeframes, ensuring that the OHLC values are correctly aggregated before being passed to the CandleStick class.
The sequence shows how data is typically transformed before pattern recognition.
Sources: lib/stolgo/helper.py:84-87
The Stolgo candlestick module provides a technical foundation for price action automation by converting visual patterns into programmable logic based on configurable ratios and multi-candle relationships. By combining these patterns with the provided trend and breakout modules, developers can build complex trading strategies grounded in price action principles.
Sources: README.md:17-25, lib/stolgo/candlestick.py:1-5