Pine Script v4 · Strategy
ma 20 high-low
A Pine Script v4 strategy built on SMA (high, 20), Crossover (close, smaH) and Crossunder (close, 0.99*smaH). It has 4 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. 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 20 high-low repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 14 of 14 trades.
- How did ma 20 high-low do on BTCUSDT 1D with default settings?
- +2,790.13%
- From 26 Jan 2024 to 21 Oct 2026, over 59 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can ma 20 high-low 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 ma 20 high-low use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does ma 20 high-low run in Zeal Terminal?
- Yes
- It compiles in 8 ms and runs 1,000 bars in 21 ms, in the browser.
BTCUSDT · 1D · default settings
- smaHigh
- smaLow
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +2,790.13%
- $2,790,130.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 59
- 20 won · 39 lost
- Profitable
- 33.9%
- of closed trades
- Profit factor
- 1.22
- gross profit ÷ gross loss
- Max drawdown
- 2,731.63%
- −$3,788,720.00
One run with the author's default settings, $100,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 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 22, 23, 25, 26. 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.
- 4 strategy calls use the when = argument (line 22, 23, 25, 26). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 59 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 12: crossover → ta.crossover
- Line 13: crossunder → ta.crossunder
- Line 14: crossunder → ta.crossunder
- Line 15: crossover → ta.crossover
⋯// © AlanAntony-//@version=4+//@version=5strategy("ma 20 high-low",overlay=true)//compute the indicators-smaH = sma(high, 20)+smaH = ta.sma(high, 20)-smaL = sma(low, 20)+smaL = ta.sma(low, 20)//plot the indicators⋯//trading logic-enterlong = crossover(close,smaH) //positive ema crossover+enterlong = ta.crossover(close,smaH) //positive ema crossover-exitlong = crossunder(close,0.99*smaH) //exiting long+exitlong = ta.crossunder(close,0.99*smaH) //exiting long-entershort = crossunder(close,smaL) //negative EMA Crossover+entershort = ta.crossunder(close,smaL) //negative EMA Crossover-exitshort = crossover(close,1.01*smaH) //exiting shorts+exitshort = ta.crossover(close,1.01*smaH) //exiting shortsnotintrade = strategy.position_size<=0⋯Same 59 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 22: when = enterlong → if block
- Line 23: when = entershort → if block
- Line 25: when = exitlong → if block
- Line 26: when = exitshort → if block
⋯if time >= start- strategy.entry( "long", strategy.long,100, when = enterlong)+ if enterlong+ strategy.entry( "long", strategy.long,100)- strategy.entry( "short", strategy.short,100, when = entershort) + if entershort+ strategy.entry( "short", strategy.short,100) - strategy.close("long", when = exitlong)+ if exitlong+ strategy.close("long")- strategy.close("short", when = exitshort)+ if exitshort+ strategy.close("short")//if time >= end⋯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: 59 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.
⋯// © AlanAntony-//@version=5+//@version=6strategy("ma 20 high-low",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)high, 20smaH · line 6 - SMA (
sma,ta.smain v5)low, 20smaL · line 7 - Crossover (
crossover,ta.crossoverin v5)close, smaHenterlong · line 12 - Crossunder (
crossunder,ta.crossunderin v5)close, 0.99*smaHexitlong · line 13 - Crossunder (
crossunder,ta.crossunderin v5)close, smaLentershort · line 14 - Crossover (
crossover,ta.crossoverin v5)close, 1.01*smaHexitshort · line 15
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 | enterlong | On | 22 |
| Open short short | entershort | On | 23 |
| Close long | exitlong | On | 25 |
| Close short | exitshort | On | 26 |
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 | +2,790.1% | +102.5% | 59 | 34% | 1.22 | 2,731.6% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +315.6% | +5.5% | 50 | 28% | 1.07 | 458.1% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +71.9% | +9.8% | 61 | 36% | 1.10 | 98.2% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −62.4% | +8.4% | 62 | 21% | 0.73 | 77.4% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +7.5% | +16.7% | 70 | 37% | 1.19 | 10.8% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +2.6% | +21.5% | 54 | 31% | 1.40 | 2.1% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −13.7% | +148.3% | 68 | 32% | 0.89 | 69.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +12.9% | +17.1% | 51 | 41% | 1.47 | 8.5% | 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 | 23 | 30% | −$1,939,750.00 |
| Sideways market | 23 | 39% | $726,892.00 |
| Falling market | 13 | 31% | $4,002,990.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
26,764.2%
The 95th percentile of the worst drawdown when the same 59 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 127.1% to 26,764.2%; the tick is what actually happened, 2,528.7%. Order luck alone moves it this much.
All 59 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 18 Mar 2024 68,393.50 | 19 Mar 2024 67,610.00 | 1 | −$78,347.00 |
| short | 20 Mar 2024 61,937.40 | 26 Mar 2024 69,880.00 | 6 | −$794,259.00 |
| long | 26 Mar 2024 69,880.00 | 3 Apr 2024 65,464.00 | 8 | −$441,601.00 |
| short | 3 Apr 2024 65,464.00 | 9 Apr 2024 71,620.00 | 6 | −$615,601.00 |
| long | 9 Apr 2024 71,620.00 | 10 Apr 2024 69,146.00 | 1 | −$247,400.00 |
| long | 11 Apr 2024 70,631.10 | 13 Apr 2024 67,116.50 | 2 | −$351,456.00 |
| short | 13 Apr 2024 67,116.50 | 16 May 2024 66,206.50 | 33 | $91,001.00 |
| long | 16 May 2024 66,206.50 | 1 Jun 2024 67,540.00 | 16 | $133,350.00 |
| long | 5 Jun 2024 70,537.80 | 8 Jun 2024 69,355.60 | 3 | −$118,223.00 |
| short | 12 Jun 2024 67,314.20 | 15 Jul 2024 60,797.90 | 33 | $651,632.00 |
| long | 15 Jul 2024 60,797.90 | 1 Aug 2024 64,628.00 | 17 | $383,010.00 |
| short | 3 Aug 2024 61,498.30 | 22 Aug 2024 61,156.00 | 19 | $34,231.00 |
| long | 22 Aug 2024 61,156.00 | 28 Aug 2024 59,415.00 | 6 | −$174,103.00 |
| short | 29 Aug 2024 59,034.90 | 14 Sep 2024 60,498.00 | 16 | −$146,309.00 |
| long | 14 Sep 2024 60,498.00 | 17 Sep 2024 58,214.00 | 3 | −$228,400.00 |
| long | 18 Sep 2024 60,314.00 | 2 Oct 2024 60,804.90 | 14 | $49,093.00 |
| short | 2 Oct 2024 60,804.90 | 15 Oct 2024 66,084.00 | 13 | −$527,908.00 |
| long | 15 Oct 2024 66,084.00 | 4 Nov 2024 68,776.00 | 20 | $269,199.00 |
| long | 7 Nov 2024 75,572.00 | 10 Dec 2024 97,276.50 | 33 | $2,170,450.00 |
| long | 12 Dec 2024 101,125.00 | 19 Dec 2024 100,204.00 | 7 | −$92,099.00 |
| short | 23 Dec 2024 95,186.30 | 7 Jan 2025 102,236.00 | 15 | −$704,932.00 |
| long | 7 Jan 2025 102,236.00 | 8 Jan 2025 96,954.60 | 1 | −$528,100.00 |
| short | 10 Jan 2025 92,552.50 | 16 Jan 2025 100,497.00 | 6 | −$794,486.00 |
| long | 16 Jan 2025 100,497.00 | 29 Jan 2025 101,336.00 | 13 | $83,817.00 |
| long | 30 Jan 2025 103,733.00 | 1 Feb 2025 102,430.00 | 2 | −$130,369.00 |
| short | 3 Feb 2025 97,700.60 | 25 Mar 2025 87,498.20 | 50 | $1,020,240.00 |
| long | 25 Mar 2025 87,498.20 | 29 Mar 2025 84,424.40 | 4 | −$307,378.00 |
| short | 31 Mar 2025 82,390.00 | 13 Apr 2025 85,276.90 | 13 | −$288,691.00 |
| long | 13 Apr 2025 85,276.90 | 14 Apr 2025 83,760.00 | 1 | −$151,691.00 |
| long | 18 Apr 2025 84,947.90 | 30 May 2025 105,590.00 | 42 | $2,064,180.00 |
| short | 31 May 2025 103,985.00 | 10 Jun 2025 110,263.00 | 10 | −$627,755.00 |
| long | 10 Jun 2025 110,263.00 | 13 Jun 2025 105,672.00 | 3 | −$459,128.00 |
| short | 21 Jun 2025 103,298.00 | 26 Jun 2025 107,341.00 | 5 | −$404,261.00 |
| long | 26 Jun 2025 107,341.00 | 2 Jul 2025 105,681.00 | 6 | −$165,946.00 |
| long | 3 Jul 2025 108,850.00 | 30 Jul 2025 117,951.00 | 27 | $910,116.00 |
| short | 1 Aug 2025 115,764.00 | 11 Aug 2025 119,294.00 | 10 | −$353,020.00 |
| long | 11 Aug 2025 119,294.00 | 16 Aug 2025 117,342.00 | 5 | −$195,223.00 |
| short | 20 Aug 2025 112,873.00 | 11 Sep 2025 113,960.00 | 22 | −$108,705.00 |
| long | 11 Sep 2025 113,960.00 | 23 Sep 2025 112,651.00 | 12 | −$130,901.00 |
| short | 23 Sep 2025 112,651.00 | 2 Oct 2025 118,595.00 | 9 | −$594,400.00 |
| long | 2 Oct 2025 118,595.00 | 11 Oct 2025 112,774.00 | 9 | −$582,050.00 |
| short | 11 Oct 2025 112,774.00 | 4 Dec 2025 93,429.90 | 54 | $1,934,450.00 |
| long | 4 Dec 2025 93,429.90 | 6 Dec 2025 89,330.00 | 2 | −$409,991.00 |
| long | 10 Dec 2025 92,678.80 | 13 Dec 2025 90,268.40 | 3 | −$241,038.00 |
| short | 15 Dec 2025 88,172.20 | 3 Jan 2026 89,995.10 | 19 | −$182,298.00 |
| long | 3 Jan 2026 89,995.10 | 21 Jan 2026 88,427.70 | 18 | −$156,748.00 |
| short | 21 Jan 2026 88,427.70 | 3 Mar 2026 68,830.10 | 41 | $1,959,760.00 |
| long | 3 Mar 2026 68,830.10 | 7 Mar 2026 68,114.00 | 4 | −$71,604.00 |
| long | 11 Mar 2026 69,948.60 | 20 Mar 2026 69,930.00 | 9 | −$1,863.00 |
| short | 23 Mar 2026 67,859.00 | 8 Apr 2026 71,924.20 | 16 | −$406,522.00 |
| long | 8 Apr 2026 71,924.20 | 30 Apr 2026 75,780.00 | 22 | $385,578.00 |
| long | 2 May 2026 78,231.10 | 16 May 2026 79,113.20 | 14 | $88,207.00 |
| short | 17 May 2026 78,148.10 | 6 Jul 2026 63,650.00 | 50 | $1,449,800.00 |
| long | 6 Jul 2026 63,650.00 | 25 Jul 2026 64,140.00 | 19 | $48,999.00 |
| long | 27 Jul 2026 65,400.00 | 28 Jul 2026 63,755.90 | 1 | −$164,414.00 |
| short | 1 Aug 2026 62,887.90 | 18 Aug 2026 64,532.10 | 17 | −$164,423.00 |
| long | 18 Aug 2026 64,532.10 | 9 Sep 2026 78,455.80 | 22 | $1,392,370.00 |
| short | 11 Sep 2026 76,568.70 | 19 Sep 2026 80,883.90 | 8 | −$431,513.00 |
| long | 19 Sep 2026 80,883.90 | 8 Oct 2026 83,321.80 | 19 | $243,795.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 20 high-low this page | — | v4 strategy | +2,790.1% | 59 | 2,731.6% |
| 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 | ||
| Moving Average Cross Strategy early Closing by Geduldtrader | sma | v4 strategy | +73.8% | 20 | 151.5% |
| MFI Simple Strategy by OztheWoz | sma | v5 strategy | −0.2% | 28 | 3.2% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 8 ms | Yes 1,000 bars in 21 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/// © AlanAntony//@version=4strategy("ma 20 high-low",overlay=true)//compute the indicatorssmaH = sma(high, 20)smaL = sma(low, 20)//plot the indicatorsplot(smaH,title="smaHigh", color=color.green, linewidth=2)plot(smaL,title="smaLow", color=color.red, linewidth=2)//trading logicenterlong = crossover(close,smaH) //positive ema crossoverexitlong = crossunder(close,0.99*smaH) //exiting longentershort = crossunder(close,smaL) //negative EMA Crossoverexitshort = crossover(close,1.01*smaH) //exiting shortsnotintrade = strategy.position_size<=0bgcolor(notintrade ? color.red:color.green)//execution logicstart = timestamp(2015,6,1,0,0)//end = timestamp(2022,6,1,0,0)if time >= start strategy.entry( "long", strategy.long,100, when = enterlong) strategy.entry( "short", strategy.short,100, when = entershort) strategy.close("long", when = exitlong) strategy.close("short", when = exitshort)//if time >= end // strategy.close_all()