Pine Script v4 · Strategy
Linear trend
A Pine Script v4 strategy built on Linreg (src, 13) and Crossunder (lr, 0). It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 100% of equity and pays 0.075% commission. 11 settings; defaults include Start Year 2017, Month 12 and Day 17.
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 trend repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 2 of 2 trades.
- How did Linear trend do on BTCUSDT 1D with default settings?
- +3.28%
- From 26 Jan 2024 to 21 Oct 2026, over 13 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Linear trend be converted to Pine Script v5?
- No
- The automatic step from v4 to v5 changes the results, so no v5 download is offered; the edits are shown below.
- Does Linear trend use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Linear trend run in Zeal Terminal?
- Yes
- It compiles in 10 ms and runs 1,000 bars in 37 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +3.28%
- $3,278.87
- Buy and hold
- +102.5%
- same period
- Closed trades
- 13
- 4 won · 9 lost
- Profitable
- 30.8%
- of closed trades
- Profit factor
- 1.09
- gross profit ÷ gross loss
- Max drawdown
- 16.84%
- −$18,371.50
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. 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 stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses an argument v6 removed
when = on line 21, 23. The upgraded version below moves each call inside an if block.
-
Note
Uses iff(), which v5 removed line 8
iff() on line 8. v5 writes it as the ternary c ? a : b.
Upgrade and export
Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.
- Upgrading to v5 renames linreg → ta.linreg, crossover → ta.crossover, ema → ta.ema, crossunder → ta.crossunder.
- 2 strategy calls use the when = argument (line 21, 23). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
DiffersTrades differ: 13 before, 13 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 8: iff(c, a, b) → (c ? a : b)
- Line 15: linreg → ta.linreg
- Line 16: linreg → ta.linreg
- Line 20: crossover → ta.crossover
- Line 20: ema → ta.ema
- Line 22: crossunder → ta.crossunder
⋯// © TradingAmmo-//@version=4+//@version=5strategy("Linear trend", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.075, currency='USD')⋯_testPeriod() =>- iff(time >= startP and time <= end, true, false)+ (time >= startP and time <= end ? true : false)src = close⋯threshold = input(0,step=0.5, title="Threshold")-fast_lr = linreg(src, len1, lag1)+fast_lr = ta.linreg(src, len1, lag1)-slow_lr = linreg(src, len2, lag2)+slow_lr = ta.linreg(src, len2, lag2)lr = fast_lr - slow_lr⋯plot(threshold, color=color.purple)-long_condition = crossover(lr, threshold) and close > ema(close, 200) and _testPeriod()+long_condition = ta.crossover(lr, threshold) and close > ta.ema(close, 200) and _testPeriod()strategy.entry('BUY', strategy.long, when=long_condition) -short_condition = crossunder(lr, threshold) +short_condition = ta.crossunder(lr, threshold) strategy.close('BUY', when=short_condition)Clean v5
VerifiedSame 13 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 5: input() → input.int()
- Line 5: input() → input.int()
- Line 5: input() → input.int()
- Line 6: input() → input.int()
- Line 6: input() → input.int()
- Line 6: input() → input.int()
7 more edits
- Line 10: input() → input.int()
- Line 11: input() → input.int()
- Line 12: input() → input.int()
- Line 13: input() → input.int()
- Line 14: input() → input.int()
- Line 21: when = long_condition → if block
- Line 23: when = short_condition → if block
⋯strategy("Linear trend", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.075, currency='USD')-startP = timestamp(input(2017, "Start Year"), input(12, "Month"), input(17, "Day"), 0, 0)+startP = timestamp(input.int(2017, "Start Year"), input.int(12, "Month"), input.int(17, "Day"), 0, 0)-end = timestamp(input(9999, "End Year"), input(1, "Month"), input(1, "Day"), 0, 0)+end = timestamp(input.int(9999, "End Year"), input.int(1, "Month"), input.int(1, "Day"), 0, 0)_testPeriod() =>⋯src = close-len1 = input(defval=13, minval=1, title="Fast LR")+len1 = input.int(defval=13, minval=1, title="Fast LR")-len2 = input(defval=55, minval=1, title="Slow LR")+len2 = input.int(defval=55, minval=1, title="Slow LR")-lag1 = input(0, title="Lag for fast")+lag1 = input.int(0, title="Lag for fast")-lag2 = input(0, title="Lag for slow")+lag2 = input.int(0, title="Lag for slow")-threshold = input(0,step=0.5, title="Threshold")+threshold = input.int(0,step=0.5, title="Threshold")fast_lr = ta.linreg(src, len1, lag1)⋯long_condition = ta.crossover(lr, threshold) and close > ta.ema(close, 200) and _testPeriod()-strategy.entry('BUY', strategy.long, when=long_condition) +if long_condition+ strategy.entry('BUY', strategy.long) short_condition = ta.crossunder(lr, threshold) -strategy.close('BUY', when=short_condition)+if short_condition+ strategy.close('BUY')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: 13 before, 0 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.
⋯// © TradingAmmo-//@version=5+//@version=6strategy("Linear trend", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.075, currency='USD')⋯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 |
|---|---|---|---|
| Start Year | 2017 | int | line 8 |
| Month | 12 | int | line 8 |
| Day | 17 | int | line 8 |
| End Year | 9999 | int | line 8 |
| Month | 1 | int | line 8 |
| Day | 1 | int | line 8 |
| Fast LR | 13 | int · min 1 | fast_lr |
| Slow LR | 55 | int · min 1 | slow_lr |
| Lag for fast | 0 | int | fast_lr |
| Lag for slow | 0 | int | slow_lr |
| Threshold | 0 | int · step 0.5 | long_condition short_condition plot |
What it calculates
- Linreg (
linreg,ta.linregin v5)src, Fast LR (13), Lag for fast (0)fast_lr · line 15 - Linreg (
linreg,ta.linregin v5)src, Slow LR (55), Lag for slow (0)slow_lr · line 16 - Crossunder (
crossunder,ta.crossunderin v5)lr, Threshold (0)short_condition · line 22
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 BUY long | long_conditioncrossover(lr, threshold) and close > ema(close, 200) and _testPeriod() | On | 21 |
| Close BUY | short_condition | On | 23 |
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 | +3.3% | +102.5% | 13 | 31% | 1.09 | 16.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −4.6% | +5.5% | 13 | 31% | 0.71 | 12.2% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +5.6% | +9.8% | 7 | 43% | 1.08 | 40.8% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +15.7% | +8.4% | 16 | 25% | 1.65 | 12.9% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −30.4% | +16.7% | 11 | 36% | 0.57 | 42.4% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +5.3% | +21.5% | 14 | 29% | 1.19 | 13.8% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.5% | +148.3% | 13 | 38% | 1.01 | 21.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +21.9% | +17.1% | 11 | 45% | 2.54 | 5.7% | 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 | 40% | $23,769.30 |
| Sideways market | 8 | 25% | −$20,490.50 |
| Falling market | 0 | — | $0.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
28.6%
The 95th percentile of the worst drawdown when the same 13 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 14.0% to 28.6%; the tick is what actually happened, 13.7%. Order luck alone moves it this much.
All 13 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 24 Aug 2024 64,037.20 | 2 Sep 2024 57,301.80 | 9 | −$10,652.20 |
| long | 15 Oct 2024 66,084.00 | 27 Oct 2024 67,092.80 | 12 | $1,227.91 |
| long | 30 Oct 2024 72,736.40 | 6 Nov 2024 69,372.00 | 7 | −$4,319.03 |
| long | 7 Nov 2024 75,572.00 | 1 Dec 2024 96,408.00 | 24 | $23,617.00 |
| long | 18 Jan 2025 104,077.00 | 3 Feb 2025 97,700.60 | 16 | −$6,886.61 |
| long | 22 May 2025 109,644.00 | 26 May 2025 109,004.00 | 4 | −$754.41 |
| long | 30 Jun 2025 108,357.00 | 27 Jul 2025 117,920.00 | 27 | $8,855.80 |
| long | 9 Sep 2025 112,065.00 | 26 Sep 2025 108,994.00 | 17 | −$3,205.92 |
| long | 2 Oct 2025 118,595.00 | 13 Oct 2025 114,959.00 | 11 | −$3,464.48 |
| long | 29 Oct 2025 112,898.00 | 1 Nov 2025 109,608.00 | 3 | −$3,195.22 |
| long | 3 Nov 2025 110,541.00 | 4 Nov 2025 106,583.00 | 1 | −$3,770.33 |
| long | 21 Aug 2026 73,027.00 | 10 Sep 2026 78,306.40 | 20 | $6,888.77 |
| long | 24 Sep 2026 84,397.60 | 30 Sep 2026 83,663.70 | 6 | −$1,062.41 |
Similar scripts
Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Linear trend this page | — | v4 strategy | +3.3% | 13 | 16.8% |
| Linear Channel - Scalp Strategy 15M by TradingAmmo | linreg | v4 strategy | +2.5% | 7 | 30.1% |
| Crypto Squeeze Strategy by Bunghole | linreg | v4 strategy | −43.8% | 15 | 64.0% |
| Trend Following based on Trend Confidence by carefulCamel61097 | linreg | v5 strategy | −1.8% | 7 | 20.7% |
| Strategy Myth-Busting #9 - HullSuite+LSMA - [MYN] by myncrypto | linreg | v5 strategy | −38.3% | 13 | 72.6% |
| Strategy Myth-Busting #4 - LSMA+HULL Crossover - [MYN] by myncrypto | linreg | v5 strategy | +22.2% | 13 | 74.6% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 10 ms | Yes 1,000 bars in 37 ms | Not measured |
| PineTS | No v5 and v6 only | — | 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. PineTS runs Pine Script v5 and v6 only.
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/// © TradingAmmo//@version=4strategy("Linear trend", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.075, currency='USD')startP = timestamp(input(2017, "Start Year"), input(12, "Month"), input(17, "Day"), 0, 0)end = timestamp(input(9999, "End Year"), input(1, "Month"), input(1, "Day"), 0, 0)_testPeriod() => iff(time >= startP and time <= end, true, false)src = closelen1 = input(defval=13, minval=1, title="Fast LR")len2 = input(defval=55, minval=1, title="Slow LR")lag1 = input(0, title="Lag for fast")lag2 = input(0, title="Lag for slow")threshold = input(0,step=0.5, title="Threshold")fast_lr = linreg(src, len1, lag1)slow_lr = linreg(src, len2, lag2)lr = fast_lr - slow_lrplot_fast = plot(lr, color = lr > 0 ? color.green : color.red)plot(threshold, color=color.purple)long_condition = crossover(lr, threshold) and close > ema(close, 200) and _testPeriod()strategy.entry('BUY', strategy.long, when=long_condition) short_condition = crossunder(lr, threshold) strategy.close('BUY', when=short_condition)