Pine Script v4 · Strategy
Two level TP + TrailStp exits example
A Pine Script v4 strategy built on Crossover or SMA (close, 14, close, 28) and Crossunder or SMA (close, 14, close, 28). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade is 100 contracts and pays no commission. 3 settings; defaults include Trailing Points 1000, Trailing Offset 300 and Take Profit Points 1000.
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 Two level TP + TrailStp exits example repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 24 of 24 trades.
- How did Two level TP + TrailStp exits example do on BTCUSDT 1D with default settings?
- +3,479.17%
- From 26 Jan 2024 to 21 Oct 2026, over 86 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Two level TP + TrailStp exits example 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 Two level TP + TrailStp exits example use a stop loss?
- Yes
- strategy.exit sets a stop on line 15.
- Does Two level TP + TrailStp exits example run in Zeal Terminal?
- Yes
- It compiles in 9 ms and runs 1,000 bars in 27 ms, in the browser.
BTCUSDT · 1D · default settings
- open profit % upper bound
- open profit % lower bound
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +3,479.17%
- $3,479,170.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 86
- 86 won · 0 lost
- Profitable
- 100.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 18.98%
- −$443,697.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 commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
3 settings have no label
In the settings dialog trailingPoints (line 11), trailingOffset (line 12), takeProfitPoints (line 13) show 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.
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 crossover → ta.crossover, sma → ta.sma, crossunder → ta.crossunder.
v4 → v5
VerifiedSame 86 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 5: crossover → ta.crossover
- Line 5: sma → ta.sma
- Line 5: sma → ta.sma
- Line 8: crossunder → ta.crossunder
- Line 8: sma → ta.sma
- Line 8: sma → ta.sma
⋯// © adolgov-//@version=4+//@version=5strategy("Two level TP + TrailStp exits example", overlay=false, default_qty_value = 100)-longCondition = crossover(sma(close, 14), sma(close, 28))+longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))if (longCondition) strategy.entry("My Long Entry Id", strategy.long)-shortCondition = crossunder(sma(close, 14), sma(close, 28))+shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))if (shortCondition)⋯Same 86 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 11: input() → input.int()
- Line 12: input() → input.int()
- Line 13: input() → input.int()
⋯ strategy.entry("My Short Entry Id", strategy.short)-trailingPoints = input(1000)+trailingPoints = input.int(1000)-trailingOffset = input(300)+trailingOffset = input.int(300)-takeProfitPoints = input(1000)+takeProfitPoints = input.int(1000)strategy.exit("partial", qty_percent = 50, profit = takeProfitPoints)⋯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: 86 before, 56 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.
⋯// © adolgov-//@version=5+//@version=6strategy("Two level TP + TrailStp exits example", overlay=false, default_qty_value = 100)⋯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 |
|---|---|---|---|
| Trailing Points (no label in the script) | 1000 | int | exit full |
| Trailing Offset (no label in the script) | 300 | int | exit full |
| Take Profit Points (no label in the script) | 1000 | int | exit partial |
What it calculates
- Crossover / SMA (
crossover,ta.crossoverin v5)sma(close, 14), sma(close, 28)longCondition · line 5 - Crossunder / SMA (
crossunder,ta.crossunderin v5)sma(close, 14), sma(close, 28)shortCondition · line 8
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 My Long Entry Id long | (longCondition) | On | 7 |
| Open My Short Entry Id short | (shortCondition) | On | 10 |
| Exit partial limit | every bar | On | 14 |
| Exit full stop | every bar | On | 15 |
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 | +3,479.2% | +102.5% | 86 | 100% | — | 19.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +816.7% | +5.5% | 92 | 100% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +145.3% | +9.8% | 75 | 96% | 3.80 | 36.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +7.4% | +8.4% | 86 | 90% | 1.12 | 23.2% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +11.1% | +16.7% | 52 | 77% | 1.77 | 5.3% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +2.5% | +21.5% | 46 | 48% | 1.50 | 1.5% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −22.0% | +148.3% | 65 | 86% | 0.65 | 58.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −3.7% | +17.1% | 67 | 69% | 0.88 | 16.4% | 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 | 30 | 100% | $1,010,100.00 |
| Sideways market | 36 | 100% | $1,481,770.00 |
| Falling market | 20 | 100% | $987,300.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
0.0%
The 95th percentile of the worst drawdown when the same 86 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 0.0% to 0.0%; the tick is what actually happened, 0.0%. Order luck alone moves it this much.
All 86 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 29 Mar 2024 70,780.60 | 29 Mar 2024 70,770.60 | 0 | $500.00 |
| short | 29 Mar 2024 70,780.60 | 29 Mar 2024 69,012.00 | 0 | $88,430.00 |
| long | 6 Apr 2024 67,820.60 | 6 Apr 2024 67,830.60 | 0 | $500.00 |
| long | 6 Apr 2024 67,820.60 | 6 Apr 2024 69,689.00 | 0 | $93,418.50 |
| short | 16 Apr 2024 63,420.00 | 16 Apr 2024 63,410.00 | 0 | $500.00 |
| short | 16 Apr 2024 63,420.00 | 16 Apr 2024 61,603.00 | 0 | $90,849.50 |
| long | 19 May 2024 66,915.20 | 19 May 2024 66,925.20 | 0 | $500.00 |
| long | 19 May 2024 66,915.20 | 19 May 2024 67,697.00 | 0 | $39,090.00 |
| short | 17 Jun 2024 66,676.90 | 17 Jun 2024 66,666.90 | 0 | $500.00 |
| short | 17 Jun 2024 66,676.90 | 17 Jun 2024 65,133.00 | 0 | $77,193.00 |
| long | 21 Jul 2024 67,140.00 | 21 Jul 2024 67,150.00 | 0 | $500.00 |
| long | 21 Jul 2024 67,140.00 | 21 Jul 2024 68,363.70 | 0 | $61,184.50 |
| short | 8 Aug 2024 55,133.80 | 8 Aug 2024 55,123.80 | 0 | $500.00 |
| short | 8 Aug 2024 55,133.80 | 8 Aug 2024 54,733.00 | 0 | $20,038.00 |
| long | 27 Aug 2024 62,834.00 | 27 Aug 2024 62,844.00 | 0 | $500.00 |
| long | 27 Aug 2024 62,834.00 | 27 Aug 2024 63,209.00 | 0 | $18,750.00 |
| short | 7 Sep 2024 53,963.00 | 7 Sep 2024 53,953.00 | 0 | $500.00 |
| short | 7 Sep 2024 53,963.00 | 7 Sep 2024 53,748.50 | 0 | $10,721.50 |
| long | 22 Sep 2024 63,349.00 | 22 Sep 2024 63,359.00 | 0 | $500.00 |
| long | 22 Sep 2024 63,349.00 | 22 Sep 2024 63,997.00 | 0 | $32,401.50 |
| short | 12 Oct 2024 62,540.00 | 12 Oct 2024 62,530.00 | 0 | $500.00 |
| short | 12 Oct 2024 62,540.00 | 12 Oct 2024 62,490.20 | 0 | $2,488.00 |
| long | 18 Oct 2024 67,421.80 | 18 Oct 2024 67,431.80 | 0 | $500.00 |
| long | 18 Oct 2024 67,421.80 | 18 Oct 2024 68,997.00 | 0 | $78,761.00 |
| short | 30 Dec 2024 93,738.20 | 30 Dec 2024 93,728.20 | 0 | $500.00 |
| short | 30 Dec 2024 93,738.20 | 30 Dec 2024 91,533.40 | 0 | $110,237.00 |
| long | 15 Jan 2025 96,560.90 | 15 Jan 2025 96,570.90 | 0 | $500.00 |
| long | 15 Jan 2025 96,560.90 | 15 Jan 2025 100,679.00 | 0 | $205,904.00 |
| short | 8 Feb 2025 96,506.80 | 8 Feb 2025 96,496.80 | 0 | $500.00 |
| short | 8 Feb 2025 96,506.80 | 8 Feb 2025 95,691.00 | 0 | $40,790.00 |
| long | 31 Mar 2025 82,390.00 | 31 Mar 2025 82,400.00 | 0 | $500.00 |
| long | 31 Mar 2025 82,390.00 | 31 Mar 2025 83,940.10 | 0 | $77,504.00 |
| short | 8 Apr 2025 79,163.20 | 8 Apr 2025 79,153.20 | 0 | $500.00 |
| short | 8 Apr 2025 79,163.20 | 8 Apr 2025 76,242.90 | 0 | $146,017.00 |
| long | 22 Apr 2025 87,516.20 | 22 Apr 2025 87,526.20 | 0 | $500.00 |
| long | 22 Apr 2025 87,516.20 | 22 Apr 2025 93,885.00 | 0 | $318,439.00 |
| short | 9 Jun 2025 105,734.00 | 9 Jun 2025 105,724.00 | 0 | $500.00 |
| short | 9 Jun 2025 105,734.00 | 9 Jun 2025 105,321.00 | 0 | $20,632.00 |
| long | 20 Jun 2025 104,659.00 | 20 Jun 2025 104,669.00 | 0 | $500.00 |
| long | 20 Jun 2025 104,659.00 | 20 Jun 2025 106,522.00 | 0 | $93,153.00 |
| short | 24 Jun 2025 105,334.00 | 24 Jun 2025 105,324.00 | 0 | $500.00 |
| short | 24 Jun 2025 105,334.00 | 24 Jun 2025 104,625.00 | 0 | $35,446.00 |
| long | 5 Jul 2025 107,984.00 | 5 Jul 2025 107,994.00 | 0 | $500.00 |
| long | 5 Jul 2025 107,984.00 | 5 Jul 2025 108,418.00 | 0 | $21,665.50 |
| short | 6 Aug 2025 114,130.00 | 6 Aug 2025 114,120.00 | 0 | $500.00 |
| short | 6 Aug 2025 114,130.00 | 6 Aug 2025 113,358.00 | 0 | $38,581.00 |
| long | 17 Aug 2025 117,381.00 | 17 Aug 2025 117,391.00 | 0 | $500.00 |
| long | 17 Aug 2025 117,381.00 | 17 Aug 2025 118,572.00 | 0 | $59,567.00 |
| short | 27 Aug 2025 111,763.00 | 27 Aug 2025 111,753.00 | 0 | $500.00 |
| short | 27 Aug 2025 111,763.00 | 27 Aug 2025 110,348.00 | 0 | $70,740.00 |
| long | 15 Sep 2025 115,268.00 | 15 Sep 2025 115,278.00 | 0 | $500.00 |
| long | 15 Sep 2025 115,268.00 | 15 Sep 2025 116,755.00 | 0 | $74,349.00 |
| short | 1 Oct 2025 114,049.00 | 1 Oct 2025 114,039.00 | 0 | $500.00 |
| short | 1 Oct 2025 114,049.00 | 1 Oct 2025 113,970.00 | 0 | $3,963.50 |
| long | 6 Oct 2025 123,482.00 | 6 Oct 2025 123,492.00 | 0 | $500.00 |
| long | 6 Oct 2025 123,482.00 | 6 Oct 2025 126,197.00 | 0 | $135,715.00 |
| short | 20 Oct 2025 108,643.00 | 20 Oct 2025 108,633.00 | 0 | $500.00 |
| short | 20 Oct 2025 108,643.00 | 20 Oct 2025 107,406.00 | 0 | $61,862.50 |
| long | 11 Dec 2025 92,015.40 | 11 Dec 2025 92,025.40 | 0 | $500.00 |
| long | 11 Dec 2025 92,015.40 | 11 Dec 2025 93,552.00 | 0 | $76,831.00 |
| short | 20 Dec 2025 88,136.90 | 20 Dec 2025 88,126.90 | 0 | $500.00 |
| short | 20 Dec 2025 88,136.90 | 20 Dec 2025 87,798.80 | 0 | $16,909.50 |
| long | 7 Jan 2026 93,748.00 | 13 Jan 2026 93,758.00 | 6 | $500.00 |
| long | 7 Jan 2026 93,748.00 | 13 Jan 2026 96,492.00 | 6 | $137,201.00 |
| short | 29 Jan 2026 89,300.00 | 29 Jan 2026 89,290.00 | 0 | $500.00 |
| short | 29 Jan 2026 89,300.00 | 29 Jan 2026 83,386.30 | 0 | $295,683.00 |
| long | 10 Mar 2026 68,432.20 | 10 Mar 2026 68,442.20 | 0 | $500.00 |
| long | 10 Mar 2026 68,432.20 | 10 Mar 2026 71,774.00 | 0 | $167,092.00 |
| short | 31 Mar 2026 66,797.40 | 31 Mar 2026 66,787.40 | 0 | $500.00 |
| short | 31 Mar 2026 66,797.40 | 31 Mar 2026 66,001.10 | 0 | $39,816.50 |
| long | 14 Apr 2026 74,418.00 | 14 Apr 2026 74,428.00 | 0 | $500.00 |
| long | 14 Apr 2026 74,418.00 | 14 Apr 2026 76,035.00 | 0 | $80,850.00 |
| short | 24 May 2026 76,752.00 | 24 May 2026 76,742.00 | 0 | $500.00 |
| short | 24 May 2026 76,752.00 | 24 May 2026 76,111.00 | 0 | $32,050.00 |
| long | 14 Jul 2026 62,334.50 | 14 Jul 2026 62,344.50 | 0 | $500.00 |
| long | 14 Jul 2026 62,334.50 | 14 Jul 2026 65,097.00 | 0 | $138,124.00 |
| short | 5 Aug 2026 64,106.60 | 5 Aug 2026 64,096.60 | 0 | $500.00 |
| short | 5 Aug 2026 64,106.60 | 5 Aug 2026 63,883.00 | 0 | $11,177.50 |
| long | 20 Aug 2026 69,334.80 | 20 Aug 2026 69,344.80 | 0 | $500.00 |
| long | 20 Aug 2026 69,334.80 | 20 Aug 2026 73,397.00 | 0 | $203,111.00 |
| short | 18 Sep 2026 76,417.00 | 18 Sep 2026 76,407.00 | 0 | $500.00 |
| short | 18 Sep 2026 76,417.00 | 18 Sep 2026 76,299.00 | 0 | $5,900.50 |
| long | 23 Sep 2026 86,208.60 | 23 Sep 2026 86,218.60 | 0 | $500.00 |
| long | 23 Sep 2026 86,208.60 | 23 Sep 2026 87,275.50 | 0 | $53,348.50 |
| short | 16 Oct 2026 82,245.90 | 16 Oct 2026 82,235.90 | 0 | $500.00 |
| short | 16 Oct 2026 82,245.90 | 16 Oct 2026 80,812.30 | 0 | $71,684.00 |
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 |
|---|---|---|---|---|---|
| Two level TP + TrailStp exits example this page | — | v4 strategy | +3,479.2% | 86 | 19.0% |
| Follow the Crypto Shorts by xtradernet | sma | v4 strategy | No trades on this market | ||
| buy down and sell high trend 200 sma and ma cross by willi130503 | sma | v4 strategy | No trades on this market | ||
| Mayer Multiple Strategy by xtradernet | sma | v4 strategy | No trades on this market | ||
| Moving Average Cross Strategy early Closing by Geduldtrader | sma | v4 strategy | +73.8% | 20 | 151.5% |
| MFI Simple Strategy by OztheWoz | sma | v5 strategy | −0.2% | 28 | 3.2% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 9 ms | Yes 1,000 bars in 27 ms | Not measured |
| PineTS | No v5 and v6 only | — | 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. 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/// © adolgov//@version=4strategy("Two level TP + TrailStp exits example", overlay=false, default_qty_value = 100)longCondition = crossover(sma(close, 14), sma(close, 28))if (longCondition) strategy.entry("My Long Entry Id", strategy.long)shortCondition = crossunder(sma(close, 14), sma(close, 28))if (shortCondition) strategy.entry("My Short Entry Id", strategy.short)trailingPoints = input(1000)trailingOffset = input(300)takeProfitPoints = input(1000)strategy.exit("partial", qty_percent = 50, profit = takeProfitPoints)strategy.exit("full", trail_points = trailingPoints, trail_offset = trailingOffset)profitPercent(price) => posSign = strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0 (price - strategy.position_avg_price) / strategy.position_avg_price * posSign * 100p1 = plot(profitPercent(high), style=plot.style_linebr, title = "open profit % upper bound")p2 = plot(profitPercent(low), style=plot.style_linebr, title = "open profit % lower bound")fill(p1, p2, color = color.red)