Donchian Flag Pole Detector

Description
The Donchian Flag Pole Detector is a custom NinjaTrader 8 indicator designed to identify potential flag patterns in the market. It analyzes price action around a Donchian Channel to detect "load" phases followed by "flag pole" phases, which are essential components of flag patterns. By leveraging customizable rules, this indicator enables traders to:
- Pinpoint starting points for flag poles using bar body momentum and bar closes.
- Recognize the impulse phase of a setup, providing a reliable foundation for strategies like pullbacks or breakouts.
- Serve as a framework for advanced setups such as the Joe Ross 123 pattern.
- Adapt to different strategies, supporting both mean reversion and trend continuation.
The indicator uses Donchian Channel boundaries as references:
- Bullish flag poles start from the lower boundary.
- Bearish flag poles start from the upper boundary.
Additionally, traders can fine-tune how extended the flag pole should be by setting:
- Total ATR multiples for the flag pole extension.
- Average body size multiples for filtering valid patterns.
The Donchian Flag Pole Detector gives traders the flexibility to:
- Use the load phase as a preparatory condition (detecting new extremes in the Donchian Channel before the flag creation).
- Decide their entry and exit strategy (pullbacks, breakouts, or reversals) based on clearly defined flag pole characteristics.
Key Features:
- Flag Pole Detection: Identifies the beginning and end of flag poles using customizable rules.
- Load Phase Recognition: Detects periods of price consolidation preceding the flag pole formation.
- Customizable Parameters: Traders can fine-tune settings such as flag pole length, average body size ratios, and ATR multiples to adapt to different market conditions.
- Average Body Size Ratio: Ensures that the average body size of bars in the flag pole exceeds that of the load phase, prioritizing patterns with substantial momentum.
- ATR-Based Thresholds: Utilizes Average True Range (ATR) multiples to ensure price movement meets volatility conditions.
- Versatile Strategy Support: Supports both trend continuation and mean reversion strategies. Can act as a base for setups like the Joe Ross 123 pattern.
- Visual Signals: Plots triangles for bullish and bearish flag poles, with hash markers outlining the flag’s range, giving traders clear visual references for potential entry levels.
Tips for Implementation:
- Strategic Confirmation: Use the detected flag boundaries as dynamic support/resistance to confirm entries, and complement signals with indicators like MACD or RSI.
- Parameter Calibration: Adjust the Donchian Channel period and load period based on volatility, and modify the AverageBodySizeRatio to fine-tune the threshold for body size.
- Backtesting: Run extensive backtests across different market conditions to confirm optimal settings for consistent flag detection.
- Reversal Opportunity Setup: Incorporate this indicator into reversal or trend-following strategies, using detected flags as potential entry points for trend continuation or reversal setups.
Example of use in a strategy or indicator
You can see a complete NinjaTrader 8 strategy example using the flag pole indicator here https://github.com/anatoliecatarau/TradingDJStrategyLab/blob/master/TdjLabDonchianFlagRangeBreakout.cs
private TdjDonchianFlagPoleDetector flagPoleDetector;
//...
protected override void OnStateChange()
{
//...
if (State == State.SetDefaults)
{
//...
DonchianChannelPeriod = 5;
LoadPeriod = 2;
BodyAveragePeriod = 3;
PoleMinBars = 1;
PoleMaxBars = 4;
AverageBodySizeRatio = 1.5;
ExtensionSizeMinATRMultiples = 1;
AverageBarExtensionATMMultiples = 0.3;
MinAverageCloseRatio = 0.5;
//...
}
else if (State == State.DataLoaded)
{
flagPoleDetector = TdjDonchianFlagPoleDetector(DonchianChannelPeriod, LoadPeriod, BodyAveragePeriod, PoleMinBars, PoleMaxBars, AverageBodySizeRatio, ExtensionSizeMinATRMultiples, AverageBarExtensionATMMultiples, MinAverageCloseRatio);
//...
}
}
#region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Donchian Channel Period", Description = "Number of periods used to calculate the Donchian Channel.", Order = 1, GroupName = "001 Detector Parameters")]
public int DonchianChannelPeriod
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Load Period", Description = "Number of bars considered for the load phase.", Order = 2, GroupName = "001 Detector Parameters")]
public int LoadPeriod
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Body Average Period", Description = "Number of bars considered the average body size calculation.", Order = 3, GroupName = "001 Detector Parameters")]
public int BodyAveragePeriod
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Pole Min Bars", Description = "Minimum number of bars required in the flag pole.", Order = 4, GroupName = "001 Detector Parameters")]
public int PoleMinBars
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Pole Max Bars", Description = "Maximum number of bars allowed in the flag pole.", Order = 5, GroupName = "001 Detector Parameters")]
public int PoleMaxBars
{ get; set; }
[NinjaScriptProperty]
[Range(0, double.MaxValue)]
[Display(Name = "Average Body Size Ratio", Description = "Maximum number of bars allowed in the flag pole.", Order = 6, GroupName = "001 Detector Parameters")]
public double AverageBodySizeRatio
{ get; set; }
[NinjaScriptProperty]
[Range(0, double.MaxValue)]
[Display(Name = "Extension Size Min ATR Multiples", Description = "The minimum size of the extension in the direction of the flag pole in ATR multiples", Order = 7, GroupName = "001 Detector Parameters")]
public double ExtensionSizeMinATRMultiples
{ get; set; }
[NinjaScriptProperty]
[Range(0, double.MaxValue)]
[Display(Name = "Average Bar Extension ATR Multiples", Description = "The average bar extension of the flag measured in ATR multiples", Order = 8, GroupName = "001 Detector Parameters")]
public double AverageBarExtensionATMMultiples
{ get; set; }
[NinjaScriptProperty]
[Range(0, double.MaxValue)]
[Display(Name = "Min Average Close Ratio", Description = "The minimum average close ratio of the bars forming the pole in the direction of the flag pole", Order = 9, GroupName = "001 Detector Parameters")]
public double MinAverageCloseRatio
{ get; set; }
#endregion
Parameters
Donchian Channel Period:
- Type: int
- Description: Number of periods used to calculate the Donchian Channel.
- Default Value: 5
- Valid Range: Any positive integer greater than or equal to 1.
Load Period:
- Type: int
- Description: Minimum number of bars considered for the load phase.
- Default Value: 2
- Valid Range: Any positive integer greater than or equal to 1.
Body Average Period:
- Type: int
- Description: Number of bars used to calculate the average body size.
- Default Value: 3
- Valid Range: Any positive integer greater than or equal to 1.
Pole Min Bars:
- Type: int
- Description: Minimum number of bars required in the flag pole.
- Default Value: 1
- Valid Range: Any positive integer greater than or equal to 1.
Pole Max Bars:
- Type: int
- Description: Maximum number of bars allowed in the flag pole.
- Default Value: 4
- Valid Range: Any positive integer greater than or equal to 1.
Average Body Size Ratio:
- Type: double
- Description: Minimum average body size ratio required for a valid flag pole.
- Default Value: 1.5
- Valid Range: Any positive double value.
Extension Size Min ATR Multiples:
- Type: double
- Description: The minimum size of the total extension of the flag pole, measured in ATR multiples.
- Default Value: 1.0
- Valid Range: Any positive double value.
Average Bar Extension ATR Multiples:
- Type: double
- Description: The average bar extension of the flag pole, measured in ATR multiples.
- Default Value: 0.3
- Valid Range: Any positive double value.
Min Average Close Ratio:
- Type: double
- Description: Minimum average close ratio of the bars forming the flag pole in the direction of the flag.
- Default Value: 0.5
- Valid Range: Any double value between 0 and 1.
Plot Offset:
- Type: int
- Description: Distance between the plotted signal and the high or low point of the bar, measured in ticks.
- Default Value: 5
- Valid Range: Any positive integer greater than or equal to 1.
Plots
IndicatorSignalUp
- Type: Plot
- Style: Triangle Up
- Color: Green
- Description: Marks the presence of a bullish flag pole pattern, serving as a potential entry signal for upward momentum.
SignalUpUpperLevel
- Type: Plot
- Style: Hash
- Color: Green
- Description: Displays the upper boundary of the bullish flag pattern, indicating a potential breakout level.
SignalUpLowerLevel
- Type: Plot
- Style: Hash
- Color: Green
- Description: Shows the lower boundary of the bullish flag, providing support within the pattern.
IndicatorSignalDown
- Type: Plot
- Style: Triangle Down
- Color: Red
- Description: Indicates a bearish flag pole pattern, highlighting potential for downward continuation.
SignalDownUpperLevel
- Type: Plot
- Style: Hash
- Color: Red
- Description: Marks the upper boundary of the bearish flag, serving as a potential resistance level.
SignalDownLowerLevel
- Type: Plot
- Style: Hash
- Color: Red
- Description: Displays the lower boundary of the bearish flag pattern, suggesting potential breakout support.