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
- 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
VerifiedSame 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))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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Length | 9 | int · min 1 | out |
| Source | close | source | out fast_ma slow_ma |
| Fast Length | 12 | int (written input.integer) | fast_ma |
| Slow Length | 26 | int (written input.integer) | slow_ma |
| Signal Smoothing | 9 | int (written input.integer) · min 1 · max 50 | signal |
| Simple MA(Oscillator) | false | bool | fast_ma slow_ma |
| Simple MA(Signal Line) | false | bool | signal |
What it calculates
- EMA (
ema,ta.emain v5)Source (close), Length (9)out · line 7 - SMA / EMA (
sma,ta.smain v5)Source (close), Fast Length (12)fast_ma · line 14 - SMA / EMA (
sma,ta.smain v5)Source (close), Slow Length (26)slow_ma · line 15 - SMA / EMA (
sma,ta.smain 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open long long | longclose > vwap and close > out and crossover(hist[1],0) and hist > hist[1] | On | 21 |
| Close long | crossunder(hist,0) | On | 23 |
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 | +35.3% | +102.5% | 21 | 57% | 1.96 | 14.7% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −5.2% | +5.5% | 21 | 33% | 0.64 | 7.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −1.7% | +9.8% | 10 | 30% | 0.12 | 1.8% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.2% | +8.4% | 22 | 18% | 0.64 | 0.4% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.1% | +16.7% | 18 | 44% | 0.68 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 25 | 32% | 1.37 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.2% | +148.3% | 21 | 33% | 0.57 | 0.4% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.1% | +17.1% | 22 | 27% | 1.65 | 0.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 8 | 50% | $17,842.20 |
| Sideways market | 11 | 64% | $13,294.80 |
| Falling market | 2 | 50% | $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.
All 21 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 14 May 2024 62,940.10 | 31 May 2024 68,352.20 | 17 | $5,412.08 |
| long | 14 Jul 2024 59,204.00 | 1 Aug 2024 64,628.00 | 18 | $5,424.00 |
| long | 14 Sep 2024 60,498.00 | 2 Oct 2024 60,804.90 | 18 | $306.93 |
| long | 16 Oct 2024 67,074.10 | 27 Oct 2024 67,092.80 | 11 | $18.62 |
| long | 30 Oct 2024 72,736.40 | 4 Nov 2024 68,776.00 | 5 | −$3,960.42 |
| long | 8 Nov 2024 75,857.90 | 27 Nov 2024 91,965.20 | 19 | $16,107.30 |
| long | 7 Jan 2025 102,236.00 | 10 Jan 2025 92,552.50 | 3 | −$9,683.11 |
| long | 17 Jan 2025 99,987.30 | 29 Jan 2025 101,336.00 | 12 | $1,348.22 |
| long | 13 Apr 2025 85,276.90 | 18 May 2025 103,127.00 | 35 | $17,849.70 |
| long | 23 May 2025 111,696.00 | 25 May 2025 107,762.00 | 2 | −$3,934.32 |
| long | 28 Jun 2025 107,048.00 | 22 Jul 2025 117,380.00 | 24 | $10,332.80 |
| long | 13 Aug 2025 120,134.00 | 18 Aug 2025 117,405.00 | 5 | −$2,729.08 |
| long | 8 Sep 2025 111,137.00 | 24 Sep 2025 111,999.00 | 16 | $861.45 |
| long | 3 Oct 2025 120,529.00 | 11 Oct 2025 112,774.00 | 8 | −$7,754.86 |
| long | 22 Dec 2025 88,658.90 | 21 Jan 2026 88,427.70 | 30 | −$231.21 |
| long | 8 Apr 2026 71,924.20 | 29 Apr 2026 76,342.80 | 21 | $4,418.56 |
| long | 6 May 2026 80,905.50 | 13 May 2026 80,504.50 | 7 | −$401.06 |
| long | 15 Jun 2026 65,746.40 | 28 Jun 2026 60,029.00 | 13 | −$5,717.44 |
| long | 3 Jul 2026 61,560.00 | 28 Jul 2026 63,755.90 | 25 | $2,195.86 |
| long | 20 Aug 2026 69,334.80 | 3 Sep 2026 77,340.00 | 14 | $8,005.23 |
| long | 23 Sep 2026 86,208.60 | 30 Sep 2026 83,663.70 | 7 | −$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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Stock trending strategy this page | — | v4 strategy | +35.3% | 21 | 14.7% |
| MACD Long Strat by TheGrindToday | ema sma | v4 strategy | +24.0% | 79 | 20.1% |
| 4-Hour Stochastic EMA Trend by slymnturkoglu | ema sma | v4 strategy | No trades on this market | ||
| 4X EMA and volume strategy by SoftKill21 | ema sma | v4 strategy | +1.8% | 3 | 25.8% |
| Simple Momentum Strategy Based on SMA, EMA and Volume by slip_stream | sma ema | v4 strategy | +7.4% | 18 | 27.4% |
| BTC WaveTrend R-R=1-1.5 by TrendCrypto2022 | ema sma | v5 strategy | +0.2% | 28 | 2.7% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 7 ms | With a workaround 1,000 bars in 28 ms | Not measured |
| PineTS | No 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))