Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (6)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$998,398$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. Lines are what the script plots.
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.

SettingDefaultType and rangeFeeds
Offset0int · min -500 · max 500MA200 MA50 MA90 MA21 MA9
Fast Length12intfast_ma
Slow Length26intslow_ma
Signal Smoothing9int · min 1 · max 50signal
Oscillator MA TypeEMAstring · options ["SMA", "EMA"]fast_ma slow_ma
Signal Line MA TypeEMAstring · options ["SMA", "EMA"]signal

What it calculates

  • SMAclose, 200out · line 9
  • SMAclose, 50outf · line 10
  • SMAclose, 90outn · line 11
  • SMAclose, 21outt · line 12
  • SMAclose, 9outthree · line 13
  • CUM / Changemath.sign(ta.change(close)) * volumeobv · line 24
  • CUMmath.sign(chng) * volumeobvalt · line 31
  • SMA / EMAsrc, Fast Length (12) · chosen by Oscillator MA Typefast_ma · line 38
  • SMA / EMAsrc, Slow Length (26) · chosen by Oscillator MA Typeslow_ma · line 39
  • SMA / EMAmacd, Signal Smoothing (9) · chosen by Signal Line MA Typesignal · line 41
  • MACDclose, 12, 26, 9macdLine · line 47
  • MACDclose, 12, 26, 9signalLine · line 47
  • MACDclose, 12, 26, 9histLine · line 47
  • Risingout, 2twohunraise · line 52
  • Risingoutt, 2twentyrise · line 53
  • Risingmacd, 2macdrise · line 54
  • Crossovermacd, signalmacdlong · line 55
  • Fallingout, 3twohunfall · line 65
  • Fallingoutt, 2twentyfall · line 66
  • Fallingoutthree, 1shortmafall · line 67
  • Fallingmacd, 1macdfall · 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.

closecloseprice datavolumevolumeprice datasma_sourceOscillator MA Typesetting · EMAfast_lengthFast Lengthsetting · 12slow_lengthSlow Lengthsetting · 26sma_signalSignal Line MA Typesetting · EMAsignal_lengthSignal Smoothingsetting · 9outSMA→ outoutfSMA→ outfoutnSMA→ outnouttSMA→ outtoutthreeSMA→ outthreeobvCUM / Change→ obvobvaltCUM→ obvaltfast_maSMA / EMA→ fast_ma · by Oscillator MA …slow_maSMA / EMA→ slow_ma · by Oscillator MA …signalSMA / EMA→ signal · by Signal Line MA …macdLineMACD→ macdLinesignalLineMACD→ signalLinehistLineMACD→ histLinetwohunraiseRising→ twohunraisetwentyriseRising→ twentyrisemacdriseRising→ macdrisemacdlongCrossover→ macdlongtwohunfallFalling→ twohunfalltwentyfallFalling→ twentyfallshortmafallFalling→ shortmafallmacdfallFalling→ macdfallsrcsrcvaluemacdmacdvaluehisthistvaluemafentry, matwohun, mafexit, matexit, matwohundmafentry · matwohun +35 conditionsmacdsellmacdsellconditionentry:My Long Entry IdOpen My Long Entry Idstrategy.entryentry:My Short Entry IdOpen My Short Entry Idstrategy.entryplot:16MA200plotplot:17MA50plotplot:18MA90plotplot:19MA21plotplot:20MA9plot

Order rules

OrderFires whenBy defaultLine
Open My Long Entry Id long(longCondition)On60
Open My Short Entry Id short(shortCondition)On74

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.

14/14trades 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+0.1%+102.5%4738%1.014.4%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+2.0%+5.5%3949%1.551.6%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−0.5%+9.8%5028%0.550.6%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+0.0%+8.4%4337%1.140.1%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+0.0%+16.7%4757%1.590.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h−0.0%+21.5%4340%0.900.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−0.0%+148.3%4643%0.810.1%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+0.0%+17.1%4637%1.230.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 barsTradesWonP&L
Rising market2330%−$4,213.20
Sideways market1250%−$17,764.90
Falling market1242%$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.

