Appearance
Personalize your experience
Auto Channel Reversion
Admin's ChoiceFreeTrades the edges of the best-fitting regression channel while it is clean
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
Trades the edges of the regression channel that fits price best right now. On every bar twelve windows (geometric ladder) are fitted and the one with the highest R squared is the channel; trades are only taken while that fit is clean, so the edges mean something. Entry: in a rising channel, the low pierces the lower k-sigma edge and the bar closes back inside (mirror for falling channels). Exit: target on the centre line, stop beyond the (k + 1)-sigma edge, time stop, and an exit when the channel's slope flips. Risk: 1 % of equity per trade.
Settings
Shortest window: 30
Longest window: 400
Minimum R squared: 0.6
Entry edge (sigma): 2
Maximum bars held: 50
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
- ▸ in a rising channel, the low pierces the lower k-sigma edge and the bar closes back inside (mirror for falling channels)
Exit Conditions
- ▸ target on the centre line, stop beyond the (k + 1)-sigma edge, time stop, and an exit when the channel's slope flips
Source Code
Edit in IDE| 1 | strategy "Auto Channel Reversion" { |
| 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 | /// Trades the edges of the regression channel that fits price best right now. On every bar twelve |
| 11 | /// windows (geometric ladder) are fitted and the one with the highest R squared is the channel; |
| 12 | /// trades are only taken while that fit is clean, so the edges mean something. |
| 13 | /// Entry: in a rising channel, the low pierces the lower k-sigma edge and the bar closes back inside |
| 14 | /// (mirror for falling channels). Exit: target on the centre line, stop beyond the (k + 1)-sigma |
| 15 | /// edge, time stop, and an exit when the channel's slope flips. Risk: 1 % of equity per trade. |
| 16 | use "viz/premium" as pr |
| 17 | l_min = param.int(30, title = "Shortest window", min = 10, max = 500, group = "Channel") |
| 18 | l_max = param.int(400, title = "Longest window", min = 50, max = 3000, group = "Channel") |
| 19 | q_min = param.float(0.6, title = "Minimum R squared", min = 0.1, max = 0.95, step = 0.01, group = "Channel") |
| 20 | k_in = param.float(2.0, title = "Entry edge (sigma)", min = 0.5, max = 4, step = 0.1, group = "Entry") |
| 21 | hold = param.int(50, title = "Maximum bars held", min = 2, max = 1000, group = "Exit") |
| 22 | ratio = math.pow(l_max / l_min, 1 / 11) |
| 23 | br = 0.0 |
| 24 | bv = na |
| 25 | bs = na |
| 26 | bsd = na |
| 27 | for k in 0..=11 { |
| 28 | len = math.round(l_min * math.pow(ratio, k)) |
| 29 | (v, s, _) = stat.linreg(close, len, 0) |
| 30 | r2 = stat.r_squared(close, len) |
| 31 | sd = stat.stdev(close - v, len) |
| 32 | if r2 > br { |
| 33 | br := r2 |
| 34 | bv := v |
| 35 | bs := s |
| 36 | bsd := sd |
| 37 | } |
| 38 | } |
| 39 | clean = br > q_min |
| 40 | lo = bv - k_in * bsd |
| 41 | hi = bv + k_in * bsd |
| 42 | flat = position.size == 0 |
| 43 | long_sig = clean and bs > 0 and low <= lo and close > lo |
| 44 | short_sig = clean and bs < 0 and high >= hi and close < hi |
| 45 | if flat and long_sig { |
| 46 | order.market("L", side.long) |
| 47 | exit.bracket("L exit", entry = "L", stop = bv - (k_in + 1) * bsd, target = bv) |
| 48 | } |
| 49 | if flat and short_sig { |
| 50 | order.market("S", side.short) |
| 51 | exit.bracket("S exit", entry = "S", stop = bv + (k_in + 1) * bsd, target = bv) |
| 52 | } |
| 53 | flip = (position.size > 0 and bs < 0) or (position.size < 0 and bs > 0) |
| 54 | if position.size != 0 and (position.bars_held >= hold or flip) { exit.close_all() } |
| 55 | risk.max_drawdown(25) |
| 56 | pu = render.line(hi, "Upper edge", color = color.translucent(color.amber.300, 40), width = 1) |
| 57 | pc = render.line(bv, "Channel centre", color = clean ? (bs > 0 ? color.sky.300 : color.amber.300) : color.slate.500, width = 2) |
| 58 | pl = render.line(lo, "Lower edge", color = color.translucent(color.sky.300, 40), width = 1) |
| 59 | render.band(pu, pl, color = clean ? (bs > 0 ? color.sky.500 : color.amber.500) : color.slate.600, opacity = 0.07, title = "Channel") |
| 60 | render.mark(long_sig and flat, shape = shape.triangle_up, at = anchor.below_bar, color = color.sky.300, size = 0.7, title = "Edge buy") |
| 61 | render.mark(short_sig and flat, shape = shape.triangle_down, at = anchor.above_bar, color = color.amber.300, size = 0.7, title = "Edge sell") |
| 62 | persist t = draw.table.create(anchor.top_right, 4, 2) |
| 63 | if bar.is_last { |
| 64 | pr.heading(t, 2, "Auto Channel Reversion", "Strategy · best-fit channel edges") |
| 65 | pr.row(t, 2, "R squared", str.format("{:.2f}", br), clean ? pr.BULL : pr.LABEL) |
| 66 | pr.row(t, 3, "Channel", clean ? (bs > 0 ? "Clean rising" : "Clean falling") : "Noisy: no trades", color.white) |
| 67 | } |
| 68 |
Reviews
No reviews yet. Be the first to review this listing.