Pine Script v5 · Strategy
Automatic Moving Average Strategy
A Pine Script v5 strategy built on EMA (cma, 3). 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 commits 10000 in cash and pays no commission. 4 settings; defaults include Sensitivity 20, Smoothing 3 and Source 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 Automatic Moving Average 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 Automatic Moving Average Strategy do on BTCUSDT 1D with default settings?
- +29.10%
- From 26 Jan 2024 to 21 Oct 2026, over 34 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Automatic Moving Average Strategy 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: 34 before, 20 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 Automatic Moving Average Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Automatic Moving Average Strategy run in Zeal Terminal?
- Yes
- It compiles in 24 ms and runs 1,000 bars in 444 ms, in the browser.
BTCUSDT · 1D · default settings
- Automatic MA
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +29.10%
- $2,909.70
- Buy and hold
- +102.5%
- same period
- Closed trades
- 34
- 7 won · 27 lost
- Profitable
- 20.6%
- of closed trades
- Profit factor
- 1.22
- gross profit ÷ gross loss
- Max drawdown
- 53.84%
- −$7,787.34
One run with the author's default settings, $10,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.
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 |
|---|---|---|---|
| SensitivityLow (High Sensitivity), High (Low Sensitivity).\n\nAdjust according to timeframe and asset. | 20 | int | line 29 |
| Smoothing | 3 | int | scma |
| Source | close | source | sum cma cma_p1 cma_m1 |
| Start year | 2020 | int | in_date_range |
What it calculates
- EMAcma, Smoothing (3)
scma · line 54
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 | change_to_uptrend | On | 68 |
| Open Short short | change_to_downtrend | On | 70 |
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 | +29.1% | +102.5% | 34 | 21% | 1.22 | 53.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +7.9% | +5.5% | 34 | 21% | 1.18 | 12.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +186.8% | +9.8% | 35 | 31% | 2.45 | 32.8% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.0% | +8.4% | 40 | 20% | 1.00 | 28.3% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −48.1% | +16.7% | 32 | 25% | 0.78 | 78.5% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +52.8% | +21.5% | 38 | 29% | 2.07 | 10.6% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +72.9% | +148.3% | 28 | 18% | 1.66 | 45.8% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −2.0% | +17.1% | 32 | 22% | 0.95 | 19.9% | 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 | 0% | −$5,617.69 |
| Sideways market | 20 | 30% | $9,229.85 |
| Falling market | 5 | 20% | −$702.46 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
58.9%
The 95th percentile of the worst drawdown when the same 34 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 31.7% to 58.9%; the tick is what actually happened, 52.8%. Order luck alone moves it this much.
All 34 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 9 Feb 2024 45,288.70 | 22 Mar 2024 65,501.30 | 42 | $4,463.07 |
| short | 22 Mar 2024 65,501.30 | 27 Mar 2024 69,988.00 | 5 | −$684.98 |
| long | 27 Mar 2024 69,988.00 | 3 Apr 2024 65,464.00 | 7 | −$646.40 |
| short | 3 Apr 2024 65,464.00 | 8 Apr 2024 69,360.40 | 5 | −$595.20 |
| long | 8 Apr 2024 69,360.40 | 14 Apr 2024 63,924.50 | 6 | −$783.71 |
| short | 14 Apr 2024 63,924.50 | 21 May 2024 71,446.60 | 37 | −$1,176.72 |
| long | 21 May 2024 71,446.60 | 13 Jun 2024 68,264.00 | 23 | −$445.46 |
| short | 13 Jun 2024 68,264.00 | 20 Jul 2024 66,660.00 | 37 | $234.97 |
| long | 20 Jul 2024 66,660.00 | 4 Aug 2024 60,698.00 | 15 | −$894.39 |
| short | 4 Aug 2024 60,698.00 | 24 Aug 2024 64,037.20 | 20 | −$550.14 |
| long | 24 Aug 2024 64,037.20 | 28 Aug 2024 59,415.00 | 4 | −$721.80 |
| short | 28 Aug 2024 59,415.00 | 20 Sep 2024 62,948.00 | 23 | −$594.63 |
| long | 20 Sep 2024 62,948.00 | 11 Oct 2024 60,326.40 | 21 | −$416.47 |
| short | 11 Oct 2024 60,326.40 | 12 Oct 2024 62,540.00 | 1 | −$366.94 |
| long | 12 Oct 2024 62,540.00 | 10 Jan 2025 92,552.50 | 90 | $4,798.93 |
| short | 10 Jan 2025 92,552.50 | 16 Jan 2025 100,497.00 | 6 | −$858.42 |
| long | 16 Jan 2025 100,497.00 | 7 Feb 2025 96,554.40 | 22 | −$392.35 |
| short | 7 Feb 2025 96,554.40 | 3 May 2025 96,887.10 | 85 | −$34.47 |
| long | 3 May 2025 96,887.10 | 22 Aug 2025 112,500.00 | 111 | $1,611.45 |
| short | 22 Aug 2025 112,500.00 | 23 Aug 2025 116,936.00 | 1 | −$394.31 |
| long | 23 Aug 2025 116,936.00 | 25 Aug 2025 113,494.00 | 2 | −$294.38 |
| short | 25 Aug 2025 113,494.00 | 12 Sep 2025 115,483.00 | 18 | −$175.26 |
| long | 12 Sep 2025 115,483.00 | 24 Sep 2025 111,999.00 | 12 | −$301.68 |
| short | 24 Sep 2025 111,999.00 | 2 Oct 2025 118,595.00 | 8 | −$588.95 |
| long | 2 Oct 2025 118,595.00 | 12 Oct 2025 110,644.00 | 10 | −$670.40 |
| short | 12 Oct 2025 110,644.00 | 13 Oct 2025 114,959.00 | 1 | −$389.94 |
| long | 13 Oct 2025 114,959.00 | 15 Oct 2025 113,028.00 | 2 | −$167.94 |
| short | 15 Oct 2025 113,028.00 | 10 Apr 2026 71,788.00 | 177 | $3,648.66 |
| long | 10 Apr 2026 71,788.00 | 30 May 2026 73,460.80 | 50 | $233.02 |
| short | 30 May 2026 73,460.80 | 23 Jul 2026 66,114.50 | 54 | $1,000.03 |
| long | 23 Jul 2026 66,114.50 | 2 Aug 2026 62,823.70 | 10 | −$497.75 |
| short | 2 Aug 2026 62,823.70 | 3 Aug 2026 63,570.00 | 1 | −$118.80 |
| long | 3 Aug 2026 63,570.00 | 15 Aug 2026 63,043.60 | 12 | −$82.81 |
| short | 15 Aug 2026 63,043.60 | 18 Aug 2026 64,532.10 | 3 | −$236.12 |
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 |
|---|---|---|---|---|---|
| Automatic Moving Average Strategy this page | — | v5 strategy | +29.1% | 34 | 53.8% |
| 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% |
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.
- 4 settings use the untyped input() (line 7, 8, 9, 10). 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 34 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.source()
- Line 10: input() → input.int()
⋯start = 20-lookback = input(20, "Sensitivity", tooltip="Low (High Sensitivity), High (Low Sensitivity).\n\nAdjust according to timeframe and asset.")+lookback = input.int(20, "Sensitivity", tooltip="Low (High Sensitivity), High (Low Sensitivity).\n\nAdjust according to timeframe and asset.")-smoothing = input(3, "Smoothing")+smoothing = input.int(3, "Smoothing")-source = input(close, "Source")+source = input.source(close, "Source")-startYear = input(2020, "Start year")+startYear = input.int(2020, "Start year")resp = 1⋯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: 34 before, 20 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.
⋯// © fondDealer96636-//@version=5+//@version=6strategy('Automatic Moving Average', overlay=true, max_bars_back=201, pyramiding=0, currency=currency.USD, default_qty_type=strategy.cash, default_qty_value=10000, initial_capital=10000)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 24 ms | Yes 1,000 bars in 444 ms | Not measured |
| PineTS | Not tested | Not tested | 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.
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/// © fondDealer96636//@version=5strategy('Automatic Moving Average', overlay=true, max_bars_back=201, pyramiding=0, currency=currency.USD, default_qty_type=strategy.cash, default_qty_value=10000, initial_capital=10000)// inputstart = 20lookback = input(20, "Sensitivity", tooltip="Low (High Sensitivity), High (Low Sensitivity).\n\nAdjust according to timeframe and asset.")smoothing = input(3, "Smoothing")source = input(close, "Source")startYear = input(2020, "Start year")resp = 1in_date_range = time >= timestamp(syminfo.timezone, startYear, 1, 1, 0, 0)// globalvar ix = -1var mal = array.new_int(0)// functionsavg(source, len) => sum = 0.0 for i = 0 to len-1 sum += source[i] sum/lenbull = close > openwick_touch(x) => bull ? ((close <= x and x <= high) or (low <= x and x <= open)) : ((open <= x and x <= high) or (low <= x and x <= close))body_touch(x) => bull ? (open < x and x < close) : (close < x and x < open)touches(t) => touches = 0 for i = 0 to lookback-1 touches += t[i] ? 1 : 0 touches// localix := ix+1prev_mal = ix >= 1 ? array.get(mal, ix-1) : startcma = avg(source, prev_mal)cma_p1 = avg(source, prev_mal+1)cma_m1 = avg(source, prev_mal-1)d = touches(wick_touch(cma))d_p1 = touches(wick_touch(cma_p1))d_m1 = touches(wick_touch(cma_m1))d_b = touches(body_touch(cma))d_p1_b = touches(body_touch(cma_p1))d_m1_b = touches(body_touch(cma_m1))any_body_touch = d_b > 0 or d_p1_b > 0 or d_m1_b > 0no_wick_touch = d <= 0 and d_p1 <= 0 and d_m1 <= 0wick_maximized = d >= d_p1 and d >= d_m1 ? prev_mal : (d_p1 >= d and d_p1 >= d_m1 ? prev_mal+resp : (d_m1 >= d and d_m1 >= d_p1 ? prev_mal-resp : na))up = cma > cma[1]down = cma < cma[1]against_trend = (up and close < cma) or (down and close > cma)new_mal = no_wick_touch or against_trend ? prev_mal-resp : (any_body_touch ? prev_mal+resp : wick_maximized)next_mal = na(new_mal) ? prev_mal : new_malarray.push(mal, next_mal < 2 ? 2 : (next_mal > 200 ? 200 : next_mal))// graphscma = ta.ema(cma, smoothing)uptrend = scma > scma[1]downtrend = scma < scma[1]plot(scma, "Automatic MA", color=uptrend ? color.green : color.red)uptrending = close > scma and uptrenddowntrending = close < scma and downtrenddefy = not uptrending and not downtrendingdefy_cross = defy and body_touch(scma)barcolor(uptrending ? color.lime : (downtrending ? color.red : (defy_cross ? color.black : color.white)))// strategychange_to_uptrend = uptrending and not uptrending[1]change_to_downtrend = downtrending and not downtrending[1]if in_date_range if change_to_uptrend strategy.entry("Long", strategy.long) if change_to_downtrend strategy.entry("Short", strategy.short)