Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (2)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$1,076,290$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
+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.

SettingDefaultType and rangeFeeds
Strategy Length21intybar xbar b a daysSinceEntry
Linear Lookback9intmacdlong MA Predicted Price

What it calculates

  • SMAclose, 200out · line 23
  • SMAclose, 50outf · line 24
  • SMAclose, 90outn · line 25
  • SMAclose, 21outt · line 26
  • SMAclose, 9outthree · line 27
  • MACDpredicted_price, 12, 26, 9macdLine · line 39
  • MACDpredicted_price, 12, 26, 9signalLine · line 39
  • MACDpredicted_price, 12, 26, 9histLine · line 39
  • Risingout, 2twohunraise · line 45
  • Risingoutt, 2twentyrise · line 46
  • RisingmacdLine, 2macdrise · line 47
  • Fallingout, 3twohunfall · line 64
  • Fallingoutt, 2twentyfall · line 65
  • Fallingoutthree, 1shortmafall · line 66
  • FallingmacdLine, 1macdfall · 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.

closecloseprice datavolumevolumeprice datalenStrategy Lengthsetting · 21timetimeprice dataopenopenprice datalinearlenLinear Lookbacksetting · 9outSMA→ outoutfSMA→ outfoutnSMA→ outnouttSMA→ outtoutthreeSMA→ outthreemacdLineMACD→ macdLinesignalLineMACD→ signalLinehistLineMACD→ histLinetwohunraiseRising→ twohunraisetwentyriseRising→ twentyrisemacdriseRising→ macdrisetwohunfallFalling→ twohunfalltwentyfallFalling→ twentyfallshortmafallFalling→ shortmafallmacdfallFalling→ macdfallvolvolvalueybar, xbarybar · xbar2 valuesb, ab · a2 valuespricepricevaluepredicted_pricepredicted_pricevaluelastEntryPricelastEntryPricevaluedaysSinceEntrydaysSinceEntryvaluepercentageChangepercentageChangevalueis_betweenis_betweenconditionmafentry, matwohun, mafexit, matexit, matwohundmafentry · matwohun +35 conditionsmacdlongmacdlongconditionmacdsellmacdsellconditionentry:My Long Entry IdOpen My Long Entry Idstrategy.entryentry:My Short Entry IdOpen My Short Entry Idstrategy.entryplot:35Predicted Priceplotplot:36MA Predicted Priceplotplot:38MA200plot

Order rules

OrderFires whenBy defaultLine
Open My Long Entry Id long(longCondition)On52
Open My Short Entry Id short(shortCondition)On72

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.

4/4trades 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+9.7%+102.5%1457%3.762.7%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+3.3%+5.5%2050%3.190.6%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+0.2%+9.8%1457%1.590.2%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+0.1%+8.4%1457%2.170.0%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+0.0%+16.7%1479%6.090.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.0%+21.5%2241%1.750.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−0.1%+148.3%1217%0.280.1%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−0.0%+17.1%1638%0.540.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 market367%$11,629.50
Sideways market743%$39,731.90
Falling market475%$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.

0%3%
All 14 trades
SideEntryExitBarsP&L
short11 Apr 2024 70,631.1027 Jul 2024 67,908.00107$2,723.08
long27 Jul 2024 67,908.006 Aug 2024 54,018.8010−$13,889.20
short6 Aug 2024 54,018.8021 Sep 2024 63,201.1046−$9,182.23
long21 Sep 2024 63,201.1020 Dec 2024 97,461.9090$34,260.80
short20 Dec 2024 97,461.9016 Mar 2025 84,338.4086$13,123.40
long16 Mar 2025 84,338.4029 May 2025 107,782.0074$23,443.30
short29 May 2025 107,782.0024 Sep 2025 111,999.00118−$4,217.02
long24 Sep 2025 111,999.0018 Oct 2025 106,432.0024−$5,567.12
short18 Oct 2025 106,432.0016 Dec 2025 86,432.1059$19,999.60
long16 Dec 2025 86,432.1031 Jan 2026 84,260.5046−$2,171.58
short31 Jan 2026 84,260.503 Mar 2026 68,830.1031$15,430.40
long3 Mar 2026 68,830.1017 May 2026 78,148.1075$9,317.99
short17 May 2026 78,148.1012 Jul 2026 63,819.0056$14,329.00
long12 Jul 2026 63,819.0012 Aug 2026 63,600.0031−$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.

ScriptSharesTypeNetTradesMax drawdown
Linear Cross Trading Strategy this page—v5 strategy+9.7%142.7%
Altered OBV On MACD by stocktechbotsma macd rising fallingv5 strategy+0.1%474.4%
Linear On MACD by stocktechbotsma macd rising fallingv5 strategy+1.5%165.6%
Rising ADX strategy by dhilipthegreatsma rising fallingv4 strategyNo trades on this market
ICHIMOKU Crypto Swing Strategy by exlux99sma rising fallingv4 strategydoes not run in Zeal Terminal
Accumulation Stage Identifier and Strategy around for Trading by stocktechbotsma macd rising fallingv5 strategy−0.0%164.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.

Clean v5

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

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 20 msYes 1,000 bars in 53 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("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)