Pine Script v5 · Strategy
Rudy's BB with Martingale
A Pine Script v5 strategy built on EMA (close, 200) and BB (close, 20). It has 2 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 is one contract and pays no commission. 1 setting; defaults include Base Contract Size 0.01.
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 Rudy's BB with Martingale repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 2 of 2 trades.
- How did Rudy's BB with Martingale do on BTCUSDT 1D with default settings?
- +0.01%
- From 26 Jan 2024 to 21 Oct 2026, over 12 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Rudy's BB with Martingale run in Pine Script v6?
- Yes
- Same 12 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. The v6 script can be downloaded below.
- Does Rudy's BB with Martingale use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Rudy's BB with Martingale run in Zeal Terminal?
- Yes
- It compiles in 15 ms and runs 1,000 bars in 32 ms, in the browser.
BTCUSDT · 1D · default settings
- MA200
- Middle BB
- Upper BB
- Lower BB
- Current profit
- Amount of last negative profits
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +0.01%
- $115.72
- Buy and hold
- +102.5%
- same period
- Closed trades
- 12
- 7 won · 5 lost
- Profitable
- 58.3%
- of closed trades
- Profit factor
- 1.28
- gross profit ÷ gross loss
- Max drawdown
- 0.05%
- −$530.55
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
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 24, 25. 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 |
|---|---|---|---|
| Base Contract Size | 0.01 | float | amount_buy |
What it calculates
- EMAclose, 200
ma200 · line 17 - BBclose, 20, 2
middle · line 18 - BBclose, 20, 2
upper · line 18 - BBclose, 20, 2
lower · 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 bb buy long | close < lower | On | 24 |
| Close bb buy | close>=upper | On | 25 |
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 | +0.0% | +102.5% | 12 | 58% | 1.28 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.0% | +5.5% | 12 | 42% | 1.61 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.0% | +9.8% | 9 | 44% | 0.64 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.0% | +8.4% | 13 | 77% | 1.15 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.0% | +16.7% | 10 | 70% | 0.76 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 12 | 67% | 1.22 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.0% | +148.3% | 9 | 67% | 1.54 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 12 | 75% | 1.66 | 0.0% | 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 | 4 | 75% | $200.06 |
| Sideways market | 6 | 50% | −$113.44 |
| Falling market | 2 | 50% | $29.11 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
0.0%
The 95th percentile of the worst drawdown when the same 12 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 0.0% to 0.0%; the tick is what actually happened, 0.0%. Order luck alone moves it this much.
All 12 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 14 Apr 2024 63,924.50 | 16 May 2024 66,206.50 | 32 | $22.82 |
| long | 15 Jun 2024 66,044.00 | 16 Jul 2024 64,724.10 | 31 | −$13.20 |
| long | 3 Aug 2024 61,498.30 | 24 Aug 2024 64,037.20 | 21 | $25.39 |
| long | 7 Sep 2024 53,963.00 | 20 Sep 2024 62,948.00 | 13 | $89.85 |
| long | 5 Feb 2025 97,763.10 | 23 Apr 2025 93,443.00 | 77 | −$43.20 |
| long | 6 Jun 2025 101,509.00 | 11 Jul 2025 116,010.00 | 35 | $145.01 |
| long | 1 Aug 2025 115,764.00 | 14 Aug 2025 123,306.00 | 13 | $75.42 |
| long | 26 Aug 2025 110,112.00 | 12 Sep 2025 115,483.00 | 17 | $53.71 |
| long | 26 Sep 2025 108,994.00 | 3 Oct 2025 120,529.00 | 7 | $115.35 |
| long | 5 Nov 2025 101,497.00 | 3 Jan 2026 89,995.10 | 59 | −$115.02 |
| long | 1 Feb 2026 78,741.10 | 5 Mar 2026 72,666.80 | 32 | −$60.74 |
| long | 23 May 2026 75,539.50 | 22 Jul 2026 66,556.10 | 60 | −$179.67 |
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 |
|---|---|---|---|---|---|
| Rudy's BB with Martingale this page | — | v5 strategy | +0.0% | 12 | 0.1% |
| BT-Bollinger Bands - Trend Following by Credsonb | bb ema | v5 strategy | +3.9% | 16 | 5.6% |
| Strategy Oil Z Score by jroche1973 | bb ema | v4 strategy | No trades on this market | ||
| Bollinger Bands Modified (Stormer) by AugustoErni | bb ema | v5 strategy | −7.8% | 29 | 8.0% |
| Strategy- Combo Z Score by jroche1973 | bb ema | v4 strategy | No trades on this market | ||
| Korea premium(KIMP) by roses27 | ema bb | v4 strategy | does not run in Zeal Terminal | ||
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.
- 2 strategy calls use the when = argument (line 24, 25). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- No v6-only features. v6 is not needed to run this script.
Same 12 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 24: when = close < lower → if block
- Line 25: when = close>=upper → if block
⋯amount_buy = lastTradeProfit() < 0 ? base_contract_size * amountOfLastNegativeProfits() : 0.01-strategy.entry("bb buy", strategy.long, amount_buy, when=close < lower)+if close < lower+ strategy.entry("bb buy", strategy.long, amount_buy)-strategy.close("bb buy", when=close>=upper)+if close>=upper+ strategy.close("bb buy") ⋯Does it run in Pine Script v6?
Yes
Same 12 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
⋯// © rudolfcicko2011-//@version=5+//@version=6strategy("Rudy BB with Martingala", overlay=true, margin_long=100, margin_short=100)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 15 ms | Yes 1,000 bars in 32 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/// © rudolfcicko2011//@version=5strategy("Rudy BB with Martingala", overlay=true, margin_long=100, margin_short=100)base_contract_size= input.float(0.01, 'Base Contract Size')lastTradeProfit() => strategy.closedtrades.profit(strategy.closedtrades-1) + strategy.closedtrades.commission(strategy.closedtrades-1) amountOfLastNegativeProfits() => result = 0 i = strategy.closedtrades - 1 while strategy.closedtrades.profit(i) < 0 result+=1 i-=1 result ma200 = ta.ema(close, 200)[middle, upper, lower] = ta.bb(close, 20, 2)plot(ma200, "MA200", color=color.green)plot(middle, "Middle BB", color=color.purple)plot(upper, "Upper BB", color=color.purple)plot(lower, "Lower BB", color=color.purple)amount_buy = lastTradeProfit() < 0 ? base_contract_size * amountOfLastNegativeProfits() : 0.01strategy.entry("bb buy", strategy.long, amount_buy, when=close < lower)strategy.close("bb buy", when=close>=upper) plotchar(strategy.openprofit, "Current profit", "", location = location.top)plotchar(amountOfLastNegativeProfits(), "Amount of last negative profits", "", location = location.top)