Zeal Terminal Open in Zeal Terminal

Pine Script v5 · Strategy

AlphaTrend Strategy with Trailing SL -

A Pine Script v5 strategy built on SMA (ta.tr, 14), RSI or MFI (close, 14) and Crossover (AlphaTrend, AlphaTrend[2]). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss. Each trade is one contract and pays no commission. 7 settings; defaults include Multiplier 1, Common Period 14 and Src close.

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 AlphaTrend Strategy with Trailing SL - repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 8 of 8 trades.
How did AlphaTrend Strategy with Trailing SL - do on BTCUSDT 1D with default settings?
−1.05%
From 26 Jan 2024 to 21 Oct 2026, over 32 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Does AlphaTrend Strategy with Trailing SL - run in Pine Script v6?
No
The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L58:C165: the argument 'title' is specified more than once. The v6 version is shown for reference and is not offered as a download.
Does AlphaTrend Strategy with Trailing SL - use a stop loss?
Yes
strategy.exit sets a stop on line 64, 66.
Does AlphaTrend Strategy with Trailing SL - run in Zeal Terminal?
Yes
It compiles in 30 ms and runs 1,000 bars in 61 ms, in the browser.

BTCUSDT · 1D · default settings

  • Plot
  • Plot
  • Long Trail Stop
  • Short Trail Stop
  • 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)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$1,009,500$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
−1.05%
−$10,458.90
Buy and hold
+102.5%
same period
Closed trades
32
9 won · 23 lost
Profitable
28.1%
of closed trades
Profit factor
0.90
gross profit ÷ gross loss
Max drawdown
5.19%
−$52,770.70

One run with the author's default settings, $1,000,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 commission

    The backtest assumes free trading. Results on any real exchange will be lower.

  • Note

    1 setting has no label

    In the settings dialog src (line 9) shows an empty name. The labels on this page come from the variable names.

  • 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
Multiplier1float · step 0.1upT downT
Common Period14intATR AlphaTrend
Src (no label in the script)closesourceAlphaTrend
Show Signals?falseboolBUY SELL
Change calculation (no volume data)?falseboolAlphaTrend
Enable Trailing Stop (%)trueboolline 61
Trailing (%)10float · min 0.0 · step 0.1longStopPrice shortStopPrice stopValue

What it calculates

  • SMAta.tr, Common Period (14)ATR · line 8
  • RSI / MFISrc (close), Common Period (14)AlphaTrend · line 15
  • CrossoverAlphaTrend, AlphaTrend[2]buySignalk · line 20
  • CrossunderAlphaTrend, AlphaTrend[2]sellSignalk · line 21
  • BarssincebuySignalkK1 · line 22
  • BarssincesellSignalkK2 · line 23
  • BarssincebuySignalk[1]O1 · line 24
  • BarssincesellSignalk[1]O2 · line 25

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.

high · low · closehigh · low · closeprice dataAPCommon Periodsetting · 14hlc3hlc3price dataclosecloseprice datalowlowprice datacoeffMultipliersetting · 1highhighprice datatrailStopTrailing (%)setting · 10ATRSMA→ ATRAlphaTrendRSI / MFI→ AlphaTrendbuySignalkCrossover→ buySignalksellSignalkCrossunder→ sellSignalkK1Barssince→ K1K2Barssince→ K2O1Barssince→ O1O2Barssince→ O2upTupTvaluedownTdownTvaluelongConditionlongConditionvalueshortConditionshortConditionvaluestopValuestopValuevaluelongStopPricelongStopPriceconditionshortStopPriceshortStopPriceconditioncolor1Colourcolor1entry:LongOpen Longstrategy.entryentry:ShortOpen Shortstrategy.entryexit:Close Longexit Close Longstrategy.exitexit:Close Shortexit Close Shortstrategy.exitplot:17plotplotplot:18plotplotfill:19Fillfillplotshape:26BUYplotshapeplotshape:27SELLplotshapeplot:58Long Trail Stopplotplot:59Short Trail Stopplot

Order rules

OrderFires whenBy defaultLine
Open Long long(longCondition)On37
Open Short short(shortCondition)On40
Exit Close Long stopstrategy.position_size > 0On64
Exit Close Short stopstrategy.position_size < 0On66

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.

8/8trades 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−1.0%+102.5%3228%0.905.2%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−0.2%+5.5%2829%0.952.0%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−0.1%+9.8%4436%0.800.3%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+0.0%+8.4%2138%1.200.0%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−0.0%+16.7%4635%0.890.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.0%+21.5%2143%1.840.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−0.0%+148.3%3531%0.960.1%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−0.0%+17.1%3438%0.590.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 market813%−$22,710.00
Sideways market1932%$1,297.70
Falling market540%$10,953.40

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

