Pine Script v5 · Strategy
ATR + STC + MA
A Pine Script v5 strategy built on EMA (BBB, 26), Lowest (BBBBBB, 12) and Highest (BBBBBB, 12). It has 6 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. 9 settings; defaults include Length 12, FastLength 26 and SlowLength 50.
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 ATR + STC + MA repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 3 of 3 trades.
- How did ATR + STC + MA do on BTCUSDT 1D with default settings?
- −1.18%
- From 26 Jan 2024 to 21 Oct 2026, over 15 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does ATR + STC + MA 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: 15 before, 17 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 ATR + STC + MA use a stop loss?
- Yes
- strategy.exit sets a stop on line 66, 71.
- Does ATR + STC + MA run in Zeal Terminal?
- Yes
- It compiles in 28 ms and runs 1,000 bars in 66 ms, in the browser.
BTCUSDT · 1D · default settings
- ATR Stop
- Moving Average
- STC Long Signal
- STC Short Signal
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −1.18%
- −$11,773.20
- Buy and hold
- +102.5%
- same period
- Closed trades
- 15
- 7 won · 8 lost
- Profitable
- 46.7%
- of closed trades
- Profit factor
- 0.80
- gross profit ÷ gross loss
- Max drawdown
- 3.27%
- −$32,993.00
One run with the author's default settings, $1,000,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
1 setting does nothing
“MA Length” is declared but never read.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
2 settings have no label
In the settings dialog nATRPeriod (line 39), nATRMultip (line 40) 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.
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 |
|---|---|---|---|
| STC | |||
| Length | 12 | int | CCC CCCC DDDD DDDDD mAAAAA |
| FastLength | 26 | int | fastMA BBBBBB mAAAAA |
| SlowLength | 50 | int | slowMA BBBBBB mAAAAA |
| ATR Stops | |||
| N ATRPeriod (no label in the script) | 5 | int | xATR |
| N ATRMultip (no label in the script) | 3.5 | float | nLoss |
| Moving Average | |||
| MA Length | 200 | int | unused |
| Strategy | |||
| Take Profit ATR Multiplier | 2 | int | exit Exit Long exit Exit Short |
| Stop Loss ATR Multiplier | 1 | int | atr_stop |
| Close position when ATR changes color | true | bool | close Long close Short line 67 line 72 |
What it calculates
- EMABBB, FastLength (26)
fastMA · line 10 - EMABBB, SlowLength (50)
slowMA · line 11 - LowestBBBBBB, Length (12)
CCC · line 23 - HighestBBBBBB, Length (12)
CCCC · line 24 - LowestDDD, Length (12)
DDDD · line 27 - HighestDDD, Length (12)
DDDDD · line 28 - ATRN ATRPeriod (5)
xATR · line 41 - SMAclose, 200
ma · line 51
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 | (longCondition) | On | 65 |
| Exit Exit Long stop and limit | (longCondition) | On | 66 |
| Close Long | atr_sig == false and early_stop | On | 68 |
| Open Short short | (shortCondition) | On | 70 |
| Exit Exit Short stop and limit | (shortCondition) | On | 71 |
| Close Short | atr_sig == true and early_stop | On | 73 |
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 | −1.2% | +102.5% | 15 | 47% | 0.80 | 3.3% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −1.3% | +5.5% | 12 | 25% | 0.24 | 1.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.1% | +9.8% | 11 | 45% | 0.68 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.0% | +8.4% | 16 | 56% | 0.96 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.0% | +16.7% | 11 | 45% | 0.98 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 12 | 42% | 1.03 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.0% | +148.3% | 19 | 63% | 1.08 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 21 | 67% | 1.36 | 0.0% | 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 | 5 | 60% | $1,518.57 |
| Sideways market | 6 | 50% | $5,012.66 |
| Falling market | 4 | 25% | −$18,304.40 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
4.4%
The 95th percentile of the worst drawdown when the same 15 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 2.0% to 4.4%; the tick is what actually happened, 3.1%. Order luck alone moves it this much.
All 15 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 5 Jun 2024 70,537.80 | 22 Jun 2024 64,143.60 | 17 | −$6,394.27 |
| short | 15 Aug 2024 58,683.40 | 6 Sep 2024 53,925.50 | 22 | $4,757.93 |
| long | 30 Oct 2024 72,736.40 | 11 Nov 2024 82,239.60 | 12 | $9,503.17 |
| long | 30 Nov 2024 97,460.00 | 31 Dec 2024 92,792.10 | 31 | −$4,667.95 |
| long | 21 Feb 2025 98,305.00 | 25 Feb 2025 91,155.00 | 4 | −$7,150.01 |
| short | 17 Mar 2025 82,574.50 | 22 Apr 2025 91,459.30 | 36 | −$8,884.75 |
| long | 10 Jun 2025 110,263.00 | 22 Jun 2025 100,213.00 | 12 | −$10,049.90 |
| long | 26 Jun 2025 107,341.00 | 10 Jul 2025 113,015.00 | 14 | $5,673.99 |
| long | 24 Jul 2025 118,756.00 | 14 Aug 2025 124,128.00 | 21 | $5,371.74 |
| short | 31 Oct 2025 108,323.00 | 4 Nov 2025 100,429.00 | 4 | $7,893.98 |
| short | 13 Nov 2025 101,654.00 | 16 Nov 2025 93,471.20 | 3 | $8,183.20 |
| short | 16 Dec 2025 86,432.10 | 14 Jan 2026 95,414.00 | 29 | −$8,981.91 |
| short | 25 Feb 2026 64,058.20 | 4 Mar 2026 72,679.10 | 7 | −$8,620.92 |
| short | 19 Jun 2026 62,958.00 | 22 Jul 2026 66,556.10 | 33 | −$3,598.14 |
| long | 19 Sep 2026 80,883.90 | 21 Sep 2026 86,074.50 | 2 | $5,190.67 |
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 |
|---|---|---|---|---|---|
| ATR + STC + MA this page | — | v5 strategy | −1.2% | 15 | 3.3% |
| -NoSKi Hammer-Star -w angle of ema Backtester - by noski1 | atr ema sma highest lowest | v5 strategy | does not run in Zeal Terminal | ||
| LONG SAZB $ by stevenz17 | ema sma atr lowest highest | v4 strategy | No trades on this market | ||
| MACD Strategy by maxencetajet | atr highest lowest ema sma | v5 strategy | +5.8% | 5 | 6.9% |
| DC Breakout Strategy - This is simplicity at its finest. by Robrecht99 | sma highest lowest atr | v5 strategy | +10.8% | 57 | 8.3% |
| Richard's Strategy by Deepak8369 | highest lowest atr ema | v4 strategy | +34.6% | 70 | 26.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.
- 9 settings use the untyped input() (line 6, 7, 8, 39, 40, 50, 55, 56, 57). 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 15 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 6: input() → input.int()
- Line 7: input() → input.int()
- Line 8: input() → input.int()
- Line 39: input() → input.int()
- Line 40: input() → input.float()
- Line 50: input() → input.int()
3 more edits
- Line 55: input() → input.int()
- Line 56: input() → input.int()
- Line 57: input() → input.bool()
⋯// STC-EEEEEE=input(12,"Length",group="STC")+EEEEEE=input.int(12,"Length",group="STC")-BBBB=input(26,"FastLength",group="STC")+BBBB=input.int(26,"FastLength",group="STC")-BBBBB=input(50,"SlowLength",group="STC")+BBBBB=input.int(50,"SlowLength",group="STC")AAAA(BBB, BBBB, BBBBB) =>⋯// ATR stops-nATRPeriod = input(5,group="ATR Stops")+nATRPeriod = input.int(5,group="ATR Stops")-nATRMultip = input(3.5,group="ATR Stops")+nATRMultip = input.float(3.5,group="ATR Stops")xATR = ta.atr(nATRPeriod)⋯// ma-ma_len = input(200, title="MA Length", group="Moving Average")+ma_len = input.int(200, title="MA Length", group="Moving Average")ma = ta.sma(close, 200)⋯// strategy entries-tp_mult = input(2, title="Take Profit ATR Multiplier", group="Strategy")+tp_mult = input.int(2, title="Take Profit ATR Multiplier", group="Strategy")-sl_mult = input(1, title="Stop Loss ATR Multiplier", group="Strategy")+sl_mult = input.int(1, title="Stop Loss ATR Multiplier", group="Strategy")-early_stop = input(true, title="Close position when ATR changes color")+early_stop = input.bool(true, title="Close position when ATR changes color")atr_stop = if close < xATRTrailingStop⋯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: 15 before, 17 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.
⋯// © Romedius-//@version=5+//@version=6strategy("My Strategy", overlay=true, margin_long=100, margin_short=100)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 28 ms | Yes 1,000 bars in 66 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/// © Romedius//@version=5strategy("My Strategy", overlay=true, margin_long=100, margin_short=100)// STCEEEEEE=input(12,"Length",group="STC")BBBB=input(26,"FastLength",group="STC")BBBBB=input(50,"SlowLength",group="STC")AAAA(BBB, BBBB, BBBBB) => fastMA = ta.ema(BBB, BBBB) slowMA = ta.ema(BBB, BBBBB) AAAA = fastMA - slowMA AAAA AAAAA(EEEEEE, BBBB, BBBBB) => //AAA=input(0.5) var AAA = 0.5 var CCCCC = 0.0 var DDD = 0.0 var DDDDDD = 0.0 var EEEEE = 0.0 BBBBBB = AAAA(close,BBBB,BBBBB) CCC = ta.lowest(BBBBBB, EEEEEE) CCCC = ta.highest(BBBBBB, EEEEEE) - CCC CCCCC := (CCCC > 0 ? ((BBBBBB - CCC) / CCCC) * 100 : nz(CCCCC[1])) DDD := (na(DDD[1]) ? CCCCC : DDD[1] + (AAA * (CCCCC - DDD[1]))) DDDD = ta.lowest(DDD, EEEEEE) DDDDD = ta.highest(DDD, EEEEEE) - DDDD DDDDDD := (DDDDD > 0 ? ((DDD - DDDD) / DDDDD) * 100 : nz(DDDDDD[1])) EEEEE := (na(EEEEE[1]) ? DDDDDD : EEEEE[1] + (AAA * (DDDDDD - EEEEE[1]))) EEEEEmAAAAA = AAAAA(EEEEEE,BBBB,BBBBB)stc = mAAAAA > mAAAAA[1] ? true : falsestc_sig = stc == true and stc[1] == false ? 1 : stc == false and stc[1] == true ? -1 : 0stc_long = stc_sig == 1stc_short = stc_sig == -1// STC end// ATR stopsnATRPeriod = input(5,group="ATR Stops")nATRMultip = input(3.5,group="ATR Stops")xATR = ta.atr(nATRPeriod)nLoss = nATRMultip * xATRxATRTrailingStop = 0.0xATRTrailingStop := close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), close - nLoss) : close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), close + nLoss) : close > nz(xATRTrailingStop[1], 0) ? close - nLoss : close + nLosspos = 0pos := close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0) ? 1 : close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)atr_sig = pos == -1 ? false : true// ATR stops end// mama_len = input(200, title="MA Length", group="Moving Average")ma = ta.sma(close, 200)ma_sig = close < ma ? false : true// ma end// strategy entriestp_mult = input(2, title="Take Profit ATR Multiplier", group="Strategy")sl_mult = input(1, title="Stop Loss ATR Multiplier", group="Strategy")early_stop = input(true, title="Close position when ATR changes color")atr_stop = if close < xATRTrailingStop close - (close - xATRTrailingStop) * sl_multelse close + (xATRTrailingStop - close) * sl_multlongCondition = atr_sig == true and stc_sig == 1 and ma_sig == trueshortCondition = atr_sig == false and stc_sig == -1 and ma_sig == falseif (longCondition) strategy.entry("Long", strategy.long) strategy.exit("Exit Long", "Long", limit=close + xATR * tp_mult, stop=atr_stop)else if atr_sig == false and early_stop strategy.close("Long")if (shortCondition) strategy.entry("Short", strategy.short) strategy.exit("Exit Short", "Short", limit=close - xATR * tp_mult, stop=atr_stop)else if atr_sig == true and early_stop strategy.close("Short") // plot stuffatr_color = pos == -1 ? color.red: pos == 1 ? color.green : color.blueplot(atr_stop, title="ATR Stop", color=atr_color)ma_color = ma_sig ? color.green : color.redplot(ma, title="Moving Average", color=ma_color)stc_color = stc_long ? color.green : color.redplotshape(stc_long, style=shape.triangleup, color=stc_color, title="STC Long Signal", size=size.tiny)plotshape(stc_short, style=shape.triangledown, color=stc_color, title="STC Short Signal", size=size.tiny)// plot stuff end