Range Breaker
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © deperp

//@version=5
strategy("Range Breaker", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.07, pyramiding=0)

// Backtest Time Period

useDateFilter = input.bool(true, title="Begin Backtest at Start Date",
     group="Backtest Time Period")
backtestStartDate = input.time(timestamp("1 Jan 2020"), 
     title="Start Date", group="Backtest Time Period",
     tooltip="This start date is in the time zone of the exchange " + 
     "where the chart's instrument trades. It doesn't use the time " + 
     "zone of the chart or of your computer.")

inTradeWindow = not useDateFilter or time >= backtestStartDate

swingLookback = input.int(20, title="Swing Lookback", minval=3)
stopTargetPercent = input.float(5, title="Stop Target Percentage", step=0.1)

// Calculate lockback swings
swings(len) =>
    var highIndex = bar_index
    var lowIndex = bar_index
    var swingHigh = float(na)
    var swingLow = float(na)
    
    upper = ta.highest(len)
    lower = ta.lowest(len)
    
    if high[len] > upper
        highIndex := bar_index[len]
        swingHigh := high[len]

    if low[len] < lower
        lowIndex := bar_index[len]
        swingLow := low[len]

    [swingHigh, swingLow, highIndex, lowIndex]


// Strategy logic
[swingHigh, swingLow, highIndex, lowIndex] = swings(swingLookback)
longCondition = inTradeWindow and (ta.crossover(close, swingHigh))
shortCondition = inTradeWindow and (ta.crossunder(close, swingLow))

if longCondition
    strategy.entry("Long", strategy.long)
if shortCondition
    strategy.entry("Short", strategy.short)

longStopTarget = close * (1 + stopTargetPercent / 100)
shortStopTarget = close * (1 - stopTargetPercent / 100)

strategy.exit("Long Stop Target", "Long", limit=longStopTarget)
strategy.exit("Short Stop Target", "Short", limit=shortStopTarget)

// Plot break lines
line.new(x1=highIndex, y1=swingHigh, x2=bar_index, y2=swingHigh, color=color.rgb(255, 82, 82, 48), width=3, xloc=xloc.bar_index, extend=extend.right)
line.new(x1=lowIndex, y1=swingLow, x2=bar_index, y2=swingLow, color=color.rgb(76, 175, 79, 47), width=3, xloc=xloc.bar_index, extend=extend.right)


// Alert conditions for entry and exit
longEntryCondition = inTradeWindow and (ta.crossover(close, swingHigh))
shortEntryCondition = inTradeWindow and (ta.crossunder(close, swingLow))

longExitCondition = close >= longStopTarget
shortExitCondition = close <= shortStopTarget

alertcondition(longEntryCondition, title="Long Entry Alert", message="Enter Long Position")
alertcondition(shortEntryCondition, title="Short Entry Alert", message="Enter Short Position")
alertcondition(longExitCondition, title="Long Exit Alert", message="Exit Long Position")
alertcondition(shortExitCondition, title="Short Exit Alert", message="Exit Short Position")