How bad could the drawdown be?

7.2%

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

0%8%
All 32 trades
SideEntryExitBarsP&L
long20 Feb 2024 51,774.705 Mar 2024 61,421.1014$9,646.39
long8 Mar 2024 66,823.2015 Mar 2024 65,765.207−$1,058.02
short29 Apr 2024 63,118.604 May 2024 64,201.505−$1,082.85
long18 May 2024 67,024.0018 Jun 2024 64,301.9031−$2,722.05
short20 Jun 2024 64,974.4015 Jul 2024 61,443.6025$3,530.77
long19 Jul 2024 63,987.902 Aug 2024 61,424.9014−$2,563.03
short5 Aug 2024 58,161.008 Aug 2024 59,420.703−$1,259.70
long25 Aug 2024 64,157.0030 Aug 2024 57,798.005−$6,359.02
short5 Sep 2024 57,970.9013 Sep 2024 59,359.308−$1,388.37
long20 Sep 2024 62,948.004 Oct 2024 60,752.7014−$2,195.28
short4 Oct 2024 60,752.7014 Oct 2024 66,359.0010−$5,606.31
long17 Oct 2024 67,620.0020 Dec 2024 95,520.4064$27,900.40
short29 Dec 2024 95,300.006 Jan 2025 102,071.008−$6,771.26
short10 Jan 2025 92,552.5017 Jan 2025 103,990.007−$11,437.20
long19 Jan 2025 104,556.003 Feb 2025 95,529.4015−$9,026.80
short24 Feb 2025 96,258.002 Mar 2025 92,675.106$3,582.90
short23 Mar 2025 83,840.6011 Apr 2025 83,954.7019−$114.08
long24 Apr 2025 93,691.105 Jun 2025 100,527.0042$6,835.51
long10 Jul 2025 111,234.0024 Aug 2025 110,976.0045−$258.22
short27 Aug 2025 111,763.0013 Sep 2025 116,029.0017−$4,266.19
long13 Sep 2025 116,029.0027 Sep 2025 109,643.0014−$6,385.95
short27 Sep 2025 109,643.002 Oct 2025 120,599.005−$10,956.00
long3 Oct 2025 120,529.0010 Oct 2025 112,193.007−$8,336.67
short16 Oct 2025 110,763.003 Dec 2025 93,213.7048$17,549.60
long15 Jan 2026 96,951.8025 Jan 2026 89,225.3010−$7,726.44
short25 Jan 2026 89,225.306 Feb 2026 69,200.9012$20,024.50
short11 Feb 2026 68,841.304 Mar 2026 70,464.0021−$1,622.69
long16 Mar 2026 72,815.3022 Mar 2026 67,396.206−$5,419.05
short30 Mar 2026 66,010.9011 Apr 2026 73,477.1012−$7,466.18
long12 Apr 2026 73,043.2028 May 2026 74,449.3046$1,406.15
short28 May 2026 74,449.3015 Jun 2026 66,973.1018$7,476.22
short27 Jun 2026 60,097.306 Jul 2026 64,487.209−$4,389.92

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
AlphaTrend Strategy with Trailing SL - this page—v5 strategy−1.0%325.2%
CCU MFI + RSI + STOCH RSI by Bangsoulmfi rsi smav4 strategy+41.8%637.4%
AlphaTrend Strategy by only_fibonaccisma rsi barssincev5 strategy−1.4%244.2%
PresentTrend - Strategy [presentTrading] by PresentTradingsma rsi barssincev5 strategy−0.0%126.1%
3 Indicator Strategy (StochRSI, MFI & EMA) With Safety Orders by ggekko21rsi sma mfiv5 strategyNo trades on this market
SRJ RSI Outperformer Strategy by Sapt_Jashrsi smav5 strategy+0.6%82.4%

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 settings use the untyped input() (line 7, 9, 10, 11). 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 32 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 7: input() → input.int()
  • Line 9: input() → input.source()
  • Line 10: input() → input.bool()
  • Line 11: input() → input.bool()
