// This Pine Script® code is licensed under Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// © PineForge contributors 2026
//
// PF probe 09 — highest/lowest breakout (was Aroon)
// Purpose: parity test for ta.highestbars / ta.lowestbars offset semantics
// (negative-offset == 0 marks current bar as the new extreme) plus EMA gate.
//
//@version=6
strategy("Highest Lowest Breakout", overlay=true, initial_capital=1000000, process_orders_on_close=false, pyramiding=1, commission_type=strategy.commission.percent, commission_value=0, slippage=0, default_qty_type=strategy.fixed, default_qty_value=1)
// === Inputs ===
lookback = input.int(20, "Lookback", minval=5)
// === Using highestbars and lowestbars ===
barsToHigh = ta.highestbars(high, lookback)
barsToLow = ta.lowestbars(low, lookback)
hh = ta.highest(high, lookback)
ll = ta.lowest(low, lookback)
// === Recent new high/low ===
// barsToHigh returns negative offset, so 0 means current bar is highest
newHigh = barsToHigh == 0
newLow = barsToLow == 0
// === Trend confirmation with MA ===
maVal = ta.ema(close, 50)
// === Signals ===
longCond = newHigh and close > maVal
shortCond = newLow and close < maVal
if longCond
strategy.entry("Long", strategy.long)
if shortCond
strategy.entry("Short", strategy.short)
// Exit at mid channel
midChan = (hh + ll) / 2
if strategy.position_size > 0 and close < midChan
strategy.close("Long")
if strategy.position_size < 0 and close > midChan
strategy.close("Short")
plot(hh, "Highest", color=color.green)
plot(ll, "Lowest", color=color.red)
plot(midChan, "Mid", color=color.gray)