//@version=6 strategy("BIRCH 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) moving = input.bool(false, "Separate moving-target trial") meanNow = ta.sma(close, n) base = meanNow[1] deviation = ta.stdev(close, n, false)[1] z = not na(deviation) and deviation > 0 ? (close - base) / deviation : na signal = not na(z) and z >= -3 and z <= -2 if not timeframe.isdaily runtime.error("Use daily bars") var float stopPrice = na var float target = na justExited = ta.change(strategy.closedtrades) > 0 if barstate.isconfirmed if strategy.position_size == 0 and not justExited if signal stopPrice := base - 3 * deviation target := base strategy.entry("L", strategy.long) strategy.exit("X", "L", stop = stopPrice, limit = target) if strategy.position_size > 0 target := moving ? meanNow : target strategy.exit("X", "L", stop = stopPrice, limit = target) entryBar = strategy.opentrades.entry_bar_index(0) if bar_index - entryBar >= 2 strategy.close("L") plot(strategy.position_size > 0 ? target : na, "Target", color.blue, style = plot.style_linebr) plot(strategy.position_size > 0 ? stopPrice : na, "Stop", color.red, style = plot.style_linebr)