Gap Filling Strategy 
// 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)