Pine Script v4 · Strategy
MM tester
A Pine Script v4 strategy built on EMA or SMA (close, 14), Crossover (MM1, MM2) and Crossunder (MM1, MM2). It has 2 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 is one contract and pays no commission. 3 settings; defaults include MM1 14, MM2 28 and MM type EMA.
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 MM tester repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 12 of 12 trades.
- How did MM tester do on BTCUSDT 1D with default settings?
- −11.11%
- From 26 Jan 2024 to 21 Oct 2026, over 33 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can MM tester 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 MM tester use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does MM tester run in Zeal Terminal?
- Yes
- It compiles in 7 ms and runs 1,000 bars in 19 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −11.11%
- −$11,111.60
- Buy and hold
- +102.5%
- same period
- Closed trades
- 33
- 8 won · 25 lost
- Profitable
- 24.2%
- of closed trades
- Profit factor
- 0.91
- gross profit ÷ gross loss
- Max drawdown
- 37.11%
- −$37,111.40
One run with the author's default settings, $100,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
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.
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, crossover → ta.crossover, crossunder → ta.crossunder.
v4 → v5
VerifiedSame 33 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 12: ema → ta.ema
- Line 12: sma → ta.sma
- Line 13: ema → ta.ema
- Line 13: sma → ta.sma
- Line 18: crossover → ta.crossover
- Line 22: crossunder → ta.crossunder
⋯// © luisgzafra-//@version=4+//@version=5strategy("MM tester", overlay=true)⋯//-MM1 = tipo_MM == "EMA"? ema(close, ema1) : sma(close, ema1)+MM1 = tipo_MM == "EMA"? ta.ema(close, ema1) : ta.sma(close, ema1)-MM2 = tipo_MM == "EMA"? ema(close, ema2) : sma(close, ema2)+MM2 = tipo_MM == "EMA"? ta.ema(close, ema2) : ta.sma(close, ema2)//⋯//-longCondition = crossover(MM1, MM2)+longCondition = ta.crossover(MM1, MM2)if (longCondition)⋯//-shortCondition = crossunder(MM1, MM2)+shortCondition = ta.crossunder(MM1, MM2)if (shortCondition)⋯Same 33 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 10: input() → input.string()
⋯//-int ema1 = input(title="MM1", defval=14, minval=0)+int ema1 = input.int(title="MM1", defval=14, minval=0)-int ema2 = input(title="MM2", defval=28, minval=0)+int ema2 = input.int(title="MM2", defval=28, minval=0)//-tipo_MM = input(title="MM type", options=["EMA", "SMA"], defval="EMA")+tipo_MM = input.string(title="MM type", options=["EMA", "SMA"], defval="EMA")//⋯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: 33 before, 24 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.
⋯// © luisgzafra-//@version=5+//@version=6strategy("MM tester", overlay=true)⋯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 |
|---|---|---|---|
| MM1 | 14 | int · min 0 | MM1 |
| MM2 | 28 | int · min 0 | MM2 |
| MM type | EMA | string · options ["EMA", "SMA"] | MM1 MM2 |
What it calculates
- EMA / SMA (
ema,ta.emain v5)close, MM1 (14) · chosen by MM typeMM1 · line 12 - EMA / SMA (
ema,ta.emain v5)close, MM2 (28) · chosen by MM typeMM2 · line 13 - Crossover (
crossover,ta.crossoverin v5)MM1, MM2longCondition · line 18 - Crossunder (
crossunder,ta.crossunderin v5)MM1, MM2shortCondition · 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 My Long Entry Id long | (longCondition) | On | 20 |
| Open My Short Entry Id short | (shortCondition) | On | 24 |
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 | −11.1% | +102.5% | 33 | 24% | 0.91 | 37.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +1.6% | +5.5% | 31 | 32% | 1.05 | 15.2% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +1.3% | +9.8% | 28 | 36% | 1.23 | 2.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.5% | +8.4% | 32 | 19% | 0.69 | 1.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.1% | +16.7% | 32 | 25% | 0.78 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 25 | 40% | 2.19 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.7% | +148.3% | 24 | 33% | 2.30 | 0.3% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.0% | +17.1% | 33 | 24% | 0.94 | 0.1% | 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 | 9 | 22% | −$20,025.20 |
| Sideways market | 15 | 27% | $12,032.20 |
| Falling market | 9 | 22% | −$3,118.64 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
58.2%
The 95th percentile of the worst drawdown when the same 33 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 32.6% to 58.2%; the tick is what actually happened, 30.0%. Order luck alone moves it this much.
All 33 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 17 Apr 2024 63,793.40 | 19 May 2024 66,915.20 | 32 | −$3,121.80 |
| long | 19 May 2024 66,915.20 | 19 Jun 2024 65,175.30 | 31 | −$1,739.88 |
| short | 19 Jun 2024 65,175.30 | 20 Jul 2024 66,660.00 | 31 | −$1,484.69 |
| long | 20 Jul 2024 66,660.00 | 5 Aug 2024 58,161.00 | 16 | −$8,499.01 |
| short | 5 Aug 2024 58,161.00 | 26 Aug 2024 64,220.00 | 21 | −$6,058.99 |
| long | 26 Aug 2024 64,220.00 | 29 Aug 2024 59,034.90 | 3 | −$5,185.09 |
| short | 29 Aug 2024 59,034.90 | 20 Sep 2024 62,948.00 | 22 | −$3,913.10 |
| long | 20 Sep 2024 62,948.00 | 31 Dec 2024 92,792.10 | 102 | $29,844.10 |
| short | 31 Dec 2024 92,792.10 | 5 Jan 2025 98,220.50 | 5 | −$5,428.46 |
| long | 5 Jan 2025 98,220.50 | 10 Jan 2025 92,552.50 | 5 | −$5,668.02 |
| short | 10 Jan 2025 92,552.50 | 16 Jan 2025 100,497.00 | 6 | −$7,944.86 |
| long | 16 Jan 2025 100,497.00 | 7 Feb 2025 96,554.40 | 22 | −$3,943.00 |
| short | 7 Feb 2025 96,554.40 | 22 Apr 2025 87,516.20 | 74 | $9,038.13 |
| long | 22 Apr 2025 87,516.20 | 23 Jun 2025 100,964.00 | 62 | $13,447.60 |
| short | 23 Jun 2025 100,964.00 | 26 Jun 2025 107,341.00 | 3 | −$6,376.72 |
| long | 26 Jun 2025 107,341.00 | 22 Aug 2025 112,500.00 | 57 | $5,159.41 |
| short | 22 Aug 2025 112,500.00 | 15 Sep 2025 115,268.00 | 24 | −$2,768.01 |
| long | 15 Sep 2025 115,268.00 | 26 Sep 2025 108,994.00 | 11 | −$6,273.52 |
| short | 26 Sep 2025 108,994.00 | 2 Oct 2025 118,595.00 | 6 | −$9,600.50 |
| long | 2 Oct 2025 118,595.00 | 16 Oct 2025 110,763.00 | 14 | −$7,831.71 |
| short | 16 Oct 2025 110,763.00 | 6 Jan 2026 93,859.70 | 82 | $16,903.60 |
| long | 6 Jan 2026 93,859.70 | 25 Jan 2026 89,225.30 | 19 | −$4,634.37 |
| short | 25 Jan 2026 89,225.30 | 17 Mar 2026 74,884.70 | 51 | $14,340.70 |
| long | 17 Mar 2026 74,884.70 | 23 Mar 2026 67,859.00 | 6 | −$7,025.67 |
| short | 23 Mar 2026 67,859.00 | 26 Mar 2026 71,336.50 | 3 | −$3,477.53 |
| long | 26 Mar 2026 71,336.50 | 27 Mar 2026 68,820.30 | 1 | −$2,516.22 |
| short | 27 Mar 2026 68,820.30 | 10 Apr 2026 71,788.00 | 14 | −$2,967.67 |
| long | 10 Apr 2026 71,788.00 | 26 May 2026 77,322.00 | 46 | $5,534.03 |
| short | 26 May 2026 77,322.00 | 19 Jul 2026 64,834.20 | 54 | $12,487.80 |
| long | 19 Jul 2026 64,834.20 | 2 Aug 2026 62,823.70 | 14 | −$2,010.56 |
| short | 2 Aug 2026 62,823.70 | 8 Aug 2026 64,923.20 | 6 | −$2,099.55 |
| long | 8 Aug 2026 64,923.20 | 13 Aug 2026 63,480.00 | 5 | −$1,443.21 |
| short | 13 Aug 2026 63,480.00 | 20 Aug 2026 69,334.80 | 7 | −$5,854.79 |
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 |
|---|---|---|---|---|---|
| MM tester this page | — | v4 strategy | −11.1% | 33 | 37.1% |
| 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% |
| Simple Momentum Strategy Based on SMA, EMA and Volume by slip_stream | sma ema | v4 strategy | +7.4% | 18 | 27.4% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 7 ms | Yes 1,000 bars in 19 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/// © luisgzafra//@version=4strategy("MM tester", overlay=true)// Inputs//int ema1 = input(title="MM1", defval=14, minval=0)int ema2 = input(title="MM2", defval=28, minval=0)//tipo_MM = input(title="MM type", options=["EMA", "SMA"], defval="EMA")//MM1 = tipo_MM == "EMA"? ema(close, ema1) : sma(close, ema1)MM2 = tipo_MM == "EMA"? ema(close, ema2) : sma(close, ema2)//plot (MM1, color = color.black)plot (MM2, color = MM1 > MM2? color.red : color.green, linewidth=2)//longCondition = crossover(MM1, MM2)if (longCondition) strategy.entry("My Long Entry Id", strategy.long)//shortCondition = crossunder(MM1, MM2)if (shortCondition) strategy.entry("My Short Entry Id", strategy.short) // END