Pine Script v5 · Strategy
MA cross strategy
A Pine Script v5 strategy built on SMA (close, 10), RSI (close, 14) and ATR (14). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. 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 MA cross strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 9 of 9 trades.
- How did MA cross strategy do on BTCUSDT 1D with default settings?
- −606.21%
- From 26 Jan 2024 to 21 Oct 2026, over 35 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does MA cross 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: 35 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.
- Does MA cross strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 18, 23.
- Does MA cross strategy run in Zeal Terminal?
- Yes
- It compiles in 15 ms and runs 1,000 bars in 19 ms, in the browser.
BTCUSDT · 1D · default settings
- Short SMA
- Long SMA
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −606.21%
- −$6,062,140.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 35
- 11 won · 24 lost
- Profitable
- 31.4%
- of closed trades
- Profit factor
- 0.53
- gross profit ÷ gross loss
- Max drawdown
- 656.02%
- −$7,005,620.00
One run with the author's default settings, $1,000,000 starting capital, commission as the script sets it. A short 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 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 17, 22. The upgraded version below moves each call inside an if block.
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
- SMAclose, 10
shortSMA · line 6 - SMAclose, 30
longSMA · line 7 - RSIclose, 14
rsi · line 8 - ATR14
atr · line 9 - CrossovershortSMA, longSMA
longCondition · line 11 - CrossundershortSMA, longSMA
shortCondition · line 12
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 | rsi > 50 | On | 17 |
| Exit exit stop and limit | (longCondition) | On | 18 |
| Open short short | rsi < 50 | On | 22 |
| Exit exit stop and limit | (shortCondition) | On | 23 |
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 | −606.2% | +102.5% | 35 | 31% | 0.53 | 656.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −46.3% | +5.5% | 34 | 44% | 0.85 | 104.8% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −11.5% | +9.8% | 34 | 38% | 0.79 | 21.5% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −3.1% | +8.4% | 31 | 35% | 0.72 | 5.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.8% | +16.7% | 26 | 50% | 1.35 | 1.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 30 | 47% | 1.00 | 0.2% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −5.5% | +148.3% | 30 | 30% | 0.45 | 6.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +2.0% | +17.1% | 34 | 59% | 2.16 | 0.8% | 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 | 10 | 30% | −$1,219,100.00 |
| Sideways market | 17 | 29% | −$3,570,040.00 |
| Falling market | 8 | 38% | −$1,273,000.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
1,803.7%
The 95th percentile of the worst drawdown when the same 35 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 161.8% to 1,803.7%; the tick is what actually happened, 629.5%. Order luck alone moves it this much.
All 35 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 17 Apr 2024 63,793.40 | 19 May 2024 66,915.20 | 32 | −$312,180.00 |
| long | 19 May 2024 66,915.20 | 16 Jun 2024 66,228.30 | 28 | −$68,695.00 |
| short | 16 Jun 2024 66,228.30 | 24 Jun 2024 61,740.60 | 8 | $448,770.00 |
| long | 20 Jul 2024 66,660.00 | 4 Aug 2024 58,171.70 | 15 | −$848,834.00 |
| short | 7 Aug 2024 56,022.00 | 23 Aug 2024 63,348.30 | 16 | −$732,628.00 |
| long | 27 Aug 2024 62,834.00 | 1 Sep 2024 57,534.80 | 5 | −$529,924.00 |
| short | 5 Sep 2024 57,970.90 | 19 Sep 2024 63,585.90 | 14 | −$561,496.00 |
| long | 20 Sep 2024 62,948.00 | 10 Oct 2024 60,636.00 | 20 | −$231,199.00 |
| short | 10 Oct 2024 60,636.00 | 15 Oct 2024 66,505.20 | 5 | −$586,919.00 |
| long | 16 Oct 2024 67,074.10 | 29 Oct 2024 72,329.40 | 13 | $525,529.00 |
| short | 27 Dec 2024 95,791.60 | 18 Jan 2025 104,077.00 | 22 | −$828,587.00 |
| long | 18 Jan 2025 104,077.00 | 3 Feb 2025 92,274.00 | 16 | −$1,180,350.00 |
| short | 8 Feb 2025 96,506.80 | 25 Feb 2025 86,625.00 | 17 | $988,181.00 |
| long | 28 Mar 2025 87,232.00 | 4 Apr 2025 83,213.10 | 7 | −$401,892.00 |
| short | 4 Apr 2025 83,213.10 | 19 Apr 2025 84,474.70 | 15 | −$126,161.00 |
| long | 19 Apr 2025 84,474.70 | 22 Apr 2025 92,008.70 | 3 | $753,403.00 |
| long | 16 Jun 2025 105,594.00 | 20 Jun 2025 104,659.00 | 4 | −$93,543.00 |
| short | 20 Jun 2025 104,659.00 | 22 Jun 2025 98,339.40 | 2 | $631,922.00 |
| long | 1 Jul 2025 107,147.00 | 10 Jul 2025 114,029.00 | 9 | $688,211.00 |
| short | 5 Aug 2025 115,055.00 | 11 Aug 2025 121,122.00 | 6 | −$606,651.00 |
| long | 14 Aug 2025 123,306.00 | 19 Aug 2025 113,284.00 | 5 | −$1,002,240.00 |
| long | 15 Sep 2025 115,268.00 | 25 Sep 2025 110,247.00 | 10 | −$502,086.00 |
| short | 29 Sep 2025 112,164.00 | 1 Oct 2025 117,033.00 | 2 | −$486,913.00 |
| long | 4 Oct 2025 122,232.00 | 10 Oct 2025 113,865.00 | 6 | −$836,721.00 |
| short | 17 Oct 2025 108,194.00 | 4 Nov 2025 98,957.00 | 18 | $923,724.00 |
| short | 19 Dec 2025 85,516.40 | 6 Jan 2026 93,859.70 | 18 | −$834,330.00 |
| long | 6 Jan 2026 93,859.70 | 25 Jan 2026 86,528.20 | 19 | −$733,150.00 |
| short | 26 Jan 2026 86,670.40 | 30 Jan 2026 81,201.10 | 4 | $546,923.00 |
| short | 29 Mar 2026 66,377.00 | 9 Apr 2026 72,945.90 | 11 | −$656,887.00 |
| long | 12 Apr 2026 73,043.20 | 22 Apr 2026 78,692.20 | 10 | $564,906.00 |
| short | 22 May 2026 77,615.50 | 28 May 2026 72,801.40 | 6 | $481,409.00 |
| long | 11 Jul 2026 64,161.70 | 3 Aug 2026 63,570.00 | 23 | −$59,171.00 |
| short | 3 Aug 2026 63,570.00 | 19 Aug 2026 67,093.70 | 16 | −$352,365.00 |
| long | 21 Aug 2026 73,027.00 | 21 Aug 2026 76,979.20 | 0 | $395,221.00 |
| long | 22 Sep 2026 86,620.00 | 16 Oct 2026 82,245.90 | 24 | −$437,408.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 |
|---|---|---|---|---|---|
| MA cross strategy this page | — | v5 strategy | −606.2% | 35 | 656.0% |
| Estrategia Larry Connors [JoseMetal] by JoseMetal | sma rsi atr | v5 strategy | +2.7% | 24 | 2.4% |
| 3 x EMA + Stochastic RSI + ATR by tomimarson | rsi sma atr | v4 strategy | −42.3% | 18 | 44.8% |
| Atrend by OTS by prim722 | rsi sma atr | v4 strategy | +12.1% | 76 | 34.4% |
| EMA crossover (daily TF) by tradeswithashish | sma rsi atr | v5 strategy | +11.0% | 12 | 11.6% |
| Impulse Strategy Signals V2 by danieljordi | sma rsi atr | v5 strategy | No trades on this market | ||
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.
- 2 strategy calls use the when = argument (line 17, 22). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- No v6-only features. v6 is not needed to run this script.
Same 35 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 17: when = rsi > 50 → if block
- Line 22: when = rsi < 50 → if block
⋯ takeProfit = high + atr * 2- strategy.entry("long", strategy.long, 100, when = rsi > 50)+ if rsi > 50+ strategy.entry("long", strategy.long, 100) strategy.exit("exit", "long", stop=stopLoss, limit=takeProfit)⋯ takeProfit = low - atr * 2- strategy.entry("short", strategy.short, 100, when = rsi < 50)+ if rsi < 50+ strategy.entry("short", strategy.short, 100) strategy.exit("exit", "short", stop=stopLoss, limit=takeProfit)⋯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: 35 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.
⋯// © Glenn234-//@version=5+//@version=6strategy("MA cross strategy", shorttitle="macs", overlay=true)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 15 ms | Yes 1,000 bars in 19 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/// © Glenn234//@version=5strategy("MA cross strategy", shorttitle="macs", overlay=true)// Create indicator'sshortSMA = ta.sma(close, 10)longSMA = ta.sma(close, 30)rsi = ta.rsi(close, 14)atr = ta.atr(14)// Crossover conditionslongCondition = ta.crossover(shortSMA, longSMA)shortCondition = ta.crossunder(shortSMA, longSMA)// trade conditionsif (longCondition) stopLoss = low - atr * 2 takeProfit = high + atr * 2 strategy.entry("long", strategy.long, 100, when = rsi > 50) strategy.exit("exit", "long", stop=stopLoss, limit=takeProfit)if (shortCondition) stopLoss = high + atr * 2 takeProfit = low - atr * 2 strategy.entry("short", strategy.short, 100, when = rsi < 50) strategy.exit("exit", "short", stop=stopLoss, limit=takeProfit)// Plot SMA to chartplot(shortSMA, color=color.red, title="Short SMA")plot(longSMA, color=color.green, title="Long SMA")