Appearance

Personalize your experience

Mode
Accent Color
Direction
MarketplacestrategiesAuto Channel Reversion
QuaScript

Auto Channel Reversion

Admin's ChoiceFree

Trades the edges of the best-fitting regression channel while it is clean

0.0(0)
0 installs 0 0 comments
Q
433 listings

The QuaTick team's QuaScript analyses, run by the native C++ engine.

View Profile
quascriptstrategyregressionchannelmean-reversion
Free
Backtest this strategy Open in QuaCode Edit in IDE
Version
Updated
Compatible

Live engine preview

QuaScriptQuaScript

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.

Auto Channel Reversionengine result
Loading the engine result…
quatick-charts · QuaScript engine on synthetic 1-minute bars · drag to pan · scroll to zoom · Run executes the script on the engine (⌘/Ctrl+Enter while editing)

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
quascript
1strategy "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.
16use "viz/premium" as pr
17l_min = param.int(30, title = "Shortest window", min = 10, max = 500, group = "Channel")
18l_max = param.int(400, title = "Longest window", min = 50, max = 3000, group = "Channel")
19q_min = param.float(0.6, title = "Minimum R squared", min = 0.1, max = 0.95, step = 0.01, group = "Channel")
20k_in = param.float(2.0, title = "Entry edge (sigma)", min = 0.5, max = 4, step = 0.1, group = "Entry")
21hold = param.int(50, title = "Maximum bars held", min = 2, max = 1000, group = "Exit")
22ratio = math.pow(l_max / l_min, 1 / 11)
23br = 0.0
24bv = na
25bs = na
26bsd = na
27for 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}
39clean = br > q_min
40lo = bv - k_in * bsd
41hi = bv + k_in * bsd
42flat = position.size == 0
43long_sig = clean and bs > 0 and low <= lo and close > lo
44short_sig = clean and bs < 0 and high >= hi and close < hi
45if 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}
49if 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}
53flip = (position.size > 0 and bs < 0) or (position.size < 0 and bs > 0)
54if position.size != 0 and (position.bars_held >= hold or flip) { exit.close_all() }
55risk.max_drawdown(25)
56pu = render.line(hi, "Upper edge", color = color.translucent(color.amber.300, 40), width = 1)
57pc = render.line(bv, "Channel centre", color = clean ? (bs > 0 ? color.sky.300 : color.amber.300) : color.slate.500, width = 2)
58pl = render.line(lo, "Lower edge", color = color.translucent(color.sky.300, 40), width = 1)
59render.band(pu, pl, color = clean ? (bs > 0 ? color.sky.500 : color.amber.500) : color.slate.600, opacity = 0.07, title = "Channel")
60render.mark(long_sig and flat, shape = shape.triangle_up, at = anchor.below_bar, color = color.sky.300, size = 0.7, title = "Edge buy")
61render.mark(short_sig and flat, shape = shape.triangle_down, at = anchor.above_bar, color = color.amber.300, size = 0.7, title = "Edge sell")
62persist t = draw.table.create(anchor.top_right, 4, 2)
63if 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 
Read-only preview. Open in QuaCode to edit and deploy.

Reviews

No reviews yet. Be the first to review this listing.

Discussion

Sign in to join the discussion. Your posts and replies show up on your profile.