Momentum Analysis
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
Momentum Analysis in the Stolgo library encompasses the identification and validation of price direction and strength through technical price action rules. It serves as a core component of the library's Price Action Trading Analysis capabilities, allowing traders to automate the detection of sustained price movements, consolidations, and subsequent breakouts or breakdowns.
The system leverages historical and intraday data feeds to evaluate specific conditions such as "Giant Trends" (sustained unidirectional movements) and price consolidation phases that precede high-momentum breakout events. By providing programmatic interfaces to these patterns, Stolgo enables the development of automated backtesting strategies for price action rules.
Sources: README.md:1-12, lib/stolgo/trend.py:5-8
Core Components and Architecture
Trend Identification
The Trend class provides the logic for detecting significant, sustained price movements referred to as "Giant" trends. These trends are characterized by a sequence of candles where the price consistently moves in one direction without retracement within the specified period.
- Giant Uptrend: Validated when all candles in the period are bullish (Close > Open) and each subsequent candle closes higher than the previous one.
- Giant Downtrend: Validated when all candles in the period are bearish (Close < Open) and each subsequent candle closes lower than the previous one.
Sources: lib/stolgo/trend.py:11-49, lib/stolgo/trend.py:51-89
Breakout and Consolidation Logic
Momentum is often preceded by a period of low volatility or consolidation. The Breakout class manages the detection of these phases and the resulting directional shifts.
| Feature | Description |
|---|---|
| Consolidation | Checks if the price has remained within a specific percentage range over a defined number of periods. |
| Breakout | Identifies when the current candle's Close price exceeds the maximum Close price of a preceding consolidation period. |
| Breakdown | Identifies when the current candle's Close price falls below the minimum Close price of a preceding consolidation period. |
Sources: lib/stolgo/breakout.py:8-76
Data Flow for Momentum Evaluation
The following diagram illustrates how data is retrieved from external providers (like Nasdaq or Samco) and processed through momentum analysis modules.
Technical Implementation
Trend Detection Logic
The Trend module utilizes iterative checks on recent price action data. It enforces a minimum data requirement based on the requested period to prevent BadDataError.
# Example of Giant Uptrend logic
recent_dfs = dfs[-1*periods:]
for candle in recent_dfs.iterrows():
if(candle[1]["Close"] < candle[1]["Open"]): # Must be green
return False
if(prev_candle and prev_candle[1]["Close"] > candle[1]["Close"]): # Higher Close
return False
Sources: lib/stolgo/trend.py:34-45, lib/stolgo/exception.py:1-3
Breakout Calculation
Breakout detection is a two-step process: first, the library validates that the historical window (minus the last candle) was in consolidation; second, it compares the most recent candle against that range.
API Parameters and Configuration
Both the Trend and Breakout classes share common configuration parameters for analysis.
| Parameter | Type | Default | Description |
|---|---|---|---|
periods | Integer | 13 | The number of recent candles to evaluate for the pattern. |
percentage | Float | 2 | The allowable price variance (percentage) for consolidation detection. |
dfs | pd.DataFrame | N/A | Input candle data containing 'Open', 'High', 'Low', 'Close'. |
Sources: lib/stolgo/trend.py:6-9, lib/stolgo/breakout.py:5-7
Conclusion
Momentum Analysis in Stolgo provides the foundational tools for identifying high-strength price movements through the Trend and Breakout modules. By integrating these with the library's robust data fetching and resampling helpers, developers can automate complex price action rules that distinguish between ranging markets and active directional trends.
Sources: README.md:5-10, lib/stolgo/trend.py:5-7