Pine Script v4 · Strategy
Moving Average Cross Strategy early Closing
A Pine Script v4 strategy built on SMA (close, 50), Crossover (sma3, sma50) and Crossunder (close, sma50). It has 3 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. It has no settings.
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 Moving Average Cross Strategy early Closing repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 8 of 8 trades.
- How did Moving Average Cross Strategy early Closing do on BTCUSDT 1D with default settings?
- +73.79%
- From 26 Jan 2024 to 21 Oct 2026, over 20 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Moving Average Cross Strategy early Closing 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 Moving Average Cross Strategy early Closing use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Moving Average Cross Strategy early Closing run in Zeal Terminal?
- Yes
- It compiles in 17 ms and runs 1,000 bars in 23 ms, in the browser.
BTCUSDT · 1D · default settings
- 50
- 3
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +73.79%
- $73,791.30
- Buy and hold
- +102.5%
- same period
- Closed trades
- 20
- 3 won · 17 lost
- Profitable
- 15.0%
- of closed trades
- Profit factor
- 1.21
- gross profit ÷ gross loss
- Max drawdown
- 151.53%
- −$216,329.00
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 15, 16, 17. The upgraded version below moves each call inside an if block.
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, crossover → ta.crossover, crossunder → ta.crossunder.
- 3 strategy calls use the when = argument (line 15, 16, 17). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 20 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: sma → ta.sma
- Line 7: sma → ta.sma
- Line 8: sma → ta.sma
- Line 11: crossover → ta.crossover
- Line 12: crossunder → ta.crossunder
- Line 13: crossunder → ta.crossunder
⋯// © Geduldtrader-//@version=4+//@version=5strategy("MA Crossover", overlay = true)start = timestamp(2009,2,1,0,0)-sma50 = sma(close, 50)+sma50 = ta.sma(close, 50)-sma40 = sma(close, 40)+sma40 = ta.sma(close, 40)-sma3 = sma(close, 3)+sma3 = ta.sma(close, 3)plot(sma50,title='50', color=#00ffaa, linewidth=2)plot(sma3,title='3', color=#2196F3, linewidth=2)-long = crossover(sma3,sma50)+long = ta.crossover(sma3,sma50)-neut = crossunder(close,sma50)+neut = ta.crossunder(close,sma50)-short = crossunder(sma3,sma40)+short = ta.crossunder(sma3,sma40)if time >= start⋯Same 20 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 15: when = long → if block
- Line 16: when = short → if block
- Line 17: when = neut → if block
⋯if time >= start- strategy.entry("Long", strategy.long, 10.0, when=long)+ if long+ strategy.entry("Long", strategy.long, 10.0)-strategy.close("Long", when = short)+if short+ strategy.close("Long")-strategy.close("Long", when = neut)+if neut+ strategy.close("Long")plot(close)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: 20 before, 0 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.
⋯// © Geduldtrader-//@version=5+//@version=6strategy("MA Crossover", overlay = true)⋯Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
The script declares no settings.
What it calculates
- SMA (
sma,ta.smain v5)close, 50sma50 · line 6 - SMA (
sma,ta.smain v5)close, 40sma40 · line 7 - SMA (
sma,ta.smain v5)close, 3sma3 · line 8 - Crossover (
crossover,ta.crossoverin v5)sma3, sma50long · line 11 - Crossunder (
crossunder,ta.crossunderin v5)close, sma50neut · line 12 - Crossunder (
crossunder,ta.crossunderin v5)sma3, sma40short · line 13
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 | long | On | 15 |
| Close Long | short | On | 16 |
| Close Long | neut | On | 17 |
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 | +73.8% | +102.5% | 20 | 15% | 1.21 | 151.5% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +115.9% | +5.5% | 18 | 33% | 2.05 | 23.2% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +17.9% | +9.8% | 15 | 27% | 1.93 | 8.9% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +4.1% | +8.4% | 21 | 33% | 1.83 | 2.4% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.0% | +16.7% | 21 | 19% | 0.97 | 0.7% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.3% | +21.5% | 21 | 24% | 2.68 | 0.1% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −1.4% | +148.3% | 23 | 30% | 0.50 | 1.5% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.8% | +17.1% | 21 | 38% | 2.05 | 0.4% | 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 | 4 | 0% | −$60,078.30 |
| Sideways market | 9 | 11% | −$58,395.40 |
| Falling market | 7 | 29% | $192,265.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
11,452.8%
The 95th percentile of the worst drawdown when the same 20 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 105.5% to 11,452.8%; the tick is what actually happened, 145.5%. Order luck alone moves it this much.
All 20 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 18 May 2024 67,024.00 | 16 Jun 2024 66,228.30 | 29 | −$7,957.50 |
| long | 18 Jul 2024 64,088.00 | 3 Aug 2024 61,498.30 | 16 | −$25,896.50 |
| long | 24 Aug 2024 64,037.20 | 28 Aug 2024 59,415.00 | 4 | −$46,222.40 |
| long | 16 Sep 2024 59,132.00 | 17 Sep 2024 58,214.00 | 1 | −$9,180.10 |
| long | 19 Sep 2024 61,760.00 | 10 Oct 2024 60,636.00 | 21 | −$11,239.70 |
| long | 5 Jan 2025 98,220.50 | 8 Jan 2025 96,954.60 | 3 | −$12,659.10 |
| long | 17 Jan 2025 99,987.30 | 3 Feb 2025 97,700.60 | 17 | −$22,867.10 |
| long | 15 Apr 2025 84,591.60 | 16 Apr 2025 83,644.00 | 1 | −$9,475.90 |
| long | 19 Apr 2025 84,474.70 | 16 Jun 2025 105,594.00 | 58 | $211,193.00 |
| long | 26 Jun 2025 107,341.00 | 2 Jul 2025 105,681.00 | 6 | −$16,594.60 |
| long | 13 Sep 2025 116,029.00 | 23 Sep 2025 112,651.00 | 10 | −$33,784.20 |
| long | 2 Oct 2025 118,595.00 | 11 Oct 2025 112,774.00 | 9 | −$58,205.00 |
| long | 4 Jan 2026 90,628.00 | 21 Jan 2026 88,427.70 | 17 | −$22,003.50 |
| long | 16 Mar 2026 72,815.30 | 20 Mar 2026 69,930.00 | 4 | −$28,852.40 |
| long | 24 Mar 2026 70,906.40 | 27 Mar 2026 68,820.30 | 3 | −$20,861.40 |
| long | 8 Apr 2026 71,924.20 | 20 May 2026 76,834.40 | 42 | $49,101.40 |
| long | 26 May 2026 77,322.00 | 27 May 2026 75,930.00 | 1 | −$13,920.00 |
| long | 17 Jul 2026 63,830.20 | 1 Aug 2026 62,887.90 | 15 | −$9,423.20 |
| long | 5 Aug 2026 64,106.60 | 13 Aug 2026 63,480.00 | 8 | −$6,265.60 |
| long | 19 Aug 2026 64,725.40 | 17 Oct 2026 81,615.90 | 59 | $168,905.00 |
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 |
|---|---|---|---|---|---|
| Moving Average Cross Strategy early Closing this page | — | v4 strategy | +73.8% | 20 | 151.5% |
| Follow the Crypto Shorts by xtradernet | sma | v4 strategy | No trades on this market | ||
| buy down and sell high trend 200 sma and ma cross by willi130503 | sma | v4 strategy | No trades on this market | ||
| Mayer Multiple Strategy by xtradernet | sma | v4 strategy | No trades on this market | ||
| MFI Simple Strategy by OztheWoz | sma | v5 strategy | −0.2% | 28 | 3.2% |
| 3 Higher High Price & Vol - BTST next day Gap up by SharemarketRaja | sma | v4 strategy | No trades on this market | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 17 ms | Yes 1,000 bars in 23 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/// © Geduldtrader//@version=4strategy("MA Crossover", overlay = true)start = timestamp(2009,2,1,0,0)sma50 = sma(close, 50)sma40 = sma(close, 40)sma3 = sma(close, 3)plot(sma50,title='50', color=#00ffaa, linewidth=2)plot(sma3,title='3', color=#2196F3, linewidth=2)long = crossover(sma3,sma50)neut = crossunder(close,sma50)short = crossunder(sma3,sma40)if time >= start strategy.entry("Long", strategy.long, 10.0, when=long)strategy.close("Long", when = short)strategy.close("Long", when = neut)plot(close)