Pine Script v5 · Strategy
Swing Trend Strategy
A Pine Script v5 strategy built on SMA or EMA or RMA or WMA or VWMA (close, 200) and ATR (10). It has 4 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 commits 100% of equity and pays 0.1 commission. 6 settings; defaults include Source close, Trend Length 200 and Moving Average Type ema.
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 Swing Trend Strategy repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did Swing Trend Strategy do on BTCUSDT 1D with default settings?
- +11.23%
- From 26 Jan 2024 to 21 Oct 2026, over 5 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Swing Trend Strategy run in Pine Script v6?
- No
- The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L55:C65: 'fill' has no 'transp' parameter in Pine Script v6. Use color.new(color, transparency) instead. The v6 version is shown for reference and is not offered as a download.
- Does Swing Trend Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Swing Trend Strategy run in Zeal Terminal?
- Yes
- It compiles in 19 ms and runs 1,000 bars in 36 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +11.23%
- $112.30
- Buy and hold
- +102.5%
- same period
- Closed trades
- 5
- 2 won · 3 lost
- Profitable
- 40.0%
- of closed trades
- Profit factor
- 1.39
- gross profit ÷ gross loss
- Max drawdown
- 16.24%
- −$197.27
One run with the author's default settings, $1,000 starting capital, commission as the script sets it. A long 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
Every trade uses all the equity
default_qty_value is 100% of equity.
-
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 41, 42, 43, 44. The upgraded version below moves each call inside an if block.
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 |
|---|---|---|---|
| Open Short Positions? | false | bool | open_short |
| Exit trade on Moving Average Cross? | true | bool | close_long close_short |
| Source | close | source | ma open_long close_long open_short close_short tcolor |
| Trend Length | 200 | int | ma |
| Moving Average TypeSelect the type of Moving Average to use to calculate the Trend | ema | string · options ['sma', 'ema', 'rma', 'wma', 'vwma'] | line 21 line 23 line 25 line 27 line 29 |
| ATR ThresholdFilter the ranging market using the Average True Range | 1. | float · step 0.5 | uptrend downtrend |
What it calculates
- SMA / EMA / RMA / WMA / VWMASource (close), Trend Length (200)
ma · line 20 - ATR10
atr · line 33
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 | open_longstrategy.position_size == 0 and src > uptrend | On | 41 |
| Close long | close_long | On | 42 |
| Open short short | open_shortuse_short and strategy.position_size == 0 and src < downtrend | On | 43 |
| Close short | close_short | On | 44 |
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.
- 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 | +11.2% | +102.5% | 5 | 40% | 1.39 | 16.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +12.5% | +5.5% | 7 | 14% | 2.14 | 7.8% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +4.1% | +9.8% | 5 | 40% | 1.14 | 29.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +28.5% | +8.4% | 5 | 40% | 4.90 | 7.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −29.3% | +16.7% | 8 | 25% | 0.38 | 35.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +29.6% | +21.5% | 6 | 17% | 4.31 | 10.1% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +12.9% | +148.3% | 6 | 33% | 1.49 | 16.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +21.7% | +17.1% | 2 | 50% | 9.86 | 3.2% | 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 | 0 | — | $0.00 |
| Sideways market | 5 | 40% | $113.42 |
| Falling market | 0 | — | $0.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
23.0%
The 95th percentile of the worst drawdown when the same 5 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 14.5% to 23.0%; the tick is what actually happened, 14.5%. Order luck alone moves it this much.
All 5 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 23 Sep 2024 63,578.80 | 2 Oct 2024 60,804.90 | 9 | −$45.54 |
| long | 15 Oct 2024 66,084.00 | 27 Feb 2025 84,250.10 | 135 | $259.94 |
| long | 3 Mar 2025 94,270.00 | 10 Mar 2025 80,734.50 | 7 | −$176.44 |
| long | 23 Apr 2025 93,443.00 | 18 Oct 2025 106,432.00 | 178 | $141.92 |
| long | 27 Oct 2025 114,559.00 | 31 Oct 2025 108,323.00 | 4 | −$66.46 |
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 |
|---|---|---|---|---|---|
| Swing Trend Strategy this page | — | v5 strategy | +11.2% | 5 | 16.2% |
| High-Low Channel Multi averages Crypto Swing strategy by exlux99 | sma ema wma rma vwma | v4 strategy | No trades on this market | ||
| Open Close Cross Strategy plus Explorer by roses27 | sma ema wma vwma atr | v4 strategy | does not run in Zeal Terminal | ||
| Smoothed Heikin Ashi Trend on Chart - TraderHalai BACKTEST by TraderHalai | sma ema vwma rma | v5 strategy | +1.3% | 113 | 5.6% |
| Strategy Myth-Busting #2 - Braid Filter+ADX+EMA-Trend - [MYN] by myncrypto | sma ema wma atr | v5 strategy | −47.2% | 4 | 48.6% |
| Nifty & BN 2 Candle Theory Back Testing and Alert Notification by abdullabashaa | ema rma atr vwma | v5 strategy | −1.9% | 3 | 1.9% |
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.
- 4 strategy calls use the when = argument (line 41, 42, 43, 44). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- No v6-only features. v6 is not needed to run this script.
Same 5 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 41: when = open_long → if block
- Line 42: when = close_long → if block
- Line 43: when = open_short → if block
- Line 44: when = close_short → if block
⋯close_short = exit_type ? strategy.position_size < 0 and src > trend : strategy.position_size < 0 and src > uptrend-strategy.entry('long', strategy.long, when=open_long)+if open_long+ strategy.entry('long', strategy.long)-strategy.close('long', when=close_long)+if close_long+ strategy.close('long')-strategy.entry('short', strategy.short, when=open_short)+if open_short+ strategy.entry('short', strategy.short)-strategy.close('short', when=close_short)+if close_short+ strategy.close('short')// ------------------ PLOTTING AND COLORING ------------------⋯Does it run in Pine Script v6?
No
The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L55:C65: 'fill' has no 'transp' parameter in Pine Script v6. Use color.new(color, transparency) instead. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © Inkedlau-//@version=5+//@version=6strategy('Swing Trend Strategy', overlay=true, pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=1000, commission_value=0.1)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 19 ms | Yes 1,000 bars in 36 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/// © Inkedlau//@version=5strategy('Swing Trend Strategy', overlay=true, pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=1000, commission_value=0.1)use_short = input.bool(false, 'Open Short Positions?')exit_type = input.bool(true, 'Exit trade on Moving Average Cross?')src = input.source(close, 'Source')len = input.int(200, 'Trend Length')ma_type = input.string('ema', 'Moving Average Type', options=['sma', 'ema', 'rma', 'wma', 'vwma'], tooltip='Select the type of Moving Average to use to calculate the Trend')atr_multiplier = input.float(1., 'ATR Threshold', step=0.5, tooltip='Filter the ranging market using the Average True Range')// ----------------------- DESCRIPTION -----------------------// THIS SCRIPT IS A TREND FOLLOWING SYSTEM THAT USES A COMBINATION OF MOVING AVERAGE AND AVERAGE TRUE RANGE// TO SPOT THE TRENDS AND ENTER THE MARKET ACCODINGLY.// THE MARKET IS CONSIDERED IN AN UPTREND WHEN THE PRICE CLOSES ABOVE THE MOVING AVERAGE + THE AVERAGE TRUE RANGE OF THE LAST 10 PERIODS// THE MARKET IS CONSIDERED IN AN DOWNTREND WHEN THE PRICE CLOSES BLOW THE MOVING AVERAGE - THE AVERAGE TRUE RANGE OF THE LAST 10 PERIODS// BY DEFAULT, THE STRATEGY WILL ENTER LONG WHEN AN UPTREND IS SPOTTED, THEN CLOSES WHEN THE PRICE CLOSES BELOW THE MOVING AVERAGE// THE STRATEGY WILL ENTER SHORT WHEN A DOWNTREND IS SPOTTED, THEN CLOSES WHEN THE PRICE CLOSES ABOVE THE MOVING AVERAGE// ------------------ INDICATORS CALCULATION------------------my_ma()=> ma = close if ma_type == 'sma' ma := ta.sma(src, len) if ma_type == 'ema' ma := ta.ema(src, len) if ma_type == 'rma' ma := ta.rma(src, len) if ma_type == 'wma' ma := ta.wma(src, len) if ma_type == 'vwma' ma := ta.vwma(src, len) matrend = my_ma()atr = ta.atr(10)uptrend = trend + atr * atr_multiplierdowntrend = trend - atr * atr_multiplier// ---------------- ENTRY AND EXIT CONDITIONS ----------------open_long = strategy.position_size == 0 and src > uptrendclose_long = exit_type ? strategy.position_size > 0 and src < trend : strategy.position_size > 0 and src < downtrendopen_short = use_short and strategy.position_size == 0 and src < downtrendclose_short = exit_type ? strategy.position_size < 0 and src > trend : strategy.position_size < 0 and src > uptrendstrategy.entry('long', strategy.long, when=open_long)strategy.close('long', when=close_long)strategy.entry('short', strategy.short, when=open_short)strategy.close('short', when=close_short)// ------------------ PLOTTING AND COLORING ------------------tcolor = src > uptrend ? color.green : src < downtrend ? color.red : naptrend = plot(trend, color=color.blue, linewidth=1)puptrend = plot(uptrend, color=color.green, linewidth=1)pdowntrend = plot(downtrend, color=color.red, linewidth=1)pclose = plot(close, color=na)fill(puptrend, pclose, color=close > uptrend ? color.green : na, transp = 90)fill(pdowntrend, pclose, color=close < downtrend ? color.red : na, transp = 90)