Pine Script v4 · Strategy
EMA Slope Trend Follower Strategy
A Pine Script v4 strategy built on Change (out), EMA (slp, 9) and Crossunder (emaslopeF, emaslopeS). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 30% of equity and pays 0.06% commission. 10 settings; defaults include Source MA Type EMA, Source MA Length 130 and Fast Slope MA Length 9.
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 EMA Slope Trend Follower Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 6 of 6 trades.
- How did EMA Slope Trend Follower Strategy do on BTCUSDT 1D with default settings?
- +18.66%
- From 26 Jan 2024 to 21 Oct 2026, over 25 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can EMA Slope Trend Follower Strategy be converted to Pine Script v5?
- Yes
- The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
- Does EMA Slope Trend Follower Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does EMA Slope Trend Follower Strategy run in Zeal Terminal?
- Yes
- It compiles in 15 ms and runs 1,000 bars in 36 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +18.66%
- $1,865.62
- Buy and hold
- +102.5%
- same period
- Closed trades
- 25
- 8 won · 17 lost
- Profitable
- 32.0%
- of closed trades
- Profit factor
- 1.85
- gross profit ÷ gross loss
- Max drawdown
- 7.44%
- −$818.16
One run with the author's default settings, $10,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.
-
Note
Uses long =, which v5 renamed line 70
long = on line 70, 73. v5 writes it as direction = strategy.long or strategy.short.
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 ema → ta.ema, sma → ta.sma, change → ta.change, stdev → ta.stdev, crossunder → ta.crossunder, crossover → ta.crossover.
v4 → v5
VerifiedSame 25 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 6: input(type = input.string) → input.string()
- Line 12: input(type = input.string) → input.string()
- Line 18: ema → ta.ema
- Line 20: sma → ta.sma
- Line 22: change → ta.change
13 more edits
- Line 23: ema → ta.ema
- Line 24: ema → ta.ema
- Line 27: ema → ta.ema
- Line 29: sma → ta.sma
- Line 32: ema → ta.ema
- Line 34: sma → ta.sma
- Line 35: stdev → ta.stdev
- Line 35: sma → ta.sma
- Line 35: stdev → ta.stdev
- Line 56: crossunder → ta.crossunder
- Line 57: crossover → ta.crossover
- Line 70: long = true → direction = strategy.long
- Line 73: long = false → direction = strategy.short
⋯// © lukescream-//@version=4+//@version=5strategy("EMA Slope Trend Follower", initial_capital=10000, default_qty_type=strategy.percent_of_equity, commission_type=strategy.commission.percent, commission_value=0.06, slippage = 2, default_qty_value=30, overlay=false)//definizione input-average = input (title="Source MA Type", type=input.string, defval="EMA",options=["EMA","SMA"])+average = input.string (title="Source MA Type", defval="EMA",options=["EMA","SMA"])len = input(130, minval=1, title="Source MA Length")⋯trendfilterperiod=input(200,title="Trend Filter MA Period")-trendfiltertype=input (title="Trend Filter MA Type", type=input.string, defval="EMA",options=["EMA","SMA"])+trendfiltertype=input.string (title="Trend Filter MA Type", defval="EMA",options=["EMA","SMA"])volatilityfilter=input(false,title="Volatility Filter")⋯out = if average == "EMA" - ema(close,len)+ ta.ema(close,len)else- sma(close,len)+ ta.sma(close,len) -slp = change(out)/out+slp = ta.change(out)/out-emaslopeF = ema(slp,slopeFlen)+emaslopeF = ta.ema(slp,slopeFlen)-emaslopeS = ema(slp,slopeSlen)+emaslopeS = ta.ema(slp,slopeSlen)//variabili accessorie e condizioniTrendConditionL=if trendfiltertype =="EMA"- close>ema(close,trendfilterperiod)+ close>ta.ema(close,trendfilterperiod)else- close>sma(close,trendfilterperiod)+ close>ta.sma(close,trendfilterperiod) TrendConditionS=if trendfiltertype =="EMA"- close<ema(close,trendfilterperiod)+ close<ta.ema(close,trendfilterperiod)else- close<sma(close,trendfilterperiod)+ close<ta.sma(close,trendfilterperiod)-VolatilityCondition=stdev(close,volatilitystdevlength)>sma(stdev(close,volatilitystdevlength),volatilitystdevmalength)+VolatilityCondition=ta.stdev(close,volatilitystdevlength)>ta.sma(ta.stdev(close,volatilitystdevlength),volatilitystdevmalength)ConditionEntryL= if trendfilter == true⋯ emaslopeF<emaslopeS-ConditionExitL=crossunder(emaslopeF,emaslopeS)+ConditionExitL=ta.crossunder(emaslopeF,emaslopeS)-ConditionExitS=crossover(emaslopeF,emaslopeS)+ConditionExitS=ta.crossover(emaslopeF,emaslopeS)//plot⋯if ConditionEntryL- strategy.entry("SLPLong",long=true)+ strategy.entry("SLPLong",direction = strategy.long) if ConditionEntryS- strategy.entry("SLPShort",long=false)+ strategy.entry("SLPShort",direction = strategy.short)Same 25 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 7: input() → input.int()
- Line 8: input() → input.int()
- Line 9: input() → input.int()
- Line 10: input() → input.bool()
- Line 11: input() → input.int()
- Line 13: input() → input.bool()
- Line 14: input() → input.int()
- Line 15: input() → input.int()
⋯average = input.string (title="Source MA Type", defval="EMA",options=["EMA","SMA"])-len = input(130, minval=1, title="Source MA Length")+len = input.int(130, minval=1, title="Source MA Length")-slopeFlen = input (9,title="Fast Slope MA Length")+slopeFlen = input.int (9,title="Fast Slope MA Length")-slopeSlen= input (21,title="Slow Slope MA Length")+slopeSlen= input.int (21,title="Slow Slope MA Length")-trendfilter=input(true,title="Trend Filter")+trendfilter=input.bool(true,title="Trend Filter")-trendfilterperiod=input(200,title="Trend Filter MA Period")+trendfilterperiod=input.int(200,title="Trend Filter MA Period")trendfiltertype=input.string (title="Trend Filter MA Type", defval="EMA",options=["EMA","SMA"])-volatilityfilter=input(false,title="Volatility Filter")+volatilityfilter=input.bool(false,title="Volatility Filter")-volatilitystdevlength=input(20,title="Vol Filter STDev Length")+volatilitystdevlength=input.int(20,title="Vol Filter STDev Length")-volatilitystdevmalength=input(30,title="Vol Filter STDev MA Length")+volatilitystdevmalength=input.int(30,title="Vol Filter STDev MA Length")//variabili⋯Does it run in Pine Script v6?
Yes
Same 25 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
⋯// © lukescream-//@version=5+//@version=6strategy("EMA Slope Trend Follower", initial_capital=10000, default_qty_type=strategy.percent_of_equity, commission_type=strategy.commission.percent, commission_value=0.06, slippage = 2, default_qty_value=30, overlay=false)⋯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 MA Type | EMA | string · options ["EMA","SMA"] | out |
| Source MA Length | 130 | int · min 1 | out |
| Fast Slope MA Length | 9 | int | emaslopeF |
| Slow Slope MA Length | 21 | int | emaslopeS |
| Trend Filter | true | bool | ConditionEntryL ConditionEntryS |
| Trend Filter MA Period | 200 | int | TrendConditionL TrendConditionS |
| Trend Filter MA Type | EMA | string · options ["EMA","SMA"] | TrendConditionL TrendConditionS |
| Volatility Filter | false | bool | ConditionEntryL ConditionEntryS |
| Vol Filter STDev Length | 20 | int | VolatilityCondition |
| Vol Filter STDev MA Length | 30 | int | VolatilityCondition |
What it calculates
- Change (
change,ta.changein v5)outslp · line 22 - EMA (
ema,ta.emain v5)slp, Fast Slope MA Length (9)emaslopeF · line 23 - EMA (
ema,ta.emain v5)slp, Slow Slope MA Length (21)emaslopeS · line 24 - Crossunder (
crossunder,ta.crossunderin v5)emaslopeF, emaslopeSConditionExitL · line 56 - Crossover (
crossover,ta.crossoverin v5)emaslopeF, emaslopeSConditionExitS · line 57
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 |
|---|---|---|---|
| Close SLPShort | strategy.position_size < 0 | On | 65 |
| Close SLPLong | strategy.position_size > 0 | On | 68 |
| Open SLPLong long | ConditionEntryL | On | 70 |
| Open SLPShort short | ConditionEntryS | On | 73 |
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 | +18.7% | +102.5% | 25 | 32% | 1.85 | 7.4% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −1.6% | +5.5% | 29 | 21% | 0.87 | 6.5% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +52.3% | +9.8% | 20 | 50% | 4.50 | 6.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +2.4% | +8.4% | 20 | 20% | 1.24 | 6.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +23.7% | +16.7% | 24 | 42% | 1.87 | 10.6% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +4.4% | +21.5% | 26 | 23% | 1.39 | 6.2% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +4.9% | +148.3% | 24 | 42% | 1.21 | 13.6% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.2% | +17.1% | 25 | 32% | 0.97 | 5.4% | 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 (Fast Slope MA Length 15, Slow Slope MA Length 36) made +21.0% before 26 Dec 2025 and +11.4% after it, against +10.8% for the defaults. 82% of the 56 combinations made money in-sample and 89% out-of-sample.
| Slow Slope MA Length ↓ · Fast Slope MA Length → | 5 | 6 | 8 | 9 | 11 | 13 | 15 |
|---|---|---|---|---|---|---|---|
| 11 | +0% | +3% | −1% | −2% | 0% | −4% | −6% |
| 13 | +1% | −0% | −1% | +3% | +4% | 0% | −8% |
| 15 | +2% | +1% | +1% | +2% | +5% | +10% | 0% |
| 18 | +1% | +5% | +9% | +9% | +10% | +10% | +13% |
| 21 | +5% | +6% | +9% | +8% | +9% | +13% | +19% |
| 25 | +2% | +6% | +6% | +10% | +17% | +20% | +17% |
| 29 | +8% | +10% | +13% | +15% | +19% | +17% | +18% |
| 36 | +13% | +13% | +12% | +20% | +18% | +19% | +21% |
| Slow Slope MA Length ↓ · Fast Slope MA Length → | 5 | 6 | 8 | 9 | 11 | 13 | 15 |
|---|---|---|---|---|---|---|---|
| 11 | +8% | +9% | +9% | +10% | 0% | −8% | −7% |
| 13 | +9% | +10% | +11% | +13% | +12% | 0% | −6% |
| 15 | +11% | +11% | +13% | +12% | +12% | +11% | 0% |
| 18 | +12% | +12% | +12% | +12% | +11% | +13% | +12% |
| 21 | +13% | +12% | +11% | +11% | +13% | +11% | +12% |
| 25 | +11% | +11% | +12% | +11% | +14% | +14% | +15% |
| 29 | +12% | +11% | +13% | +13% | +14% | +14% | +12% |
| 36 | +13% | +11% | +12% | +14% | +12% | +11% | +11% |
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 | 7 | 43% | $278.95 |
| Sideways market | 15 | 27% | $1,073.48 |
| Falling market | 3 | 33% | $513.19 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
13.1%
The 95th percentile of the worst drawdown when the same 25 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 5.6% to 13.1%; the tick is what actually happened, 6.8%. Order luck alone moves it this much.
All 25 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 13 Aug 2024 59,346.60 | 24 Aug 2024 64,037.30 | 11 | −$240.71 |
| long | 24 Aug 2024 64,037.30 | 31 Aug 2024 59,124.00 | 7 | −$227.92 |
| short | 31 Aug 2024 59,124.00 | 15 Sep 2024 59,993.00 | 15 | −$45.47 |
| long | 19 Sep 2024 61,760.00 | 10 Oct 2024 60,636.00 | 21 | −$55.14 |
| short | 10 Oct 2024 60,636.00 | 14 Oct 2024 62,870.00 | 4 | −$107.65 |
| long | 14 Oct 2024 62,870.00 | 11 Dec 2024 96,593.00 | 58 | $1,495.38 |
| long | 16 Dec 2024 104,464.00 | 19 Dec 2024 100,204.00 | 3 | −$136.09 |
| long | 19 Jan 2025 104,556.00 | 29 Jan 2025 101,336.00 | 10 | −$102.44 |
| short | 27 Feb 2025 84,250.10 | 26 Mar 2025 87,392.90 | 27 | −$122.21 |
| long | 26 Mar 2025 87,392.90 | 30 Mar 2025 82,648.50 | 4 | −$173.91 |
| short | 30 Mar 2025 82,648.50 | 14 Apr 2025 83,760.00 | 15 | −$45.20 |
| long | 22 Apr 2025 87,516.20 | 1 Jun 2025 104,592.00 | 40 | $594.91 |
| long | 7 Jul 2025 109,204.00 | 8 Jul 2025 108,263.00 | 1 | −$31.87 |
| long | 10 Jul 2025 111,234.00 | 31 Jul 2025 117,840.00 | 21 | $188.34 |
| long | 14 Aug 2025 123,306.00 | 15 Aug 2025 118,295.00 | 1 | −$137.79 |
| long | 13 Sep 2025 116,029.00 | 24 Sep 2025 111,999.00 | 11 | −$116.86 |
| long | 3 Oct 2025 120,529.00 | 12 Oct 2025 110,644.00 | 9 | −$267.68 |
| short | 18 Oct 2025 106,432.00 | 10 Dec 2025 92,678.80 | 53 | $402.02 |
| short | 16 Dec 2025 86,432.10 | 23 Dec 2025 88,620.80 | 7 | −$86.49 |
| short | 26 Jan 2026 86,670.30 | 4 Mar 2026 68,338.00 | 37 | $680.37 |
| short | 31 Mar 2026 66,797.40 | 1 Apr 2026 68,284.50 | 1 | −$80.69 |
| long | 11 May 2026 82,210.10 | 20 May 2026 76,834.30 | 9 | −$227.13 |
| short | 20 May 2026 76,834.30 | 6 Jul 2026 63,650.00 | 47 | $570.36 |
| long | 21 Aug 2026 73,027.00 | 16 Sep 2026 75,644.50 | 26 | $121.72 |
| long | 20 Sep 2026 81,250.00 | 9 Oct 2026 81,754.40 | 19 | $17.78 |
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 |
|---|---|---|---|---|---|
| EMA Slope Trend Follower Strategy this page | — | v4 strategy | +18.7% | 25 | 7.4% |
| EMA Slope AyE by fajardoarelys | ema | v4 strategy | No trades on this market | ||
| ROoT by realisticDove62527 | ema | v5 strategy | −4.7% | 225 | 9.4% |
| sachin5986 by mohitesachin78 | ema | v4 strategy | +32.1% | 35 | 17.6% |
| sonu1997 by Sonu1997 | ema | v4 strategy | +960.7% | 2 | 1,103.8% |
| Moving Average Crossover by Muh4mm4d | ema | v4 strategy | +19,990.1% | 30 | 1,274.2% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 15 ms | With a workaround 1,000 bars in 36 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = true, long = false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 70, 73) so the engine trades the way the script says.
“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/// © lukescream//@version=4strategy("EMA Slope Trend Follower", initial_capital=10000, default_qty_type=strategy.percent_of_equity, commission_type=strategy.commission.percent, commission_value=0.06, slippage = 2, default_qty_value=30, overlay=false)//definizione inputaverage = input (title="Source MA Type", type=input.string, defval="EMA",options=["EMA","SMA"])len = input(130, minval=1, title="Source MA Length")slopeFlen = input (9,title="Fast Slope MA Length")slopeSlen= input (21,title="Slow Slope MA Length")trendfilter=input(true,title="Trend Filter")trendfilterperiod=input(200,title="Trend Filter MA Period")trendfiltertype=input (title="Trend Filter MA Type", type=input.string, defval="EMA",options=["EMA","SMA"])volatilityfilter=input(false,title="Volatility Filter")volatilitystdevlength=input(20,title="Vol Filter STDev Length")volatilitystdevmalength=input(30,title="Vol Filter STDev MA Length")//variabiliout = if average == "EMA" ema(close,len)else sma(close,len) slp = change(out)/outemaslopeF = ema(slp,slopeFlen)emaslopeS = ema(slp,slopeSlen)//variabili accessorie e condizioniTrendConditionL=if trendfiltertype =="EMA" close>ema(close,trendfilterperiod)else close>sma(close,trendfilterperiod) TrendConditionS=if trendfiltertype =="EMA" close<ema(close,trendfilterperiod)else close<sma(close,trendfilterperiod)VolatilityCondition=stdev(close,volatilitystdevlength)>sma(stdev(close,volatilitystdevlength),volatilitystdevmalength)ConditionEntryL= if trendfilter == true if volatilityfilter == true emaslopeF>emaslopeS and TrendConditionL and VolatilityCondition else emaslopeF>emaslopeS and TrendConditionLelse if volatilityfilter == true emaslopeF>emaslopeS and VolatilityCondition else emaslopeF>emaslopeSConditionEntryS= if trendfilter == true if volatilityfilter == true emaslopeF<emaslopeS and TrendConditionS and VolatilityCondition else emaslopeF<emaslopeS and TrendConditionSelse if volatilityfilter == true emaslopeF<emaslopeS and VolatilityCondition else emaslopeF<emaslopeSConditionExitL=crossunder(emaslopeF,emaslopeS)ConditionExitS=crossover(emaslopeF,emaslopeS)//plotplot (slp, color=color.white)plot (emaslopeF,color=color.yellow)plot (emaslopeS,color=color.red)//ingressi e usciteif ConditionExitS if strategy.position_size < 0 strategy.close("SLPShort")if ConditionExitL if strategy.position_size > 0 strategy.close("SLPLong")if ConditionEntryL strategy.entry("SLPLong",long=true) if ConditionEntryS strategy.entry("SLPShort",long=false)