Donchian Flag Pole Detector

Donchian Flag Pole Detector

Description

The Donchian Flag Pole Detector is a powerful pattern-recognition indicator that detects potential bullish and bearish flag formations based on price action and Donchian Channel behavior. It identifies a structured load phase—where price pushes beyond prior channel extremes—and tracks the subsequent flag pole impulse.

Traders can use this tool to spot moments of strong directional momentum that are suitable for pullback entries, breakout formations, or continuation setups. The indicator includes customizable filters that validate the strength of the pole using metrics like average bar body size, ATR-based extension, and close position within each bar.

A new feature, LoadExtremeBreak, now ensures that the flag pole breaks the extreme set during the load phase to validate momentum—helping filter out weak or failed patterns.

Key Features:

  • Detects structured load phases using Donchian Channel expansion.
  • Tracks bullish and bearish flag poles with customizable min/max bar counts.
  • Measures pole quality based on:
    • Body momentum (body size average).
    • Directional consistency (close ratio within bars).
    • Extension strength using ATR multiples.
  • Optional LoadExtremeBreak logic ensures the pole breaks above/below the high/low of the loading phase, adding precision to setups.
  • Plots visual markers (triangles and levels) for signal generation and integration into strategy scripts.
  • Can serve as a base pattern engine for custom strategies, such as breakout pullbacks, mean reversion, or setups like Joe Ross 123.

Tips for Implementation:

  • Strategic Alignment: Use the plotted flag pole as a trigger for setups involving pullbacks, continuations, or structured consolidations.
  • Momentum Confirmation: Keep LoadExtremeBreak enabled to ensure only strong, clean breakouts are validated. Disable for early signals if needed.
  • Body Ratio Tuning: Adjust AverageBodySizeRatio to refine momentum threshold—lower values allow shorter impulses, higher values require stronger moves.
  • ATR Filtering: Use ExtensionSizeMinATRMultiples and AverageBarExtensionATRMultiples to reject low-volatility or choppy impulses.
  • Load Configuration: The LoadPeriod defines how many consecutive Donchian Channel breakouts are needed to consider a new setup. Set to 0 to disable load filtering.
  • Backtesting: Essential to test parameter sets across timeframes and instruments. Ideal for confirming whether breakout moves translate to real edge in live conditions.

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;
		LoadExtremeBreak = false;
		BodyAveragePeriod = 3;
		PoleMinBars = 1;
		PoleMaxBars = 4;
		AverageBodySizeRatio = 1.5;
		ExtensionSizeMinATRMultiples = 1;
		AverageBarExtensionATRMultiples = 0.3;
		MinAverageCloseRatio = 0.5;
    	//...
    }
	else if (State == State.DataLoaded)
	{
        	flagPoleDetector = TdjDonchianFlagPoleDetector(DonchianChannelPeriod, LoadPeriod, LoadExtremeBreak, BodyAveragePeriod, PoleMinBars, PoleMaxBars, AverageBodySizeRatio, ExtensionSizeMinATRMultiples, AverageBarExtensionATRMultiples, 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="Parameters")]
public int DonchianChannelPeriod
{ get; set; }

[NinjaScriptProperty]
[Range(0, int.MaxValue)]
[Display(Name="Load Period", Description="Number of bars considered for the load phase.", Order=2, GroupName="Parameters")]
public int LoadPeriod
{ get; set; }

[NinjaScriptProperty]
[Display(Name = "Load Extreme Break", Description = "When enabled, the bar that made a new high or low during the loading phase, must be broken", Order = 3, GroupName = "Parameters")]
public bool LoadExtremeBreak
{ get; set; }

[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Body Average Period", Description = "Number of bars considered the average body size calculation.", Order = 4, GroupName = "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=5, GroupName="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=6, GroupName="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 = 7, GroupName = "Parameters")]
public double AverageBodySizeRatio
{ get; set; }

[NinjaScriptProperty]
[Range(0, double.MaxValue)]
[Display(Name = "Extension Size Min ATR Multiples", Description = "The minimum size of the total extension of the flag measured in ATR multiples", Order = 8, GroupName = "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 = 9, GroupName = "Parameters")]
public double AverageBarExtensionATRMultiples
{ 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 = 10, GroupName = "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 0 (no load required).

Load Extreme Break

  • Type: Boolean
  • Description: If enabled, the flag pole must break the high (for long) or low (for short) established during the load phase. This helps validate true breakout energy.
  • Default: false (set true to enforce the breakout logic)

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.