0%8%
All 47 trades
SideEntryExitBarsP&L
long9 Mar 2024 68,124.2016 Mar 2024 69,499.807$1,375.64
short16 Mar 2024 69,499.8011 Apr 2024 70,631.1026−$1,131.24
long11 Apr 2024 70,631.1014 Apr 2024 63,924.503−$6,706.56
short14 Apr 2024 63,924.5021 Apr 2024 64,940.607−$1,016.06
long21 Apr 2024 64,940.601 May 2024 60,672.0010−$4,268.57
short1 May 2024 60,672.0016 May 2024 66,206.5015−$5,534.50
long16 May 2024 66,206.5015 Jun 2024 66,044.0030−$162.52
short15 Jun 2024 66,044.002 Jul 2024 62,900.0017$3,143.99
long2 Jul 2024 62,900.005 Jul 2024 57,050.003−$5,849.98
short5 Jul 2024 57,050.0018 Aug 2024 59,492.0044−$2,441.97
long18 Aug 2024 59,492.003 Oct 2024 60,649.3046$1,157.28
short3 Oct 2024 60,649.3017 Oct 2024 67,620.0014−$6,970.73
long17 Oct 2024 67,620.005 Nov 2024 67,850.0019$230.01
short5 Nov 2024 67,850.008 Nov 2024 75,857.903−$8,007.88
long8 Nov 2024 75,857.903 Dec 2024 95,840.6025$19,982.70
short3 Dec 2024 95,840.605 Jan 2025 98,220.5033−$2,379.90
long5 Jan 2025 98,220.5010 Jan 2025 92,552.505−$5,668.02
short10 Jan 2025 92,552.5019 Jan 2025 104,556.009−$12,003.70
long19 Jan 2025 104,556.002 Feb 2025 100,636.0014−$3,920.57
short2 Feb 2025 100,636.0015 Mar 2025 83,983.2041$16,652.50
long15 Mar 2025 83,983.2029 May 2025 107,782.0075$23,798.60
short29 May 2025 107,782.0016 Jun 2025 105,594.0018$2,187.76
long16 Jun 2025 105,594.0022 Jun 2025 102,120.006−$3,474.00
short22 Jun 2025 102,120.0029 Jun 2025 107,297.007−$5,176.77
long29 Jun 2025 107,297.0026 Jul 2025 117,614.0027$10,317.50
short26 Jul 2025 117,614.0014 Aug 2025 123,306.0019−$5,692.13
long14 Aug 2025 123,306.0020 Aug 2025 112,873.006−$10,433.50
short20 Aug 2025 112,873.003 Sep 2025 111,240.0014$1,632.94
long3 Sep 2025 111,240.0024 Sep 2025 111,999.0021$758.79
short24 Sep 2025 111,999.0010 Nov 2025 104,723.0047$7,275.85
long10 Nov 2025 104,723.0015 Nov 2025 94,594.005−$10,128.90
short15 Nov 2025 94,594.0030 Nov 2025 90,802.4015$3,791.56
long30 Nov 2025 90,802.4021 Jan 2026 88,427.7052−$2,374.78
short21 Jan 2026 88,427.7025 Jan 2026 89,225.304−$797.68
long25 Jan 2026 89,225.3031 Jan 2026 84,260.506−$4,964.84
short31 Jan 2026 84,260.5021 Feb 2026 68,020.0021$16,240.50
long21 Feb 2026 68,020.0028 Mar 2026 66,407.3035−$1,612.72
short28 Mar 2026 66,407.3011 Apr 2026 72,962.7014−$6,555.43
long11 Apr 2026 72,962.7030 Apr 2026 75,780.0019$2,817.29
short30 Apr 2026 75,780.006 May 2026 80,905.506−$5,125.53
long6 May 2026 80,905.5014 May 2026 79,313.608−$1,591.92
short14 May 2026 79,313.6017 Jun 2026 65,675.0034$13,638.60
long17 Jun 2026 65,675.0012 Aug 2026 63,600.0056−$2,075.01
short12 Aug 2026 63,600.0020 Aug 2026 69,334.808−$5,734.77
long20 Aug 2026 69,334.808 Sep 2026 79,112.0019$9,777.22
short8 Sep 2026 79,112.0020 Sep 2026 81,250.0012−$2,137.99
long20 Sep 2026 81,250.009 Oct 2026 81,754.5019$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.

ScriptSharesTypeNetTradesMax drawdown
Altered OBV On MACD this page—v5 strategy+0.1%474.4%
Linear On MACD by stocktechbotcum sma macd rising fallingv5 strategy+1.5%165.6%
Linear Cross Trading Strategy by stocktechbotsma macd rising fallingv5 strategy+9.7%142.7%
Accumulation Stage Identifier and Strategy around for Trading by stocktechbotsma cum macd rising fallingv5 strategy−0.0%164.1%
PMA Strategy Idea by QuantCTema rising fallingv4 strategy+51.7%12930.8%
OBV Pyr by RafaelZioniema cum smav4 strategy−1.2%279.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.

Clean v5

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

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 23 msYes 1,000 bars in 58 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/// © 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)