Pine Script v5 · Strategy
Linear Regression Channel Breakout Strategy
A Pine Script v5 strategy with no technical-analysis calculation. It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade is one contract and pays no commission. 14 settings; defaults include Source close, Length 20 and Deviation 2.3.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does Linear Regression Channel Breakout Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 4 of 4 trades.
- How did Linear Regression Channel Breakout Strategy do on BTCUSDT 1D with default settings?
- +4.83%
- From 26 Jan 2024 to 21 Oct 2026, over 14 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Linear Regression Channel Breakout Strategy run in Pine Script v6?
- No
- The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 14 before, 20 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
- Does Linear Regression Channel Breakout Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 77, 78.
- Does Linear Regression Channel Breakout Strategy run in Zeal Terminal?
- Yes
- It compiles in 47 ms and runs 1,000 bars in 125 ms, in the browser.
BTCUSDT · 1D · default settings
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +4.83%
- $48,307.10
- Buy and hold
- +102.5%
- same period
- Closed trades
- 14
- 8 won · 6 lost
- Profitable
- 57.1%
- of closed trades
- Profit factor
- 2.22
- gross profit ÷ gross loss
- Max drawdown
- 2.12%
- −$21,892.10
One run with the author's default settings, $1,000,000 starting capital, commission as the script sets it. A long position was still open at the last bar. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Source | close | source | mid slope dev y1_ y2_ |
| Length | 20 | int · min 10 | mid slope intercept endy dev y1_ y2_ sidelab line 34 line 51 line 55 line 61 |
| Deviation | 2.3 | float · min 0.1 · step 0.1 | outofchannel longCondition shortCondition line 51 line 55 line 62 |
| Extend Lines | true | bool | line 51 line 55 |
| Show Fibonacci Levels | false | bool | line 52 |
| Show Broken Channel | true | bool | line 44 |
| Brokencol (no label in the script) | color.blue | color | line 47 |
| Up/Down Trend Colors | color.lime | color | line 22 line 63 |
| Dncol (no label in the script) | color.red | color | line 23 line 63 |
| Line Width | 2 | int | line 51 line 55 |
| Stop Loss / Take Profit: | |||
| SL | false | bool | exit Exit Long exit Exit Short |
| , % | 18.0 | float · min 0.0 · step 0.1 | stoploss |
| TP | false | bool | exit Exit Long exit Exit Short |
| , % | 18.0 | float · min 0.0 · step 0.1 | takeprofit |
What it calculates
No technical-analysis calculation: the script works on prices and settings directly.
Signal flow
How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Long long | longCondition | On | 67 |
| Open Short short | shortCondition | On | 70 |
| Exit Exit Long stop and limit | every bar | On | 77 |
| Exit Exit Short stop and limit | every bar | On | 78 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
No
No repainting patterns in the source, and the live replay matched history.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
- ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | +4.8% | +102.5% | 14 | 57% | 2.22 | 2.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +1.3% | +5.5% | 14 | 43% | 1.74 | 1.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.6% | +9.8% | 18 | 56% | 3.57 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.1% | +8.4% | 16 | 38% | 1.80 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 17 | 53% | 1.12 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 14 | 43% | 3.22 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.1% | +148.3% | 19 | 26% | 0.35 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.0% | +17.1% | 20 | 25% | 0.42 | 0.0% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 5 | 80% | $24,056.80 |
| Sideways market | 6 | 50% | $19,827.40 |
| Falling market | 3 | 33% | $4,422.91 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
3.1%
The 95th percentile of the worst drawdown when the same 14 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 1.4% to 3.1%; the tick is what actually happened, 1.7%. Order luck alone moves it this much.
All 14 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 29 Feb 2024 62,432.10 | 17 Mar 2024 65,300.60 | 17 | $2,868.53 |
| short | 17 Mar 2024 65,300.60 | 16 Jul 2024 64,724.10 | 121 | $576.58 |
| long | 16 Jul 2024 64,724.10 | 3 Aug 2024 61,498.30 | 18 | −$3,225.72 |
| short | 3 Aug 2024 61,498.30 | 7 Nov 2024 75,572.00 | 96 | −$14,073.70 |
| long | 7 Nov 2024 75,572.00 | 20 Dec 2024 97,461.90 | 43 | $21,889.90 |
| short | 20 Dec 2024 97,461.90 | 23 Apr 2025 93,443.00 | 124 | $4,018.87 |
| long | 23 Apr 2025 93,443.00 | 2 Aug 2025 113,298.00 | 101 | $19,854.90 |
| short | 2 Aug 2025 113,298.00 | 2 Oct 2025 118,595.00 | 61 | −$5,297.07 |
| long | 2 Oct 2025 118,595.00 | 11 Oct 2025 112,774.00 | 9 | −$5,820.50 |
| short | 11 Oct 2025 112,774.00 | 6 Jan 2026 93,859.70 | 87 | $18,914.80 |
| long | 6 Jan 2026 93,859.70 | 21 Jan 2026 88,427.70 | 15 | −$5,432.05 |
| short | 21 Jan 2026 88,427.70 | 5 Mar 2026 72,666.80 | 43 | $15,760.90 |
| long | 5 Mar 2026 72,666.80 | 3 Jun 2026 66,760.80 | 90 | −$5,905.93 |
| short | 3 Jun 2026 66,760.80 | 4 Jul 2026 62,583.30 | 31 | $4,177.58 |
Similar scripts
No other indicator or strategy here shares enough of its calculations.
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- 4 settings use the untyped input() (line 5, 8, 9, 14). It still compiles in v6; input.int, input.float or input.source state the type.
- No v6-only features. v6 is not needed to run this script.
Same 14 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 5: input() → input.source()
- Line 8: input() → input.bool()
- Line 9: input() → input.bool()
- Line 14: input() → input.int()
⋯strategy('Linear Regression Channel Breakout Strategy', overlay=true)-src = input(defval=close, title='Source')+src = input.source(defval=close, title='Source')len = input.int(defval=20, title='Length', minval=10)devlen = input.float(defval=2.3, title='Deviation', minval=0.1, step=0.1)-extendit = input(defval=true, title='Extend Lines')+extendit = input.bool(defval=true, title='Extend Lines')-showfibo = input(defval=false, title='Show Fibonacci Levels')+showfibo = input.bool(defval=false, title='Show Fibonacci Levels')showbroken = input.bool(defval=true, title='Show Broken Channel', inline='brk')⋯dncol = input.color(defval=color.red, title='', inline='trcols')-widt = input(defval=2, title='Line Width')+widt = input.int(defval=2, title='Line Width')issl = input.bool(title='SL', inline='linesl1', group='Stop Loss / Take Profit:', defval=false)⋯Does it run in Pine Script v6?
No
The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 14 before, 20 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © LonesomeTheBlue © chanu_lev10k-//@version=5+//@version=6strategy('Linear Regression Channel Breakout Strategy', overlay=true)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 47 ms | Yes 1,000 bars in 125 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © LonesomeTheBlue © chanu_lev10k//@version=5strategy('Linear Regression Channel Breakout Strategy', overlay=true)src = input(defval=close, title='Source')len = input.int(defval=20, title='Length', minval=10)devlen = input.float(defval=2.3, title='Deviation', minval=0.1, step=0.1)extendit = input(defval=true, title='Extend Lines')showfibo = input(defval=false, title='Show Fibonacci Levels')showbroken = input.bool(defval=true, title='Show Broken Channel', inline='brk')brokencol = input.color(defval=color.blue, title='', inline='brk')upcol = input.color(defval=color.lime, title='Up/Down Trend Colors', inline='trcols')dncol = input.color(defval=color.red, title='', inline='trcols')widt = input(defval=2, title='Line Width')issl = input.bool(title='SL', inline='linesl1', group='Stop Loss / Take Profit:', defval=false)slpercent = input.float(title=', %', inline='linesl1', group='Stop Loss / Take Profit:', defval=18.0, minval=0.0, step=0.1)istp = input.bool(title='TP', inline='linetp1', group='Stop Loss / Take Profit:', defval=false)tppercent = input.float(title=', %', inline='linetp1', group='Stop Loss / Take Profit:', defval=18.0, minval=0.0, step=0.1)var fibo_ratios = array.new_float(0)var colors = array.new_color(2)if barstate.isfirst array.unshift(colors, upcol) array.unshift(colors, dncol) array.push(fibo_ratios, 0.236) array.push(fibo_ratios, 0.382) array.push(fibo_ratios, 0.618) array.push(fibo_ratios, 0.786)get_channel(src, len) => mid = math.sum(src, len) / len slope = ta.linreg(src, len, 0) - ta.linreg(src, len, 1) intercept = mid - slope * math.floor(len / 2) + (1 - len % 2) / 2 * slope endy = intercept + slope * (len - 1) dev = 0.0 for x = 0 to len - 1 by 1 dev += math.pow(src[x] - (slope * (len - x) + intercept), 2) dev dev := math.sqrt(dev / len) [intercept, endy, dev, slope][y1_, y2_, dev, slope] = get_channel(src, len)outofchannel = slope > 0 and close < y2_ - dev * devlen ? 0 : slope < 0 and close > y2_ + dev * devlen ? 2 : -1var reglines = array.new_line(3)var fibolines = array.new_line(4)for x = 0 to 2 by 1 if not showbroken or outofchannel != x or nz(outofchannel[1], -1) != -1 line.delete(array.get(reglines, x)) else line.set_color(array.get(reglines, x), color=brokencol) line.set_width(array.get(reglines, x), width=2) line.set_style(array.get(reglines, x), style=line.style_dotted) line.set_extend(array.get(reglines, x), extend=extend.none) array.set(reglines, x, line.new(x1=bar_index - (len - 1), y1=y1_ + dev * devlen * (x - 1), x2=bar_index, y2=y2_ + dev * devlen * (x - 1), color=array.get(colors, math.round(math.max(math.sign(slope), 0))), style=x % 2 == 1 ? line.style_solid : line.style_dashed, width=widt, extend=extendit ? extend.right : extend.none))if showfibo for x = 0 to 3 by 1 line.delete(array.get(fibolines, x)) array.set(fibolines, x, line.new(x1=bar_index - (len - 1), y1=y1_ - dev * devlen + dev * devlen * 2 * array.get(fibo_ratios, x), x2=bar_index, y2=y2_ - dev * devlen + dev * devlen * 2 * array.get(fibo_ratios, x), color=array.get(colors, math.round(math.max(math.sign(slope), 0))), style=line.style_dotted, width=widt, extend=extendit ? extend.right : extend.none))var label sidelab = label.new(x=bar_index - (len - 1), y=y1_, text='S', size=size.large)txt = slope > 0 ? slope > slope[1] ? '⇑' : '⇗' : slope < 0 ? slope < slope[1] ? '⇓' : '⇘' : '⇒'stl = slope > 0 ? slope > slope[1] ? label.style_label_up : label.style_label_upper_right : slope < 0 ? slope < slope[1] ? label.style_label_down : label.style_label_lower_right : label.style_label_rightlabel.set_style(sidelab, stl)label.set_text(sidelab, txt)label.set_x(sidelab, bar_index - (len - 1))label.set_y(sidelab, slope > 0 ? y1_ - dev * devlen : slope < 0 ? y1_ + dev * devlen : y1_)label.set_color(sidelab, slope > 0 ? upcol : slope < 0 ? dncol : color.blue)// Long Short conditionslongCondition = (slope < 0 and close > y2_ + dev * devlen) or (slope > 0 and close > y2_ + dev * devlen)if longCondition strategy.entry('Long', strategy.long)shortCondition = (slope > 0 and close < y2_ - dev * devlen) or (slope < 0 and close < y2_ - dev * devlen)if shortCondition strategy.entry('Short', strategy.short)// Entry price / Take Profit / Stop Loss//entryprice = strategy.position_avg_priceentryprice = ta.valuewhen(condition=longCondition or shortCondition, source=close, occurrence=0)pm = longCondition ? 1 : shortCondition ? -1 : 1 / math.sign(strategy.position_size)takeprofit = entryprice * (1 + pm * tppercent * 0.01)stoploss = entryprice * (1 - pm * slpercent * 0.01)strategy.exit(id='Exit Long', from_entry='Long', stop=issl ? stoploss : na, limit=istp ? takeprofit : na, alert_message='Exit Long')strategy.exit(id='Exit Short', from_entry='Short', stop=issl ? stoploss : na, limit=istp ? takeprofit : na, alert_message='Exit Short')