Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

Stock trending strategy

A Pine Script v4 strategy built on EMA (close, 9) and SMA or EMA (close, 12). It has 2 order rules, 1 entry among them, and trades long only 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 Length 9, Source close and Fast Length 12.

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 Stock trending strategy repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 6 of 6 trades.
How did Stock trending strategy do on BTCUSDT 1D with default settings?
+35.32%
From 26 Jan 2024 to 21 Oct 2026, over 21 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can Stock trending strategy be converted to Pine Script v5?
Yes
The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
Does Stock trending strategy use a stop loss?
No
Every exit waits for a signal; nothing limits a losing trade before it fires.
Does Stock trending strategy run in Zeal Terminal?
Yes
It compiles in 7 ms and runs 1,000 bars in 28 ms, in the browser.

BTCUSDT · 1D · default settings

  • EMA
  • SMA
  • SMA
  • SMA
  • 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 (7)
0.0050,000100,000AprJulOct2025AprJulOct2026AprJulOct$135,327$202,527Equity from $100,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. The script plots nothing itself; the lines and the lower pane are its own calculations, drawn for reference. Zeal Terminal's engine currently reads an order side written as 1 as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 21) so the engine trades the way the script says.
Net profit
+35.32%
$35,324.30
Buy and hold
+102.5%
same period
Closed trades
21
12 won · 9 lost
Profitable
57.1%
of closed trades
Profit factor
1.96
gross profit ÷ gross loss
Max drawdown
14.65%
−$20,397.00

One run with the author's default settings, $100,000 starting capital, commission as the script sets it. 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.

  • Note

    Uses an argument v6 removed

    when = on line 21, 23. The upgraded version below moves each call inside an if block.

Upgrade and export

Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.

  • Upgrading to v5 renames ema → ta.ema, sma → ta.sma, crossover → ta.crossover, crossunder → ta.crossunder.
  • 2 strategy calls use the when = argument (line 21, 23). It is deprecated in v5 and removed in v6, where each call moves inside an if block.

v4 → v5

Verified

Same 21 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 3: //@version=4 → //@version=5
  • Line 7: ema → ta.ema
  • Line 8: input(type = input.integer) → input.int()
  • Line 9: input(type = input.integer) → input.int()
  • Line 10: input(type = input.integer) → input.int()
  • Line 11: input(type = input.bool) → input.bool()
13 more edits
  • Line 12: input(type = input.bool) → input.bool()
  • Line 14: sma → ta.sma
  • Line 14: ema → ta.ema
  • Line 15: sma → ta.sma
  • Line 15: ema → ta.ema
  • Line 17: sma → ta.sma
  • Line 17: ema → ta.ema
  • Line 19: vwap → ta.vwap
  • Line 19: crossover → ta.crossover
  • Line 20: vwap → ta.vwap
  • Line 20: crossunder → ta.crossunder
  • Line 21: strategy.entry(…, 1) → strategy.entry(…, strategy.long)
  • Line 23: crossunder → ta.crossunder
⋯// © exlux99-//@version=4+//@version=5strategy("stock trending strategy",overlay=true)⋯src = input(close, title="Source")-out = ema(src, len)+out = ta.ema(src, len)-fast_length = input(title="Fast Length", type=input.integer, defval=12)+fast_length = input.int(title="Fast Length", defval=12)-slow_length = input(title="Slow Length", type=input.integer, defval=26)+slow_length = input.int(title="Slow Length", defval=26)-signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)+signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9)-sma_source = input(title="Simple MA(Oscillator)", type=input.bool, defval=false)+sma_source = input.bool(title="Simple MA(Oscillator)", defval=false)-sma_signal = input(title="Simple MA(Signal Line)", type=input.bool, defval=false)+sma_signal = input.bool(title="Simple MA(Signal Line)", defval=false)// Calculating-fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)+fast_ma = sma_source ? ta.sma(src, fast_length) : ta.ema(src, fast_length)-slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)+slow_ma = sma_source ? ta.sma(src, slow_length) : ta.ema(src, slow_length)macd = fast_ma - slow_ma-signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)+signal = sma_signal ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)hist = macd - signal-long =  close > vwap and close > out and crossover(hist[1],0) and hist > hist[1]+long =  close > ta.vwap and close > out and ta.crossover(hist[1],0) and hist > hist[1]-short =  close < vwap and close < out and crossunder(hist[1],0) and hist < hist[1]+short =  close < ta.vwap and close < out and ta.crossunder(hist[1],0) and hist < hist[1]-strategy.entry("long",1,when= long)+strategy.entry("long",strategy.long,when= long)//strategy.entry("short",0,when=short)-strategy.close("long", when = crossunder(hist,0))+strategy.close("long", when = ta.crossunder(hist,0))//strategy.close("short", when = crossover(hist,0))

