Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (3)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOctopen profit % upper bound$3,579,130$202,527Equity from $100,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
+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

Verified

Same 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)⋯

Clean v5

VerifiedDownload the v5 script

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.

SettingDefaultType and rangeFeeds
Trailing Points (no label in the script)1000intexit full
Trailing Offset (no label in the script)300intexit full
Take Profit Points (no label in the script)1000intexit partial

What it calculates

  • Crossover / SMA (crossover, ta.crossover in v5)sma(close, 14), sma(close, 28)longCondition · line 5
  • Crossunder / SMA (crossunder, ta.crossunder in 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.

closecloseprice datahighhighprice datalowlowprice datalongConditionCrossover / SMA→ longConditionshortConditionCrossunder / SMA→ shortConditionposSignposSignvalueentry:My Long Entry IdOpen My Long Entry Idstrategy.entryentry:My Short Entry IdOpen My Short Entry Idstrategy.entryexit:partialexit partialstrategy.exitexit:fullexit fullstrategy.exitplot:19open profit % upper bou…plotplot:20open profit % lower bou…plotfill:21Fillfill

Order rules

OrderFires whenBy defaultLine
Open My Long Entry Id long(longCondition)On7
Open My Short Entry Id short(shortCondition)On10
Exit partial limitevery barOn14
Exit full stopevery barOn15

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.

24/24trades 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+3,479.2%+102.5%86100%—19.0%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+816.7%+5.5%92100%—0.0%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+145.3%+9.8%7596%3.8036.0%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+7.4%+8.4%8690%1.1223.2%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+11.1%+16.7%5277%1.775.3%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+2.5%+21.5%4648%1.501.5%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−22.0%+148.3%6586%0.6558.2%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−3.7%+17.1%6769%0.8816.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 barsTradesWonP&L
Rising market30100%$1,010,100.00
Sideways market36100%$1,481,770.00
Falling market20100%$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.

0%1%
All 86 trades
SideEntryExitBarsP&L
short29 Mar 2024 70,780.6029 Mar 2024 70,770.600$500.00
short29 Mar 2024 70,780.6029 Mar 2024 69,012.000$88,430.00
long6 Apr 2024 67,820.606 Apr 2024 67,830.600$500.00
long6 Apr 2024 67,820.606 Apr 2024 69,689.000$93,418.50
short16 Apr 2024 63,420.0016 Apr 2024 63,410.000$500.00
short16 Apr 2024 63,420.0016 Apr 2024 61,603.000$90,849.50
long19 May 2024 66,915.2019 May 2024 66,925.200$500.00
long19 May 2024 66,915.2019 May 2024 67,697.000$39,090.00
short17 Jun 2024 66,676.9017 Jun 2024 66,666.900$500.00
short17 Jun 2024 66,676.9017 Jun 2024 65,133.000$77,193.00
long21 Jul 2024 67,140.0021 Jul 2024 67,150.000$500.00
long21 Jul 2024 67,140.0021 Jul 2024 68,363.700$61,184.50
short8 Aug 2024 55,133.808 Aug 2024 55,123.800$500.00
short8 Aug 2024 55,133.808 Aug 2024 54,733.000$20,038.00
long27 Aug 2024 62,834.0027 Aug 2024 62,844.000$500.00
long27 Aug 2024 62,834.0027 Aug 2024 63,209.000$18,750.00
short7 Sep 2024 53,963.007 Sep 2024 53,953.000$500.00
short7 Sep 2024 53,963.007 Sep 2024 53,748.500$10,721.50
long22 Sep 2024 63,349.0022 Sep 2024 63,359.000$500.00
long22 Sep 2024 63,349.0022 Sep 2024 63,997.000$32,401.50
short12 Oct 2024 62,540.0012 Oct 2024 62,530.000$500.00
short12 Oct 2024 62,540.0012 Oct 2024 62,490.200$2,488.00
long18 Oct 2024 67,421.8018 Oct 2024 67,431.800$500.00
long18 Oct 2024 67,421.8018 Oct 2024 68,997.000$78,761.00
short30 Dec 2024 93,738.2030 Dec 2024 93,728.200$500.00
short30 Dec 2024 93,738.2030 Dec 2024 91,533.400$110,237.00
long15 Jan 2025 96,560.9015 Jan 2025 96,570.900$500.00
long15 Jan 2025 96,560.9015 Jan 2025 100,679.000$205,904.00
short8 Feb 2025 96,506.808 Feb 2025 96,496.800$500.00
short8 Feb 2025 96,506.808 Feb 2025 95,691.000$40,790.00
long31 Mar 2025 82,390.0031 Mar 2025 82,400.000$500.00
long31 Mar 2025 82,390.0031 Mar 2025 83,940.100$77,504.00
short8 Apr 2025 79,163.208 Apr 2025 79,153.200$500.00
short8 Apr 2025 79,163.208 Apr 2025 76,242.900$146,017.00
long22 Apr 2025 87,516.2022 Apr 2025 87,526.200$500.00
long22 Apr 2025 87,516.2022 Apr 2025 93,885.000$318,439.00
short9 Jun 2025 105,734.009 Jun 2025 105,724.000$500.00
short9 Jun 2025 105,734.009 Jun 2025 105,321.000$20,632.00
long20 Jun 2025 104,659.0020 Jun 2025 104,669.000$500.00
long20 Jun 2025 104,659.0020 Jun 2025 106,522.000$93,153.00
short24 Jun 2025 105,334.0024 Jun 2025 105,324.000$500.00
short24 Jun 2025 105,334.0024 Jun 2025 104,625.000$35,446.00
long5 Jul 2025 107,984.005 Jul 2025 107,994.000$500.00
long5 Jul 2025 107,984.005 Jul 2025 108,418.000$21,665.50
short6 Aug 2025 114,130.006 Aug 2025 114,120.000$500.00
short6 Aug 2025 114,130.006 Aug 2025 113,358.000$38,581.00
long17 Aug 2025 117,381.0017 Aug 2025 117,391.000$500.00
long17 Aug 2025 117,381.0017 Aug 2025 118,572.000$59,567.00
short27 Aug 2025 111,763.0027 Aug 2025 111,753.000$500.00
short27 Aug 2025 111,763.0027 Aug 2025 110,348.000$70,740.00
long15 Sep 2025 115,268.0015 Sep 2025 115,278.000$500.00
long15 Sep 2025 115,268.0015 Sep 2025 116,755.000$74,349.00
short1 Oct 2025 114,049.001 Oct 2025 114,039.000$500.00
short1 Oct 2025 114,049.001 Oct 2025 113,970.000$3,963.50
long6 Oct 2025 123,482.006 Oct 2025 123,492.000$500.00
long6 Oct 2025 123,482.006 Oct 2025 126,197.000$135,715.00
short20 Oct 2025 108,643.0020 Oct 2025 108,633.000$500.00
short20 Oct 2025 108,643.0020 Oct 2025 107,406.000$61,862.50
long11 Dec 2025 92,015.4011 Dec 2025 92,025.400$500.00
long11 Dec 2025 92,015.4011 Dec 2025 93,552.000$76,831.00
short20 Dec 2025 88,136.9020 Dec 2025 88,126.900$500.00
short20 Dec 2025 88,136.9020 Dec 2025 87,798.800$16,909.50
long7 Jan 2026 93,748.0013 Jan 2026 93,758.006$500.00
long7 Jan 2026 93,748.0013 Jan 2026 96,492.006$137,201.00
short29 Jan 2026 89,300.0029 Jan 2026 89,290.000$500.00
short29 Jan 2026 89,300.0029 Jan 2026 83,386.300$295,683.00
long10 Mar 2026 68,432.2010 Mar 2026 68,442.200$500.00
long10 Mar 2026 68,432.2010 Mar 2026 71,774.000$167,092.00
short31 Mar 2026 66,797.4031 Mar 2026 66,787.400$500.00
short31 Mar 2026 66,797.4031 Mar 2026 66,001.100$39,816.50
long14 Apr 2026 74,418.0014 Apr 2026 74,428.000$500.00
long14 Apr 2026 74,418.0014 Apr 2026 76,035.000$80,850.00
short24 May 2026 76,752.0024 May 2026 76,742.000$500.00
short24 May 2026 76,752.0024 May 2026 76,111.000$32,050.00
long14 Jul 2026 62,334.5014 Jul 2026 62,344.500$500.00
long14 Jul 2026 62,334.5014 Jul 2026 65,097.000$138,124.00
short5 Aug 2026 64,106.605 Aug 2026 64,096.600$500.00
short5 Aug 2026 64,106.605 Aug 2026 63,883.000$11,177.50
long20 Aug 2026 69,334.8020 Aug 2026 69,344.800$500.00
long20 Aug 2026 69,334.8020 Aug 2026 73,397.000$203,111.00
short18 Sep 2026 76,417.0018 Sep 2026 76,407.000$500.00
short18 Sep 2026 76,417.0018 Sep 2026 76,299.000$5,900.50
long23 Sep 2026 86,208.6023 Sep 2026 86,218.600$500.00
long23 Sep 2026 86,208.6023 Sep 2026 87,275.500$53,348.50
short16 Oct 2026 82,245.9016 Oct 2026 82,235.900$500.00
short16 Oct 2026 82,245.9016 Oct 2026 80,812.300$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.

ScriptSharesTypeNetTradesMax drawdown
Two level TP + TrailStp exits example this page—v4 strategy+3,479.2%8619.0%
Follow the Crypto Shorts by xtradernetsmav4 strategyNo trades on this market
buy down and sell high trend 200 sma and ma cross by willi130503smav4 strategyNo trades on this market
Mayer Multiple Strategy by xtradernetsmav4 strategyNo trades on this market
Moving Average Cross Strategy early Closing by Geduldtradersmav4 strategy+73.8%20151.5%
MFI Simple Strategy by OztheWozsmav5 strategy−0.2%283.2%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 9 msYes 1,000 bars in 27 msNot measured
PineTSNo 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)