Pine Script v5 · Strategy
Contrarian Scalping Counter Trend Bb Envelope Adx and Stochastic
A Pine Script v5 strategy built on SMA (close, 20), Stdev (close, 20) and EMA or SMA (close, 20). 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. 10 settings; defaults include Length BB 20, Source close and StdDev BB 2.0.
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 Contrarian Scalping Counter Trend Bb Envelope Adx and Stochastic repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 4 of 4 trades.
- How did Contrarian Scalping Counter Trend Bb Envelope Adx and Stochastic do on BTCUSDT 1D with default settings?
- +1.85%
- 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 Contrarian Scalping Counter Trend Bb Envelope Adx and Stochastic 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, 19 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 Contrarian Scalping Counter Trend Bb Envelope Adx and Stochastic use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Contrarian Scalping Counter Trend Bb Envelope Adx and Stochastic run in Zeal Terminal?
- Yes
- It compiles in 18 ms and runs 1,000 bars in 49 ms, in the browser.
BTCUSDT · 1D · default settings
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +1.85%
- $18,521.90
- Buy and hold
- +102.5%
- same period
- Closed trades
- 15
- 12 won · 3 lost
- Profitable
- 80.0%
- of closed trades
- Profit factor
- 1.64
- gross profit ÷ gross loss
- Max drawdown
- 3.25%
- −$32,781.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
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
2 settings have no label
In the settings dialog percent (line 15), exponential (line 16) 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 46, 47, 48, 49. 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 |
|---|---|---|---|
| Length BB | 20 | int · min 1 | basis dev |
| Source | close | source | basis dev envelope |
| StdDev BB | 2.0 | float · min 0.001 · max 50 | dev |
| Length Envelope | 20 | int · min 1 | envelope truerange plus minus |
| Percent (no label in the script) | 1.0 | float | k |
| Exponential (no label in the script) | false | bool | envelope |
| ADX Smoothing | 14 | int | adx sig |
| DI Length | 14 | int | plus minus sig |
| %K Length | 50 | int · min 1 | stock |
| %K Smoothing | 20 | int · min 1 | stock |
What it calculates
- SMASource (close), Length BB (20)
basis · line 9 - StdevSource (close), Length BB (20)
dev · line 10 - EMA / SMASource (close), Length Envelope (20)
envelope · line 17 - Changehigh
up · line 25 - Changelow
down · line 26 - RMAta.tr, Length Envelope (20)
truerange · line 29 - RMAmath.abs(plus - minus) / (sum == 0 ? 1 : sum), ADX Smoothing (14)
adx · line 36 - SMA / Stochta.stoch(close, high, low, periodK), %K Smoothing (20)
stock · line 41
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 short short | shortclose> upper and close >upper_env and sig < 30 and stock > 50 | On | 46 |
| Close short | short_exitclose < lower or close<lower_env or stock <50 | On | 47 |
| Open long long | longclose< lower and close <lower_env and sig < 30 and stock < 50 | On | 48 |
| Close long | long_exitclose > lower or close>lower_env or stock >50 | On | 49 |
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.9% | +102.5% | 15 | 80% | 1.64 | 3.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.6% | +5.5% | 21 | 71% | 0.65 | 1.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.1% | +9.8% | 14 | 50% | 0.76 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.0% | +8.4% | 22 | 59% | 0.85 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.0% | +16.7% | 14 | 43% | 0.40 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.0% | +21.5% | 21 | 52% | 0.38 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.0% | +148.3% | 20 | 60% | 0.55 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 17 | 100% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
Settings heatmap
Two settings that feed the entry signal, moved around their defaults. The best in-sample pair (%K Length 85, %K Smoothing 28) made +2.6% before 26 Dec 2025 and +0.3% after it, against +0.3% for the defaults. 100% of the 64 combinations made money in-sample and 61% out-of-sample.
| %K Smoothing ↓ · %K Length → | 25 | 30 | 35 | 43 | 50 | 60 | 70 | 85 |
|---|---|---|---|---|---|---|---|---|
| 10 | +2% | +2% | +2% | +2% | +1% | +2% | +1% | +2% |
| 12 | +1% | +2% | +2% | +2% | +1% | +1% | +2% | +2% |
| 14 | +0% | +1% | +2% | +1% | +1% | +1% | +2% | +2% |
| 17 | +1% | +2% | +2% | +1% | +1% | +2% | +1% | +2% |
| 20 | +1% | +1% | +1% | +1% | +2% | +2% | +1% | +2% |
| 24 | +1% | +1% | +1% | +1% | +2% | +2% | +2% | +3% |
| 28 | +2% | +1% | +2% | +2% | +2% | +2% | +2% | +3% |
| 34 | +2% | +2% | +1% | +2% | +1% | +1% | +1% | +3% |
| %K Smoothing ↓ · %K Length → | 25 | 30 | 35 | 43 | 50 | 60 | 70 | 85 |
|---|---|---|---|---|---|---|---|---|
| 10 | +1% | +1% | +1% | −1% | −1% | +0% | −0% | +0% |
| 12 | +1% | +1% | +1% | −0% | −1% | +0% | −0% | +0% |
| 14 | +1% | +1% | +1% | −0% | −1% | −0% | −0% | +0% |
| 17 | +1% | +1% | +0% | −0% | +0% | −0% | −0% | +0% |
| 20 | +1% | +1% | −0% | −1% | +0% | −0% | −0% | +0% |
| 24 | +1% | +1% | −0% | +0% | +0% | −0% | +0% | +0% |
| 28 | +1% | −0% | +0% | +0% | −0% | −0% | +0% | +0% |
| 34 | +0% | +0% | +0% | −0% | −0% | −0% | +0% | +0% |
Net P&L of trades opened in each period, as a share of starting capital. marks the defaults and the best in-sample cell. Hover a cell for its trade count. 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 | 80% | −$7,194.42 |
| Sideways market | 7 | 86% | $20,102.20 |
| Falling market | 3 | 67% | $5,614.07 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
2.9%
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.2% to 2.9%; the tick is what actually happened, 2.2%. Order luck alone moves it this much.
All 15 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 2 May 2024 58,365.00 | 4 May 2024 62,882.00 | 2 | $4,517.04 |
| short | 30 Oct 2024 72,736.40 | 5 Nov 2024 67,850.00 | 6 | $4,886.40 |
| short | 7 Nov 2024 75,572.00 | 20 Dec 2024 97,461.90 | 43 | −$21,889.90 |
| short | 7 Jan 2025 102,236.00 | 9 Jan 2025 95,060.60 | 2 | $7,174.99 |
| long | 7 Apr 2025 78,430.00 | 10 Apr 2025 82,615.20 | 3 | $4,185.22 |
| short | 11 Jul 2025 116,010.00 | 1 Aug 2025 115,764.00 | 21 | $245.94 |
| short | 14 Aug 2025 123,306.00 | 20 Aug 2025 112,873.00 | 6 | $10,433.50 |
| long | 26 Sep 2025 108,994.00 | 27 Sep 2025 109,643.00 | 1 | $648.97 |
| long | 5 Nov 2025 101,497.00 | 6 Nov 2025 103,885.00 | 1 | $2,387.93 |
| long | 7 Nov 2025 101,346.00 | 8 Nov 2025 103,339.00 | 1 | $1,993.05 |
| long | 16 Dec 2025 86,432.10 | 17 Dec 2025 87,863.40 | 1 | $1,431.35 |
| short | 14 Jan 2026 95,414.00 | 21 Jan 2026 88,427.70 | 7 | $6,986.33 |
| long | 1 Feb 2026 78,741.10 | 3 Feb 2026 78,738.60 | 2 | −$2.50 |
| short | 6 May 2026 80,905.50 | 17 May 2026 78,148.10 | 11 | $2,757.48 |
| short | 20 Aug 2026 69,334.80 | 11 Sep 2026 76,568.70 | 22 | −$7,233.95 |
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 |
|---|---|---|---|---|---|
| Contrarian Scalping Counter Trend Bb Envelope Adx and Stochastic this page | — | v5 strategy | +1.9% | 15 | 3.2% |
| Best TradingView Strategy - For NASDAQ and DOW30 and other Index by The_Bigger_Bull | sma stdev rma ema | v5 strategy | +2.2% | 17 | 3.4% |
| Webhook Starter Kit [HullBuster] by tradingtudi | ema stoch sma stdev | v4 strategy | does not run in Zeal Terminal | ||
| Strategy Myth-Busting #3 - BB_BUY+SuperTrend - [MYN] by myncrypto | ema sma stdev rma | v5 strategy | −115.1% | 6 | 123.3% |
| Volume Weighted Bollinger Bands Strategy by Ankit_1618 | ema sma stdev | v4 strategy | +10.0% | 15 | 27.5% |
| BB21_MA200_Strategy by mohanee | ema sma stdev | v4 strategy | No trades on this market | ||
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 46, 47, 48, 49). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- 5 settings use the untyped input() (line 7, 15, 16, 22, 23). 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 7: input() → input.source()
- Line 15: input() → input.float()
- Line 16: input() → input.bool()
- Line 22: input() → input.int()
- Line 23: input() → input.int()
- Line 46: when = short → if block
3 more edits
- Line 47: when = short_exit → if block
- Line 48: when = long → if block
- Line 49: when = long_exit → if block
⋯length = input.int(20, minval=1, title="Length BB")-src = input(close, title="Source")+src = input.source(close, title="Source")mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev BB")⋯len = input.int(20, title="Length Envelope", minval=1)-percent = input(1.0)+percent = input.float(1.0)-exponential = input(false)+exponential = input.bool(false)envelope = exponential ? ta.ema(src, len) : ta.sma(src, len)⋯//adx-adxlen = input(14, title="ADX Smoothing")+adxlen = input.int(14, title="ADX Smoothing")-dilen = input(14, title="DI Length")+dilen = input.int(14, title="DI Length")dirmov(len) =>⋯long_exit=close > lower or close>lower_env or stock >50-strategy.entry("short",strategy.short,when=short)+if short+ strategy.entry("short",strategy.short)-strategy.close("short",when=short_exit)+if short_exit+ strategy.close("short")-strategy.entry("long",strategy.long,when=long)+if long+ strategy.entry("long",strategy.long)-strategy.close('long',when=long_exit)+if long_exit+ strategy.close('long')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, 19 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.
⋯// © exlux99-//@version=5+//@version=6strategy("Contrarian Scalping Counter Trend",overlay=true)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 18 ms | Yes 1,000 bars in 49 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/// © exlux99//@version=5strategy("Contrarian Scalping Counter Trend",overlay=true)//bollinger bandslength = input.int(20, minval=1, title="Length BB")src = input(close, title="Source")mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev BB")basis = ta.sma(src, length)dev = mult * ta.stdev(src, length)upper = basis + devlower = basis - dev//envelopelen = input.int(20, title="Length Envelope", minval=1)percent = input(1.0)exponential = input(false)envelope = exponential ? ta.ema(src, len) : ta.sma(src, len)k = percent/100.0upper_env = envelope * (1 + k)lower_env = envelope * (1 - k)//adxadxlen = input(14, title="ADX Smoothing")dilen = input(14, title="DI Length")dirmov(len) => up = ta.change(high) down = -ta.change(low) plusDM = na(up) ? na : (up > down and up > 0 ? up : 0) minusDM = na(down) ? na : (down > up and down > 0 ? down : 0) truerange = ta.rma(ta.tr, len) plus = fixnan(100 * ta.rma(plusDM, len) / truerange) minus = fixnan(100 * ta.rma(minusDM, len) / truerange) [plus, minus]adx(dilen, adxlen) => [plus, minus] = dirmov(dilen) sum = plus + minus adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)sig = adx(dilen, adxlen)//stochasticperiodK = input.int(50, title="%K Length", minval=1)smoothK = input.int(20, title="%K Smoothing", minval=1)stock = ta.sma(ta.stoch(close, high, low, periodK), smoothK)short=close> upper and close >upper_env and sig < 30 and stock > 50long=close< lower and close <lower_env and sig < 30 and stock < 50short_exit= close < lower or close<lower_env or stock <50long_exit=close > lower or close>lower_env or stock >50strategy.entry("short",strategy.short,when=short)strategy.close("short",when=short_exit)strategy.entry("long",strategy.long,when=long)strategy.close('long',when=long_exit)