Clean v5

VerifiedDownload the v5 script

Same 21 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.source()
  • Line 21: when = long → if block
  • Line 23: when = ta.crossunder(hist,0) → if block
⋯strategy("stock trending strategy",overlay=true)-len = input(9, minval=1, title="Length")+len = input.int(9, minval=1, title="Length")-src = input(close, title="Source")+src = input.source(close, title="Source")out = ta.ema(src, len)⋯short =  close < ta.vwap and close < out and ta.crossunder(hist[1],0) and hist < hist[1]-strategy.entry("long",strategy.long,when= long)+if long+    strategy.entry("long",strategy.long)//strategy.entry("short",0,when=short)-strategy.close("long", when = ta.crossunder(hist,0))+if ta.crossunder(hist,0)+    strategy.close("long")//strategy.close("short", when = crossover(hist,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: 21 before, 17 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.

⋯// © exlux99-//@version=5+//@version=6strategy("stock trending strategy",overlay=true)⋯

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
Length9int · min 1out
Sourceclosesourceout fast_ma slow_ma
Fast Length12int (written input.integer)fast_ma
Slow Length26int (written input.integer)slow_ma
Signal Smoothing9int (written input.integer) · min 1 · max 50signal
Simple MA(Oscillator)falseboolfast_ma slow_ma
Simple MA(Signal Line)falseboolsignal

What it calculates

  • EMA (ema, ta.ema in v5)Source (close), Length (9)out · line 7
  • SMA / EMA (sma, ta.sma in v5)Source (close), Fast Length (12)fast_ma · line 14
  • SMA / EMA (sma, ta.sma in v5)Source (close), Slow Length (26)slow_ma · line 15
  • SMA / EMA (sma, ta.sma in v5)macd, Signal Smoothing (9)signal · line 17

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 datalenLengthsetting · 9fast_lengthFast Lengthsetting · 12slow_lengthSlow Lengthsetting · 26signal_lengthSignal Smoothingsetting · 9outEMA→ outfast_maSMA / EMA→ fast_maslow_maSMA / EMA→ slow_masignalSMA / EMA→ signalmacdmacdvaluehisthistvaluelonglongconditionshortshortconditionentry:longOpen longstrategy.entryclose:longClose longstrategy.close

Order rules

OrderFires whenBy defaultLine
Open long longlongclose > vwap and close > out and crossover(hist[1],0) and hist > hist[1]On21
Close longcrossunder(hist,0)On23

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.

6/6trades 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+35.3%+102.5%2157%1.9614.7%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−5.2%+5.5%2133%0.647.0%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−1.7%+9.8%1030%0.121.8%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−0.2%+8.4%2218%0.640.4%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−0.1%+16.7%1844%0.680.1%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.0%+21.5%2532%1.370.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−0.2%+148.3%2133%0.570.4%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+0.1%+17.1%2227%1.650.1%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 market850%$17,842.20
Sideways market1164%$13,294.80
Falling market250%$4,187.35

Rising and falling mean the market moved more than 15% over the 90 bars before entry.

How bad could the drawdown be?

