Pine Script v5 · Strategy
MA Candles Supertrend Strategy
A Pine Script v5 strategy built on SMA or EMA or HMA or RMA or VWMA or WMA (source, length). 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. 11 settings; defaults include Moving Average Type rma, Loopback Bars 20 and Moving Average Type rma.
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 Candles Supertrend Strategy repaint?
- Yes
- At least one pattern in the source can repaint: higher-timeframe data.
- How did MA Candles Supertrend Strategy do on BTCUSDT 1D with default settings?
- +67.74%
- From 26 Jan 2024 to 21 Oct 2026, over 6 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does MA Candles Supertrend Strategy run in Pine Script v6?
- Yes
- Same 6 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. The v6 script can be downloaded below.
- Does MA Candles Supertrend Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does MA Candles Supertrend Strategy run in Zeal Terminal?
- Yes
- It compiles in 25 ms and runs 1,000 bars in 98 ms, in the browser.
BTCUSDT · 1D · default settings
- TrailingStop
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +67.74%
- $13,548.60
- Buy and hold
- +102.5%
- same period
- Closed trades
- 6
- 3 won · 3 lost
- Profitable
- 50.0%
- of closed trades
- Profit factor
- 2.14
- gross profit ÷ gross loss
- Max drawdown
- 35.80%
- −$11,286.20
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
6 settings have no label
In the settings dialog LoopbackBars (line 6), AtrLength (line 8), AtrMult (line 9), wicks (line 11), dThreshold (line 12), rThreshold (line 13) 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 78, 79, 80, 81. 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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Moving Average Type | rma | string · options ['ema', 'sma', 'hma', 'rma', 'vwma', 'wma'] | oOpen oClose oHigh oLow line 21 line 24 line 27 line 30 line 33 |
| Loopback Bars (no label in the script) | 20 | int · step 10 | oOpen oClose oHigh oLow |
| Moving Average Type | rma | string · options ['ema', 'sma', 'hma', 'rma', 'vwma', 'wma'] | averagetruerange longStop shortStop dir |
| ATR Length (no label in the script) | 30 | int · step 10 | averagetruerange longStop shortStop dir |
| ATR Mult (no label in the script) | 1 | int | atr longStop shortStop dir |
| Wicks (no label in the script) | true | bool | longWicks shortWicks longStop shortStop dir |
| D Threshold (no label in the script) | 0.2 | float · max 1 · step 0.1 | yearlyHighCondition yearlyLowCondition |
| R Threshold (no label in the script) | 0.7 | float · max 1 · step 0.1 | yearlyHighCondition yearlyLowCondition |
| Trade Direction | strategy.direction.long | string · options [strategy.direction.all, strategy.direction.long, strategy.direction.short] | line 18 line 77 |
| 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 / WMAsource, length
ma · line 20
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 | longConditionexitShortCondition and yearlyHighCondition and inDateRange | On | 78 |
| Close Long | exitLongConditiondir == -1 and (dir[1] == -1 and close < close[1] or close < longStop) | On | 79 |
| Open Short short | shortConditionexitLongCondition and yearlyLowCondition and inDateRange | On | 80 |
| Close Short | exitShortConditiondir == 1 and (dir[1] == 1 and close > close[1] or close > shortStop) | On | 81 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
Yes
At least one pattern in the source can repaint; it is marked Check below.
- CheckHigher-timeframe data1 request.security call(s) use lookahead_on (line 38). Without a [1] offset this reads future bars in history.
- 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 | +67.7% | +102.5% | 6 | 50% | 2.14 | 35.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | 0.0% | +5.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +23.4% | +9.8% | 6 | 50% | 1.42 | 25.7% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −12.2% | +8.4% | 4 | 25% | 0.10 | 12.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −10.0% | +16.7% | 8 | 50% | 0.92 | 65.8% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | 0.0% | +21.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +171.6% | +148.3% | 4 | 75% | 4.18 | 22.7% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | 0.0% | +17.1% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
Higher-timeframe data for request.security(): BINANCE:BTCUSDT 12M (aggregated from Binance BTCUSDT 1M), fetched from Binance's public market data for each market.
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 | 1 | 0% | −$3,540.24 |
| Sideways market | 5 | 60% | $17,088.90 |
| Falling market | 0 | — | $0.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
34.5%
The 95th percentile of the worst drawdown when the same 6 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 20.0% to 34.5%; the tick is what actually happened, 29.0%. Order luck alone moves it this much.
All 6 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 28 Jan 2024 42,120.60 | 24 Apr 2024 66,414.00 | 87 | $11,528.80 |
| long | 25 May 2024 68,550.00 | 27 Jun 2024 60,865.00 | 33 | −$3,540.24 |
| long | 23 Jul 2024 67,532.00 | 6 Aug 2024 54,018.80 | 14 | −$5,605.01 |
| long | 27 Sep 2024 65,174.00 | 25 Feb 2025 91,552.90 | 151 | $9,053.37 |
| long | 28 Apr 2025 93,749.30 | 30 Aug 2025 108,377.00 | 124 | $4,897.98 |
| long | 5 Oct 2025 122,391.00 | 15 Oct 2025 113,028.00 | 10 | −$2,786.32 |
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 Candles Supertrend Strategy this page | — | v5 strategy | +67.7% | 6 | 35.8% |
| 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% |
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 strategy calls use the when = argument (line 78, 79, 80, 81). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- 2 settings use the untyped input() (line 9, 11). 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.
Same 6 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 9: input() → input.int()
- Line 11: input() → input.bool()
- Line 78: when = longCondition → if block
- Line 79: when = exitLongCondition → if block
- Line 80: when = shortCondition → if block
- Line 81: when = exitShortCondition → if block
⋯AtrLength = input.int(30, step=10)-AtrMult = input(1)+AtrMult = input.int(1)adoptiveWicks = false // does not work-wicks = input(true)+wicks = input.bool(true)dThreshold = input.float(0.2, step=0.1, maxval=1)⋯strategy.risk.allow_entry_in(tradeDirection)-strategy.entry('Long', strategy.long, when=longCondition, oca_name='oca_buy', oca_type=strategy.oca.cancel)+if longCondition+ strategy.entry('Long', strategy.long, oca_name='oca_buy', oca_type=strategy.oca.cancel)-strategy.close('Long', when=exitLongCondition)+if exitLongCondition+ strategy.close('Long')-strategy.entry('Short', strategy.short, when=shortCondition, oca_name='oca_sell', oca_type=strategy.oca.cancel)+if shortCondition+ strategy.entry('Short', strategy.short, oca_name='oca_sell', oca_type=strategy.oca.cancel)-strategy.close('Short', when=exitShortCondition)+if exitShortCondition+ strategy.close('Short')Does it run in Pine Script v6?
Yes
Same 6 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
⋯// © HeWhoMustNotBeNamed-//@version=5+//@version=6strategy('MA Candles Supertrend Strategy', shorttitle='MACSTS', overlay=true, 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)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 25 ms | With a workaround 1,000 bars in 98 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“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/// © HeWhoMustNotBeNamed//@version=5strategy('MA Candles Supertrend Strategy', shorttitle='MACSTS', overlay=true, 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.string(title='Moving Average Type', defval='rma', options=['ema', 'sma', 'hma', 'rma', 'vwma', 'wma'])LoopbackBars = input.int(20, step=10)AtrMAType = input.string(title='Moving Average Type', defval='rma', options=['ema', 'sma', 'hma', 'rma', 'vwma', 'wma'])AtrLength = input.int(30, step=10)AtrMult = input(1)adoptiveWicks = false // does not workwicks = input(true)dThreshold = input.float(0.2, step=0.1, maxval=1)rThreshold = input.float(0.7, step=0.1, maxval=1)tradeDirection = input.string(title='Trade Direction', defval=strategy.direction.long, options=[strategy.direction.all, strategy.direction.long, strategy.direction.short])i_startTime = input.time(defval=timestamp('01 Jan 2010 00:00 +0000'), title='Start Time')i_endTime = input.time(defval=timestamp('01 Jan 2099 00:00 +0000'), title='End Time')inDateRange = time >= i_startTime and time <= i_endTimestrategy.risk.allow_entry_in(tradeDirection)f_getMovingAverage(source, MAType, length) => ma = ta.sma(source, length) if MAType == 'ema' ma := ta.ema(source, length) ma if MAType == 'hma' ma := ta.hma(source, length) ma if MAType == 'rma' ma := ta.rma(source, length) ma if MAType == 'vwma' ma := ta.vwma(source, length) ma if MAType == 'wma' ma := ta.wma(source, length) ma maf_secureSecurity(_symbol, _res, _src, _offset) => request.security(_symbol, _res, _src[_offset], lookahead=barmerge.lookahead_on)f_getYearlyHighLowCondition() => yhighrange = f_secureSecurity(syminfo.tickerid, '12M', high, 1) ylowrange = f_secureSecurity(syminfo.tickerid, '12M', low, 1) yearlyHighCondition = close > yhighrange * (1 - dThreshold) or close > ylowrange * (1 + rThreshold) yearlyLowCondition = close < ylowrange * (1 + dThreshold) or close < yhighrange * (1 - rThreshold) [yearlyHighCondition, yearlyLowCondition]f_getSupertrend(oOpen, oClose, oHigh, oLow, AtrMAType, AtrLength, AtrMult, wicks) => truerange = math.max(oHigh, oClose[1]) - math.min(oLow, oClose[1]) averagetruerange = f_getMovingAverage(truerange, AtrMAType, AtrLength) atr = averagetruerange * AtrMult longWicks = adoptiveWicks and close < oClose or wicks shortWicks = adoptiveWicks and close > oClose or wicks longStop = oClose - atr longStopPrev = nz(longStop[1], longStop) longStop := (longWicks ? oLow[1] : oClose[1]) > longStopPrev ? math.max(longStop, longStopPrev) : longStop shortStop = oClose + atr shortStopPrev = nz(shortStop[1], shortStop) shortStop := (shortWicks ? oHigh[1] : oClose[1]) < shortStopPrev ? math.min(shortStop, shortStopPrev) : shortStop dir = 1 dir := nz(dir[1], dir) dir := dir == -1 and (longWicks ? oHigh : oClose) > shortStopPrev ? 1 : dir == 1 and (shortWicks[1] ? oLow : oClose) < longStopPrev ? -1 : dir [dir, longStop, shortStop]oOpen = f_getMovingAverage(open, MAType, LoopbackBars)oClose = f_getMovingAverage(close, MAType, LoopbackBars)oHigh = f_getMovingAverage(high, MAType, LoopbackBars)oLow = f_getMovingAverage(low, MAType, LoopbackBars)colorByPreviousClose = falsecandleColor = colorByPreviousClose ? oClose[1] < oClose ? color.green : oClose[1] > oClose ? color.red : color.silver : oOpen < oClose ? color.green : oOpen > oClose ? color.red : color.silverplotcandle(oOpen, oHigh, oLow, oClose, 'Oscilator Candles', color=candleColor)[yearlyHighCondition, yearlyLowCondition] = f_getYearlyHighLowCondition()[dir, longStop, shortStop] = f_getSupertrend(oOpen, oClose, oHigh, oLow, AtrMAType, AtrLength, AtrMult, wicks)trailingStop = dir == 1 ? longStop : shortStoptrendColor = dir == 1 ? color.green : color.redplot(trailingStop, title='TrailingStop', color=trendColor, linewidth=2, style=plot.style_linebr)exitLongCondition = dir == -1 and (dir[1] == -1 and close < close[1] or close < longStop)exitShortCondition = dir == 1 and (dir[1] == 1 and close > close[1] or close > shortStop)longCondition = exitShortCondition and yearlyHighCondition and inDateRangeshortCondition = exitLongCondition and yearlyLowCondition and inDateRangestrategy.risk.allow_entry_in(tradeDirection)strategy.entry('Long', strategy.long, when=longCondition, oca_name='oca_buy', oca_type=strategy.oca.cancel)strategy.close('Long', when=exitLongCondition)strategy.entry('Short', strategy.short, when=shortCondition, oca_name='oca_sell', oca_type=strategy.oca.cancel)strategy.close('Short', when=exitShortCondition)