Paste the complete script into Pine Editor on a standard candlestick chart. Defaults enable next-bar follow-through and disable the prior-close context filter. The strategy trades one unit, freezes the pair stop and submits a time exit after three held bars if protection has not closed the trade. It has no profit target. Pattern labels include candidates that cannot be traded while another candidate or position is active.
Original educational implementation of this page specification. A syntax check validates compilation, not predictive value or broker execution.pine
//@version=6
strategy("Two-bar reversal: overlap and follow-through", overlay = true,
initial_capital = 100000, currency = currency.USD, pyramiding = 0,
default_qty_type = strategy.fixed, default_qty_value = 1,
commission_type = strategy.commission.cash_per_contract, commission_value = 0.01,
slippage = 1, process_orders_on_close = false,
calc_on_every_tick = false, calc_on_order_fills = false)
int overlapPct = input.int(75, "Minimum body overlap %", minval = 1, maxval = 100)
int extremePct = input.int(10, "Extreme tolerance % of larger range", minval = 0, maxval = 100)
bool followThrough = input.bool(true, "Require next-bar closing follow-through")
bool contextFilter = input.bool(false, "Require three prior directional closes")
int holdBars = input.int(3, "Held bars before time exit", minval = 1)
float tick = syminfo.mintick
float o = math.round(open / tick)
float h = math.round(high / tick)
float l = math.round(low / tick)
float c = math.round(close / tick)
float body = math.abs(c - o)
float span = h - l
float shared = math.max(0, math.min(math.max(o, c), math.max(o[1], c[1])) -
math.max(math.min(o, c), math.min(o[1], c[1])))
float largeBody = math.max(body, body[1])
float largeRange = math.max(span, span[1])
bool valid = barstate.isconfirmed and bar_index >= 1 and
span > 0 and span[1] > 0 and body > 0 and body[1] > 0
bool sizeOK = body * 2 >= span and body[1] * 2 >= span[1]
bool overlapOK = 100 * shared >= overlapPct * largeBody
bool downContext = bar_index >= 4 and c[4] > c[3] and c[3] > c[2]
bool upContext = bar_index >= 4 and c[4] < c[3] and c[3] < c[2]
bool bull = valid and sizeOK and overlapOK and c[1] < o[1] and c > o and
100 * math.abs(l - l[1]) <= extremePct * largeRange and
100 * (c - l) >= 75 * span and (not contextFilter or downContext)
bool bear = valid and sizeOK and overlapOK and c[1] > o[1] and c < o and
100 * math.abs(h - h[1]) <= extremePct * largeRange and
100 * (c - l) <= 25 * span and (not contextFilter or upContext)
var int pending = 0
var int pairBar = na
var float pendingExtreme = na
var float pendingStop = na
var float activeStop = na
bool closedHere = strategy.closedtrades > nz(strategy.closedtrades[1])
int orderSide = 0
float orderStop = na
if barstate.isconfirmed
if strategy.position_size != 0
pending := 0
int held = bar_index - strategy.opentrades.entry_bar_index(0) + 1
if held >= holdBars
strategy.close_all(comment = "Time exit")
else
activeStop := na
if closedHere
pending := 0
else if pending != 0
bool onNextBar = bar_index == pairBar + 1
bool confirms = pending == 1 ? c >= pendingExtreme + 1 and l > pendingStop :
c <= pendingExtreme - 1 and h < pendingStop
if onNextBar and confirms
orderSide := pending
orderStop := pendingStop * tick
pending := 0
else if bull or bear
int side = bull ? 1 : -1
float pairStop = bull ? math.min(l[1], l) - 1 : math.max(h[1], h) + 1
if followThrough
pending := side
pairBar := bar_index
pendingExtreme := bull ? h : l
pendingStop := pairStop
else
orderSide := side
orderStop := pairStop * tick
if orderSide == 1
activeStop := orderStop
strategy.entry("Long", strategy.long)
strategy.exit("Long stop", "Long", stop = orderStop)
else if orderSide == -1
activeStop := orderStop
strategy.entry("Short", strategy.short)
strategy.exit("Short stop", "Short", stop = orderStop)
plotshape(bull, title = "Bullish pair", text = "2BR+", style = shape.labelup,
location = location.belowbar, color = color.green, textcolor = color.white, size = size.tiny)
plotshape(bear, title = "Bearish pair", text = "2BR-", style = shape.labeldown,
location = location.abovebar, color = color.red, textcolor = color.white, size = size.tiny)
plot(activeStop, "Frozen trade stop", color = color.orange, style = plot.style_linebr)