19.2%

The 95th percentile of the worst drawdown when the same 21 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 7.9% to 19.2%; the tick is what actually happened, 8.3%. Order luck alone moves it this much.

0%22%
All 21 trades
SideEntryExitBarsP&L
long14 May 2024 62,940.1031 May 2024 68,352.2017$5,412.08
long14 Jul 2024 59,204.001 Aug 2024 64,628.0018$5,424.00
long14 Sep 2024 60,498.002 Oct 2024 60,804.9018$306.93
long16 Oct 2024 67,074.1027 Oct 2024 67,092.8011$18.62
long30 Oct 2024 72,736.404 Nov 2024 68,776.005−$3,960.42
long8 Nov 2024 75,857.9027 Nov 2024 91,965.2019$16,107.30
long7 Jan 2025 102,236.0010 Jan 2025 92,552.503−$9,683.11
long17 Jan 2025 99,987.3029 Jan 2025 101,336.0012$1,348.22
long13 Apr 2025 85,276.9018 May 2025 103,127.0035$17,849.70
long23 May 2025 111,696.0025 May 2025 107,762.002−$3,934.32
long28 Jun 2025 107,048.0022 Jul 2025 117,380.0024$10,332.80
long13 Aug 2025 120,134.0018 Aug 2025 117,405.005−$2,729.08
long8 Sep 2025 111,137.0024 Sep 2025 111,999.0016$861.45
long3 Oct 2025 120,529.0011 Oct 2025 112,774.008−$7,754.86
long22 Dec 2025 88,658.9021 Jan 2026 88,427.7030−$231.21
long8 Apr 2026 71,924.2029 Apr 2026 76,342.8021$4,418.56
long6 May 2026 80,905.5013 May 2026 80,504.507−$401.06
long15 Jun 2026 65,746.4028 Jun 2026 60,029.0013−$5,717.44
long3 Jul 2026 61,560.0028 Jul 2026 63,755.9025$2,195.86
long20 Aug 2026 69,334.803 Sep 2026 77,340.0014$8,005.23
long23 Sep 2026 86,208.6030 Sep 2026 83,663.707−$2,544.91

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
Stock trending strategy this page—v4 strategy+35.3%2114.7%
MACD Long Strat by TheGrindTodayema smav4 strategy+24.0%7920.1%
4-Hour Stochastic EMA Trend by slymnturkogluema smav4 strategyNo trades on this market
4X EMA and volume strategy by SoftKill21ema smav4 strategy+1.8%325.8%
Simple Momentum Strategy Based on SMA, EMA and Volume by slip_streamsma emav4 strategy+7.4%1827.4%
BTC WaveTrend R-R=1-1.5 by TrendCrypto2022ema smav5 strategy+0.2%282.7%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 7 msWith a workaround 1,000 bars in 28 msNot measured
PineTSNo v5 and v6 only—Not measured

Zeal Terminal's engine currently reads an order side written as 1 as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 21) so the engine trades the way the script says.

“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. PineTS runs Pine Script v5 and v6 only.

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/// © exlux99//@version=4strategy("stock trending strategy",overlay=true)len = input(9, minval=1, title="Length")src = input(close, title="Source")out = ema(src, len)fast_length = input(title="Fast Length", type=input.integer, defval=12)slow_length = input(title="Slow Length", type=input.integer, defval=26)signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)sma_source = input(title="Simple MA(Oscillator)", type=input.bool, defval=false)sma_signal = input(title="Simple MA(Signal Line)", type=input.bool, defval=false)// Calculatingfast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)macd = fast_ma - slow_masignal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)hist = macd - signallong =  close > vwap and close > out and crossover(hist[1],0) and hist > hist[1]short =  close < vwap and close < out and crossunder(hist[1],0) and hist < hist[1]strategy.entry("long",1,when= long)//strategy.entry("short",0,when=short)strategy.close("long", when = crossunder(hist,0))//strategy.close("short", when = crossover(hist,0))