This Pine Script is designed to identify pullbacks and breakouts in the price movement by utilizing a combination of technical indicators, including a 20-period exponential moving average (EMA).
User Inputs
rangeLength: The number of bars to consider for analysis (default = 10).
pullbackBars: Lookback period for validating pullbacks (default = 3).
pullbackThreshold: The minimum required price change to qualify as a pullback (default = 1 tick/pip).
useEMAFilter: Option to enable or disable the EMA trend filtering (default = true).
emaLength: The period for the EMA calculation (default = 20).
Trend Detection
The script calculates the EMA and determines the current trend:
inUptrend: True if the close price is above the EMA.
inDowntrend: True if the close price is below the EMA.
Trading Range Detection
Detects if the price is in a trading range by checking:
Count of EMA crosses.
Overlapping of bars (average bar size vs. high-low distance).
Average True Range (ATR) for volatility.
If certain conditions are met, the indicator will ignore pullback detection.
Pullback Detection
Regular Pullbacks:
Bullish Pullback: Identified if the current low goes below the previous low and price is in an uptrend.
Bearish Pullback: Identified if the current high goes above the previous high and price is in a downtrend.
Implied Pullbacks:
Bullish Implied Pullback: A bearish candle in an uptrend (not followed by a regular pullback).
Bearish Implied Pullback: A bullish candle in a downtrend (not followed by a regular pullback).
Breakout Detection
Breakout Up: Detected when price crosses above the EMA after not having detected a regular pullback.
Breakout Down: Detected when price crosses below the EMA after not having detected a regular pullback.
Bar Coloring & Table
Bars are colored based on conditions:
Purple: Breakout bars.
Red: Regular bullish pullbacks.
Green: Regular bearish pullbacks.
Orange: Implied bullish pullbacks.
Dark Yellow: Implied bearish pullbacks.
A visual table is displayed in the top-right corner, reflecting definitions and colors of different pullbacks and breakout types for easy reference.
Further Enhancements
Resetting of implied pullbacks if a regular pullback has occurred.
Alerts can be added for pullbacks, breakouts, and significant trend changes.
Additional adjustments such as table styling and positioning can be made as needed.