Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (4)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$16,037$20,253Equity from $10,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots.
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.

SettingDefaultType and rangeFeeds
SensitivityLow (High Sensitivity), High (Low Sensitivity).\n\nAdjust according to timeframe and asset.20intline 29
Smoothing3intscma
Sourceclosesourcesum cma cma_p1 cma_m1
Start year2020intin_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.

smoothingSmoothingsetting · 3closecloseprice datatimetimeprice datastartYearStart yearsetting · 2020openopenprice datascmaEMA→ scmaix, prev_malix · prev_mal2 valuessumsumvaluetouchestouchesvaluecma, cma_p1, cma_m1cma · cma_p1 +13 valuesd, d_bd · d_b2 valuesd_p1, d_p1_bd_p1 · d_p1_b2 valuesd_m1, d_m1_bd_m1 · d_m1_b2 valueswick_maximizedwick_maximizedvaluenew_malnew_malvaluenext_malnext_malvaluechange_to_uptrendchange_to_uptrendconditionchange_to_downtrendchange_to_downtrendconditionin_date_rangein_date_rangeconditionbullbullconditionany_body_touch, no_wick_touchany_body_touch · no_wic…2 conditionsup, downup · down2 conditionsagainst_trendagainst_trendconditionuptrend, downtrenduptrend · downtrend2 conditionsuptrendinguptrendingconditiondowntrendingdowntrendingconditiondefydefyconditiondefy_crossdefy_crossconditionentry:LongOpen Longstrategy.entryentry:ShortOpen Shortstrategy.entryplot:57Automatic MAplotbarcolor:62barcolorbarcolor

Order rules

OrderFires whenBy defaultLine
Open Long longchange_to_uptrendOn68
Open Short shortchange_to_downtrendOn70

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.

6/6trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+29.1%+102.5%3421%1.2253.8%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+7.9%+5.5%3421%1.1812.6%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+186.8%+9.8%3531%2.4532.8%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+0.0%+8.4%4020%1.0028.3%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−48.1%+16.7%3225%0.7878.5%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+52.8%+21.5%3829%2.0710.6%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+72.9%+148.3%2818%1.6645.8%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−2.0%+17.1%3222%0.9519.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 barsTradesWonP&L
Rising market90%−$5,617.69
Sideways market2030%$9,229.85
Falling market520%−$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.

0%68%
All 34 trades
SideEntryExitBarsP&L
long9 Feb 2024 45,288.7022 Mar 2024 65,501.3042$4,463.07
short22 Mar 2024 65,501.3027 Mar 2024 69,988.005−$684.98
long27 Mar 2024 69,988.003 Apr 2024 65,464.007−$646.40
short3 Apr 2024 65,464.008 Apr 2024 69,360.405−$595.20
long8 Apr 2024 69,360.4014 Apr 2024 63,924.506−$783.71
short14 Apr 2024 63,924.5021 May 2024 71,446.6037−$1,176.72
long21 May 2024 71,446.6013 Jun 2024 68,264.0023−$445.46
short13 Jun 2024 68,264.0020 Jul 2024 66,660.0037$234.97
long20 Jul 2024 66,660.004 Aug 2024 60,698.0015−$894.39
short4 Aug 2024 60,698.0024 Aug 2024 64,037.2020−$550.14
long24 Aug 2024 64,037.2028 Aug 2024 59,415.004−$721.80
short28 Aug 2024 59,415.0020 Sep 2024 62,948.0023−$594.63
long20 Sep 2024 62,948.0011 Oct 2024 60,326.4021−$416.47
short11 Oct 2024 60,326.4012 Oct 2024 62,540.001−$366.94
long12 Oct 2024 62,540.0010 Jan 2025 92,552.5090$4,798.93
short10 Jan 2025 92,552.5016 Jan 2025 100,497.006−$858.42
long16 Jan 2025 100,497.007 Feb 2025 96,554.4022−$392.35
short7 Feb 2025 96,554.403 May 2025 96,887.1085−$34.47
long3 May 2025 96,887.1022 Aug 2025 112,500.00111$1,611.45
short22 Aug 2025 112,500.0023 Aug 2025 116,936.001−$394.31
long23 Aug 2025 116,936.0025 Aug 2025 113,494.002−$294.38
short25 Aug 2025 113,494.0012 Sep 2025 115,483.0018−$175.26
long12 Sep 2025 115,483.0024 Sep 2025 111,999.0012−$301.68
short24 Sep 2025 111,999.002 Oct 2025 118,595.008−$588.95
long2 Oct 2025 118,595.0012 Oct 2025 110,644.0010−$670.40
short12 Oct 2025 110,644.0013 Oct 2025 114,959.001−$389.94
long13 Oct 2025 114,959.0015 Oct 2025 113,028.002−$167.94
short15 Oct 2025 113,028.0010 Apr 2026 71,788.00177$3,648.66
long10 Apr 2026 71,788.0030 May 2026 73,460.8050$233.02
short30 May 2026 73,460.8023 Jul 2026 66,114.5054$1,000.03
long23 Jul 2026 66,114.502 Aug 2026 62,823.7010−$497.75
short2 Aug 2026 62,823.703 Aug 2026 63,570.001−$118.80
long3 Aug 2026 63,570.0015 Aug 2026 63,043.6012−$82.81
short15 Aug 2026 63,043.6018 Aug 2026 64,532.103−$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.

ScriptSharesTypeNetTradesMax drawdown
Automatic Moving Average Strategy this page—v5 strategy+29.1%3453.8%
EMA Slope AyE by fajardoarelysemav4 strategyNo trades on this market
ROoT by realisticDove62527emav5 strategy−4.7%2259.4%
sachin5986 by mohitesachin78emav4 strategy+32.1%3517.6%
sonu1997 by Sonu1997emav4 strategy+960.7%21,103.8%
Moving Average Crossover by Muh4mm4demav4 strategy+19,990.1%301,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.

Clean v5

VerifiedDownload the v5 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

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 24 msYes 1,000 bars in 444 msNot measured
PineTSNot testedNot testedNot 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)