Pine Script Breakout + FVG Strategy and Indicator
A 4,600-line Pine Script v6 strategy and companion indicator that turn a discretionary futures breakout method into a 10-stage rule pipeline with multi-timeframe fair value gaps, session targets and rejection logging.
The problem
The client traded a discretionary breakout method on index and energy futures: swing structure, fair value gaps (FVGs) and session levels. He wanted it written as exact rules, so it could be backtested on TradingView and alerted live, without repainting.
What I built
- 10-stage entry pipeline:
- swing high/low detection
- breakout time window after the swing
- mitigation of a chart-timeframe FVG
- structure failure (a lower low or higher high)
- lookback-extreme check
- optional 15-minute FVG touch
- optional round-number touch
- a cap on FVGs blocking the path to target
- min/max risk-to-reward check
- entry
- Swing logic: a swing needs an expansion leg of at least two same-colour candles and a correction candle. It is confirmed when price breaks the correction line.
- Multi-timeframe FVGs: 15-minute and 1-hour gaps tracked through their full lifecycle (untapped, touching, tapped, touching again, invalidated). Invalidation needs at least two confirming closes.
- Session targets: Tokyo, London and New York highs and lows that sit near round-number levels. Only the strongest two per session are kept, and they are cleared after New York.
- Position sizing: fixed contracts, percent of equity, or risk percent from the stop distance and point value. Trades under a minimum size are rejected.
- Trade management: a stop beyond the swing plus a buffer, a target at the nearest level, conditional breakeven after N minutes, partial exits, and add-on entries when new same-direction FVGs appear.
- Companion indicator with about 100 inputs and alert conditions for valid long and short signals.
- Settings UX plan: 12 input groups reduced to 7, with conservative, balanced and aggressive presets and an advanced-settings toggle.
Details
- No repainting: higher-timeframe data is requested with an offset and lookahead off. Signal drawing and FVG updates only run on confirmed bars, and swings count only once the correction completes.
- Rejection logging: about 200 log calls explain why each candidate signal failed (for example “break but no FVG mitigation”). The client used these logs to check the rules on his own charts.
- Performance within Pine’s limits: capped arrays, early exits in loops and a two-phase FVG pre-filter keep the script inside TradingView’s execution budget.