//@version=6
strategy("Trend Breakout", overlay = true,
initial_capital = 100000,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 50,
commission_type = strategy.commission.percent,
commission_value = 0.1)
length = input.int(180, "Length", minval = 5, maxval = 200)
mult = input.float(2.25, "Multiplier", minval = 0.25, maxval = 5.0, step = 0.25)
src = input.source(close, "Source")
trailing = input.bool(false, "Use trailing stop")
trail = input.float(3.0, "Trail %", minval = 0.25, maxval = 20.0, step = 0.25, group = "Risk")
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
longSignal = ta.crossover(close, upper)
shortSignal = ta.crossunder(close, lower)
if longSignal
strategy.entry("L", strategy.long)
if shortSignal
strategy.entry("S", strategy.short)
if trailing and strategy.position_size != 0
points = math.max(1, math.round(strategy.position_avg_price * trail / 100 / syminfo.mintick))
strategy.exit("L trail", "L", trail_points = points, trail_offset = points)
strategy.exit("S trail", "S", trail_points = points, trail_offset = points)
plot(basis, "Basis", color = color.teal, linewidth = 2)
plot(upper, "Upper", color = color.gray)
plot(lower, "Lower", color = color.gray)
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 GammaExpansion
// Original OptiPine example. Spot candles also support simulated short positions here.
// Half-equity sizing leaves room for adverse moves without immediate margin liquidation.
// The optional trail activates after a Trail % gain and follows at that distance.