Pine Script v5 · Strategy
Trend Following based on Trend Confidence
A Pine Script v5 strategy built on Linreg (close, 30). It has 6 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss. Each trade commits 100% of equity and pays 0.1 commission. 11 settings; defaults include Source close, Length 30 and Long entry threshold 0.25.
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 Trend Following based on Trend Confidence repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 3 of 3 trades.
- How did Trend Following based on Trend Confidence do on BTCUSDT 1D with default settings?
- −1.77%
- From 26 Jan 2024 to 21 Oct 2026, over 7 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Trend Following based on Trend Confidence 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: 7 before, 7 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 Trend Following based on Trend Confidence use a stop loss?
- Yes
- strategy.exit sets a stop on line 49, 54.
- Does Trend Following based on Trend Confidence run in Zeal Terminal?
- Yes
- It compiles in 28 ms and runs 1,000 bars in 77 ms, in the browser.
BTCUSDT · 1D · default settings
- Trend Confidence
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −1.77%
- −$17.67
- Buy and hold
- +102.5%
- same period
- Closed trades
- 7
- 2 won · 5 lost
- Profitable
- 28.6%
- of closed trades
- Profit factor
- 0.91
- gross profit ÷ gross loss
- Max drawdown
- 20.66%
- −$240.21
One run with the author's default settings, $1,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
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
1 setting has no label
In the settings dialog source (line 14) shows an empty name. The labels on this page come from the variable names.
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 (no label in the script) | close | source | linreg linreg_p deviationSum slope_perc deviation_perc |
| Start trading interval | timestamp('2000-01-01') | time | in_interval |
| End trading interval | timestamp('2030-01-01') | time | in_interval |
| Length | 30 | int | linreg linreg_p deviation line 37 |
| Longs | true | bool | entry TC Long Entry exit TC Long Exit close TC Long Entry line 47 line 50 |
| Shorts | true | bool | entry TC Short Entry exit TC Short Exit close TC Short Entry line 52 line 55 |
| Long entry threshold | 0.25 | float · step 0.01 | entry TC Long Entry exit TC Long Exit line 47 plot |
| Long exit threshold | -0.10 | float · step 0.01 | close TC Long Entry line 50 plot |
| Short entry threshold | -0.25 | float · step 0.01 | entry TC Short Entry exit TC Short Exit line 52 plot |
| Short exit threshold | -0.05 | float · step 0.01 | close TC Short Entry line 55 plot |
| Stop loss (percentage) | 10 | float · step 1 | sl_long sl_short |
What it calculates
- LinregSource (close), Length (30), 0
linreg · line 31 - LinregSource (close), Length (30), 0+1
linreg_p · line 32
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 TC Long Entry long | in_interval and longs_on and ta.crossover(trend_confidence, long_entry) | On | 48 |
| Exit TC Long Exit stop | in_interval and longs_on and ta.crossover(trend_confidence, long_entry) | On | 49 |
| Close TC Long Entry | in_interval and longs_on and ta.crossunder(trend_confidence, long_exit) | On | 51 |
| Open TC Short Entry short | in_interval and shorts_on and ta.crossunder(trend_confidence, short_entry) | On | 53 |
| Exit TC Short Exit stop | in_interval and shorts_on and ta.crossunder(trend_confidence, short_entry) | On | 54 |
| Close TC Short Entry | in_interval and shorts_on and ta.crossover(trend_confidence, short_exit) | On | 56 |
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 | −1.8% | +102.5% | 7 | 29% | 0.91 | 20.7% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −2.7% | +5.5% | 10 | 40% | 0.70 | 8.5% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +41.0% | +9.8% | 8 | 50% | 2.09 | 21.9% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +8.0% | +8.4% | 8 | 50% | 2.68 | 3.3% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −38.5% | +16.7% | 11 | 36% | 0.52 | 46.6% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −7.6% | +21.5% | 9 | 33% | 0.69 | 22.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +3.2% | +148.3% | 7 | 29% | 1.07 | 28.3% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −15.1% | +17.1% | 8 | 13% | 0.06 | 16.3% | 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 | 1 | 100% | $43.51 |
| Sideways market | 4 | 25% | $61.13 |
| Falling market | 2 | 0% | −$122.32 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
17.3%
The 95th percentile of the worst drawdown when the same 7 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 8.8% to 17.3%; the tick is what actually happened, 15.5%. Order luck alone moves it this much.
All 7 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 1 Jul 2024 62,772.00 | 19 Jul 2024 63,988.00 | 18 | −$21.37 |
| long | 20 Nov 2024 92,310.90 | 14 Feb 2025 96,608.10 | 86 | $43.51 |
| long | 4 May 2025 95,856.50 | 2 Sep 2025 109,237.00 | 121 | $140.36 |
| short | 21 Nov 2025 86,637.20 | 12 Dec 2025 92,513.40 | 21 | −$81.17 |
| short | 10 Feb 2026 70,137.90 | 5 Mar 2026 72,666.80 | 23 | −$41.15 |
| long | 25 Apr 2026 77,437.20 | 30 May 2026 73,460.70 | 35 | −$55.39 |
| short | 8 Jun 2026 63,332.00 | 8 Jul 2026 63,364.10 | 30 | −$2.47 |
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 |
|---|---|---|---|---|---|
| Trend Following based on Trend Confidence this page | — | v5 strategy | −1.8% | 7 | 20.7% |
| Linear trend by TradingAmmo | linreg | 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% |
| 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% |
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.
- 2 settings use the untyped input() (line 14, 17). 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 7 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 14: input() → input.source()
- Line 17: input() → input.int()
⋯// Inputs-source = input(close)+source = input.source(close)since = input.time(timestamp('2000-01-01'), title='Start trading interval')till = input.time(timestamp('2030-01-01'), title='End trading interval')-length = input(30, title='Length')+length = input.int(30, title='Length')longs_on = input.bool(true, title='Longs')⋯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: 7 before, 7 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.
⋯// ################################################################################################-//@version=5+//@version=6strategy("Trend Following based on Trend Confidence", overlay=false, pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=100, margin_long=1./2*50, margin_short=1./2*50, commission_value=0.1, slippage=5, initial_capital=1000)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 28 ms | With a workaround 1,000 bars in 77 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“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/// © carefulCamel61097// ################################################################################################// "This is a trend following strategy that performed very well on the past 5 years"// "Intended to be used on BTC-USDT, 4hr timeframe"// "A factor 2 Leverage can be added by changing Order Size to 200% of equity"// "Higher leverage is not recommended due to big drawdowns"// "Also seems to work on 1D timeframe, although ideal parameters may be different"// "Also seems to work on ETH-USDT and some other altcoins, although ideal parameters are different"// ################################################################################################//@version=5strategy("Trend Following based on Trend Confidence", overlay=false, pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=100, margin_long=1./2*50, margin_short=1./2*50, commission_value=0.1, slippage=5, initial_capital=1000)// Inputssource = input(close)since = input.time(timestamp('2000-01-01'), title='Start trading interval')till = input.time(timestamp('2030-01-01'), title='End trading interval')length = input(30, title='Length')longs_on = input.bool(true, title='Longs')shorts_on = input.bool(true, title='Shorts')// Parameters for best performance 2018 - 2022// long_entry = input.float(0.26, step=0.01, title='Long entry threshold')// long_exit = input.float(-0.10, step=0.01, title='Long exit threshold')// short_entry = input.float(-0.24, step=0.01, title='Short entry threshold')// short_exit = input.float(-0.04, step=0.01, title='Short exit threshold')long_entry = input.float(0.25, step=0.01, title='Long entry threshold')long_exit = input.float(-0.10, step=0.01, title='Long exit threshold')short_entry = input.float(-0.25, step=0.01, title='Short entry threshold')short_exit = input.float(-0.05, step=0.01, title='Short exit threshold')stop_loss = input.float(10, step=1, title='Stop loss (percentage)') / 100// Trend Confidencelinreg = ta.linreg(source, length, 0)linreg_p = ta.linreg(source, length, 0+1)x = bar_indexslope = linreg - linreg_pintercept = linreg - x*slopedeviationSum = 0.0for i = 0 to length-1 deviationSum := deviationSum + math.pow(source[i]-(slope*(x-i)+intercept), 2)deviation = math.sqrt(deviationSum/(length))slope_perc = slope / source[0]deviation_perc = deviation / source[0]trend_confidence = slope_perc / deviation_perc// Strategyin_interval = (time > since) and (time < till)sl_long = strategy.position_avg_price * (1 - stop_loss)sl_short = strategy.position_avg_price * (1 + stop_loss)if in_interval and longs_on and ta.crossover(trend_confidence, long_entry) strategy.entry("TC Long Entry", strategy.long) strategy.exit("TC Long Exit", stop=sl_long)if in_interval and longs_on and ta.crossunder(trend_confidence, long_exit) strategy.close("TC Long Entry")if in_interval and shorts_on and ta.crossunder(trend_confidence, short_entry) strategy.entry("TC Short Entry", strategy.short) strategy.exit("TC Short Exit", stop=sl_short)if in_interval and shorts_on and ta.crossover(trend_confidence, short_exit) strategy.close("TC Short Entry")// Plots plot(trend_confidence, "Trend Confidence", color.rgb(255, 255, 255))plot(long_entry, "", color.rgb(0, 255, 0), linewidth=1)plot(long_exit, "", color.rgb(255, 0, 0), linewidth=1)plot(short_entry, "", color=bar_index % 10 == 0 ? color.rgb(0, 255, 0) : #00000000, linewidth=1)plot(short_exit, "", color=bar_index % 10 == 0 ? color.rgb(255, 0, 0) : #00000000, linewidth=1)