Support and Resistance Analysis
Relevant source files
The following files were used as context for generating this wiki page:
Support and Resistance Analysis in the Stolgo library provides automated tools for identifying price levels where a trend might pause or reverse. By leveraging price action trading rules, the library allows developers to detect periods of consolidation and subsequent breakouts or breakdowns from established price ranges. This analysis is a core component of Stolgo's goal to automate price action strategies and backtesting.
Sources: README.md:1-12, lib/stolgo/breakout.py:4-8
Core Logic and Consolidation
The foundation of support and resistance analysis in Stolgo is the detection of price consolidation. Consolidation occurs when the price remains within a specific percentage range over a defined number of periods. The library identifies these ranges by calculating the maximum and minimum closing prices within the recent data subset.
Consolidation Detection Logic
The Breakout class implements is_consolidating, which checks if the difference between the maximum and minimum closing prices stays within a user-defined percentage threshold.
The adjustment factor is calculated as 1 - (percentage / 100). If the minimum close is greater than this adjusted maximum, the price is considered to be within a horizontal support/resistance zone.
Sources: lib/stolgo/breakout.py:10-36
Breakout and Breakdown Analysis
Once a consolidation zone (support/resistance range) is identified, the library provides methods to detect when the price exits this range. These events are categorized as "Breakouts" (crossing resistance) or "Breakdowns" (crossing support).
Detection Mechanism
- Validation: The system first validates that the data leading up to the last candle was in a state of consolidation.
- Comparison: The closing price of the most recent candle is compared against the high (resistance) or low (support) of the consolidation period.
| Feature | Function | Logic |
|---|---|---|
| Breakout | is_breaking_out | last_close > max(recent_closes) |
| Breakdown | is_breaking_down | last_close < min(recent_closes) |
Sources: lib/stolgo/breakout.py:38-74
Breakout Flow
This sequence diagram illustrates how the Breakout module determines if a price has breached a resistance level.
Sources: lib/stolgo/breakout.py:38-55
Supporting Components
Trend Identification
Before support and resistance levels can be effectively utilized, the underlying trend must be identified. The Trend class provides methods to detect "giant" uptrends and downtrends based on consecutive candle behavior.
- Uptrend: Every candle in the period must have a
Close > Openand a higher close than the previous candle. - Downtrend: Every candle in the period must have a
Close < Openand a lower close than the previous candle.
Sources: lib/stolgo/trend.py:8-76
Data Integration
The analysis relies on OHLC (Open, High, Low, Close) data retrieved via the Samco or Nasdaq modules. The helper module provides a get_data_resample function that ensures data is correctly aggregated into the timeframe required for analysis (e.g., converting 1-minute ticks to daily bars), preserving the High/Low values critical for identifying resistance and support.
# Aggregation logic used for support/resistance data preparation
ohlc_dict = {
'Open':'first',
'High':'max',
'Low':'min',
'Close': 'last',
'Volume':'sum'
}
Sources: lib/stolgo/samco.py:125-150, lib/stolgo/nasdaq.py:86-120, lib/stolgo/helper.py:82-85
Class Relationships
The following diagram shows the relationship between the data acquisition layers and the analysis modules.
Summary
Support and Resistance Analysis in Stolgo focuses on the mathematical detection of horizontal price ranges. By defining consolidation through the Breakout class, users can programmatically identify when price action is restricted between support and resistance levels and automate trading decisions based on the breach of these levels.