Pine Script v4 · Strategy
ChBrkOutStrategySMA
A Pine Script v4 strategy built on SMA (close, 9), Highest (high, 7) and Lowest (low, 7). 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. 1 setting; defaults include Length 7.
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 ChBrkOutStrategySMA repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 12 of 12 trades.
- How did ChBrkOutStrategySMA do on BTCUSDT 1D with default settings?
- −4.53%
- From 26 Jan 2024 to 21 Oct 2026, over 42 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can ChBrkOutStrategySMA 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 ChBrkOutStrategySMA use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does ChBrkOutStrategySMA run in Zeal Terminal?
- Yes
- It compiles in 10 ms and runs 1,000 bars in 32 ms, in the browser.
BTCUSDT · 1D · default settings
- Upper Bound
- Lower Bound
- Fast SMA
- Slow SMA
- Avg
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −4.53%
- −$9,059.84
- Buy and hold
- +102.5%
- same period
- Closed trades
- 42
- 12 won · 30 lost
- Profitable
- 28.6%
- of closed trades
- Profit factor
- 0.94
- gross profit ÷ gross loss
- Max drawdown
- 20.88%
- −$44,562.30
One run with the author's default settings, $200,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
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 31, 33. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 17
transp = on line 17. v5 folds the transparency into the colour with color.new(c, transp).
-
Note
Uses long =, which v5 renamed line 21
long = on line 21, 24. 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 sma → ta.sma, highest → ta.highest, lowest → ta.lowest, avg → math.avg, crossunder → ta.crossunder, crossover → ta.crossover.
- 2 strategy calls use the when = argument (line 31, 33). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 42 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 5: input(type = input.integer) → input.int()
- Line 7: sma → ta.sma
- Line 8: sma → ta.sma
- Line 9: highest → ta.highest
- Line 10: lowest → ta.lowest
6 more edits
- Line 17: transp = 95 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 18: avg → math.avg
- Line 21: long = true → direction = strategy.long
- Line 24: long = false → direction = strategy.short
- Line 26: crossunder → ta.crossunder
- Line 27: crossover → ta.crossover
⋯// © anshuanshu333-//@version=4+//@version=5strategy("ChBrkOutStrategySMA", overlay=true, initial_capital = 200000)-length = input(title="Length", type=input.integer, minval=1, maxval=1000, defval=7)+length = input.int(title="Length", minval=1, maxval=1000, defval=7) -fastSMA = sma(close,9)+fastSMA = ta.sma(close,9)-slowSMA = sma(close,21)+slowSMA = ta.sma(close,21)-upBound = highest(high, length)+upBound = ta.highest(high, length)-downBound = lowest(low, length)+downBound = ta.lowest(low, length)boundLongEntry = ((close >= upBound) or (high >= upBound)) and fastSMA>slowSMA and (close > open)⋯plot(slowSMA,title = "Slow SMA" ,color = color.green, linewidth =1)-fill(u,l, transp=95)+fill(u,l)-plot(avg(upBound,downBound), title = "Avg", color=color.gray,linewidth =1)+plot(math.avg(upBound,downBound), title = "Avg", color=color.gray,linewidth =1) if (boundLongEntry )- strategy.entry("LE", long = true)+ strategy.entry("LE", direction = strategy.long) if (boundShortEntry)- strategy.entry("SE", long = false)+ strategy.entry("SE", direction = strategy.short) -SmaLongExit = crossunder(fastSMA,slowSMA)+SmaLongExit = ta.crossunder(fastSMA,slowSMA)-SmaShortExit = crossover(fastSMA,slowSMA)+SmaShortExit = ta.crossover(fastSMA,slowSMA) ⋯Same 42 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 31: when = SmaLongExit → if block
- Line 33: when = SmaShortExit → if block
⋯if (strategy.position_size > 0)- strategy.close(id="LE",when= SmaLongExit)+ if SmaLongExit+ strategy.close(id="LE")if (strategy.position_size < 0)- strategy.close(id="SE",when= SmaShortExit)+ if SmaShortExit+ strategy.close(id="SE")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: 42 before, 52 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.
⋯// © anshuanshu333-//@version=5+//@version=6strategy("ChBrkOutStrategySMA", overlay=true, initial_capital = 200000)⋯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 |
|---|---|---|---|
| Length | 7 | int (written input.integer) · min 1 · max 1000 | upBound downBound |
What it calculates
- SMA (
sma,ta.smain v5)close, 9fastSMA · line 7 - SMA (
sma,ta.smain v5)close, 21slowSMA · line 8 - Highest (
highest,ta.highestin v5)high, Length (7)upBound · line 9 - Lowest (
lowest,ta.lowestin v5)low, Length (7)downBound · line 10 - Crossunder (
crossunder,ta.crossunderin v5)fastSMA, slowSMASmaLongExit · line 26 - Crossover (
crossover,ta.crossoverin v5)fastSMA, slowSMASmaShortExit · line 27
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 LE long | (boundLongEntry ) | On | 21 |
| Open SE short | (boundShortEntry) | On | 24 |
| Close LE | SmaLongExit | On | 31 |
| Close SE | SmaShortExit | On | 33 |
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 | −4.5% | +102.5% | 42 | 29% | 0.94 | 20.9% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.1% | +5.5% | 38 | 42% | 1.01 | 7.1% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.1% | +9.8% | 37 | 43% | 0.96 | 1.5% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.1% | +8.4% | 44 | 25% | 1.21 | 0.2% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.0% | +16.7% | 38 | 45% | 0.82 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 40 | 28% | 1.04 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.3% | +148.3% | 36 | 36% | 1.93 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 41 | 34% | 1.14 | 0.1% | 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 | 13 | 15% | −$36,805.80 |
| Sideways market | 19 | 42% | $34,862.60 |
| Falling market | 10 | 20% | −$7,116.69 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
34.3%
The 95th percentile of the worst drawdown when the same 42 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 17.0% to 34.3%; the tick is what actually happened, 17.2%. Order luck alone moves it this much.
All 42 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 16 Feb 2024 51,880.00 | 23 Mar 2024 63,796.60 | 36 | $11,916.60 |
| long | 9 Apr 2024 71,620.00 | 14 Apr 2024 63,924.50 | 5 | −$7,695.48 |
| short | 14 Apr 2024 63,924.50 | 17 May 2024 65,235.20 | 33 | −$1,310.69 |
| long | 18 May 2024 67,024.00 | 15 Jun 2024 66,044.00 | 28 | −$980.01 |
| short | 15 Jun 2024 66,044.00 | 18 Jul 2024 64,088.00 | 33 | $1,956.00 |
| long | 20 Jul 2024 66,660.00 | 5 Aug 2024 58,161.00 | 16 | −$8,499.01 |
| short | 5 Aug 2024 58,161.00 | 23 Aug 2024 60,375.80 | 18 | −$2,214.83 |
| long | 24 Aug 2024 64,037.20 | 3 Sep 2024 59,132.10 | 10 | −$4,905.12 |
| short | 7 Sep 2024 53,963.00 | 17 Sep 2024 58,214.00 | 10 | −$4,251.02 |
| long | 18 Sep 2024 60,314.00 | 7 Oct 2024 62,819.90 | 19 | $2,505.92 |
| short | 11 Oct 2024 60,326.40 | 17 Oct 2024 67,620.00 | 6 | −$7,293.60 |
| long | 17 Oct 2024 67,620.00 | 25 Dec 2024 98,663.60 | 69 | $31,043.60 |
| short | 31 Dec 2024 92,792.10 | 8 Jan 2025 96,954.60 | 8 | −$4,162.55 |
| short | 14 Jan 2025 94,536.10 | 18 Jan 2025 104,077.00 | 4 | −$9,541.36 |
| long | 18 Jan 2025 104,077.00 | 4 Feb 2025 101,329.00 | 17 | −$2,748.96 |
| short | 19 Feb 2025 95,671.70 | 26 Mar 2025 87,392.90 | 35 | $8,278.86 |
| short | 7 Apr 2025 78,430.00 | 18 Apr 2025 84,947.90 | 11 | −$6,517.92 |
| long | 22 Apr 2025 87,516.20 | 4 Jun 2025 105,377.00 | 43 | $17,860.70 |
| short | 6 Jun 2025 101,509.00 | 15 Jun 2025 105,415.00 | 9 | −$3,905.94 |
| short | 21 Jun 2025 103,298.00 | 1 Jul 2025 107,147.00 | 10 | −$3,848.53 |
| long | 3 Jul 2025 108,850.00 | 1 Aug 2025 115,764.00 | 29 | $6,914.48 |
| short | 2 Aug 2025 113,298.00 | 13 Aug 2025 120,134.00 | 11 | −$6,836.17 |
| long | 14 Aug 2025 123,306.00 | 23 Aug 2025 116,936.00 | 9 | −$6,370.44 |
| short | 25 Aug 2025 113,494.00 | 12 Sep 2025 115,483.00 | 18 | −$1,989.10 |
| long | 12 Sep 2025 115,483.00 | 27 Sep 2025 109,643.00 | 15 | −$5,839.23 |
| long | 6 Oct 2025 123,482.00 | 16 Oct 2025 110,763.00 | 10 | −$12,719.00 |
| short | 18 Oct 2025 106,432.00 | 31 Oct 2025 108,323.00 | 13 | −$1,891.19 |
| short | 5 Nov 2025 101,497.00 | 5 Dec 2025 92,078.10 | 30 | $9,419.17 |
| long | 10 Dec 2025 92,678.80 | 17 Dec 2025 87,863.40 | 7 | −$4,815.38 |
| short | 19 Dec 2025 85,516.40 | 3 Jan 2026 89,995.10 | 15 | −$4,478.73 |
| long | 3 Jan 2026 89,995.10 | 24 Jan 2026 89,600.30 | 21 | −$394.88 |
| short | 26 Jan 2026 86,670.40 | 6 Mar 2026 70,891.00 | 39 | $15,779.30 |
| long | 14 Mar 2026 70,930.00 | 27 Mar 2026 68,820.30 | 13 | −$2,109.70 |
| short | 28 Mar 2026 66,407.30 | 9 Apr 2026 71,069.90 | 12 | −$4,662.65 |
| long | 10 Apr 2026 71,788.00 | 20 May 2026 76,834.40 | 40 | $5,046.38 |
| short | 23 May 2026 75,539.50 | 22 Jun 2026 63,312.00 | 30 | $12,227.50 |
| short | 26 Jun 2026 59,794.60 | 9 Jul 2026 62,290.00 | 13 | −$2,495.37 |
| long | 15 Jul 2026 65,044.00 | 1 Aug 2026 62,887.90 | 17 | −$2,156.11 |
| short | 1 Aug 2026 62,887.90 | 12 Aug 2026 63,600.00 | 11 | −$712.13 |
| long | 20 Aug 2026 69,334.80 | 13 Sep 2026 77,278.70 | 24 | $7,943.95 |
| short | 16 Sep 2026 75,644.50 | 22 Sep 2026 86,620.00 | 6 | −$10,975.50 |
| long | 22 Sep 2026 86,620.00 | 11 Oct 2026 82,988.40 | 19 | −$3,631.66 |
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 |
|---|---|---|---|---|---|
| ChBrkOutStrategySMA this page | — | v4 strategy | −4.5% | 42 | 20.9% |
| KDJ Strategy @ionvolution by ionvolution | sma highest lowest | v5 strategy | No trades on this market | ||
| Trading range display with Box by potatoshop | sma highest lowest | v5 strategy | +0.7% | 27 | 3.9% |
| Open Drive by Marcns_ | highest lowest sma | v5 strategy | No trades on this market | ||
| Trend Bounce [racer8] by racer8 | highest lowest sma | v4 strategy | +2.4% | 22 | 32.2% |
| Lawyers Trend Pro Strategy by ErdemDemir | sma lowest highest | v4 strategy | +12.1% | 102 | 41.7% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 10 ms | With a workaround 1,000 bars in 32 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 21, 24) 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/// © anshuanshu333//@version=4strategy("ChBrkOutStrategySMA", overlay=true, initial_capital = 200000)length = input(title="Length", type=input.integer, minval=1, maxval=1000, defval=7) fastSMA = sma(close,9)slowSMA = sma(close,21)upBound = highest(high, length)downBound = lowest(low, length)boundLongEntry = ((close >= upBound) or (high >= upBound)) and fastSMA>slowSMA and (close > open)boundShortEntry =((close <= downBound) or (low <= downBound)) and fastSMA<slowSMA and (close <open)u=plot(upBound, title = "Upper Bound",color=color.blue, linewidth=1)l=plot(downBound, title = "Lower Bound",color=color.red, linewidth=1)plot(fastSMA,title = "Fast SMA", color = color.red, linewidth =2)plot(slowSMA,title = "Slow SMA" ,color = color.green, linewidth =1)fill(u,l, transp=95)plot(avg(upBound,downBound), title = "Avg", color=color.gray,linewidth =1) if (boundLongEntry ) strategy.entry("LE", long = true) if (boundShortEntry) strategy.entry("SE", long = false) SmaLongExit = crossunder(fastSMA,slowSMA)SmaShortExit = crossover(fastSMA,slowSMA) //Close TRades if (strategy.position_size > 0) strategy.close(id="LE",when= SmaLongExit)if (strategy.position_size < 0) strategy.close(id="SE",when= SmaShortExit)