Pine Script v5 · Strategy
Linear Cross Trading Strategy
A Pine Script v5 strategy built on SMA (close, 200), MACD (predicted_price, 12) and Rising (out, 2). 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. 2 settings; defaults include Strategy Length 21 and Linear Lookback 9.
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 Cross Trading Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 4 of 4 trades.
- How did Linear Cross Trading Strategy do on BTCUSDT 1D with default settings?
- +9.74%
- From 26 Jan 2024 to 21 Oct 2026, over 14 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Linear Cross Trading Strategy 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: 14 before, 14 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 Cross Trading Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Linear Cross Trading Strategy run in Zeal Terminal?
- Yes
- It compiles in 20 ms and runs 1,000 bars in 53 ms, in the browser.
BTCUSDT · 1D · default settings
- Predicted Price
- MA Predicted Price
- MA200
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +9.74%
- $97,381.60
- Buy and hold
- +102.5%
- same period
- Closed trades
- 14
- 8 won · 6 lost
- Profitable
- 57.1%
- of closed trades
- Profit factor
- 3.76
- gross profit ÷ gross loss
- Max drawdown
- 2.73%
- −$27,326.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.
-
Likely bug
ta.sma(close, 200) ignores the settings line 23
Line 23 hard-codes 200 where line 36 uses “Linear Lookback”. Changing those settings moves one band but not this one.
ta.sma(close, 200)ta.sma(close, linearlen) -
Likely bug
ta.sma(close, 50) ignores the settings line 24
Line 24 hard-codes 50 where line 36 uses “Linear Lookback”. Changing those settings moves one band but not this one.
ta.sma(close, 50)ta.sma(close, linearlen) -
Likely bug
ta.sma(close, 90) ignores the settings line 25
Line 25 hard-codes 90 where line 36 uses “Linear Lookback”. Changing those settings moves one band but not this one.
ta.sma(close, 90)ta.sma(close, linearlen) -
Likely bug
ta.sma(close, 21) ignores the settings line 26
Line 26 hard-codes 21 where line 36 uses “Linear Lookback”. Changing those settings moves one band but not this one.
ta.sma(close, 21)ta.sma(close, linearlen) -
Likely bug
ta.sma(close, 9) ignores the settings line 27
Line 27 hard-codes 9 where line 36 uses “Linear Lookback”. Changing those settings moves one band but not this one.
ta.sma(close, 9)ta.sma(close, linearlen) -
Likely bug
ta.sma(close, 50) ignores the settings line 42
Line 42 hard-codes 50 where line 36 uses “Linear Lookback”. Changing those settings moves one band but not this one.
ta.sma(close, 50)ta.sma(close, linearlen) -
Likely bug
ta.sma(close, 90) ignores the settings line 42
Line 42 hard-codes 90 where line 36 uses “Linear Lookback”. Changing those settings moves one band but not this one.
ta.sma(close, 90)ta.sma(close, linearlen) -
Likely bug
ta.sma(close, 200) ignores the settings line 44
Line 44 hard-codes 200 where line 36 uses “Linear Lookback”. Changing those settings moves one band but not this one.
ta.sma(close, 200)ta.sma(close, linearlen) -
Likely bug
ta.sma(close, 50) ignores the settings line 61
Line 61 hard-codes 50 where line 36 uses “Linear Lookback”. Changing those settings moves one band but not this one.
ta.sma(close, 50)ta.sma(close, linearlen) -
Likely bug
ta.sma(close, 90) ignores the settings line 61
Line 61 hard-codes 90 where line 36 uses “Linear Lookback”. Changing those settings moves one band but not this one.
ta.sma(close, 90)ta.sma(close, linearlen) -
Likely bug
ta.sma(close, 21) ignores the settings line 62
Line 62 hard-codes 21 where line 36 uses “Linear Lookback”. Changing those settings moves one band but not this one.
ta.sma(close, 21)ta.sma(close, linearlen) -
Likely bug
ta.sma(close, 50) ignores the settings line 62
Line 62 hard-codes 50 where line 36 uses “Linear Lookback”. Changing those settings moves one band but not this one.
ta.sma(close, 50)ta.sma(close, linearlen) -
Likely bug
ta.sma(close, 200) ignores the settings line 63
Line 63 hard-codes 200 where line 36 uses “Linear Lookback”. Changing those settings moves one band but not this one.
ta.sma(close, 200)ta.sma(close, linearlen) -
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 |
|---|---|---|---|
| Strategy Length | 21 | int | ybar xbar b a daysSinceEntry |
| Linear Lookback | 9 | int | macdlong MA Predicted Price |
What it calculates
- SMAclose, 200
out · line 23 - SMAclose, 50
outf · line 24 - SMAclose, 90
outn · line 25 - SMAclose, 21
outt · line 26 - SMAclose, 9
outthree · line 27 - MACDpredicted_price, 12, 26, 9
macdLine · line 39 - MACDpredicted_price, 12, 26, 9
signalLine · line 39 - MACDpredicted_price, 12, 26, 9
histLine · line 39 - Risingout, 2
twohunraise · line 45 - Risingoutt, 2
twentyrise · line 46 - RisingmacdLine, 2
macdrise · line 47 - Fallingout, 3
twohunfall · line 64 - Fallingoutt, 2
twentyfall · line 65 - Fallingoutthree, 1
shortmafall · line 66 - FallingmacdLine, 1
macdfall · line 67
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 | 52 |
| Open My Short Entry Id short | (shortCondition) | On | 72 |
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 | +9.7% | +102.5% | 14 | 57% | 3.76 | 2.7% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +3.3% | +5.5% | 20 | 50% | 3.19 | 0.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.2% | +9.8% | 14 | 57% | 1.59 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.1% | +8.4% | 14 | 57% | 2.17 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 14 | 79% | 6.09 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 22 | 41% | 1.75 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.1% | +148.3% | 12 | 17% | 0.28 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.0% | +17.1% | 16 | 38% | 0.54 | 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 | 3 | 67% | $11,629.50 |
| Sideways market | 7 | 43% | $39,731.90 |
| Falling market | 4 | 75% | $46,020.20 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
2.8%
The 95th percentile of the worst drawdown when the same 14 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 1.4% to 2.8%; the tick is what actually happened, 2.3%. Order luck alone moves it this much.
All 14 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 11 Apr 2024 70,631.10 | 27 Jul 2024 67,908.00 | 107 | $2,723.08 |
| long | 27 Jul 2024 67,908.00 | 6 Aug 2024 54,018.80 | 10 | −$13,889.20 |
| short | 6 Aug 2024 54,018.80 | 21 Sep 2024 63,201.10 | 46 | −$9,182.23 |
| long | 21 Sep 2024 63,201.10 | 20 Dec 2024 97,461.90 | 90 | $34,260.80 |
| short | 20 Dec 2024 97,461.90 | 16 Mar 2025 84,338.40 | 86 | $13,123.40 |
| long | 16 Mar 2025 84,338.40 | 29 May 2025 107,782.00 | 74 | $23,443.30 |
| short | 29 May 2025 107,782.00 | 24 Sep 2025 111,999.00 | 118 | −$4,217.02 |
| long | 24 Sep 2025 111,999.00 | 18 Oct 2025 106,432.00 | 24 | −$5,567.12 |
| short | 18 Oct 2025 106,432.00 | 16 Dec 2025 86,432.10 | 59 | $19,999.60 |
| long | 16 Dec 2025 86,432.10 | 31 Jan 2026 84,260.50 | 46 | −$2,171.58 |
| short | 31 Jan 2026 84,260.50 | 3 Mar 2026 68,830.10 | 31 | $15,430.40 |
| long | 3 Mar 2026 68,830.10 | 17 May 2026 78,148.10 | 75 | $9,317.99 |
| short | 17 May 2026 78,148.10 | 12 Jul 2026 63,819.00 | 56 | $14,329.00 |
| long | 12 Jul 2026 63,819.00 | 12 Aug 2026 63,600.00 | 31 | −$219.00 |
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 Cross Trading Strategy this page | — | v5 strategy | +9.7% | 14 | 2.7% |
| Altered OBV On MACD by stocktechbot | sma macd rising falling | v5 strategy | +0.1% | 47 | 4.4% |
| Linear On MACD by stocktechbot | sma macd rising falling | v5 strategy | +1.5% | 16 | 5.6% |
| Rising ADX strategy by dhilipthegreat | sma rising falling | v4 strategy | No trades on this market | ||
| ICHIMOKU Crypto Swing Strategy by exlux99 | sma rising falling | v4 strategy | does not run in Zeal Terminal | ||
| Accumulation Stage Identifier and Strategy around for Trading by stocktechbot | sma macd rising falling | v5 strategy | −0.0% | 16 | 4.1% |
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 17, 18). 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 14 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 17: input() → input.int()
- Line 18: input() → input.int()
⋯// Length of linear regression-len = input(defval = 21, title = 'Strategy Length')+len = input.int(defval = 21, title = 'Strategy Length')-linearlen=input(defval = 9, title = 'Linear Lookback')+linearlen=input.int(defval = 9, title = 'Linear Lookback')[a, b] = linregs(price, vol, len)⋯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: 14 before, 14 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 Cross", overlay=true, margin_long=100, margin_short=0)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 20 ms | Yes 1,000 bars in 53 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 Cross", overlay=true, margin_long=100, margin_short=0)//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 = 'Strategy Length')linearlen=input(defval = 9, title = 'Linear Lookback')[a, b] = linregs(price, vol, len)// Calculate linear regression for stock price based on volume//eps = request.earnings(syminfo.ticker, earnings.actual)//MA For double confirmationout = ta.sma(close, 200)outf = ta.sma(close, 50)outn = ta.sma(close, 90)outt = ta.sma(close, 21)outthree = ta.sma(close, 9)// 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//MACD//[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)// Plot predicted stock priceplot(predicted_price, color=color.rgb(65, 59, 150), linewidth=2, title="Predicted Price")plot(ta.sma(predicted_price,linearlen), color=color.rgb(199, 43, 64), linewidth=2, title="MA Predicted Price")//offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)plot(out, color=color.blue, title="MA200")[macdLine, signalLine, histLine] = ta.macd(predicted_price, 12, 26, 9)//BUY SignallongCondition=falsemafentry =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(macdLine,2)macdlong = ta.crossover(predicted_price, ta.wma(predicted_price,linearlen)) and (signalLine < macdLine)if macdlong and macdrise longCondition := trueif (longCondition) strategy.entry("My Long Entry Id", strategy.long)//Sell SignallastEntryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1)daysSinceEntry = lendaysSinceEntry := int((time - strategy.opentrades.entry_time(strategy.opentrades - 1)) / (24 * 60 * 60 * 1000))percentageChange = (close - lastEntryPrice) / lastEntryPrice * 100//trailChange = (ta.highest(close,daysSinceEntry) - close) / close * 100//label.new(bar_index, high, color=color.black, textcolor=color.white,text=str.tostring(int(trailChange)))shortCondition=falsemafexit =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(macdLine,1)macdsell = macdLine < signalLineif macdfall and macdsell and (macdLine < signalLine) and ta.falling(low,2) shortCondition := trueif (shortCondition) strategy.entry("My Short Entry Id", strategy.short)