//@version=6 strategy("ALDER price approximation", overlay = true, initial_capital = 10000, pyramiding = 0, default_qty_type = strategy.fixed, default_qty_value = 1, commission_type = strategy.commission.cash_per_order, commission_value = 2, slippage = 1, margin_long = 100, margin_short = 100) n = input.int(5, "Formation sessions", minval = 2) multiple = input.float(2, "Range multiple", minval = 0.1) if not timeframe.isdaily runtime.error("Use daily bars for this lesson") ceiling = ta.highest(high, n)[1] average = ta.sma(close, n) rangeMean = ta.sma(ta.tr(true), n) ready = not na(ceiling) and not na(rangeMean) signal = ready and close > ceiling and close > average var float distance = na var float highestClose = na var float stopPrice = na flat = strategy.position_size == 0 justExited = ta.change(strategy.closedtrades) > 0 if barstate.isconfirmed and flat and not justExited if signal distance := math.ceil(rangeMean * multiple / syminfo.mintick) * syminfo.mintick highestClose := na stopPrice := na strategy.entry("L", strategy.long) strategy.exit("X", "L", loss = math.round(distance / syminfo.mintick)) if barstate.isconfirmed and strategy.position_size > 0 highestClose := na(highestClose) ? close : math.max(highestClose, close) initial = strategy.position_avg_price - distance candidate = math.max(initial, highestClose - distance) stopPrice := na(stopPrice) ? candidate : math.max(stopPrice, candidate) strategy.exit("X", "L", stop = stopPrice) plot(ceiling, "Prior high", color.gray) plot(average, "Simple average", color.blue) plot(flat ? na : stopPrice, "Next stop", color.red, style = plot.style_linebr)