Pine Script v4 · Strategy
cATRpillar Strategy
A Pine Script v4 strategy built on ATR (4), SMA (close, 4) and EMA (close, lookback*2). 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 commits 100000% of equity and pays no commission. 5 settings; defaults include Periods 4, Range Multiplier .1 and Take Profit 300.
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 cATRpillar Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 7 of 7 trades.
- Can cATRpillar 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 cATRpillar Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 37, 42.
- Does cATRpillar Strategy run in Zeal Terminal?
- Yes
- It compiles in 11 ms and runs 1,000 bars in 41 ms, in the browser.
BTCUSDT · 1D · default settings
- Close
- High
- Low
- EMA
- Short
- Long
- Winning trade
- Losing trade
- Equity
- Buy and hold
The value of each line the script plots, on the last of 1,000 bars, with the default settings.
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.
-
Check
Every trade uses all the equity
default_qty_value is 100000% of equity.
-
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 35, 40. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 21
transp = on line 21, 22, 23, 24, 43, 44. v5 folds the transparency into the colour with color.new(c, transp).
-
Note
Uses long =, which v5 renamed line 35
long = on line 35, 40. v5 writes it as direction = strategy.long or strategy.short.
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 atr → ta.atr, sma → ta.sma, ema → ta.ema.
- 2 strategy calls use the when = argument (line 35, 40). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 27 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 7: input(type = input.integer) → input.int()
- Line 8: input(type = input.float) → input.float()
- Line 9: input(type = input.float) → input.float()
- Line 10: input(type = input.float) → input.float()
- Line 11: input(type = input.float) → input.float()
11 more edits
- Line 14: atr → ta.atr
- Line 15: sma → ta.sma
- Line 16: ema → ta.ema
- Line 21: transp = 80 → color.new(#26A69A, 80)
- Line 22: transp = 80 → color.new(color.fuchsia, 80)
- Line 23: transp = 80 → color.new(color.lime, 80)
- Line 24: transp = 80 → color.new(color.white, 80)
- Line 35: long = true → direction = strategy.long
- Line 40: long = false → direction = strategy.short
- Line 43: transp = 80 → color.new(color.fuchsia, 80)
- Line 44: transp = 80 → color.new(color.lime, 80)
⋯// © cwagoner78-//@version=4+//@version=5strategy("cATRpillar", overlay=true, default_qty_value=100000, default_qty_type=strategy.percent_of_equity, initial_capital=10000, currency=currency.USD)⋯//inputs-lookback = input(title="Periods", type=input.integer, defval=4)+lookback = input.int(title="Periods", defval=4)-atrMult = input(title="Range Multiplier", type=input.float, defval=.1)+atrMult = input.float(title="Range Multiplier", defval=.1)-takeProfit = input(title="Take Profit", type=input.float, defval=300)+takeProfit = input.float(title="Take Profit", defval=300)-stopLoss = input(title="Stop Loss", type=input.float, defval=100)+stopLoss = input.float(title="Stop Loss", defval=100)-lots = input(title="Lots to Trade", type=input.float, defval=1)+lots = input.float(title="Lots to Trade", defval=1)//------------//indicators-atr=atr(lookback)*atrMult+atr=ta.atr(lookback)*atrMult-sma=sma(close, lookback)+sma=ta.sma(close, lookback)-ema=ema(close,lookback*2)+ema=ta.ema(close,lookback*2)rangeLo=sma-atr⋯//draw objects-p0 =plot(close, title="Close", color=#26A69A, linewidth=0, transp=80,style=plot.style_stepline)+p0 =plot(close, title="Close", color=color.new(#26A69A, 80), linewidth=0,style=plot.style_stepline)-p1 =plot(rangeHi, title="High", color=color.fuchsia, linewidth=0, transp=80,style=plot.style_stepline)+p1 =plot(rangeHi, title="High", color=color.new(color.fuchsia, 80), linewidth=0,style=plot.style_stepline)-p2 =plot(rangeLo, title="Low", color=color.lime, linewidth=0, transp=80,style=plot.style_stepline)+p2 =plot(rangeLo, title="Low", color=color.new(color.lime, 80), linewidth=0,style=plot.style_stepline)-p3 =plot(ema, title="EMA", color=color.white, linewidth=0, transp=80, style=plot.style_stepline)+p3 =plot(ema, title="EMA", color=color.new(color.white, 80), linewidth=0, style=plot.style_stepline)fill(p1, p0, color=color.fuchsia)⋯longCondition=atrLong and open>ema+atr-strategy.entry(id="cATRpillar-Buy", long=true, when=longCondition)+strategy.entry(id="cATRpillar-Buy", direction = strategy.long, when=longCondition)longCloseCondition=exitLong⋯shortCondition=atrShort and open<ema-atr-strategy.entry(id="cATRpillar-Sell", long=false, when=shortCondition)+strategy.entry(id="cATRpillar-Sell", direction = strategy.short, when=shortCondition)shortCloseCondition=exitShortstrategy.exit(id="cATRpillar-Exit", qty=lots, profit=takeProfit, loss=stopLoss)-plotshape(shortCondition, title= "Short", location=location.belowbar, color=color.fuchsia, transp=80, style=shape.triangledown, size=size.tiny)+plotshape(shortCondition, title= "Short", location=location.belowbar, color=color.new(color.fuchsia, 80), style=shape.triangledown, size=size.tiny)-plotshape(longCondition, title= "Long", location=location.abovebar, color=color.lime, transp=80, style=shape.triangleup, size=size.tiny)+plotshape(longCondition, title= "Long", location=location.abovebar, color=color.new(color.lime, 80), style=shape.triangleup, size=size.tiny)//------------Same 27 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 35: when = longCondition → if block
- Line 40: when = shortCondition → if block
⋯longCondition=atrLong and open>ema+atr-strategy.entry(id="cATRpillar-Buy", direction = strategy.long, when=longCondition)+if longCondition+ strategy.entry(id="cATRpillar-Buy", direction = strategy.long)longCloseCondition=exitLong⋯shortCondition=atrShort and open<ema-atr-strategy.entry(id="cATRpillar-Sell", direction = strategy.short, when=shortCondition)+if shortCondition+ strategy.entry(id="cATRpillar-Sell", direction = strategy.short)shortCloseCondition=exitShort⋯Does it run in Pine Script v6?
No
The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L21:C61: 'linewidth' must be at least 1 in Pine Script v6. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © cwagoner78-//@version=5+//@version=6strategy("cATRpillar", overlay=true, default_qty_value=100000, default_qty_type=strategy.percent_of_equity, initial_capital=10000, currency=currency.USD)⋯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 |
|---|---|---|---|
| Periods | 4 | int (written input.integer) | atr sma ema |
| Range Multiplier | .1 | float | atr |
| Take Profit | 300 | float | exit cATRpillar-Exit |
| Stop Loss | 100 | float | exit cATRpillar-Exit |
| Lots to Trade | 1 | float | exit cATRpillar-Exit |
What it calculates
- ATR (
atr,ta.atrin v5)Periods (4)atr · line 14 - SMA (
sma,ta.smain v5)close, Periods (4)sma · line 15 - EMA (
ema,ta.emain v5)close, lookback*2ema · line 16
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 cATRpillar-Buy long | longConditionatrLong and open>ema+atr | On | 35 |
| Exit cATRpillar-Exit stop and limit | every bar | On | 37 |
| Open cATRpillar-Sell short | shortConditionatrShort and open<ema-atr | On | 40 |
| Exit cATRpillar-Exit stop and limit | every bar | On | 42 |
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.02827e+24% | +102.5% | 27 | 78% | 1.55107e+22 | 88,097.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +821,548,000,000.0% | +5.5% | 23 | 57% | 5,716.39 | 21,909.1% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +8.83498e+38% | +9.8% | 43 | 74% | 1.08345e+38 | 58,175.5% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +11,409,700,000.0% | +8.4% | 17 | 65% | 18,139,400,000.00 | 34,565.6% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +534,400,999,999,999,967,232.0% | +16.7% | 23 | 70% | 2.70857e+21 | 209,250.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +136,623.0% | +21.5% | 7 | 57% | 3,595,350.00 | 35,626.5% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +531.2% | +148.3% | 2 | 50% | 11,548.00 | 335,614.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +1,351,590,000,000.0% | +17.1% | 19 | 53% | 4,460,710,000,000.00 | 44,206.1% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
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 with EOM and VORTEX by SoftKill21 | atr ema sma | v4 strategy | +66.2% | 22 | 29.9% |
| Optimized Zhaocaijinbao strategy by fzj20020403 | sma ema atr | v5 strategy | +15.6% | 12 | 9.8% |
| EMA & Supertrend by bhavikmota | ema sma atr | v4 strategy | +2,123.6% | 13 | 980.9% |
| Koala System EURUSD 15min by SoftKill21 | sma ema atr | v4 strategy | No trades on this market | ||
| Buy&Sell Bullish Engulfing - The Quant Science by thequantscience | sma ema atr | v5 strategy | No trades on this market | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 11 ms | With a workaround 1,000 bars in 41 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = true, long = false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 35, 40) so the engine trades the way the script says.
“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/// © cwagoner78//@version=4strategy("cATRpillar", overlay=true, default_qty_value=100000, default_qty_type=strategy.percent_of_equity, initial_capital=10000, currency=currency.USD)//------------//inputslookback = input(title="Periods", type=input.integer, defval=4)atrMult = input(title="Range Multiplier", type=input.float, defval=.1)takeProfit = input(title="Take Profit", type=input.float, defval=300)stopLoss = input(title="Stop Loss", type=input.float, defval=100)lots = input(title="Lots to Trade", type=input.float, defval=1)//------------//indicatorsatr=atr(lookback)*atrMultsma=sma(close, lookback)ema=ema(close,lookback*2)rangeLo=sma-atrrangeHi=sma+atr//------------//draw objectsp0 =plot(close, title="Close", color=#26A69A, linewidth=0, transp=80,style=plot.style_stepline)p1 =plot(rangeHi, title="High", color=color.fuchsia, linewidth=0, transp=80,style=plot.style_stepline)p2 =plot(rangeLo, title="Low", color=color.lime, linewidth=0, transp=80,style=plot.style_stepline)p3 =plot(ema, title="EMA", color=color.white, linewidth=0, transp=80, style=plot.style_stepline)fill(p1, p0, color=color.fuchsia)fill(p0, p2, color=color.lime)//------------//TradingatrShort=open[1] > rangeHi and open < rangeLoatrLong=open[1] < rangeLo and open > rangeHiexitLong=open>rangeLoexitShort=open<rangeHi//LonglongCondition=atrLong and open>ema+atrstrategy.entry(id="cATRpillar-Buy", long=true, when=longCondition)longCloseCondition=exitLongstrategy.exit(id="cATRpillar-Exit", qty=lots, profit=takeProfit, loss=stopLoss)//ShortshortCondition=atrShort and open<ema-atrstrategy.entry(id="cATRpillar-Sell", long=false, when=shortCondition)shortCloseCondition=exitShortstrategy.exit(id="cATRpillar-Exit", qty=lots, profit=takeProfit, loss=stopLoss)plotshape(shortCondition, title= "Short", location=location.belowbar, color=color.fuchsia, transp=80, style=shape.triangledown, size=size.tiny)plotshape(longCondition, title= "Long", location=location.abovebar, color=color.lime, transp=80, style=shape.triangleup, size=size.tiny)//------------