The converted example keeps the signal calculations and plots. It ignores new signals while a position is open. On an accepted signal it snapshots ATR, converts the stop and target distances to ticks, and submits the bracket together with the entry. The bracket distances stay fixed for that trade. With these calculation settings, a market entry normally fills at the following bar open, with the configured slippage.
Complete Pine Script v6 conversion example. Costs and sizing are illustrative.pinescript
//@version=6
strategy("Trend Pullback Strategy [Conversion Demo]", overlay = true,
initial_capital = 10000,
default_qty_type = strategy.percent_of_equity, default_qty_value = 10,
commission_type = strategy.commission.percent, commission_value = 0.1,
slippage = 2, pyramiding = 0,
margin_long = 100, margin_short = 100,
calc_on_every_tick = false, calc_on_order_fills = false,
process_orders_on_close = false)
// Indicator parameter inputs
fastLen = input.int(20, "Fast EMA Length", minval = 1)
slowLen = input.int(50, "Slow EMA Length", minval = 1)
rsiLen = input.int(14, "RSI Length", minval = 1)
rsiOb = input.int(70, "RSI Overbought", minval = 50, maxval = 100)
rsiOs = input.int(30, "RSI Oversold", minval = 0, maxval = 50)
// Illustrative exit inputs; choose values for your own rules
atrLen = input.int(14, "ATR Length", minval = 1)
slMult = input.float(2.0, "Stop ATR Multiple", minval = 0.1)
tpMult = input.float(3.0, "Target ATR Multiple", minval = 0.1)
// Indicator calculations
fastEma = ta.ema(close, fastLen)
slowEma = ta.ema(close, slowLen)
rsiVal = ta.rsi(close, rsiLen)
atrVal = ta.atr(atrLen)
// Visual chart lines
plot(fastEma, "Fast EMA", color = color.blue, linewidth = 2)
plot(slowEma, "Slow EMA", color = color.orange, linewidth = 2)
// Evaluate crosses on every bar before combining conditions
crossUp = ta.crossover(close, fastEma)
crossDown = ta.crossunder(close, fastEma)
// Confirm signals at bar close in both versions
bullTrend = fastEma > slowEma
bearTrend = fastEma < slowEma
longSignal = barstate.isconfirmed and bullTrend and crossUp and rsiVal > 50 and rsiVal < rsiOb
shortSignal = barstate.isconfirmed and bearTrend and crossDown and rsiVal < 50 and rsiVal > rsiOs
// Keep every signal arrow, including signals skipped while in a position
plotshape(longSignal, title = "Buy Arrow", style = shape.triangleup,
location = location.belowbar, color = color.green, size = size.small)
plotshape(shortSignal, title = "Sell Arrow", style = shape.triangledown,
location = location.abovebar, color = color.red, size = size.small)
// Snapshot ATR distances when submitting the entry and its bracket.
// loss/profit are ticks from the eventual fill, not absolute prices.
if strategy.position_size == 0 and not na(atrVal)
stopTicks = math.max(1, math.round(atrVal * slMult / syminfo.mintick))
targetTicks = math.max(1, math.round(atrVal * tpMult / syminfo.mintick))
if longSignal
strategy.entry("Long", strategy.long)
strategy.exit("Exit Long", from_entry = "Long", loss = stopTicks, profit = targetTicks)
else if shortSignal
strategy.entry("Short", strategy.short)
strategy.exit("Exit Short", from_entry = "Short", loss = stopTicks, profit = targetTicks)