Pine Script v5 · Strategy
Altered OBV On MACD
A Pine Script v5 strategy built on SMA (close, 200), CUM or Change and CUM (math.sign(chng) * volume). It has 2 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. 6 settings; defaults include Offset 0, Fast Length 12 and Slow Length 26.
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 Altered OBV On MACD repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 14 of 14 trades.
- How did Altered OBV On MACD do on BTCUSDT 1D with default settings?
- +0.13%
- From 26 Jan 2024 to 21 Oct 2026, over 47 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Altered OBV On MACD 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: 47 before, 58 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 Altered OBV On MACD use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Altered OBV On MACD run in Zeal Terminal?
- Yes
- It compiles in 23 ms and runs 1,000 bars in 58 ms, in the browser.
BTCUSDT · 1D · default settings
- MA200
- MA50
- MA90
- MA21
- MA9
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +0.13%
- $1,344.94
- Buy and hold
- +102.5%
- same period
- Closed trades
- 47
- 18 won · 29 lost
- Profitable
- 38.3%
- of closed trades
- Profit factor
- 1.01
- gross profit ÷ gross loss
- Max drawdown
- 4.43%
- −$44,406.10
One run with the author's default settings, $1,000,000 starting capital, commission as the script sets it. A short 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.
-
Likely bug
ta.sma(close, 200) ignores the settings line 9
Line 9 hard-codes 200 where line 38 uses “Fast Length”. Changing those settings moves one band but not this one.
ta.sma(close, 200)ta.sma(close, fast_length) -
Likely bug
ta.sma(close, 50) ignores the settings line 10
Line 10 hard-codes 50 where line 38 uses “Fast Length”. Changing those settings moves one band but not this one.
ta.sma(close, 50)ta.sma(close, fast_length) -
Likely bug
ta.sma(close, 90) ignores the settings line 11
Line 11 hard-codes 90 where line 38 uses “Fast Length”. Changing those settings moves one band but not this one.
ta.sma(close, 90)ta.sma(close, fast_length) -
Likely bug
ta.sma(close, 21) ignores the settings line 12
Line 12 hard-codes 21 where line 38 uses “Fast Length”. Changing those settings moves one band but not this one.
ta.sma(close, 21)ta.sma(close, fast_length) -
Likely bug
ta.sma(close, 9) ignores the settings line 13
Line 13 hard-codes 9 where line 38 uses “Fast Length”. Changing those settings moves one band but not this one.
ta.sma(close, 9)ta.sma(close, fast_length) -
Likely bug
ta.sma(close, 50) ignores the settings line 49
Line 49 hard-codes 50 where line 38 uses “Fast Length”. Changing those settings moves one band but not this one.
ta.sma(close, 50)ta.sma(close, fast_length) -
Likely bug
ta.sma(close, 90) ignores the settings line 49
Line 49 hard-codes 90 where line 38 uses “Fast Length”. Changing those settings moves one band but not this one.
ta.sma(close, 90)ta.sma(close, fast_length) -
Likely bug
ta.sma(close, 200) ignores the settings line 51
Line 51 hard-codes 200 where line 38 uses “Fast Length”. Changing those settings moves one band but not this one.
ta.sma(close, 200)ta.sma(close, fast_length) -
Likely bug
ta.sma(close, 50) ignores the settings line 62
Line 62 hard-codes 50 where line 38 uses “Fast Length”. Changing those settings moves one band but not this one.
ta.sma(close, 50)ta.sma(close, fast_length) -
Likely bug
ta.sma(close, 90) ignores the settings line 62
Line 62 hard-codes 90 where line 38 uses “Fast Length”. Changing those settings moves one band but not this one.
ta.sma(close, 90)ta.sma(close, fast_length) -
Likely bug
ta.sma(close, 21) ignores the settings line 63
Line 63 hard-codes 21 where line 38 uses “Fast Length”. Changing those settings moves one band but not this one.
ta.sma(close, 21)ta.sma(close, fast_length) -
Likely bug
ta.sma(close, 50) ignores the settings line 63
Line 63 hard-codes 50 where line 38 uses “Fast Length”. Changing those settings moves one band but not this one.
ta.sma(close, 50)ta.sma(close, fast_length) -
Likely bug
ta.sma(close, 200) ignores the settings line 64
Line 64 hard-codes 200 where line 38 uses “Fast Length”. Changing those settings moves one band but not this one.
ta.sma(close, 200)ta.sma(close, fast_length) -
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.
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 |
|---|---|---|---|
| Offset | 0 | int · min -500 · max 500 | MA200 MA50 MA90 MA21 MA9 |
| Fast Length | 12 | int | fast_ma |
| Slow Length | 26 | int | slow_ma |
| Signal Smoothing | 9 | int · min 1 · max 50 | signal |
| Oscillator MA Type | EMA | string · options ["SMA", "EMA"] | fast_ma slow_ma |
| Signal Line MA Type | EMA | string · options ["SMA", "EMA"] | signal |
What it calculates
- SMAclose, 200
out · line 9 - SMAclose, 50
outf · line 10 - SMAclose, 90
outn · line 11 - SMAclose, 21
outt · line 12 - SMAclose, 9
outthree · line 13 - CUM / Changemath.sign(ta.change(close)) * volume
obv · line 24 - CUMmath.sign(chng) * volume
obvalt · line 31 - SMA / EMAsrc, Fast Length (12) · chosen by Oscillator MA Type
fast_ma · line 38 - SMA / EMAsrc, Slow Length (26) · chosen by Oscillator MA Type
slow_ma · line 39 - SMA / EMAmacd, Signal Smoothing (9) · chosen by Signal Line MA Type
signal · line 41 - MACDclose, 12, 26, 9
macdLine · line 47 - MACDclose, 12, 26, 9
signalLine · line 47 - MACDclose, 12, 26, 9
histLine · line 47 - Risingout, 2
twohunraise · line 52 - Risingoutt, 2
twentyrise · line 53 - Risingmacd, 2
macdrise · line 54 - Crossovermacd, signal
macdlong · line 55 - Fallingout, 3
twohunfall · line 65 - Fallingoutt, 2
twentyfall · line 66 - Fallingoutthree, 1
shortmafall · line 67 - Fallingmacd, 1
macdfall · line 68
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 My Long Entry Id long | (longCondition) | On | 60 |
| Open My Short Entry Id short | (shortCondition) | On | 74 |
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.1% | +102.5% | 47 | 38% | 1.01 | 4.4% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +2.0% | +5.5% | 39 | 49% | 1.55 | 1.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.5% | +9.8% | 50 | 28% | 0.55 | 0.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.0% | +8.4% | 43 | 37% | 1.14 | 0.1% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 47 | 57% | 1.59 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.0% | +21.5% | 43 | 40% | 0.90 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.0% | +148.3% | 46 | 43% | 0.81 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 46 | 37% | 1.23 | 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 | 23 | 30% | −$4,213.20 |
| Sideways market | 12 | 50% | −$17,764.90 |
| Falling market | 12 | 42% | $23,323.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
7.4%
The 95th percentile of the worst drawdown when the same 47 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 3.2% to 7.4%; the tick is what actually happened, 4.1%. Order luck alone moves it this much.
All 47 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 9 Mar 2024 68,124.20 | 16 Mar 2024 69,499.80 | 7 | $1,375.64 |
| short | 16 Mar 2024 69,499.80 | 11 Apr 2024 70,631.10 | 26 | −$1,131.24 |
| long | 11 Apr 2024 70,631.10 | 14 Apr 2024 63,924.50 | 3 | −$6,706.56 |
| short | 14 Apr 2024 63,924.50 | 21 Apr 2024 64,940.60 | 7 | −$1,016.06 |
| long | 21 Apr 2024 64,940.60 | 1 May 2024 60,672.00 | 10 | −$4,268.57 |
| short | 1 May 2024 60,672.00 | 16 May 2024 66,206.50 | 15 | −$5,534.50 |
| long | 16 May 2024 66,206.50 | 15 Jun 2024 66,044.00 | 30 | −$162.52 |
| short | 15 Jun 2024 66,044.00 | 2 Jul 2024 62,900.00 | 17 | $3,143.99 |
| long | 2 Jul 2024 62,900.00 | 5 Jul 2024 57,050.00 | 3 | −$5,849.98 |
| short | 5 Jul 2024 57,050.00 | 18 Aug 2024 59,492.00 | 44 | −$2,441.97 |
| long | 18 Aug 2024 59,492.00 | 3 Oct 2024 60,649.30 | 46 | $1,157.28 |
| short | 3 Oct 2024 60,649.30 | 17 Oct 2024 67,620.00 | 14 | −$6,970.73 |
| long | 17 Oct 2024 67,620.00 | 5 Nov 2024 67,850.00 | 19 | $230.01 |
| short | 5 Nov 2024 67,850.00 | 8 Nov 2024 75,857.90 | 3 | −$8,007.88 |
| long | 8 Nov 2024 75,857.90 | 3 Dec 2024 95,840.60 | 25 | $19,982.70 |
| short | 3 Dec 2024 95,840.60 | 5 Jan 2025 98,220.50 | 33 | −$2,379.90 |
| long | 5 Jan 2025 98,220.50 | 10 Jan 2025 92,552.50 | 5 | −$5,668.02 |
| short | 10 Jan 2025 92,552.50 | 19 Jan 2025 104,556.00 | 9 | −$12,003.70 |
| long | 19 Jan 2025 104,556.00 | 2 Feb 2025 100,636.00 | 14 | −$3,920.57 |
| short | 2 Feb 2025 100,636.00 | 15 Mar 2025 83,983.20 | 41 | $16,652.50 |
| long | 15 Mar 2025 83,983.20 | 29 May 2025 107,782.00 | 75 | $23,798.60 |
| short | 29 May 2025 107,782.00 | 16 Jun 2025 105,594.00 | 18 | $2,187.76 |
| long | 16 Jun 2025 105,594.00 | 22 Jun 2025 102,120.00 | 6 | −$3,474.00 |
| short | 22 Jun 2025 102,120.00 | 29 Jun 2025 107,297.00 | 7 | −$5,176.77 |
| long | 29 Jun 2025 107,297.00 | 26 Jul 2025 117,614.00 | 27 | $10,317.50 |
| short | 26 Jul 2025 117,614.00 | 14 Aug 2025 123,306.00 | 19 | −$5,692.13 |
| long | 14 Aug 2025 123,306.00 | 20 Aug 2025 112,873.00 | 6 | −$10,433.50 |
| short | 20 Aug 2025 112,873.00 | 3 Sep 2025 111,240.00 | 14 | $1,632.94 |
| long | 3 Sep 2025 111,240.00 | 24 Sep 2025 111,999.00 | 21 | $758.79 |
| short | 24 Sep 2025 111,999.00 | 10 Nov 2025 104,723.00 | 47 | $7,275.85 |
| long | 10 Nov 2025 104,723.00 | 15 Nov 2025 94,594.00 | 5 | −$10,128.90 |
| short | 15 Nov 2025 94,594.00 | 30 Nov 2025 90,802.40 | 15 | $3,791.56 |
| long | 30 Nov 2025 90,802.40 | 21 Jan 2026 88,427.70 | 52 | −$2,374.78 |
| short | 21 Jan 2026 88,427.70 | 25 Jan 2026 89,225.30 | 4 | −$797.68 |
| long | 25 Jan 2026 89,225.30 | 31 Jan 2026 84,260.50 | 6 | −$4,964.84 |
| short | 31 Jan 2026 84,260.50 | 21 Feb 2026 68,020.00 | 21 | $16,240.50 |
| long | 21 Feb 2026 68,020.00 | 28 Mar 2026 66,407.30 | 35 | −$1,612.72 |
| short | 28 Mar 2026 66,407.30 | 11 Apr 2026 72,962.70 | 14 | −$6,555.43 |
| long | 11 Apr 2026 72,962.70 | 30 Apr 2026 75,780.00 | 19 | $2,817.29 |
| short | 30 Apr 2026 75,780.00 | 6 May 2026 80,905.50 | 6 | −$5,125.53 |
| long | 6 May 2026 80,905.50 | 14 May 2026 79,313.60 | 8 | −$1,591.92 |
| short | 14 May 2026 79,313.60 | 17 Jun 2026 65,675.00 | 34 | $13,638.60 |
| long | 17 Jun 2026 65,675.00 | 12 Aug 2026 63,600.00 | 56 | −$2,075.01 |
| short | 12 Aug 2026 63,600.00 | 20 Aug 2026 69,334.80 | 8 | −$5,734.77 |
| long | 20 Aug 2026 69,334.80 | 8 Sep 2026 79,112.00 | 19 | $9,777.22 |
| short | 8 Sep 2026 79,112.00 | 20 Sep 2026 81,250.00 | 12 | −$2,137.99 |
| long | 20 Sep 2026 81,250.00 | 9 Oct 2026 81,754.50 | 19 | $504.47 |
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 |
|---|---|---|---|---|---|
| Altered OBV On MACD this page | — | v5 strategy | +0.1% | 47 | 4.4% |
| Linear On MACD by stocktechbot | cum sma macd rising falling | v5 strategy | +1.5% | 16 | 5.6% |
| Linear Cross Trading Strategy by stocktechbot | sma macd rising falling | v5 strategy | +9.7% | 14 | 2.7% |
| Accumulation Stage Identifier and Strategy around for Trading by stocktechbot | sma cum macd rising falling | v5 strategy | −0.0% | 16 | 4.1% |
| PMA Strategy Idea by QuantCT | ema rising falling | v4 strategy | +51.7% | 129 | 30.8% |
| OBV Pyr by RafaelZioni | ema cum sma | v4 strategy | −1.2% | 27 | 9.5% |
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 settings use the untyped input() (line 21, 22). 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 47 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 21: input() → input.int()
- Line 22: input() → input.int()
⋯plot(outthree, color=color.fuchsia, title="MA9", offset=offset)-fast_length = input(title="Fast Length", defval=12)+fast_length = input.int(title="Fast Length", defval=12)-slow_length = input(title="Slow Length", defval=26)+slow_length = input.int(title="Slow Length", defval=26)chng = 0⋯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: 47 before, 58 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.
⋯// © stocktechbot-//@version=5+//@version=6strategy("Altered OBV On MACD", overlay=true, margin_long=100, margin_short=100)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 23 ms | Yes 1,000 bars in 58 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/// © stocktechbot//@version=5strategy("Altered OBV On MACD", overlay=true, margin_long=100, margin_short=100)// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © stocktechbot//@version=5//SMA Tredlineout = ta.sma(close, 200)outf = ta.sma(close, 50)outn = ta.sma(close, 90)outt = ta.sma(close, 21)outthree = ta.sma(close, 9)//sma plotoffset = input.int(title="Offset", defval=0, minval=-500, maxval=500)plot(out, color=color.blue, title="MA200", offset=offset)plot(outf, color=color.maroon, title="MA50", offset=offset)plot(outn, color=color.orange, title="MA90", offset=offset)plot(outt, color=color.olive, title="MA21", offset=offset)plot(outthree, color=color.fuchsia, title="MA9", offset=offset)fast_length = input(title="Fast Length", defval=12)slow_length = input(title="Slow Length", defval=26)chng = 0obv = ta.cum(math.sign(ta.change(close)) * volume)if close < close[1] and (open < close) chng := 1else if close > close[1] chng := 1else chng := -1obvalt = ta.cum(math.sign(chng) * volume)//src = input(title="Source", defval=close)src = obvaltsignal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9)sma_source = input.string(title="Oscillator MA Type", defval="EMA", options=["SMA", "EMA"])sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])// Calculatingfast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)macd = fast_ma - slow_masignal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)hist = macd - signal//hline(0, "Zero Line", color=color.new(#787B86, 50))//plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))//plot(macd, title="MACD", color=col_macd)//plot(signal, title="Signal", color=col_signal)[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)//BUY Signalmafentry =ta.sma(close, 50) > ta.sma(close, 90)//matentry = ta.sma(close, 21) > ta.sma(close, 50)matwohun = close > ta.sma(close, 200)twohunraise = ta.rising(out, 2)twentyrise = ta.rising(outt, 2)macdrise = ta.rising(macd,2)macdlong = ta.crossover(macd, signal)longCondition=falseif macdlong and macdrise longCondition := trueif (longCondition) strategy.entry("My Long Entry Id", strategy.long)//Sell Signalmafexit =ta.sma(close, 50) < ta.sma(close, 90)matexit = ta.sma(close, 21) < ta.sma(close, 50)matwohund = close < ta.sma(close, 200)twohunfall = ta.falling(out, 3)twentyfall = ta.falling(outt, 2)shortmafall = ta.falling(outthree, 1)macdfall = ta.falling(macd,1)macdsell = macd < signalshortCondition = falseif macdfall and macdsell and (macdLine < signalLine) and ta.falling(low,2) shortCondition := trueif (shortCondition) strategy.entry("My Short Entry Id", strategy.short)