Appearance
Personalize your experience
Regression Breakout Retest
FreeCounter-slope channel breaks, entered on the retest of the broken edge
Live engine preview
The QuaScript engine's own output on the chart the terminal uses. Change the settings or edit the code and press Run to recompute it.
About
Breaks of a clean regression channel against its own slope start new trends; the safest entry is the retest of the broken edge. A falling channel (R squared above the level) whose upper 2-sigma edge is closed through arms a long setup at that edge; when price pulls back to within the tolerance of the frozen edge and closes above it, the strategy buys. Mirror for shorts. Exit: stop below the retest low by half an ATR, target of the channel height projected from the edge, setups expire after a number of bars.
Settings
Channel window: 120
Minimum R squared: 0.55
Edge (sigma): 2
Retest tolerance (ATR): 0.3
Setup expiry (bars): 30
Backtest it yourself
Open it on the backtesting page to run it on any symbol, interval and date range, then use Monte Carlo, optimisation, walk-forward and robustness tests.
Strategy Rules
Entry Conditions
Exit Conditions
- ▸ stop below the retest low by half an ATR, target of the channel height projected from the edge, setups expire after a number of bars
Source Code
Edit in IDE| 1 | strategy "Regression Breakout Retest" { |
| 2 | overlay = true |
| 3 | capital = 100000 |
| 4 | commission = 0.05 |
| 5 | slippage = 1 |
| 6 | sizing = risk_percent |
| 7 | size = 1 |
| 8 | max_leverage = 1 |
| 9 | } |
| 10 | /// Breaks of a clean regression channel against its own slope start new trends; the safest entry is |
| 11 | /// the retest of the broken edge. A falling channel (R squared above the level) whose upper 2-sigma |
| 12 | /// edge is closed through arms a long setup at that edge; when price pulls back to within the |
| 13 | /// tolerance of the frozen edge and closes above it, the strategy buys. Mirror for shorts. |
| 14 | /// Exit: stop below the retest low by half an ATR, target of the channel height projected from |
| 15 | /// the edge, setups expire after a number of bars. |
| 16 | use "viz/premium" as pr |
| 17 | len = param.int(120, title = "Channel window", min = 20, max = 2000, group = "Channel") |
| 18 | q_min = param.float(0.55, title = "Minimum R squared", min = 0.1, max = 0.95, step = 0.01, group = "Channel") |
| 19 | k_sd = param.float(2.0, title = "Edge (sigma)", min = 0.5, max = 4, step = 0.1, group = "Channel") |
| 20 | tol = param.float(0.3, title = "Retest tolerance (ATR)", min = 0.05, max = 2, step = 0.05, group = "Entry") |
| 21 | expiry = param.int(30, title = "Setup expiry (bars)", min = 2, max = 500, group = "Entry") |
| 22 | (v, s, _) = stat.linreg(close, len, 0) |
| 23 | sd = stat.stdev(close - v, len) |
| 24 | q = stat.r_squared(close, len) |
| 25 | a = ta.atr(14) |
| 26 | up_edge = v + k_sd * sd |
| 27 | dn_edge = v - k_sd * sd |
| 28 | persist long_lvl = na |
| 29 | persist long_h = na |
| 30 | persist long_age = 0 |
| 31 | persist short_lvl = na |
| 32 | persist short_h = na |
| 33 | persist short_age = 0 |
| 34 | if q[1] > q_min and s[1] < 0 and close > up_edge[1] and close[1] <= up_edge[2] { |
| 35 | long_lvl := up_edge[1] |
| 36 | long_h := 2 * k_sd * sd[1] |
| 37 | long_age := 0 |
| 38 | } |
| 39 | if q[1] > q_min and s[1] > 0 and close < dn_edge[1] and close[1] >= dn_edge[2] { |
| 40 | short_lvl := dn_edge[1] |
| 41 | short_h := 2 * k_sd * sd[1] |
| 42 | short_age := 0 |
| 43 | } |
| 44 | long_age += 1 |
| 45 | short_age += 1 |
| 46 | if long_age > expiry { long_lvl := na } |
| 47 | if short_age > expiry { short_lvl := na } |
| 48 | flat = position.size == 0 |
| 49 | long_sig = not math.is_na(long_lvl) and long_age > 1 and low <= long_lvl + tol * a and close > long_lvl |
| 50 | short_sig = not math.is_na(short_lvl) and short_age > 1 and high >= short_lvl - tol * a and close < short_lvl |
| 51 | if flat and long_sig { |
| 52 | order.market("L", side.long) |
| 53 | exit.bracket("L exit", entry = "L", stop = low - 0.5 * a, target = long_lvl + long_h) |
| 54 | long_lvl := na |
| 55 | } |
| 56 | if flat and short_sig { |
| 57 | order.market("S", side.short) |
| 58 | exit.bracket("S exit", entry = "S", stop = high + 0.5 * a, target = short_lvl - short_h) |
| 59 | short_lvl := na |
| 60 | } |
| 61 | risk.max_drawdown(25) |
| 62 | pu = render.line(up_edge, "Upper edge", color = color.translucent(color.rose.300, 45), width = 1) |
| 63 | pc = render.line(v, "Regression", color = q > q_min ? (s > 0 ? color.sky.300 : color.amber.300) : color.slate.500, width = 1.5) |
| 64 | pd = render.line(dn_edge, "Lower edge", color = color.translucent(color.emerald.300, 45), width = 1) |
| 65 | render.band(pu, pd, color = color.indigo.500, opacity = 0.05, title = "Channel") |
| 66 | render.step(long_lvl, "Long retest level", color = color.emerald.300, width = 2) |
| 67 | render.step(short_lvl, "Short retest level", color = color.rose.300, width = 2) |
| 68 | render.mark(long_sig and flat, shape = shape.tag_up, at = anchor.below_bar, color = color.emerald.400, text = "Retest", title = "Retest buy") |
| 69 | render.mark(short_sig and flat, shape = shape.tag_down, at = anchor.above_bar, color = color.rose.400, text = "Retest", title = "Retest sell") |
| 70 |
Reviews
No reviews yet. Be the first to review this listing.