Pine Script v5 · Strategy
Linear On MACD
A Pine Script v5 strategy built on CUM or Change, CUM (math.sign(chng) * volume) and SMA or EMA (src, 12). 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. 7 settings; defaults include Fast Length 12, Slow Length 26 and Risk tolerance LOW.
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 Linear On MACD repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 8 of 8 trades.
- How did Linear On MACD do on BTCUSDT 1D with default settings?
- +1.47%
- From 26 Jan 2024 to 21 Oct 2026, over 16 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Linear 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: 16 before, 66 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 Linear On MACD use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Linear On MACD run in Zeal Terminal?
- Yes
- It compiles in 45 ms and runs 1,000 bars in 51 ms, in the browser.
BTCUSDT · 1D · default settings
- Predicted Stock Price
- Predicted Stock Price
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +1.47%
- $14,704.50
- Buy and hold
- +102.5%
- same period
- Closed trades
- 16
- 6 won · 10 lost
- Profitable
- 37.5%
- of closed trades
- Profit factor
- 1.25
- gross profit ÷ gross loss
- Max drawdown
- 5.57%
- −$56,140.50
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.
-
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 |
|---|---|---|---|
| Fast Length | 12 | int | fast_ma |
| Slow Length | 26 | int | slow_ma |
| Risk tolerance | LOW | string · options ["LOW", "HIGH"] | line 73 |
| 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 |
| Lookback | 21 | int | ybar xbar b a risklevel Predicted Stock Price |
What it calculates
- CUM / Changemath.sign(ta.change(close)) * volume
obv · line 9 - CUMmath.sign(chng) * volume
obvalt · line 16 - SMA / EMAsrc, Fast Length (12) · chosen by Oscillator MA Type
fast_ma · line 23 - SMA / EMAsrc, Slow Length (26) · chosen by Oscillator MA Type
slow_ma · line 24 - SMA / EMAmacd, Signal Smoothing (9) · chosen by Signal Line MA Type
signal · line 26 - MACDclose, 12, 26, 9
macdLine · line 32 - MACDclose, 12, 26, 9
signalLine · line 32 - MACDclose, 12, 26, 9
histLine · line 32 - Risingmacd, 2
macdrise · line 57 - Fallingmacd, 1
macdfall · line 69 - VWMApredicted_price, Lookback (21)
risklevel · line 72
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 | 66 |
| Open My Short Entry Id short | (shortCondition) | On | 78 |
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 | +1.5% | +102.5% | 16 | 38% | 1.25 | 5.6% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +1.4% | +5.5% | 15 | 47% | 1.67 | 1.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.3% | +9.8% | 16 | 69% | 3.42 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.1% | +8.4% | 21 | 33% | 1.90 | 0.1% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 26 | 42% | 1.11 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.0% | +21.5% | 25 | 32% | 0.64 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.0% | +148.3% | 19 | 47% | 0.76 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 29 | 41% | 1.61 | 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 | 1 | 100% | $8,343.81 |
| Sideways market | 6 | 50% | $30,992.00 |
| Falling market | 9 | 22% | −$24,631.30 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
5.0%
The 95th percentile of the worst drawdown when the same 16 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 2.8% to 5.0%; the tick is what actually happened, 3.1%. Order luck alone moves it this much.
All 16 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 16 Mar 2024 69,499.80 | 22 Aug 2024 61,156.00 | 159 | $8,343.81 |
| long | 22 Aug 2024 61,156.00 | 2 Sep 2024 57,301.80 | 11 | −$3,854.26 |
| short | 2 Sep 2024 57,301.80 | 2 Apr 2025 85,158.40 | 212 | −$27,856.60 |
| long | 2 Apr 2025 85,158.40 | 31 May 2025 103,985.00 | 59 | $18,827.10 |
| short | 31 May 2025 103,985.00 | 11 Sep 2025 113,960.00 | 103 | −$9,974.53 |
| long | 11 Sep 2025 113,960.00 | 24 Sep 2025 111,999.00 | 13 | −$1,961.20 |
| short | 24 Sep 2025 111,999.00 | 3 Dec 2025 91,277.90 | 70 | $20,720.90 |
| long | 3 Dec 2025 91,277.90 | 21 Jan 2026 88,427.70 | 49 | −$2,850.22 |
| short | 21 Jan 2026 88,427.70 | 11 Mar 2026 69,948.60 | 49 | $18,479.00 |
| long | 11 Mar 2026 69,948.60 | 28 Mar 2026 66,407.30 | 17 | −$3,541.36 |
| short | 28 Mar 2026 66,407.30 | 27 Jul 2026 65,400.00 | 121 | $1,007.28 |
| long | 27 Jul 2026 65,400.00 | 28 Jul 2026 63,755.90 | 1 | −$1,644.14 |
| short | 28 Jul 2026 63,755.90 | 8 Aug 2026 64,923.20 | 11 | −$1,167.34 |
| long | 8 Aug 2026 64,923.20 | 12 Aug 2026 63,600.00 | 4 | −$1,323.19 |
| short | 12 Aug 2026 63,600.00 | 20 Aug 2026 69,334.80 | 8 | −$5,734.77 |
| long | 20 Aug 2026 69,334.80 | 11 Sep 2026 76,568.70 | 22 | $7,233.95 |
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 |
|---|---|---|---|---|---|
| Linear On MACD this page | — | v5 strategy | +1.5% | 16 | 5.6% |
| Altered OBV On MACD by stocktechbot | sma cum macd rising falling | v5 strategy | +0.1% | 47 | 4.4% |
| 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% |
| 5min MACD scalp by Joel by Zippy111 | ema macd rising falling | v5 strategy | does not run in Zeal Terminal | ||
| cRSI + Waves Strategy with VWMA overlay by Dr_Roboto | vwma macd rising falling | v4 strategy | No trades on this market | ||
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.
- 3 settings use the untyped input() (line 5, 6, 45). 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 16 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 5: input() → input.int()
- Line 6: input() → input.int()
- Line 45: input() → input.int()
⋯strategy("Linear On MACD", overlay=true, margin_long=100, margin_short=100)-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)tolerance = input.string(title="Risk tolerance", defval = "LOW", options=["LOW", "HIGH"])⋯// Length of linear regression-len = input(defval = 21, title = 'Lookback')+len = input.int(defval = 21, title = 'Lookback')// Calculate linear regression for stock price based on volume⋯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: 16 before, 66 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("Linear On MACD", overlay=true, margin_long=100, margin_short=100)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 45 ms | Yes 1,000 bars in 51 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("Linear On MACD", overlay=true, margin_long=100, margin_short=100)fast_length = input(title="Fast Length", defval=12)slow_length = input(title="Slow Length", defval=26)tolerance = input.string(title="Risk tolerance", defval = "LOW", options=["LOW", "HIGH"])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)//Linear Regressionvol = volume// Function to calculate linear regressionlinregs(y, x, len) => ybar = math.sum(y, len)/len xbar = math.sum(x, len)/len b = math.sum((x - xbar)*(y - ybar),len)/math.sum((x - xbar)*(x - xbar),len) a = ybar - b*xbar [a, b]// Historical stock price dataprice = close// Length of linear regressionlen = input(defval = 21, title = 'Lookback')// Calculate linear regression for stock price based on volume[a, b] = linregs(price, vol, len)// Predicted stock price based on volumepredicted_price = a + b*vol// Check if predicted price is between open and closeis_between = open < predicted_price and predicted_price < close// Plot predicted stock priceplot(predicted_price, color=color.rgb(218, 27, 132), linewidth=2, title="Predicted Stock Price")plot(ta.vwma(predicted_price,len), color=color.rgb(199, 43, 64), linewidth=2, title="Predicted Stock Price")//BUY Signallincrossunder = close > predicted_pricemacdrise = ta.rising(macd,2)//macdvollong = ta.crossover(macd, signal)//macdlong = ta.crossover(macdLine, signalLine)macdvollong = macd > signalmacdlong = macdLine > signalLinelongCondition=falseif macdlong and macdvollong and is_between and ta.rising(predicted_price,1) longCondition := trueif (longCondition) strategy.entry("My Long Entry Id", strategy.long)//Sell Signallincrossover = close < predicted_pricemacdfall = ta.falling(macd,1)macdsell = macd < signalshortCondition = falserisklevel = predicted_priceif (tolerance == "HIGH") risklevel := ta.vwma(predicted_price,len)if macdfall and macdsell and (macdLine < signalLine) and (close < risklevel) shortCondition := trueif (shortCondition) strategy.entry("My Short Entry Id", strategy.short)