Strategy · Pine Script v4 · On the chart

Gap Filling Strategy

by regalouisei· code credited to alexgrover

This strategy detects daily opening gaps and trades them on the price chart.

View on GitHub

Repaints

GitHub stars2on the repository
Pine versionv4older, still runs
Lines of code33
Settings2inputs
LicenceNone statedfile: MPL-2.0
Last update8 months ago25 Feb 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

33 lines of Pine Script v4 from pinescript/editors_picks/gap-filling-strategy.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

gap-filling-strategy.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © alexgrover

//@version=4
strategy("Gap Filling Strategy",overlay=true)
invert = input(false),clw = input("New Session","Close When:",
  options=["New Session","New Gap","Reverse Position"])
//----
ses = change(time("D")) 
o = open,c = close
//----
upgap = o > high[1] and min(c,o) > max(c[1],o[1])
dngap = o < low[1] and min(c[1],o[1]) > max(c,o)
//----
val = upgap ? max(c[1],o[1]) : min(c[1],o[1])
lim = valuewhen(ses and (upgap or dngap),val,0)
//----
strategy.close_all(clw == "New Session" ? ses :
  clw == "New Gap" ? ses and (upgap or dngap) : false)
if invert
    strategy.entry("Buy", strategy.long, when=ses and upgap)
    strategy.entry("Sell", strategy.short, when=ses and dngap)
else
    strategy.entry("Buy", strategy.long, when=ses and dngap)
    strategy.entry("Sell", strategy.short, when=ses and upgap)
if invert
    strategy.exit("ExitBuy","Buy",stop=lim)
    strategy.exit("ExitSell","Sell",stop=lim)
else
    strategy.exit("ExitBuy","Buy",limit=lim)
    strategy.exit("ExitSell","Sell",limit=lim)
//----
plot(lim,"Limit/Stop",#ff1100,2)

Overview

Gap Filling Strategy marks an up or down gap from the current open and close versus the previous bar, then stores the prior extreme as the fill level. By default it buys down gaps and shorts up gaps, exiting with a limit there. Invert reverses entries so that level is a stop. Close When flattens on a new daily session or new gap, or waits for the opposite signal. Tests use the live close and can change before the bar closes, and no stop exists unless invert is on.

Details

Published by

regalouisei on GitHub

Code credit

alexgrover, as written in the script's header

Type

Strategy, Pine Script v4

Draws

On the price chart

Category

Mean reversion, Sessions & time, Support & resistance

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • The strategy enters trades without a stop loss

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
invertboolfalseinvert
Close When:stringNew Sessionclw
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

3C QFL Mean Reversal

by hasnocool
106 stars

3C QFL Mean Reversal is a strategy based on the Quickfinger Luc (QFL) trading methodology. It identifies breakdown buying opportunities below established price base levels and enters mean-reversion trades. The strategy includes multi-safety order DCA scaling,

Strategy v5 Mean reversionSupport & resistance Repaints
Updated 2 years ago · GPL-3.0 GitHub repository of 3C QFL Mean Reversal
5 stars

This strategy enters one long trade when the bar low comes within a set point tolerance of the prior day's low and the close finishes above that level. Entries are allowed only inside a UTC session and are blocked after a Friday cutoff hour. It stays flat unti

Strategy v5 Support & resistanceMean reversion Repaints
Updated 4 months ago · MIT GitHub repository of PDL — Prior Day Level Retest (Bull)
1 stars

This strategy identifies Initial Balance periods on NQ and ES futures charts using user-defined start and end hours in Eastern Time. It classifies days as narrow or wide based on average range lookback and skips narrow or trending days per settings. Entries tr

Strategy v6 Mean reversionSupport & resistance
Updated 7 months ago · MIT GitHub repository of IB Mean Reversion Fade

XSP Daily Lotto

by majohnst
1 stars

This strategy models a zero-days-to-expiration (0DTE) Opening Range fade system on the price chart, designed around Mini-SPX index options. It calculates an opening range box and gauges directional sentiment from an external symbol like ES futures. Bullish con

Strategy v6 Sessions & timeSupport & resistance
Updated 3 months ago · None stated GitHub repository of XSP Daily Lotto

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers