Pine Script v5 · Strategy
AlphaTrend Strategy
A Pine Script v5 strategy built on SMA (ta.tr, 14), RSI or MFI (close, 14) and Crossover (AlphaTrend, AlphaTrend[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. 5 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 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 AlphaTrend Strategy do on BTCUSDT 1D with default settings?
- −1.39%
- From 26 Jan 2024 to 21 Oct 2026, over 24 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does AlphaTrend 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: 24 before, 23 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 AlphaTrend Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does AlphaTrend Strategy run in Zeal Terminal?
- Yes
- It compiles in 23 ms and runs 1,000 bars in 46 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −1.39%
- −$13,893.60
- Buy and hold
- +102.5%
- same period
- Closed trades
- 24
- 8 won · 16 lost
- Profitable
- 33.3%
- of closed trades
- Profit factor
- 0.87
- gross profit ÷ gross loss
- Max drawdown
- 4.24%
- −$42,414.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 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
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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Multiplier | 1 | float · step 0.1 | upT downT |
| Common Period | 14 | int | ATR AlphaTrend |
| Src (no label in the script) | close | source | AlphaTrend |
| Show Signals? | true | bool | longCondition shortCondition |
| Change calculation (no volume data)? | false | bool | AlphaTrend |
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 - BarssincebuySignalk
K1 · line 22 - BarssincesellSignalk
K2 · 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open BUY long | (longCondition) | On | 31 |
| Open SELL short | (shortCondition ) | On | 35 |
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 | −1.4% | +102.5% | 24 | 33% | 0.87 | 4.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.2% | +5.5% | 26 | 27% | 0.93 | 2.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.1% | +9.8% | 23 | 35% | 1.10 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.0% | +8.4% | 19 | 37% | 1.21 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.0% | +16.7% | 23 | 30% | 0.61 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 19 | 42% | 1.95 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.0% | +148.3% | 21 | 29% | 0.96 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.0% | +17.1% | 32 | 41% | 0.61 | 0.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 3 | 0% | −$19,604.10 |
| Sideways market | 17 | 35% | −$96.42 |
| Falling market | 4 | 50% | $5,806.91 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
7.4%
The 95th percentile of the worst drawdown when the same 24 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 3.3% to 7.4%; the tick is what actually happened, 3.4%. Order luck alone moves it this much.
All 24 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 18 May 2024 67,024.00 | 20 Jun 2024 64,974.40 | 33 | −$2,049.63 |
| short | 20 Jun 2024 64,974.40 | 19 Jul 2024 63,987.90 | 29 | $986.45 |
| long | 19 Jul 2024 63,987.90 | 5 Aug 2024 58,161.00 | 17 | −$5,826.92 |
| short | 5 Aug 2024 58,161.00 | 25 Aug 2024 64,157.00 | 20 | −$5,996.02 |
| long | 25 Aug 2024 64,157.00 | 5 Sep 2024 57,970.90 | 11 | −$6,186.12 |
| short | 5 Sep 2024 57,970.90 | 20 Sep 2024 62,948.00 | 15 | −$4,977.10 |
| long | 20 Sep 2024 62,948.00 | 4 Oct 2024 60,752.70 | 14 | −$2,195.28 |
| short | 4 Oct 2024 60,752.70 | 17 Oct 2024 67,620.00 | 13 | −$6,867.28 |
| long | 17 Oct 2024 67,620.00 | 29 Dec 2024 95,300.00 | 73 | $27,680.00 |
| short | 29 Dec 2024 95,300.00 | 19 Jan 2025 104,556.00 | 21 | −$9,256.23 |
| long | 19 Jan 2025 104,556.00 | 24 Feb 2025 96,258.00 | 36 | −$8,298.23 |
| short | 24 Feb 2025 96,258.00 | 24 Apr 2025 93,691.10 | 59 | $2,566.93 |
| long | 24 Apr 2025 93,691.10 | 27 Aug 2025 111,763.00 | 125 | $18,072.10 |
| short | 27 Aug 2025 111,763.00 | 13 Sep 2025 116,029.00 | 17 | −$4,266.19 |
| long | 13 Sep 2025 116,029.00 | 27 Sep 2025 109,643.00 | 14 | −$6,385.95 |
| short | 27 Sep 2025 109,643.00 | 3 Oct 2025 120,529.00 | 6 | −$10,885.90 |
| long | 3 Oct 2025 120,529.00 | 16 Oct 2025 110,763.00 | 13 | −$9,766.07 |
| short | 16 Oct 2025 110,763.00 | 15 Jan 2026 96,951.80 | 91 | $13,811.50 |
| long | 15 Jan 2026 96,951.80 | 25 Jan 2026 89,225.30 | 10 | −$7,726.44 |
| short | 25 Jan 2026 89,225.30 | 16 Mar 2026 72,815.30 | 50 | $16,410.10 |
| long | 16 Mar 2026 72,815.30 | 30 Mar 2026 66,010.90 | 14 | −$6,804.32 |
| short | 30 Mar 2026 66,010.90 | 12 Apr 2026 73,043.20 | 13 | −$7,032.23 |
| long | 12 Apr 2026 73,043.20 | 28 May 2026 74,449.30 | 46 | $1,406.15 |
| short | 28 May 2026 74,449.30 | 16 Jul 2026 64,756.30 | 49 | $9,693.03 |
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 |
|---|---|---|---|---|---|
| AlphaTrend Strategy this page | — | v5 strategy | −1.4% | 24 | 4.2% |
| CCU MFI + RSI + STOCH RSI by Bangsoul | mfi rsi sma | v4 strategy | +41.8% | 6 | 37.4% |
| PresentTrend - Strategy [presentTrading] by PresentTrading | sma rsi barssince | v5 strategy | −0.0% | 12 | 6.1% |
| AlphaTrend Strategy with Trailing SL - by rohanarora1313 | sma rsi barssince | v5 strategy | −1.0% | 32 | 5.2% |
| 3 Indicator Strategy (StochRSI, MFI & EMA) With Safety Orders by ggekko21 | rsi sma mfi | v5 strategy | No trades on this market | ||
| SRJ RSI Outperformer Strategy by Sapt_Jash | rsi sma | v5 strategy | +0.6% | 8 | 2.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.
Same 24 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=true)+showsignalsk = input.bool(title='Show Signals?', defval=true)-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 runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 24 before, 23 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.
⋯// developer © KivancOzbilgic-//@version=5+//@version=6strategy('AlphaTrend', shorttitle='AT', overlay=true, format=format.price, precision=2)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 23 ms | Yes 1,000 bars in 46 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/// author © KivancOzbilgic// developer © KivancOzbilgic//@version=5strategy('AlphaTrend', shorttitle='AT', overlay=true, format=format.price, precision=2)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=true)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[2] ? #00E60F : AlphaTrend < AlphaTrend[2] ? #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))longCondition = buySignalk and showsignalsk and O1 > K2if (longCondition) strategy.entry("BUY", strategy.long, comment = "BUY ENTRY")shortCondition = sellSignalk and showsignalsk and O2 > K1if (shortCondition ) strategy.entry("SELL", strategy.short, comment = "SELL ENTRY")// alertcondition(buySignalk and O1 > K2, title='Potential BUY Alarm', message='BUY SIGNAL!')// alertcondition(sellSignalk and O2 > K1, title='Potential SELL Alarm', message='SELL SIGNAL!')// alertcondition(buySignalk[1] and O1[1] > K2, title='Confirmed BUY Alarm', message='BUY SIGNAL APPROVED!')// alertcondition(sellSignalk[1] and O2[1] > K1, title='Confirmed SELL Alarm', message='SELL SIGNAL APPROVED!')// alertcondition(ta.cross(close, AlphaTrend), title='Price Cross Alert', message='Price - AlphaTrend Crossing!')// alertcondition(ta.crossover(low, AlphaTrend), title='Candle CrossOver Alarm', message='LAST BAR is ABOVE ALPHATREND')// alertcondition(ta.crossunder(high, AlphaTrend), title='Candle CrossUnder Alarm', message='LAST BAR is BELOW ALPHATREND!')// alertcondition(ta.cross(close[1], AlphaTrend[1]), title='Price Cross Alert After Bar Close', message='Price - AlphaTrend Crossing!')// alertcondition(ta.crossover(low[1], AlphaTrend[1]), title='Candle CrossOver Alarm After Bar Close', message='LAST BAR is ABOVE ALPHATREND!')// alertcondition(ta.crossunder(high[1], AlphaTrend[1]), title='Candle CrossUnder Alarm After Bar Close', message='LAST BAR is BELOW ALPHATREND!')