⋯coeff = input.float(1, 'Multiplier', step=0.1)-AP = input(14, 'Common Period')+AP = input.int(14, 'Common Period')ATR = ta.sma(ta.tr, AP)-src = input(close)+src = input.source(close)-showsignalsk = input(title='Show Signals?', defval=false)+showsignalsk = input.bool(title='Show Signals?', defval=false)-novolumedata = input(title='Change calculation (no volume data)?', defval=false)+novolumedata = input.bool(title='Change calculation (no volume data)?', defval=false)upT = low - ATR * coeff⋯

Does it run in Pine Script v6?

No

The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L58:C165: the argument 'title' is specified more than once. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.

⋯// developer © KivancOzbilgic-//@version=5+//@version=6strategy("AlphaTrend Strategy", shorttitle='ATst', overlay=true, format=format.price, precision=2, margin_long=100, margin_short=100)⋯

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 30 msYes 1,000 bars in 61 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/// author © KivancOzbilgic// developer © KivancOzbilgic//@version=5strategy("AlphaTrend Strategy", shorttitle='ATst', overlay=true, format=format.price, precision=2, margin_long=100, margin_short=100)coeff = input.float(1, 'Multiplier', step=0.1)AP = input(14, 'Common Period')ATR = ta.sma(ta.tr, AP)src = input(close)showsignalsk = input(title='Show Signals?', defval=false)novolumedata = input(title='Change calculation (no volume data)?', defval=false)upT = low - ATR * coeffdownT = high + ATR * coeffAlphaTrend = 0.0AlphaTrend := (novolumedata ? ta.rsi(src, AP) >= 50 : ta.mfi(hlc3, AP) >= 50) ? upT < nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : upT : downT > nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : downTcolor1 = AlphaTrend > AlphaTrend[1] ? #00E60F : AlphaTrend < AlphaTrend[1] ? #80000B : AlphaTrend[1] > AlphaTrend[3] ? #00E60F : #80000Bk1 = plot(AlphaTrend, color=color.new(#0022FC, 0), linewidth=3)k2 = plot(AlphaTrend[2], color=color.new(#FC0400, 0), linewidth=3)fill(k1, k2, color=color1)buySignalk = ta.crossover(AlphaTrend, AlphaTrend[2])sellSignalk = ta.crossunder(AlphaTrend, AlphaTrend[2])K1 = ta.barssince(buySignalk)K2 = ta.barssince(sellSignalk)O1 = ta.barssince(buySignalk[1])O2 = ta.barssince(sellSignalk[1])plotshape(buySignalk and showsignalsk and O1 > K2 ? AlphaTrend[2] * 0.9999 : na, title='BUY', text='BUY', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(#0022FC, 0), textcolor=color.new(color.white, 0))plotshape(sellSignalk and showsignalsk and O2 > K1 ? AlphaTrend[2] * 1.0001 : na, title='SELL', text='SELL', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0))// //ENTER SOME SETUP TRADES FOR TSL EXAMPLE// longCondition = ta.crossover(ta.sma(close, 10), ta.sma(close, 20))// if longCondition//     strategy.entry('My Long Entry Id', strategy.long)// shortCondition = ta.crossunder(ta.sma(close, 10), ta.sma(close, 20))// if shortCondition//     strategy.entry('My Short Entry Id', strategy.short)longCondition = buySignalkif (longCondition)    strategy.entry("Long", strategy.long)shortCondition = sellSignalkif (shortCondition)    strategy.entry("Short", strategy.short)    enableTrailing = input.bool(title='Enable Trailing Stop (%)',defval = true)//TRAILING STOP CODEtrailStop = input.float(title='Trailing (%)', minval=0.0, step=0.1, defval=10) * 0.01longStopPrice = 0.0shortStopPrice = 0.0longStopPrice := if strategy.position_size > 0    stopValue = close * (1 - trailStop)    math.max(stopValue, longStopPrice[1])else    0shortStopPrice := if strategy.position_size < 0    stopValue = close * (1 + trailStop)    math.min(stopValue, shortStopPrice[1])else    999999//PLOT TSL LINESplot(series=strategy.position_size > 0 ? longStopPrice : na, color=color.new(color.red, 0), style=plot.style_linebr, linewidth=1, title='Long Trail Stop', offset=1, title='Long Trail Stop')plot(series=strategy.position_size < 0 ? shortStopPrice : na, color=color.new(color.red, 0), style=plot.style_linebr, linewidth=1, title='Short Trail Stop', offset=1, title='Short Trail Stop')    if enableTrailing    //EXIT TRADE @ TSL    if strategy.position_size > 0        strategy.exit(id='Close Long', stop=longStopPrice)    if strategy.position_size < 0        strategy.exit(id='Close Short', stop=shortStopPrice)