Trying to add stops to my trading view backtest.
Every time I add a stop loss it either gets ignored or I get stopped out and immediately reenter the position. I wanted to add a percentage stop and the option to either add a stop that moves with the moving average or a fixed stop value.
// @version = 5 strategy("Pairs Trading Moving Average", shorttitle = "Pairs Trading MA", overlay = true, process_orders_on_close = true, backtest_fill_limits_assumption = 0) // Inputs long_short = input.string(title = "Long/Short?", options = ["Long", "Short", "Both"], defval = "Both" ) LEVERAGE = input.float(title="Leverage", defval=1) maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "VWAP", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA", "DEMA", "TEMA", "ZLEMA", "THMA", "EHMA"], group="MA Settings") len = input(30, title="Length") multiplier = input.float(title = "Stdev Multiplier", defval = 2) src = input(hlc3, title="Source") off_s = input(title="Offset Plot", defval=1) startDate = input.int(title="Start Date", defval=1) startMonth = input.int(title="Start Month", defval=1) startYear = input.int(title="Start Year", defval=2018) endDate = input.int(title="End Date", defval=1) endMonth = input.int(title="End Month", defval=1) endYear = input.int(title="End Year", defval=2025) // Moving Averages vwap(src, len) => math.sum((src * volume), len) / math.sum(volume, len) dema(src, len) => 2 * ta.ema(src, len) - ta.ema(ta.ema(src, len), len) tema(src, len) => 3 * (ta.ema(src, len) - ta.ema(ta.ema(src, len), len)) + ta.ema(ta.ema(ta.ema(src, len), len), len) zlema(src, len) => lag = math.round((len - 1) / 2) ema_data = src + (src - src[lag]) zl= ta.ema(ema_data, len) ehma(src, len) => ta.ema(2 * ta.ema(src, len / 2) - ta.ema(src, len), math.round(math.sqrt(len))) thma(src, len) => ta.wma(ta.wma(src,len / 3) * 3 - ta.wma(src, len / 2) - ta.wma(src, len), len) ma(src, len, type) => switch type "EHMA" => ehma(src, len) "THMA" => thma(src, len) "ZLEMA" => zlema(src, len) "TEMA" => tema(src, len) "DEMA" => dema(src, len) "SMA" => ta.sma(src, len) "VWAP" => vwap(src, len) "EMA" => ta.ema(src, len) "SMMA (RMA)" => ta.rma(src, len) "WMA" => ta.wma(src, len) "VWMA" => ta.vwma(src, len) "HMA" => ta.hma(src, len) // Variables out = ma(src, len, maTypeInput) above = out + (ta.stdev(src, len) * multiplier) below = out - (ta.stdev(src, len) * multiplier) inDateRange = (time >= timestamp(syminfo.timezone, startYear,startMonth, startDate, 0, 0)) and (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0)) // Plots plot(out, offset=off_s) plot(above, color = color.red, offset=off_s) plot(below, color = color.green, offset=off_s) // Position Sizing long_qty = math.floor(strategy.equity * LEVERAGE / below) short_qty = math.floor(strategy.equity * LEVERAGE / above) // Order Execution if inDateRange if long_short == "Long" strategy.cancel_all() strategy.entry("Bullish IB", strategy.long, qty=long_qty, limit=below) strategy.exit("Bullish Exit","Bullish IB", limit=out) if long_short == "Short" strategy.cancel_all() strategy.entry("Bearish IB", strategy.short, qty=short_qty, limit=above) strategy.exit("Bearish Exit","Bearish IB", limit=out) if long_short == "Both" strategy.cancel_all() strategy.entry("Bearish IB", strategy.short, qty=short_qty, limit=above) strategy.exit("Bearish Exit","Bearish IB", limit=out) strategy.entry("Bullish IB", strategy.long, qty=long_qty, limit=below) strategy.exit("Bullish Exit","Bullish IB", limit=out)