Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (7)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$12,211$20,253Equity from $10,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots. 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.
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

Verified

Same 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<ema89

Clean v5

VerifiedDownload the v5 script

Same 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<ema89

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: 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.

SettingDefaultType and rangeFeeds
MA Length200intsma200
BB Length21intbasis dev
BB Sourceclosesourcebasis dev
StdDev2.0float · min 0.001 · max 50dev
Stop Loss%5int · min 1stopLossVal qty1 tpVal entry LE
Risk % of capital == Based on this trade size is claculated numberOfShares = (AvailableCapital*risk/100) / stopLossPoints10int · min 1qty1 entry LE
Offset0int (written input.integer) · min -500 · max 500Upper Lower Basis

What it calculates

  • EMA (ema, ta.ema in v5)close, MA Length (200)sma200 · line 14
  • SMA (sma, ta.sma in v5)BB Source (close), BB Length (21)basis · line 17
  • Stdev (stdev, ta.stdev in 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.

closecloseprice datasmaLengthMA Lengthsetting · 200bbLengthBB Lengthsetting · 21multStdDevsetting · 2.0stopLossStop Loss%setting · 5riskCapitalRisk % of capital == B…setting · 10sma200EMA→ sma200basisSMA→ basisdevStdev→ devstopLossVal, tpValstopLossVal · tpVal2 valuesupperBand, lowerBandupperBand · lowerBand2 valuesqty1qty1valueentry:LEOpen LEstrategy.entryclose:LEClose LEstrategy.closeclose_all:close_all strategy.close_allplot:15SMA 200plotplot:23Basisplotplot:24Upperplotplot:25Lowerplotfill:BackgroundBackgroundfillbarcolor:33barcolorbarcolor

Order rules

OrderFires whenBy defaultLine
Open LE longstrategy.position_size<1 and upperBand>sma200 and lowerBand > sma200 and crossover(close, basis)On32
Close LEabs(strategy.position_size)>=1 and close>tpVal and crossunder(lowerBand, sma200)On36
Close abs(strategy.position_size)>=1 and close < stopLossValOn40
Close abs(strategy.position_size)>=1 and crossunder(basis, sma200)On41

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.

Not measuredNo trades to compareThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, but neither the live nor the historical run produced any trades in them, so the replay says nothing either way.
  • 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 1D0.0%+102.5%00%—16.7%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h0.0%+5.5%00%—5.1%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−11.7%+9.8%10%0.0018.6%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+16.6%+8.4%5100%—0.2%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−29.9%+16.7%838%0.1334.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h−20.2%+21.5%40%0.0020.6%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+10.8%+148.3%771%1.998.4%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+19.5%+17.1%4100%—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.

ScriptSharesTypeNetTradesMax drawdown
BB21_MA200_Strategy this page—v4 strategy0.0%016.7%
Volume Weighted Bollinger Bands Strategy by Ankit_1618ema sma stdevv4 strategy+10.0%1527.5%
Super Breakout day trading by beststockalertstdev sma emav4 strategyNo trades on this market
Super Auto Breakout Day Trade Volatile stocks by beststockalertstdev sma emav4 strategy−6.1%5129.1%
VFI strategy [based on VFI indicator published by UTS] by mohaneesma stdev emav4 strategy+181.4%8154.2%
McClellan Oscillator Strategy by sparrow_hawk_737ema sma stdevv3 strategyNo trades on this market

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 15 msWith a workaround 1,000 bars in 69 msNot measured
PineTSNo 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