Pine Script v4 · Strategy
MA Strength Strategy
A Pine Script v4 strategy built on SMA or EMA or HMA or RMA or VWMA or WMA (source, length), Crossunder (upwardSum, indexSma) and Crossover (upwardSum, indexSma). 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 commits 100% of equity and pays 0.01% commission. 10 settings; defaults include Moving Average Type ema, Lookback Period 10 and Moving Average Type hma.
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 Strength 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 MA Strength Strategy do on BTCUSDT 1D with default settings?
- +71.34%
- From 26 Jan 2024 to 21 Oct 2026, over 13 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can MA Strength Strategy 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 Strength Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does MA Strength Strategy run in Zeal Terminal?
- Yes
- It compiles in 29 ms and runs 1,000 bars in 131 ms, in the browser.
BTCUSDT · 1D · default settings
- Moving Average Strength
- Strength MA
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +71.34%
- $14,268.70
- Buy and hold
- +102.5%
- same period
- Closed trades
- 13
- 5 won · 8 lost
- Profitable
- 38.5%
- of closed trades
- Profit factor
- 2.16
- gross profit ÷ gross loss
- Max drawdown
- 32.07%
- −$12,746.70
One run with the author's default settings, $20,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
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
5 settings have no label
In the settings dialog LookbackPeriod (line 6), IndexMAPeriod (line 8), considerTrendDirection (line 9), considerTrendDirectionForExit (line 10), offset (line 11) show an empty name. The labels on this page come from the variable names.
-
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 63, 64, 65, 66. 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, ema → ta.ema, hma → ta.hma, rma → ta.rma, vwma → ta.vwma, wma → ta.wma, sum → math.sum, crossover → ta.crossover, crossunder → ta.crossunder.
- 4 strategy calls use the when = argument (line 63, 64, 65, 66). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 13 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 13: input(type = input.time) → input.time()
- Line 14: input(type = input.time) → input.time()
- Line 17: sma → ta.sma
- Line 19: ema → ta.ema
- Line 21: hma → ta.hma
8 more edits
- Line 23: rma → ta.rma
- Line 25: vwma → ta.vwma
- Line 27: wma → ta.wma
- Line 54: sum → math.sum
- Line 58: crossover → ta.crossover
- Line 59: crossunder → ta.crossunder
- Line 60: crossunder → ta.crossunder
- Line 61: crossover → ta.crossover
⋯// © HeWhoMustNotBeNamed-//@version=4+//@version=5strategy("MA Strength Strategy", overlay=false, initial_capital = 20000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01)⋯tradeDirection = input(title="Trade Direction", defval=strategy.direction.long, options=[strategy.direction.all, strategy.direction.long, strategy.direction.short])-i_startTime = input(defval = timestamp("01 Jan 2010 00:00 +0000"), title = "Start Time", type = input.time)+i_startTime = input.time(defval = timestamp("01 Jan 2010 00:00 +0000"), title = "Start Time")-i_endTime = input(defval = timestamp("01 Jan 2099 00:00 +0000"), title = "End Time", type = input.time)+i_endTime = input.time(defval = timestamp("01 Jan 2099 00:00 +0000"), title = "End Time")inDateRange = time >= i_startTime and time <= i_endTimef_getMovingAverage(source, MAType, length)=>- ma = sma(source, length)+ ma = ta.sma(source, length) if(MAType == "ema")- ma := ema(source,length)+ ma := ta.ema(source,length) if(MAType == "hma")- ma := hma(source,length)+ ma := ta.hma(source,length) if(MAType == "rma")- ma := rma(source,length)+ ma := ta.rma(source,length) if(MAType == "vwma")- ma := vwma(source,length)+ ma := ta.vwma(source,length) if(MAType == "wma")- ma := wma(source,length)+ ma := ta.wma(source,length) ma⋯[trendStrength, upwardScore] = f_getMaAlignment(MAType, includePartiallyAligned)-upwardSum = sum(upwardScore, LookbackPeriod)+upwardSum = math.sum(upwardScore, LookbackPeriod)indexSma = f_getMovingAverage(upwardSum,IndexMAType,IndexMAPeriod)⋯plot(indexSma, title="Strength MA", color=color.red, linewidth=1, style=plot.style_linebr)-buyCondition = crossover(upwardSum,indexSma) and (upwardSum > upwardSum[offset] or not considerTrendDirection) +buyCondition = ta.crossover(upwardSum,indexSma) and (upwardSum > upwardSum[offset] or not considerTrendDirection) -sellCondition = crossunder(upwardSum,indexSma) and (upwardSum < upwardSum[offset] or not considerTrendDirection)+sellCondition = ta.crossunder(upwardSum,indexSma) and (upwardSum < upwardSum[offset] or not considerTrendDirection)-exitBuyCondition = crossunder(upwardSum,indexSma)+exitBuyCondition = ta.crossunder(upwardSum,indexSma)-exitSellCondition = crossover(upwardSum,indexSma) +exitSellCondition = ta.crossover(upwardSum,indexSma) strategy.risk.allow_entry_in(tradeDirection)⋯Same 13 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 5: input() → input.string()
- Line 6: input() → input.int()
- Line 7: input() → input.string()
- Line 8: input() → input.int()
- Line 9: input() → input.bool()
- Line 10: input() → input.bool()
5 more edits
- Line 11: input() → input.int()
- Line 63: when = inDateRange and buyCondition → if block
- Line 64: when = considerTrendDirectionForExit? sellCondition : exitBuyCondition → if block
- Line 65: when = inDateRange and sellCondition → if block
- Line 66: when = considerTrendDirectionForExit? buyCondition : exitSellCondition → if block
⋯strategy("MA Strength Strategy", overlay=false, initial_capital = 20000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01)-MAType = input(title="Moving Average Type", defval="ema", options=["ema", "sma", "hma", "rma", "vwma", "wma"])+MAType = input.string(title="Moving Average Type", defval="ema", options=["ema", "sma", "hma", "rma", "vwma", "wma"])-LookbackPeriod = input(10, step=10)+LookbackPeriod = input.int(10, step=10)-IndexMAType = input(title="Moving Average Type", defval="hma", options=["ema", "sma", "hma", "rma", "vwma", "wma"])+IndexMAType = input.string(title="Moving Average Type", defval="hma", options=["ema", "sma", "hma", "rma", "vwma", "wma"])-IndexMAPeriod = input(200, step=10)+IndexMAPeriod = input.int(200, step=10)-considerTrendDirection = input(true)+considerTrendDirection = input.bool(true)-considerTrendDirectionForExit = input(true)+considerTrendDirectionForExit = input.bool(true)-offset = input(1, step=1)+offset = input.int(1, step=1)tradeDirection = input(title="Trade Direction", defval=strategy.direction.long, options=[strategy.direction.all, strategy.direction.long, strategy.direction.short])⋯strategy.risk.allow_entry_in(tradeDirection)-strategy.entry("Buy", strategy.long, when= inDateRange and buyCondition, oca_name="oca_buy", oca_type=strategy.oca.cancel)+if inDateRange and buyCondition+ strategy.entry("Buy", strategy.long, oca_name="oca_buy", oca_type=strategy.oca.cancel)-strategy.close("Buy", when = considerTrendDirectionForExit? sellCondition : exitBuyCondition)+if considerTrendDirectionForExit? sellCondition : exitBuyCondition+ strategy.close("Buy")-strategy.entry("Sell", strategy.short, when= inDateRange and sellCondition, oca_name="oca_sell", oca_type=strategy.oca.cancel)+if inDateRange and sellCondition+ strategy.entry("Sell", strategy.short, oca_name="oca_sell", oca_type=strategy.oca.cancel)-strategy.close( "Sell", when = considerTrendDirectionForExit? buyCondition : exitSellCondition)+if considerTrendDirectionForExit? buyCondition : exitSellCondition+ strategy.close( "Sell")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: 13 before, 13 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.
⋯// © HeWhoMustNotBeNamed-//@version=5+//@version=6strategy("MA Strength Strategy", overlay=false, initial_capital = 20000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01)⋯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 |
|---|---|---|---|
| Moving Average Type | ema | string · options ["ema", "sma", "hma", "rma", "vwma", "wma"] | ma5 ma10 ma20 ma30 ma50 ma100 ma200 upwardScore trendStrength line 18 line 20 line 22 line 24 line 26 |
| Lookback Period (no label in the script) | 10 | int · step 10 | upwardSum |
| Moving Average Type | hma | string · options ["ema", "sma", "hma", "rma", "vwma", "wma"] | indexSma |
| Index MAPeriod (no label in the script) | 200 | int · step 10 | indexSma |
| Consider Trend Direction (no label in the script) | true | bool | buyCondition sellCondition |
| Consider Trend Direction For Exit (no label in the script) | true | bool | close Buy close Sell |
| Offset (no label in the script) | 1 | int · step 1 | buyCondition sellCondition |
| Trade Direction | strategy.direction.long | value · options [strategy.direction.all, strategy.direction.long, strategy.direction.short] | line 62 |
| Start Time | timestamp("01 Jan 2010 00:00 +0000") | time | inDateRange |
| End Time | timestamp("01 Jan 2099 00:00 +0000") | time | inDateRange |
What it calculates
- SMA / EMA / HMA / RMA / VWMA / WMA (
sma,ta.smain v5)source, lengthma · line 17 - Crossunder (
crossunder,ta.crossunderin v5)upwardSum, indexSmaexitBuyCondition · line 60 - Crossover (
crossover,ta.crossoverin v5)upwardSum, indexSmaexitSellCondition · line 61
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 Buy long | inDateRange and buyCondition | On | 63 |
| Close Buy | considerTrendDirectionForExit? sellCondition : exitBuyCondition | On | 64 |
| Open Sell short | inDateRange and sellCondition | On | 65 |
| Close Sell | considerTrendDirectionForExit? buyCondition : exitSellCondition | On | 66 |
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 | +71.3% | +102.5% | 13 | 38% | 2.16 | 32.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +24.0% | +5.5% | 12 | 33% | 2.73 | 6.7% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +141.6% | +9.8% | 9 | 56% | 4.55 | 33.5% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +16.7% | +8.4% | 15 | 33% | 1.70 | 10.4% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +84.9% | +16.7% | 11 | 45% | 2.09 | 31.7% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +50.3% | +21.5% | 9 | 56% | 4.07 | 11.5% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −7.5% | +148.3% | 9 | 33% | 0.83 | 35.5% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +7.7% | +17.1% | 12 | 42% | 1.56 | 5.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 | 3 | 33% | $1,616.46 |
| Sideways market | 5 | 40% | $5,343.68 |
| Falling market | 5 | 40% | $7,308.60 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
27.1%
The 95th percentile of the worst drawdown when the same 13 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 13.2% to 27.1%; the tick is what actually happened, 26.7%. Order luck alone moves it this much.
All 13 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 19 Sep 2024 61,760.00 | 4 Dec 2024 95,849.70 | 76 | $11,033.20 |
| long | 25 Jan 2025 104,871.00 | 29 Jan 2025 101,336.00 | 4 | −$1,052.07 |
| long | 30 Jan 2025 103,733.00 | 1 Feb 2025 102,430.00 | 2 | −$382.71 |
| long | 15 Apr 2025 84,591.60 | 19 Jun 2025 104,887.00 | 65 | $7,093.91 |
| long | 3 Jul 2025 108,850.00 | 19 Jul 2025 117,925.00 | 16 | $3,051.25 |
| long | 18 Sep 2025 116,448.00 | 28 Sep 2025 109,636.00 | 10 | −$2,332.33 |
| long | 6 Oct 2025 123,482.00 | 19 Oct 2025 107,185.00 | 13 | −$4,944.07 |
| long | 31 Dec 2025 88,485.50 | 31 Jan 2026 84,260.50 | 31 | −$1,556.42 |
| long | 22 Feb 2026 67,975.90 | 25 Feb 2026 64,058.20 | 3 | −$1,787.36 |
| long | 26 Feb 2026 67,988.00 | 20 May 2026 76,834.40 | 83 | $3,782.83 |
| long | 11 Jul 2026 64,161.70 | 5 Aug 2026 64,106.60 | 25 | −$34.87 |
| long | 9 Aug 2026 64,962.60 | 18 Aug 2026 64,532.10 | 9 | −$224.36 |
| long | 22 Aug 2026 78,338.00 | 16 Oct 2026 82,245.90 | 55 | $1,621.74 |
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 Strength Strategy this page | — | v4 strategy | +71.3% | 13 | 32.1% |
| Smoothed Heikin Ashi Trend on Chart - TraderHalai BACKTEST by TraderHalai | sma ema hma vwma rma | v5 strategy | +1.3% | 113 | 5.6% |
| High-Low Channel Multi averages Crypto Swing strategy by exlux99 | sma ema wma rma vwma | v4 strategy | No trades on this market | ||
| Ultra Moving Average Rating Trend Strategy by exlux99 | ema wma sma rma vwma hma | v5 strategy | +2.8% | 5 | 2.4% |
| VCP pivot buy by perrycc007 | sma ema rma vwma wma | v4 strategy | does not run in Zeal Terminal | ||
| Multi Trend Cross Strategy Template by TradeAutomation | ema hma rma sma vwma wma | v5 strategy | +33.0% | 3 | 7.3% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 29 ms | Yes 1,000 bars in 131 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/// © HeWhoMustNotBeNamed//@version=4strategy("MA Strength Strategy", overlay=false, initial_capital = 20000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01)MAType = input(title="Moving Average Type", defval="ema", options=["ema", "sma", "hma", "rma", "vwma", "wma"])LookbackPeriod = input(10, step=10)IndexMAType = input(title="Moving Average Type", defval="hma", options=["ema", "sma", "hma", "rma", "vwma", "wma"])IndexMAPeriod = input(200, step=10)considerTrendDirection = input(true)considerTrendDirectionForExit = input(true)offset = input(1, step=1)tradeDirection = input(title="Trade Direction", defval=strategy.direction.long, options=[strategy.direction.all, strategy.direction.long, strategy.direction.short])i_startTime = input(defval = timestamp("01 Jan 2010 00:00 +0000"), title = "Start Time", type = input.time)i_endTime = input(defval = timestamp("01 Jan 2099 00:00 +0000"), title = "End Time", type = input.time)inDateRange = time >= i_startTime and time <= i_endTimef_getMovingAverage(source, MAType, length)=> ma = sma(source, length) if(MAType == "ema") ma := ema(source,length) if(MAType == "hma") ma := hma(source,length) if(MAType == "rma") ma := rma(source,length) if(MAType == "vwma") ma := vwma(source,length) if(MAType == "wma") ma := wma(source,length) ma f_getMaAlignment(MAType, includePartiallyAligned)=> ma5 = f_getMovingAverage(close,MAType,5) ma10 = f_getMovingAverage(close,MAType,10) ma20 = f_getMovingAverage(close,MAType,20) ma30 = f_getMovingAverage(close,MAType,30) ma50 = f_getMovingAverage(close,MAType,50) ma100 = f_getMovingAverage(close,MAType,100) ma200 = f_getMovingAverage(close,MAType,200) upwardScore = 0.0 upwardScore := close > ma5? upwardScore+1.10:upwardScore upwardScore := ma5 > ma10? upwardScore+1.10:upwardScore upwardScore := ma10 > ma20? upwardScore+1.10:upwardScore upwardScore := ma20 > ma30? upwardScore+1.10:upwardScore upwardScore := ma30 > ma50? upwardScore+1.15:upwardScore upwardScore := ma50 > ma100? upwardScore+1.20:upwardScore upwardScore := ma100 > ma200? upwardScore+1.25:upwardScore upwards = close > ma5 and ma5 > ma10 and ma10 > ma20 and ma20 > ma30 and ma30 > ma50 and ma50 > ma100 and ma100 > ma200 downwards = close < ma5 and ma5 < ma10 and ma10 < ma20 and ma20 < ma30 and ma30 < ma50 and ma50 < ma100 and ma100 < ma200 trendStrength = upwards?1:downwards?-1:includePartiallyAligned ? (upwardScore > 6? 0.5: upwardScore < 2?-0.5:upwardScore>4?0.25:-0.25) : 0 [trendStrength, upwardScore] includePartiallyAligned = true[trendStrength, upwardScore] = f_getMaAlignment(MAType, includePartiallyAligned)upwardSum = sum(upwardScore, LookbackPeriod)indexSma = f_getMovingAverage(upwardSum,IndexMAType,IndexMAPeriod)plot(upwardSum, title="Moving Average Strength", color=color.green, linewidth=2, style=plot.style_linebr)plot(indexSma, title="Strength MA", color=color.red, linewidth=1, style=plot.style_linebr)buyCondition = crossover(upwardSum,indexSma) and (upwardSum > upwardSum[offset] or not considerTrendDirection) sellCondition = crossunder(upwardSum,indexSma) and (upwardSum < upwardSum[offset] or not considerTrendDirection)exitBuyCondition = crossunder(upwardSum,indexSma)exitSellCondition = crossover(upwardSum,indexSma) strategy.risk.allow_entry_in(tradeDirection)strategy.entry("Buy", strategy.long, when= inDateRange and buyCondition, oca_name="oca_buy", oca_type=strategy.oca.cancel)strategy.close("Buy", when = considerTrendDirectionForExit? sellCondition : exitBuyCondition)strategy.entry("Sell", strategy.short, when= inDateRange and sellCondition, oca_name="oca_sell", oca_type=strategy.oca.cancel)strategy.close( "Sell", when = considerTrendDirectionForExit? buyCondition : exitSellCondition)