Pine Script v4 · Strategy
Simple Momentum Strategy Based on SMA, EMA and Volume
A Pine Script v4 strategy built on SMA (close, 10), EMA (close, 20) and Crossunder (sma_line, ema_line). 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 is one contract and pays no commission. 3 settings; defaults include SMA (small length) 10, EMA (large length) 20 and Volume Trigger 10.
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 Simple Momentum Strategy Based on SMA, EMA and Volume 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 Simple Momentum Strategy Based on SMA, EMA and Volume do on BTCUSDT 1D with default settings?
- +7.38%
- From 26 Jan 2024 to 21 Oct 2026, over 18 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Simple Momentum Strategy Based on SMA, EMA and Volume 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 Simple Momentum Strategy Based on SMA, EMA and Volume use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Simple Momentum Strategy Based on SMA, EMA and Volume run in Zeal Terminal?
- Yes
- It compiles in 8 ms and runs 1,000 bars in 27 ms, in the browser.
BTCUSDT · 1D · default settings
- SMA
- EMA
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +7.38%
- $7,379.47
- Buy and hold
- +102.5%
- same period
- Closed trades
- 18
- 7 won · 11 lost
- Profitable
- 38.9%
- of closed trades
- Profit factor
- 1.11
- gross profit ÷ gross loss
- Max drawdown
- 27.44%
- −$37,298.10
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
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.
-
Note
Uses an argument v6 removed
when = on line 36, 37. The upgraded version below moves each call inside an if block.
-
Note
Uses long =, which v5 renamed line 36
long = on line 36. 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 sma → ta.sma, ema → ta.ema, cross → ta.cross, crossunder → ta.crossunder.
- 2 strategy calls use the when = argument (line 36, 37). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 18 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 15: input(type = input.integer) → input.int()
- Line 16: input(type = input.integer) → input.int()
- Line 17: input(type = input.integer) → input.int()
- Line 18: sma → ta.sma
- Line 19: ema → ta.ema
3 more edits
- Line 23: cross → ta.cross
- Line 34: crossunder → ta.crossunder
- Line 36: long = true → direction = strategy.long
⋯// © slip_stream-//@version=4+//@version=5// Simple strategy for riding the momentum and optimising the timings of truer/longer price moves upwards for an long posistions on a daily basis (can be used, but with less effect⋯// Obtain inputs-sma_length = input(defval = 10, minval=1, type = input.integer, title = "SMA (small length)")+sma_length = input.int(defval = 10, minval=1, title = "SMA (small length)")-ema_length = input(defval = 20,minval=1, type = input.integer, title = "EMA (large length)")+ema_length = input.int(defval = 20,minval=1, title = "EMA (large length)")-volume_trigger = input(defval = 10, title = "Volume Trigger", type = input.integer)+volume_trigger = input.int(defval = 10, title = "Volume Trigger")-sma_line = sma(close, sma_length)+sma_line = ta.sma(close, sma_length)-ema_line = ema(close, ema_length)+ema_line = ta.ema(close, ema_length)// plot SMA and EMA lines with a cross for when they intersect⋯plot(ema_line, color = #e3d024, title = "EMA")-plot(cross(sma_line, ema_line) ? sma_line : na, style = plot.style_cross, linewidth = 4, color = color.white)+plot(ta.cross(sma_line, ema_line) ? sma_line : na, style = plot.style_cross, linewidth = 4, color = color.white)// Create variables⋯// exit criteria for the trade: when the sma line goes under the ema line-trade_exit_criteria = crossunder (sma_line, ema_line)+trade_exit_criteria = ta.crossunder (sma_line, ema_line)if (year >= 2019)- strategy.entry(id = "long", long = true, qty = 1, when = trade_entry_criteria)+ strategy.entry(id = "long", direction = strategy.long, qty = 1, when = trade_entry_criteria) strategy.close(id = "long", when = trade_exit_criteria, qty = 1)⋯Same 18 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 36: when = trade_entry_criteria → if block
- Line 37: when = trade_exit_criteria → if block
⋯if (year >= 2019)- strategy.entry(id = "long", direction = strategy.long, qty = 1, when = trade_entry_criteria)+ if trade_entry_criteria+ strategy.entry(id = "long", direction = strategy.long, qty = 1)- strategy.close(id = "long", when = trade_exit_criteria, qty = 1)+ if trade_exit_criteria+ strategy.close(id = "long", qty = 1) // for when you want to brute force close all open positions: strategy.close_all (when = trade_exit_criteria)Does it run in Pine Script v6?
Yes
Same 18 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.
⋯// © slip_stream-//@version=5+//@version=6// Simple strategy for riding the momentum and optimising the timings of truer/longer price moves upwards for an long posistions on a daily basis (can be used, but with less effect⋯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 |
|---|---|---|---|
| SMA (small length) | 10 | int (written input.integer) · min 1 | sma_line |
| EMA (large length) | 20 | int (written input.integer) · min 1 | ema_line |
| Volume Trigger | 10 | int (written input.integer) | enter_trade_3_bar_up |
What it calculates
- SMA (
sma,ta.smain v5)close, SMA (small length) (10)sma_line · line 18 - EMA (
ema,ta.emain v5)close, EMA (large length) (20)ema_line · line 19 - Crossunder (
crossunder,ta.crossunderin v5)sma_line, ema_linetrade_exit_criteria · line 34
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 | trade_entry_criteriaenter_trade_3_bar_up or enter_3_bar_play or enter_4_bar_play | On | 36 |
| Close long | trade_exit_criteria | On | 37 |
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 | +7.4% | +102.5% | 18 | 39% | 1.11 | 27.4% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.3% | +5.5% | 20 | 25% | 0.98 | 11.8% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +1.1% | +9.8% | 15 | 40% | 1.42 | 1.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.3% | +8.4% | 20 | 30% | 1.53 | 0.2% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.1% | +16.7% | 16 | 44% | 1.56 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 20 | 35% | 1.85 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.5% | +148.3% | 21 | 57% | 2.12 | 0.4% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.1% | +17.1% | 21 | 33% | 0.68 | 0.2% | 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% | $7,667.28 |
| Sideways market | 8 | 25% | −$19,922.30 |
| Falling market | 5 | 60% | $19,634.50 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
39.1%
The 95th percentile of the worst drawdown when the same 18 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 18.8% to 39.1%; the tick is what actually happened, 25.6%. Order luck alone moves it this much.
All 18 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 15 Feb 2024 51,795.20 | 19 Apr 2024 63,470.10 | 64 | $11,674.90 |
| long | 21 May 2024 71,446.60 | 18 Jun 2024 66,504.30 | 28 | −$4,942.29 |
| long | 23 Jul 2024 67,532.00 | 7 Aug 2024 56,022.00 | 15 | −$11,510.00 |
| long | 27 Aug 2024 62,834.00 | 4 Sep 2024 57,487.70 | 8 | −$5,346.26 |
| long | 21 Sep 2024 63,201.10 | 10 Oct 2024 60,636.00 | 19 | −$2,565.04 |
| long | 16 Oct 2024 67,074.10 | 28 Dec 2024 94,299.00 | 73 | $27,224.90 |
| long | 20 Jan 2025 101,332.00 | 8 Feb 2025 96,506.80 | 19 | −$4,824.77 |
| long | 20 Apr 2025 85,077.00 | 8 Jun 2025 105,552.00 | 49 | $20,475.10 |
| long | 13 Jun 2025 105,672.00 | 22 Jun 2025 102,120.00 | 9 | −$3,551.72 |
| long | 2 Jul 2025 105,681.00 | 7 Aug 2025 114,992.00 | 36 | $9,311.14 |
| long | 14 Aug 2025 123,306.00 | 24 Aug 2025 115,438.00 | 10 | −$7,868.38 |
| long | 6 Oct 2025 123,482.00 | 17 Oct 2025 108,194.00 | 11 | −$15,288.10 |
| long | 7 Jan 2026 93,748.00 | 27 Jan 2026 88,347.10 | 20 | −$5,400.89 |
| long | 13 Mar 2026 70,541.30 | 16 Mar 2026 72,815.30 | 3 | $2,273.91 |
| long | 17 Mar 2026 74,884.70 | 28 Mar 2026 66,407.30 | 11 | −$8,477.39 |
| long | 12 Apr 2026 73,043.20 | 23 May 2026 75,539.50 | 41 | $2,496.34 |
| long | 13 Jul 2026 63,780.00 | 3 Aug 2026 63,570.00 | 21 | −$209.99 |
| long | 22 Aug 2026 78,338.00 | 16 Oct 2026 82,245.90 | 55 | $3,907.90 |
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 |
|---|---|---|---|---|---|
| Simple Momentum Strategy Based on SMA, EMA and Volume this page | — | v4 strategy | +7.4% | 18 | 27.4% |
| MACD Long Strat by TheGrindToday | ema sma | v4 strategy | +24.0% | 79 | 20.1% |
| Stock trending strategy by exlux99 | ema sma | v4 strategy | +35.3% | 21 | 14.7% |
| 4-Hour Stochastic EMA Trend by slymnturkoglu | ema sma | v4 strategy | No trades on this market | ||
| 4X EMA and volume strategy by SoftKill21 | ema sma | v4 strategy | +1.8% | 3 | 25.8% |
| BTC WaveTrend R-R=1-1.5 by TrendCrypto2022 | ema sma | v5 strategy | +0.2% | 28 | 2.7% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 8 ms | With a workaround 1,000 bars in 27 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 36) so the engine trades the way the script says. 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. 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/// © slip_stream//@version=4// Simple strategy for riding the momentum and optimising the timings of truer/longer price moves upwards for an long posistions on a daily basis (can be used, but with less effect// on other time frames. Volume settings would have to be adjusted by the user accordingly. (short positions are not used).// This strategy has default settings of a short(er) SMA of 10, a long(er) EMA of 20, and Volume trigger of 10 units and above. All these settings can be changed by the user// using the GUI settings and not having to change the script.// The strategy will only open a long position when there is a clear indication that price momentum is upwards through the SMA moving and remaining above the EMA (mandatory) and price period indicators// of either 1) a standard 3 bar movement upwards, 2) a standard but "aggressive" 3 or 4 bar play where the low of the middle resting bars can be equal to or higher than (i.e. not// the more standard low of about half) of the opening of the ignition bar. The "aggression" of the 3/4 bar play was done in order to counteract the conservatisme of having a mandatory// SMA remaining higher than the EMA (this would have to be changed in the script by the user if they want to optimise to their own specifications. However, be warned, all programmatic// settings for the maximum acceptable low of the middle resting bars runs a risk of ignoring good entry points due to the low being minutely e.g. 0.01%, lower than the user defined setting)strategy(title = "Simple Momentum Strategy Based on SMA, EMA and Volume", overlay = true, pyramiding = 1, initial_capital = 100000, currency = currency.USD)// Obtain inputssma_length = input(defval = 10, minval=1, type = input.integer, title = "SMA (small length)")ema_length = input(defval = 20,minval=1, type = input.integer, title = "EMA (large length)")volume_trigger = input(defval = 10, title = "Volume Trigger", type = input.integer)sma_line = sma(close, sma_length)ema_line = ema(close, ema_length)// plot SMA and EMA lines with a cross for when they intersectplot(sma_line, color = #8b0000, title = "SMA")plot(ema_line, color = #e3d024, title = "EMA")plot(cross(sma_line, ema_line) ? sma_line : na, style = plot.style_cross, linewidth = 4, color = color.white)// Create variables// variables to check if trade should be entered//three consecutive bar bar moves upwards and volume of at least one bar is more than 10enter_trade_3_bar_up = sma_line > ema_line and close[1] >= close [2] and close[3] >= close[4] and close[2] >= close[3] and (volume[1] >= volume_trigger or volume[2] >= volume_trigger or volume[3] >= volume_trigger)// aggressive three bar play that ensures the low of the middle bar is equal to or greater than the open of the instigator bar. Volume is not taken into consideration (i.e. aggressive/risky)enter_3_bar_play = sma_line > ema_line and close[1] > close[3] and low[2] >= open[3]// aggressive four bar play similar to the 3 bar play aboveenter_4_bar_play = sma_line > ema_line and close[1] > close[4] and low[2] >= open[4]trade_entry_criteria = enter_trade_3_bar_up or enter_3_bar_play or enter_4_bar_play // has one of the trade entry criterias returned true?// exit criteria for the trade: when the sma line goes under the ema linetrade_exit_criteria = crossunder (sma_line, ema_line)if (year >= 2019) strategy.entry(id = "long", long = true, qty = 1, when = trade_entry_criteria) strategy.close(id = "long", when = trade_exit_criteria, qty = 1) // for when you want to brute force close all open positions: strategy.close_all (when = trade_exit_criteria)