Pine Script v4 · Strategy
BB21_MA200_Strategy
A Pine Script v4 strategy built on EMA (close, 200), SMA (close, 21) and Stdev (close, 21). It has 4 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 100 in cash and pays no commission. 7 settings; defaults include MA Length 200, BB Length 21 and BB Source close.
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 BB21_MA200_Strategy repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did BB21_MA200_Strategy do on BTCUSDT 1D with default settings?
- Not measured
- It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings; it opened a long position that was still open on the last bar, so there is no result to report. On ETHUSDT 1D it did trade; that row of the markets table below has its figures. A backtest, not investment advice.
- Can BB21_MA200_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 BB21_MA200_Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does BB21_MA200_Strategy run in Zeal Terminal?
- Yes
- It compiles in 15 ms and runs 1,000 bars in 69 ms, in the browser.
BTCUSDT · 1D · default settings
- SMA 200
- Basis
- Upper
- Lower
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- 0.00%
- $0.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 0
- 0 won · 0 lost
- Profitable
- 0.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 16.68%
- −$1,668.08
One run with the author's default settings, $10,000 starting capital, commission as the script sets it. A long position was still open at the last bar. 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 32, 36, 40, 41. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 26
transp = on line 26. v5 folds the transparency into the colour with color.new(c, transp).
-
Note
Uses long =, which v5 renamed line 32
long = on line 32. 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 ema → ta.ema, sma → ta.sma, stdev → ta.stdev, tostring → str.tostring, crossover → ta.crossover, abs → math.abs, crossunder → ta.crossunder.
- 4 strategy calls use the when = argument (line 32, 36, 40, 41). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 1 trades and the same P&L on ETHUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 14: ema → ta.ema
- Line 17: sma → ta.sma
- Line 18: stdev → ta.stdev
- Line 21: input(type = input.integer) → input.int()
- Line 26: transp = 95 → color.new(color.teal, 95)
13 more edits
- Line 32: tostring → str.tostring
- Line 32: tostring → str.tostring
- Line 32: tostring → str.tostring
- Line 32: long = true → direction = strategy.long
- Line 32: crossover → ta.crossover
- Line 36: tostring → str.tostring
- Line 36: abs → math.abs
- Line 36: crossunder → ta.crossunder
- Line 40: tostring → str.tostring
- Line 40: abs → math.abs
- Line 41: tostring → str.tostring
- Line 41: abs → math.abs
- Line 41: crossunder → ta.crossunder
⋯// © mohanee-//@version=4+//@version=5strategy(title="BB9_MA200_Strategy", overlay=true, pyramiding=0, initial_capital=10000, default_qty_type=strategy.cash, default_qty_value=100, currency=currency.USD) //default_qty_value=10, default_qty_type=strategy.fixed, ⋯riskCapital = input(title="Risk % of capital == Based on this trade size is claculated numberOfShares = (AvailableCapital*risk/100) / stopLossPoints", defval=10, minval=1)-sma200=ema(close,smaLength)+sma200=ta.ema(close,smaLength)plot(sma200, title="SMA 200", color=color.orange)//bollinger calculation-basis = sma(bbsrc, bbLength)+basis = ta.sma(bbsrc, bbLength)-dev = mult * stdev(bbsrc, bbLength)+dev = mult * ta.stdev(bbsrc, bbLength)upperBand = basis + devlowerBand = basis - dev-offset = input(0, "Offset", type = input.integer, minval = -500, maxval = 500)+offset = input.int(0, "Offset", minval = -500, maxval = 500)//plot bb⋯p2 = plot(lowerBand, "Lower", color=color.teal, offset = offset)-fill(p1, p2, title = "Background", color=color.teal, transp=95)+fill(p1, p2, title = "Background", color=color.new(color.teal, 95))//Echeck how many units can be purchased based on risk manage ment and stop loss⋯//strategy.risk.max_position_size(100)-strategy.entry(id="LE", comment="LE equity="+tostring(strategy.equity ,"######.##")+" risk = " +tostring(strategy.equity * riskCapital * 0.01, "####") + " SL = " +tostring(close * stopLoss * 0.01, "####.##"), qty=qty1 , long=true, when=strategy.position_size<1 and upperBand>sma200 and lowerBand > sma200 and crossover(close, basis) ) // // aroonOsc<0 //(strategy.initial_capital * 0.10)/close+strategy.entry(id="LE", comment="LE equity="+str.tostring(strategy.equity ,"######.##")+" risk = " +str.tostring(strategy.equity * riskCapital * 0.01, "####") + " SL = " +str.tostring(close * stopLoss * 0.01, "####.##"), qty=qty1 , direction = strategy.long, when=strategy.position_size<1 and upperBand>sma200 and lowerBand > sma200 and ta.crossover(close, basis) ) // // aroonOsc<0 //(strategy.initial_capital * 0.10)/closebarcolor(color=strategy.position_size>=1? color.blue: na)⋯tpVal=strategy.position_size>1 ? strategy.position_avg_price * (1+(stopLoss/100) ) : 0.00-strategy.close(id="LE", comment="Partial points="+tostring(close - strategy.position_avg_price, "####.##"), qty_percent=30 , when=abs(strategy.position_size)>=1 and close>tpVal and crossunder(lowerBand, sma200) ) //close<ema55 and rsi5Val<20 //ema34<ema55+strategy.close(id="LE", comment="Partial points="+str.tostring(close - strategy.position_avg_price, "####.##"), qty_percent=30 , when=math.abs(strategy.position_size)>=1 and close>tpVal and ta.crossunder(lowerBand, sma200) ) //close<ema55 and rsi5Val<20 //ema34<ema55//close All on stop loss⋯stopLossVal:= strategy.position_size>1 ? strategy.position_avg_price * (1-(stopLoss/100) ) : 0.00-strategy.close_all( comment="SL Exit points="+tostring(close - strategy.position_avg_price, "####.##"), when=abs(strategy.position_size)>=1 and close < stopLossVal ) //close<ema55 and rsi5Val<20 //ema34<ema55 //close<ema89//+strategy.close_all( comment="SL Exit points="+str.tostring(close - strategy.position_avg_price, "####.##"), when=math.abs(strategy.position_size)>=1 and close < stopLossVal ) //close<ema55 and rsi5Val<20 //ema34<ema55 //close<ema89//-strategy.close_all( comment="BB9 X SMA200 points="+tostring(close - strategy.position_avg_price, "####.##"), when=abs(strategy.position_size)>=1 and crossunder(basis, sma200) ) //close<ema55 and rsi5Val<20 //ema34<ema55 //close<ema89+strategy.close_all( comment="BB9 X SMA200 points="+str.tostring(close - strategy.position_avg_price, "####.##"), when=math.abs(strategy.position_size)>=1 and ta.crossunder(basis, sma200) ) //close<ema55 and rsi5Val<20 //ema34<ema55 //close<ema89Same 1 trades and the same P&L on ETHUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 8: input() → input.int()
- Line 9: input() → input.int()
- Line 10: input() → input.source()
- Line 11: input() → input.float()
- Line 12: input() → input.int()
- Line 13: input() → input.int()
4 more edits
- Line 32: when = strategy.position_size<1 and upperBand>sma200 and lowerBand > sma200 and ta.crossover(close, basis) → if block
- Line 36: when = math.abs(strategy.position_size)>=1 and close>tpVal and ta.crossunder(lowerBand, sma200) → if block
- Line 40: when = math.abs(strategy.position_size)>=1 and close < stopLossVal → if block
- Line 41: when = math.abs(strategy.position_size)>=1 and ta.crossunder(basis, sma200) → if block
⋯//variables BEGIN-smaLength=input(200,title="MA Length")+smaLength=input.int(200,title="MA Length")-bbLength=input(21,title="BB Length") +bbLength=input.int(21,title="BB Length") -bbsrc = input(close, title="BB Source")+bbsrc = input.source(close, title="BB Source")-mult = input(2.0, minval=0.001, maxval=50, title="StdDev")+mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")-stopLoss = input(title="Stop Loss%", defval=5, minval=1)+stopLoss = input.int(title="Stop Loss%", defval=5, minval=1)-riskCapital = input(title="Risk % of capital == Based on this trade size is claculated numberOfShares = (AvailableCapital*risk/100) / stopLossPoints", defval=10, minval=1)+riskCapital = input.int(title="Risk % of capital == Based on this trade size is claculated numberOfShares = (AvailableCapital*risk/100) / stopLossPoints", defval=10, minval=1)sma200=ta.ema(close,smaLength)⋯//strategy.risk.max_position_size(100)-strategy.entry(id="LE", comment="LE equity="+str.tostring(strategy.equity ,"######.##")+" risk = " +str.tostring(strategy.equity * riskCapital * 0.01, "####") + " SL = " +str.tostring(close * stopLoss * 0.01, "####.##"), qty=qty1 , direction = strategy.long, when=strategy.position_size<1 and upperBand>sma200 and lowerBand > sma200 and ta.crossover(close, basis) ) // // aroonOsc<0 //(strategy.initial_capital * 0.10)/close+if strategy.position_size<1 and upperBand>sma200 and lowerBand > sma200 and ta.crossover(close, basis)+ strategy.entry(id="LE", comment="LE equity="+str.tostring(strategy.equity ,"######.##")+" risk = " +str.tostring(strategy.equity * riskCapital * 0.01, "####") + " SL = " +str.tostring(close * stopLoss * 0.01, "####.##"), qty=qty1 , direction = strategy.long ) // // aroonOsc<0 //(strategy.initial_capital * 0.10)/closebarcolor(color=strategy.position_size>=1? color.blue: na)⋯tpVal=strategy.position_size>1 ? strategy.position_avg_price * (1+(stopLoss/100) ) : 0.00-strategy.close(id="LE", comment="Partial points="+str.tostring(close - strategy.position_avg_price, "####.##"), qty_percent=30 , when=math.abs(strategy.position_size)>=1 and close>tpVal and ta.crossunder(lowerBand, sma200) ) //close<ema55 and rsi5Val<20 //ema34<ema55+if math.abs(strategy.position_size)>=1 and close>tpVal and ta.crossunder(lowerBand, sma200)+ strategy.close(id="LE", comment="Partial points="+str.tostring(close - strategy.position_avg_price, "####.##"), qty_percent=30 ) //close<ema55 and rsi5Val<20 //ema34<ema55//close All on stop loss⋯stopLossVal:= strategy.position_size>1 ? strategy.position_avg_price * (1-(stopLoss/100) ) : 0.00-strategy.close_all( comment="SL Exit points="+str.tostring(close - strategy.position_avg_price, "####.##"), when=math.abs(strategy.position_size)>=1 and close < stopLossVal ) //close<ema55 and rsi5Val<20 //ema34<ema55 //close<ema89//+if math.abs(strategy.position_size)>=1 and close < stopLossVal+ strategy.close_all( comment="SL Exit points="+str.tostring(close - strategy.position_avg_price, "####.##") ) //close<ema55 and rsi5Val<20 //ema34<ema55 //close<ema89//-strategy.close_all( comment="BB9 X SMA200 points="+str.tostring(close - strategy.position_avg_price, "####.##"), when=math.abs(strategy.position_size)>=1 and ta.crossunder(basis, sma200) ) //close<ema55 and rsi5Val<20 //ema34<ema55 //close<ema89+if math.abs(strategy.position_size)>=1 and ta.crossunder(basis, sma200)+ strategy.close_all( comment="BB9 X SMA200 points="+str.tostring(close - strategy.position_avg_price, "####.##") ) //close<ema55 and rsi5Val<20 //ema34<ema55 //close<ema89Does 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: 1 before, 5 after on ETHUSDT 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.
⋯// © mohanee-//@version=5+//@version=6strategy(title="BB9_MA200_Strategy", overlay=true, pyramiding=0, initial_capital=10000, default_qty_type=strategy.cash, default_qty_value=100, currency=currency.USD) //default_qty_value=10, default_qty_type=strategy.fixed, ⋯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 |
|---|---|---|---|
| MA Length | 200 | int | sma200 |
| BB Length | 21 | int | basis dev |
| BB Source | close | source | basis dev |
| StdDev | 2.0 | float · min 0.001 · max 50 | dev |
| Stop Loss% | 5 | int · min 1 | stopLossVal qty1 tpVal entry LE |
| Risk % of capital == Based on this trade size is claculated numberOfShares = (AvailableCapital*risk/100) / stopLossPoints | 10 | int · min 1 | qty1 entry LE |
| Offset | 0 | int (written input.integer) · min -500 · max 500 | Upper Lower Basis |
What it calculates
- EMA (
ema,ta.emain v5)close, MA Length (200)sma200 · line 14 - SMA (
sma,ta.smain v5)BB Source (close), BB Length (21)basis · line 17 - Stdev (
stdev,ta.stdevin v5)BB Source (close), BB Length (21)dev · line 18
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 | strategy.position_size<1 and upperBand>sma200 and lowerBand > sma200 and crossover(close, basis) | On | 32 |
| Close LE | abs(strategy.position_size)>=1 and close>tpVal and crossunder(lowerBand, sma200) | On | 36 |
| Close | abs(strategy.position_size)>=1 and close < stopLossVal | On | 40 |
| Close | abs(strategy.position_size)>=1 and crossunder(basis, sma200) | On | 41 |
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.
- 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 | 0.0% | +102.5% | 0 | 0% | — | 16.7% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | 0.0% | +5.5% | 0 | 0% | — | 5.1% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −11.7% | +9.8% | 1 | 0% | 0.00 | 18.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +16.6% | +8.4% | 5 | 100% | — | 0.2% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −29.9% | +16.7% | 8 | 38% | 0.13 | 34.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −20.2% | +21.5% | 4 | 0% | 0.00 | 20.6% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +10.8% | +148.3% | 7 | 71% | 1.99 | 8.4% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +19.5% | +17.1% | 4 | 100% | — | 0.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 |
|---|---|---|---|---|---|
| BB21_MA200_Strategy this page | — | v4 strategy | 0.0% | 0 | 16.7% |
| Volume Weighted Bollinger Bands Strategy by Ankit_1618 | ema sma stdev | v4 strategy | +10.0% | 15 | 27.5% |
| Super Breakout day trading by beststockalert | stdev sma ema | v4 strategy | No trades on this market | ||
| Super Auto Breakout Day Trade Volatile stocks by beststockalert | stdev sma ema | v4 strategy | −6.1% | 51 | 29.1% |
| VFI strategy [based on VFI indicator published by UTS] by mohanee | sma stdev ema | v4 strategy | +181.4% | 8 | 154.2% |
| McClellan Oscillator Strategy by sparrow_hawk_737 | ema sma stdev | v3 strategy | No trades on this market | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 15 ms | With a workaround 1,000 bars in 69 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 32) 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/// © mohanee//@version=4strategy(title="BB9_MA200_Strategy", overlay=true, pyramiding=0, initial_capital=10000, default_qty_type=strategy.cash, default_qty_value=100, currency=currency.USD) //default_qty_value=10, default_qty_type=strategy.fixed, var stopLossVal=0.00//var qty1=0.00//variables BEGINsmaLength=input(200,title="MA Length")bbLength=input(21,title="BB Length") bbsrc = input(close, title="BB Source")mult = input(2.0, minval=0.001, maxval=50, title="StdDev")stopLoss = input(title="Stop Loss%", defval=5, minval=1)riskCapital = input(title="Risk % of capital == Based on this trade size is claculated numberOfShares = (AvailableCapital*risk/100) / stopLossPoints", defval=10, minval=1)sma200=ema(close,smaLength)plot(sma200, title="SMA 200", color=color.orange)//bollinger calculationbasis = sma(bbsrc, bbLength)dev = mult * stdev(bbsrc, bbLength)upperBand = basis + devlowerBand = basis - devoffset = input(0, "Offset", type = input.integer, minval = -500, maxval = 500)//plot bbplot(basis, "Basis", color=color.teal, style=plot.style_circles , offset = offset)p1 = plot(upperBand, "Upper", color=color.teal, offset = offset)p2 = plot(lowerBand, "Lower", color=color.teal, offset = offset)fill(p1, p2, title = "Background", color=color.teal, transp=95)//Echeck how many units can be purchased based on risk manage ment and stop lossqty1 = (strategy.equity * riskCapital / 100 ) / (close*stopLoss/100) //check if cash is sufficient to buy qty1 , if capital not available use the available capital onlyqty1:= (qty1 * close >= strategy.equity ) ? (strategy.equity / close) : qty1//strategy.risk.max_position_size(100)strategy.entry(id="LE", comment="LE equity="+tostring(strategy.equity ,"######.##")+" risk = " +tostring(strategy.equity * riskCapital * 0.01, "####") + " SL = " +tostring(close * stopLoss * 0.01, "####.##"), qty=qty1 , long=true, when=strategy.position_size<1 and upperBand>sma200 and lowerBand > sma200 and crossover(close, basis) ) // // aroonOsc<0 //(strategy.initial_capital * 0.10)/closebarcolor(color=strategy.position_size>=1? color.blue: na)//partial ExittpVal=strategy.position_size>1 ? strategy.position_avg_price * (1+(stopLoss/100) ) : 0.00strategy.close(id="LE", comment="Partial points="+tostring(close - strategy.position_avg_price, "####.##"), qty_percent=30 , when=abs(strategy.position_size)>=1 and close>tpVal and crossunder(lowerBand, sma200) ) //close<ema55 and rsi5Val<20 //ema34<ema55//close All on stop loss//stoplossstopLossVal:= strategy.position_size>1 ? strategy.position_avg_price * (1-(stopLoss/100) ) : 0.00strategy.close_all( comment="SL Exit points="+tostring(close - strategy.position_avg_price, "####.##"), when=abs(strategy.position_size)>=1 and close < stopLossVal ) //close<ema55 and rsi5Val<20 //ema34<ema55 //close<ema89//strategy.close_all( comment="BB9 X SMA200 points="+tostring(close - strategy.position_avg_price, "####.##"), when=abs(strategy.position_size)>=1 and crossunder(basis, sma200) ) //close<ema55 and rsi5Val<20 //ema34<ema55 //close<ema89