Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (10)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$1,018,520$2,025,273Equity from $1,000,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. The script plots nothing itself; the lines and the lower pane are its own calculations, drawn for reference.
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.

SettingDefaultType and rangeFeeds
Length BB20int · min 1basis dev
Sourceclosesourcebasis dev envelope
StdDev BB2.0float · min 0.001 · max 50dev
Length Envelope20int · min 1envelope truerange plus minus
Percent (no label in the script)1.0floatk
Exponential (no label in the script)falseboolenvelope
ADX Smoothing14intadx sig
DI Length14intplus minus sig
%K Length50int · min 1stock
%K Smoothing20int · min 1stock

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
  • Changehighup · line 25
  • Changelowdown · 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.

closecloseprice datalengthLength BBsetting · 20multStdDev BBsetting · 2.0lenLength Envelopesetting · 20highhighprice datalowlowprice datahigh · low · closehigh · low · closeprice dataadxlenADX Smoothingsetting · 14periodK%K Lengthsetting · 50smoothK%K Smoothingsetting · 20percentPercentsetting · 1.0dilenDI Lengthsetting · 14basisSMA→ basisdevStdev→ devenvelopeEMA / SMA→ envelopeupChange→ updownChange→ downtruerangeRMA→ truerangeadxRMA→ adxstockSMA / Stoch→ stockupper, lowerupper · lower2 valueskkvalueupper_env, lower_envupper_env · lower_env2 valuesplusDM, minusDMplusDM · minusDM2 valuesplus, minusplus · minus2 valuessumsumvaluesigsigvalueshortshortconditionlonglongconditionshort_exitshort_exitconditionlong_exitlong_exitconditionentry:shortOpen shortstrategy.entryclose:shortClose shortstrategy.closeentry:longOpen longstrategy.entryclose:longClose longstrategy.close

Order rules

OrderFires whenBy defaultLine
Open short shortshortclose> upper and close >upper_env and sig < 30 and stock > 50On46
Close shortshort_exitclose < lower or close<lower_env or stock <50On47
Open long longlongclose< lower and close <lower_env and sig < 30 and stock < 50On48
Close longlong_exitclose > lower or close>lower_env or stock >50On49

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.

4/4trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+1.9%+102.5%1580%1.643.2%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−0.6%+5.5%2171%0.651.4%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−0.1%+9.8%1450%0.760.2%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−0.0%+8.4%2259%0.850.0%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−0.0%+16.7%1443%0.400.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h−0.0%+21.5%2152%0.380.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−0.0%+148.3%2060%0.550.1%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+0.0%+17.1%17100%—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.

Before 26 Dec 2025 · in-sample
%K Smoothing ↓ · %K Length →2530354350607085
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%
From 26 Dec 2025 · out-of-sample
%K Smoothing ↓ · %K Length →2530354350607085
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 barsTradesWonP&L
Rising market580%−$7,194.42
Sideways market786%$20,102.20
Falling market367%$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.

0%3%
All 15 trades
SideEntryExitBarsP&L
long2 May 2024 58,365.004 May 2024 62,882.002$4,517.04
short30 Oct 2024 72,736.405 Nov 2024 67,850.006$4,886.40
short7 Nov 2024 75,572.0020 Dec 2024 97,461.9043−$21,889.90
short7 Jan 2025 102,236.009 Jan 2025 95,060.602$7,174.99
long7 Apr 2025 78,430.0010 Apr 2025 82,615.203$4,185.22
short11 Jul 2025 116,010.001 Aug 2025 115,764.0021$245.94
short14 Aug 2025 123,306.0020 Aug 2025 112,873.006$10,433.50
long26 Sep 2025 108,994.0027 Sep 2025 109,643.001$648.97
long5 Nov 2025 101,497.006 Nov 2025 103,885.001$2,387.93
long7 Nov 2025 101,346.008 Nov 2025 103,339.001$1,993.05
long16 Dec 2025 86,432.1017 Dec 2025 87,863.401$1,431.35
short14 Jan 2026 95,414.0021 Jan 2026 88,427.707$6,986.33
long1 Feb 2026 78,741.103 Feb 2026 78,738.602−$2.50
short6 May 2026 80,905.5017 May 2026 78,148.1011$2,757.48
short20 Aug 2026 69,334.8011 Sep 2026 76,568.7022−$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.

ScriptSharesTypeNetTradesMax drawdown
Contrarian Scalping Counter Trend Bb Envelope Adx and Stochastic this page—v5 strategy+1.9%153.2%
Best TradingView Strategy - For NASDAQ and DOW30 and other Index by The_Bigger_Bullsma stdev rma emav5 strategy+2.2%173.4%
Webhook Starter Kit [HullBuster] by tradingtudiema stoch sma stdevv4 strategydoes not run in Zeal Terminal
Strategy Myth-Busting #3 - BB_BUY+SuperTrend - [MYN] by myncryptoema sma stdev rmav5 strategy−115.1%6123.3%
Volume Weighted Bollinger Bands Strategy by Ankit_1618ema sma stdevv4 strategy+10.0%1527.5%
BB21_MA200_Strategy by mohaneeema sma stdevv4 strategyNo 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.

Clean v5

VerifiedDownload the v5 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

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 18 msYes 1,000 bars in 49 msNot measured
PineTSNot testedNot testedNot 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)