Pine Script v5 · Strategy
Classic Long Term Trend Following System
A Pine Script v5 strategy built on EMA (close, 50), Highest (high[1], 50) and Lowest (low[1], 50). 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 is one contract and pays no commission. 19 settings; defaults include Length Fast EMA 50, Length Slow EMA 150 and Source Fast MA close.
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 Classic Long Term Trend Following System repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 1 of 1 trades.
- How did Classic Long Term Trend Following System do on BTCUSDT 1D with default settings?
- +2.92%
- From 26 Jan 2024 to 21 Oct 2026, over 5 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Classic Long Term Trend Following System 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: 5 before, 5 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 Classic Long Term Trend Following System use a stop loss?
- Yes
- strategy.exit sets a stop on line 69, 75.
- Does Classic Long Term Trend Following System run in Zeal Terminal?
- Yes
- It compiles in 23 ms and runs 1,000 bars in 64 ms, in the browser.
BTCUSDT · 1D · default settings
- Fast EMA
- Slow EMA
- Upper
- Lower
- ATR Trailing Stop
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +2.92%
- $29,164.40
- Buy and hold
- +102.5%
- same period
- Closed trades
- 5
- 3 won · 2 lost
- Profitable
- 60.0%
- of closed trades
- Profit factor
- 4.38
- gross profit ÷ gross loss
- Max drawdown
- 1.15%
- −$11,846.90
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
3 settings do nothing
“Backtest Start Date”, “Backtest End Date”, “ Color Lower Channel” are declared but never read.
-
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 |
|---|---|---|---|
| backtest window | |||
| Backtest Start Date | timestamp("01 Jan 2017 00:00 +0000") | time | unused |
| Backtest End Date | timestamp("01 Jan 2100 00:00 +0000") | time | unused |
| Moving Average Inputs | |||
| Length Fast EMA | 50 | int · min 1 | fast |
| Length Slow EMA | 150 | int · min 1 | slow |
| Color Fast EMA | color.new(color.red, 0) | color | Fast EMA |
| Color Slow EMA | color.new(color.blue, 0) | color | Slow EMA |
| Source Fast MA | close | source | fast |
| Source Slow MA | close | source | slow |
| Donchian Channels Inputs | |||
| Length Upper Channel | 50 | int · min 1 | h1 |
| Length Lower Channel | 50 | int · min 1 | l1 |
| Fill Color | color.new(color.purple, 95) | color | fill |
| Color Upper Channel | color.new(color.orange, 0) | color | Upper Lower |
| Color Lower Channel | color.new(color.orange, 0) | color | unused |
| ATR Position Size Inputs | |||
| Leverage Used | 1 | int · min 1 | amount |
| Point Value/ATR Multipliers from Entry | 4 | int · min 1 | amount |
| ATR Period | 14 | int · min 1 | atr |
| Risk Per Trade | 0.01 | float | amount |
| ATR Trailing Stop Inputs | |||
| ATR Period | 14 | int | xATR |
| ATR Multiplier | 4 | float · step .1 | Stop stoploss |
What it calculates
- EMASource Fast MA (close), Length Fast EMA (50)
fast · line 15 - EMASource Slow MA (close), Length Slow EMA (150)
slow · line 16 - Highesthigh[1], Length Upper Channel (50)
h1 · line 22 - Lowestlow[1], Length Lower Channel (50)
l1 · line 23 - ATRATR Period (14)
atr · line 35 - ATRATR Period (14)
xATR · line 40 - Crossoverfast, slow
entrycondition1 · line 61 - Crossunderfast, slow
sellcondition1 · line 63
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 | (close > h1 and entrycondition2) | On | 67 |
| Exit exit sl stop | (close > h1 and entrycondition2) | On | 69 |
| Close long | (sellcondition1 and sellcondition2) | On | 71 |
| Open short short | (close < l1 and sellcondition2) | On | 73 |
| Exit exit sl stop | (close < l1 and sellcondition2) | On | 75 |
| Close short | (entrycondition1 and entrycondition2) | On | 77 |
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 | +2.9% | +102.5% | 5 | 60% | 4.38 | 1.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +1.8% | +5.5% | 6 | 50% | 1.69 | 2.8% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.7% | +9.8% | 5 | 40% | 1.37 | 1.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +1.6% | +8.4% | 5 | 40% | 1.85 | 1.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −2.2% | +16.7% | 8 | 25% | 0.50 | 3.5% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +2.8% | +21.5% | 7 | 43% | 1.98 | 2.3% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +2.0% | +148.3% | 4 | 75% | 9.55 | 0.8% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +4.2% | +17.1% | 5 | 40% | 3.25 | 1.5% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
Settings heatmap
Two settings that feed the entry signal, moved around their defaults. The best in-sample pair (Length Fast EMA 25, Length Slow EMA 150) made +4.1% before 26 Dec 2025 and −0.5% after it, against −0.5% for the defaults. 98% of the 64 combinations made money in-sample and 0% out-of-sample.
| Length Slow EMA ↓ · Length Fast EMA → | 25 | 30 | 35 | 43 | 50 | 60 | 70 | 85 |
|---|---|---|---|---|---|---|---|---|
| 75 | +2% | +3% | +3% | +2% | +3% | +3% | +3% | −3% |
| 90 | +3% | +3% | +3% | +3% | +3% | +3% | +3% | +3% |
| 105 | +3% | +3% | +4% | +3% | +3% | +3% | +3% | +3% |
| 128 | +3% | +4% | +4% | +3% | +3% | +3% | +3% | +3% |
| 150 | +4% | +3% | +3% | +3% | +3% | +3% | +3% | +3% |
| 180 | +3% | +3% | +3% | +3% | +3% | +3% | +3% | +3% |
| 210 | +3% | +3% | +3% | +3% | +3% | +3% | +3% | +3% |
| 255 | +3% | +3% | +3% | +3% | +3% | +3% | +3% | +3% |
| Length Slow EMA ↓ · Length Fast EMA → | 25 | 30 | 35 | 43 | 50 | 60 | 70 | 85 |
|---|---|---|---|---|---|---|---|---|
| 75 | −1% | −1% | −1% | −2% | −1% | −1% | −1% | −0% |
| 90 | −1% | −1% | −2% | −1% | −1% | −1% | −1% | −1% |
| 105 | −1% | −1% | −1% | −1% | −1% | −1% | −1% | −1% |
| 128 | −1% | −1% | −1% | −1% | −1% | −1% | −1% | −1% |
| 150 | −1% | −1% | −1% | −1% | −1% | −1% | −1% | −1% |
| 180 | −1% | −1% | −1% | −1% | −1% | −1% | −1% | −1% |
| 210 | −1% | −1% | −1% | −1% | −1% | −1% | −1% | −1% |
| 255 | −1% | −1% | −1% | −1% | −1% | −1% | −1% | −1% |
Net P&L of trades opened in each period, as a share of starting capital. marks the defaults and the best in-sample cell. Hover a cell for its trade count. 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 | 0% | −$3,573.02 |
| Sideways market | 3 | 67% | $23,845.10 |
| Falling market | 1 | 100% | $8,892.24 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
0.8%
The 95th percentile of the worst drawdown when the same 5 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 0.5% to 0.8%; the tick is what actually happened, 0.5%. Order luck alone moves it this much.
All 5 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 16 Oct 2024 67,074.10 | 13 Jan 2025 90,013.20 | 89 | $26,189.60 |
| long | 2 May 2025 96,489.90 | 22 Jun 2025 99,649.50 | 51 | $2,710.33 |
| long | 11 Jul 2025 116,010.00 | 21 Aug 2025 112,033.00 | 41 | −$3,573.02 |
| short | 15 Nov 2025 94,594.00 | 22 Apr 2026 79,418.50 | 158 | $8,892.24 |
| short | 3 Jun 2026 66,760.80 | 20 Aug 2026 71,177.50 | 78 | −$5,054.76 |
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 |
|---|---|---|---|---|---|
| Classic Long Term Trend Following System this page | — | v5 strategy | +2.9% | 5 | 1.1% |
| Richard's Strategy by Deepak8369 | highest lowest atr ema | v4 strategy | +34.6% | 70 | 26.3% |
| Strategy Myth-Busting #1 - UT Bot+STC+Hull [MYN] by myncrypto | atr ema lowest highest | v5 strategy | −30.7% | 10 | 63.5% |
| Strategy for UT Bot Alerts indicator by StrategiesForEveryone | ema atr highest lowest | v5 strategy | −4.5% | 42 | 15.7% |
| Simple and Profitable Scalping Strategy (ForexSignals TV) by kevinmck100 | ema atr highest lowest | v5 strategy | No trades on this market | ||
| ATR + STC + MA by Romedius | ema lowest highest atr | v5 strategy | −1.2% | 15 | 3.3% |
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.
- 3 settings use the untyped input() (line 11, 12, 34). 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 5 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 11: input() → input.source()
- Line 12: input() → input.source()
- Line 34: input() → input.float()
- Line 59: input() → input.int()
⋯len2 = input.int(150, minval = 1, title = "Length Slow EMA", group = "Moving Average Inputs")-src1 = input(close, title = "Source Fast MA")+src1 = input.source(close, title = "Source Fast MA")-src2 = input(close, title = "Source Slow MA")+src2 = input.source(close, title = "Source Slow MA")maFast = input.color(color.new(color.red, 0), title = "Color Fast EMA", group = "Moving Average Inputs", inline = "maFast")⋯length = input.int(title = "ATR Period", defval = 14, minval = 1, group ="ATR Position Size Inputs")-risk = input(title = "Risk Per Trade", defval = 0.01, group = "ATR Position Size Inputs")+risk = input.float(title = "Risk Per Trade", defval = 0.01, group = "ATR Position Size Inputs")atr = ta.atr(length)⋯PlotColor = Position == -1 ? color.orange : Position == 1 ? color.orange : color.orange-plot(xATRTrailingStop, color=PlotColor, linewidth = input(2, "Line Width", group = "ATR Trailing Stop Inputs"), style = plot.style_circles, title = "ATR Trailing Stop")+plot(xATRTrailingStop, color=PlotColor, linewidth = input.int(2, "Line Width", group = "ATR Trailing Stop Inputs"), style = plot.style_circles, title = "ATR Trailing Stop")// Buy and Sell Conditions //⋯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: 5 before, 5 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.
⋯// © Robrecht99-//@version=5+//@version=6strategy("Classic Trend Following System", overlay=true, margin_long=0, margin_short=0, pyramiding=1)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 23 ms | Yes 1,000 bars in 64 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
The engine and this page's reading of the source count a different number of settings, so one of them has missed one.
“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/// © Robrecht99//@version=5strategy("Classic Trend Following System", overlay=true, margin_long=0, margin_short=0, pyramiding=1)// Backtest Range //Start = input.time(defval = timestamp("01 Jan 2017 00:00 +0000"), title = "Backtest Start Date", group = "backtest window")Finish = input.time(defval = timestamp("01 Jan 2100 00:00 +0000"), title = "Backtest End Date", group = "backtest window")// Moving Averages //len1 = input.int(50, minval = 1, title = "Length Fast EMA", group = "Moving Average Inputs")len2 = input.int(150, minval = 1, title = "Length Slow EMA", group = "Moving Average Inputs")src1 = input(close, title = "Source Fast MA")src2 = input(close, title = "Source Slow MA")maFast = input.color(color.new(color.red, 0), title = "Color Fast EMA", group = "Moving Average Inputs", inline = "maFast")maSlow = input.color(color.new(color.blue, 0), title = "Color Slow EMA", group = "Moving Average Inputs", inline = "maSlow")fast = ta.ema(src1, len1)slow = ta.ema(src2, len2)plot(fast, color = maFast, title = "Fast EMA")plot(slow, color = maSlow, title = "Slow EMA")// Donchian Channels //Length1 = input.int(title = "Length Upper Channel", defval=50, minval=1, group = "Donchian Channels Inputs")Length2 = input.int(title = "Length Lower Channel", defval=50, minval=1, group = "Donchian Channels Inputs")h1 = ta.highest(high[1], Length1)l1 = ta.lowest(low[1], Length2)fillColor = input.color(color.new(color.purple, 95), title = "Fill Color", group = "Donchian Channels Inputs")upperColor = input.color(color.new(color.orange, 0), title = " Color Upper Channel", group = "Donchian Channels Inputs", inline = "upper")lowerColor = input.color(color.new(color.orange, 0), title = " Color Lower Channel", group = "Donchian Channels Inputs", inline = "lower")u = plot(h1, "Upper", color = upperColor)l = plot(l1, "Lower", color = upperColor)fill(u, l, color=fillColor)// ATR and Position Size //leverage = input.int(title = "Leverage Used", defval = 1, minval = 1, group = "ATR Position Size Inputs")pointvalue = input.int(title = "Point Value/ATR Multipliers from Entry", defval = 4, minval = 1, group = "ATR Position Size Inputs")length = input.int(title = "ATR Period", defval = 14, minval = 1, group ="ATR Position Size Inputs")risk = input(title = "Risk Per Trade", defval = 0.01, group = "ATR Position Size Inputs")atr = ta.atr(length)amount = (risk * strategy.initial_capital / (pointvalue * atr * leverage))// ATR Trailing Stops //xATRPeriod = input.int(14, "ATR Period", group = "ATR Trailing Stop Inputs")xATRMultiplier = input.float(4, "ATR Multiplier", step = .1, group = "ATR Trailing Stop Inputs")xATR = ta.atr(xATRPeriod)Stop = xATRMultiplier * xATRvar xATRTrailingStop = 0.0xATRTrailingStop := if close>xATRTrailingStop[1] and close[1] > xATRTrailingStop[1] math.max(xATRTrailingStop[1], close - Stop)else if close < xATRTrailingStop[1] and close[1] < xATRTrailingStop[1] math.min(xATRTrailingStop[1], close + Stop)else if close > xATRTrailingStop[1] close - Stop else close+Stopvar Position = 0.0Position := if close[1] < xATRTrailingStop[1] and close > xATRTrailingStop[1] 1else if close[1] > xATRTrailingStop[1] and close < xATRTrailingStop[1] -1else Position[1]PlotColor = Position == -1 ? color.orange : Position == 1 ? color.orange : color.orangeplot(xATRTrailingStop, color=PlotColor, linewidth = input(2, "Line Width", group = "ATR Trailing Stop Inputs"), style = plot.style_circles, title = "ATR Trailing Stop")// Buy and Sell Conditions //entrycondition1 = ta.crossover(fast, slow)entrycondition2 = fast > slowsellcondition1 = ta.crossunder(fast, slow)sellcondition2 = slow > fast// Buy and Sell Signals //if (close > h1 and entrycondition2) strategy.entry("long", strategy.long, qty=amount) stoploss = close - atr * xATRMultiplier strategy.exit("exit sl", stop=stoploss, trail_offset=stoploss)if (sellcondition1 and sellcondition2) strategy.close(id="long")if (close < l1 and sellcondition2) strategy.entry("short", strategy.short, qty=amount) stoploss = close + atr * xATRMultiplier strategy.exit("exit sl", stop=stoploss, trail_offset=stoploss)if (entrycondition1 and entrycondition2) strategy.close(id="short")