Flag breakout buy sell
Buy or sell on a close outside a sideways range that followed a sharp move; silent at default settings.
Flag breakout runs two independent state machines, one for buys and one for sells. Each side arms when the change in close over roc_period bars passes impulse_up (buy) or impulse_down (sell), then waits for a sideways channel or a converging triangle to hold for hold_n bars in a row. The range edges are the highest and lowest closes over the last 100 bars by default, and a breakout is a close beyond one of them, held for confirm_days bars.
How to read it
- Buy and sell are separate markers, drawn on the candle where the breakout fires.
- A marker needs a sharp move first, then a sideways stretch, then a close past the edge; no marker means the setup has not completed.
- Each breakout gets one marker on its first qualifying bar, so a breakout that keeps running is not repeated.
- At default settings SPY one-minute charts show no markers; the try presets loosen the thresholds.
What it outputs
- Buy
- A close above the range high, after an upward impulse and a consolidation. Fires once per breakout.
- Sell
- A close below the range low, after a downward impulse and a consolidation. Fires once per breakout.
When it fires
Fires once per breakout: the first bar a close beats the range edge for confirm_days bars, during a consolidation that followed an impulse in that direction. A buy breaks up after a sharp rise; a sell breaks down after a sharp fall. It marks a breakout that happened, not the next move.
Settings
- roc_period
- How many bars back the impulse measures the change in close. A longer look-back lets a move build over more bars, so the same threshold trips more often; a shorter one only catches fast bursts.
- impulse_up
- The rise over roc_period bars that arms the buy side. Default 0.05 (5%) is larger than any 20-bar move in the SPY sample, so raising it only removes setups; lowering it to 0.003 arms buys on small rises.
- impulse_down
- The fall over roc_period bars that arms the sell side, entered as a negative number. Default -0.05 (-5%) is never reached in the SPY sample. Raising it toward zero, such as -0.003, arms sells on small falls; lowering it removes setups.
- hold_n
- Consecutive bars the channel or triangle must hold before a consolidation counts. Higher values demand a longer sideways stretch and fewer setups; 3 accepts short pauses and produces more.
- band
- Margin past the range edge the close must clear. 0 means any close beyond the edge. Positive values, such as 0.001 (0.1%), need a clear break and fire less often; negative values fire just short of the edge.
- confirm_days
- Consecutive bars the breakout must hold before the marker fires. 1 marks the first bar beyond the edge. Higher values wait for the break to hold, removing single-bar pokes at the cost of a later marker.
Pitfalls
- Default settings never fired on the SPY sample: its 20,160 closes spanned 749.79 to 775.02, about 3.4%, so no 20-bar move could reach the 5% impulse.
- After an impulse, a side waits for a consolidation with no time limit and no other exit, so an old impulse can keep that side armed long after the move.
- The range edges and the breakout both use closing prices, so an intrabar wick past the edge never counts.