Paste this complete strategy into Pine Editor and add it to a standard candlestick chart. It trades one unit while flat, ignores signals while a position is open and never reverses directly. It places the fixed stop with the entry order. If the stop has not closed the trade, a time exit is submitted after five held bars by default, counting the entry bar as the first. There is no profit target.
Original educational strategy for the rules on this page. Pattern labels show every qualifying sequence, including signals ignored while a trade is open. They are not entry-fill markers.pine
//@version=6
strategy("Three-bar reversal: confirmed close, next open", 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 bufferTicks = input.int(1, "Minimum separation in ticks", minval = 1)
int holdBars = input.int(5, "Bars held before time exit", minval = 1)
float q = syminfo.mintick
float o = math.round(open / q)
float h = math.round(high / q)
float l = math.round(low / q)
float c = math.round(close / q)
bool ready = barstate.isconfirmed and bar_index >= 2 and
h > l and h[1] > l[1] and h[2] > l[2]
bool bull = ready and c[2] <= o[2] - bufferTicks and
h[1] <= h[2] - bufferTicks and l[1] <= l[2] - bufferTicks and
c >= o + bufferTicks and c >= math.max(h[2], h[1]) + bufferTicks
bool bear = ready and c[2] >= o[2] + bufferTicks and
h[1] >= h[2] + bufferTicks and l[1] >= l[2] + bufferTicks and
c <= o - bufferTicks and c <= math.min(l[2], l[1]) - bufferTicks
bool closedThisBar = strategy.closedtrades > nz(strategy.closedtrades[1])
var float stopPrice = na
if strategy.position_size == 0
stopPrice := na
if not closedThisBar
if bull
stopPrice := (math.min(l, math.min(l[1], l[2])) - 1) * q
strategy.entry("Long", strategy.long)
strategy.exit("Long stop", "Long", stop = stopPrice)
else if bear
stopPrice := (math.max(h, math.max(h[1], h[2])) + 1) * q
strategy.entry("Short", strategy.short)
strategy.exit("Short stop", "Short", stop = stopPrice)
else
int held = bar_index - strategy.opentrades.entry_bar_index(0) + 1
if held >= holdBars
strategy.close_all(comment = "Time exit")
plotshape(bull, title = "Confirmed bullish pattern", text = "3BR+",
style = shape.labelup, location = location.belowbar, color = color.green,
textcolor = color.white, size = size.tiny)
plotshape(bear, title = "Confirmed bearish pattern", text = "3BR-",
style = shape.labeldown, location = location.abovebar, color = color.red,
textcolor = color.white, size = size.tiny)
plot(stopPrice, "Fixed stop", color = color.orange, style = plot.style_linebr)