Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

Stop loss and Take Profit in $$ 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 3 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 one contract and pays no commission. 2 settings; defaults include Take Profit $$ 200 and Stop Loss $$ 100.

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 Stop loss and Take Profit in $$ example repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 12 of 12 trades.
How did Stop loss and Take Profit in $$ example do on BTCUSDT 1D with default settings?
−3.71%
From 26 Jan 2024 to 21 Oct 2026, over 43 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can Stop loss and Take Profit in $$ 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 Stop loss and Take Profit in $$ example use a stop loss?
Yes
strategy.exit sets a stop on line 18.
Does Stop loss and Take Profit in $$ example run in Zeal Terminal?
Yes
It compiles in 14 ms and runs 1,000 bars in 29 ms, in the browser.

BTCUSDT · 1D · default settings

  • Plot
  • Plot
  • Plot
  • 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 (2)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$96,286$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.71%
−$3,712.95
Buy and hold
+102.5%
same period
Closed trades
43
20 won · 23 lost
Profitable
46.5%
of closed trades
Profit factor
0.90
gross profit ÷ gross loss
Max drawdown
14.14%
−$15,043.70

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

    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, abs → math.abs, sign → math.sign.

v4 → v5

Verified

Same 43 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 5: //@version=4 → //@version=5
  • Line 8: crossover → ta.crossover
  • Line 8: sma → ta.sma
  • Line 8: sma → ta.sma
  • Line 11: crossunder → ta.crossunder
  • Line 11: sma → ta.sma
3 more edits
  • Line 11: sma → ta.sma
  • Line 15: abs → math.abs
  • Line 21: sign → math.sign
⋯// -//@version=4+//@version=5strategy("Stop loss and Take Profit in $$ example", overlay=true)// random entry condition-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)⋯moneyToSLPoints(money) =>-    strategy.position_size !=0 ? (money / syminfo.pointvalue / abs(strategy.position_size)) / syminfo.mintick : na+    strategy.position_size !=0 ? (money / syminfo.pointvalue / math.abs(strategy.position_size)) / syminfo.mintick : nap = moneyToSLPoints(input(200, title = "Take Profit $$"))⋯pointsToPrice(pp) =>-    na(pp) ? na : strategy.position_avg_price + pp * sign(strategy.position_size) * syminfo.mintick+    na(pp) ? na : strategy.position_avg_price + pp * math.sign(strategy.position_size) * syminfo.mintick    ⋯

Clean v5

VerifiedDownload the v5 script

Same 43 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 16: input() → input.int()
  • Line 17: input() → input.int()
⋯    strategy.position_size !=0 ? (money / syminfo.pointvalue / math.abs(strategy.position_size)) / syminfo.mintick : na-p = moneyToSLPoints(input(200, title = "Take Profit $$"))+p = moneyToSLPoints(input.int(200, title = "Take Profit $$"))-l = moneyToSLPoints(input(100, title = "Stop Loss $$"))+l = moneyToSLPoints(input.int(100, title = "Stop Loss $$"))strategy.exit("x", profit = p, loss = l)⋯

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: 43 before, 38 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.

⋯// -//@version=5+//@version=6strategy("Stop loss and Take Profit in $$ example", overlay=true)⋯

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
Take Profit $$200intpp exit x plot
Stop Loss $$100intlp exit x plot

What it calculates

  • Crossover / SMA (crossover, ta.crossover in v5)sma(close, 14), sma(close, 28)longCondition · line 8
  • Crossunder / SMA (crossunder, ta.crossunder in v5)sma(close, 14), sma(close, 28)shortCondition · line 11

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 datapTake Profit $$setting · 200lStop Loss $$setting · 100longConditionCrossover / SMA→ longConditionshortConditionCrossunder / SMA→ shortConditionentry:My Long Entry IdOpen My Long Entry Idstrategy.entryentry:My Short Entry IdOpen My Short Entry Idstrategy.entryexit:xexit xstrategy.exitplot:23plotplotplot:24plotplotplot:25plotplotfill:26Fillfill × 2fill:27Fillfill × 2

Order rules

OrderFires whenBy defaultLine
Open My Long Entry Id long(longCondition)On10
Open My Short Entry Id short(shortCondition)On13
Exit x stop and limitevery barOn18

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.

12/12trades 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.7%+102.5%4347%0.9014.1%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−4.5%+5.5%4628%0.577.1%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+0.7%+9.8%3839%1.321.0%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−0.4%+8.4%4730%0.740.9%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+0.3%+16.7%3259%3.110.1%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.1%+21.5%4141%2.040.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−0.2%+148.3%3625%0.801.0%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−0.2%+17.1%4330%0.550.3%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 market1540%−$6,612.10
Sideways market1856%−$1,468.08
Falling market1040%$4,367.23

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

How bad could the drawdown be?

21.8%

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

0%25%
All 43 trades
SideEntryExitBarsP&L
short29 Mar 2024 70,780.6030 Mar 2024 69,850.501$930.07
long6 Apr 2024 67,820.607 Apr 2024 68,896.001$1,075.37
short16 Apr 2024 63,420.0017 Apr 2024 63,793.401−$373.41
long19 May 2024 66,915.2020 May 2024 66,274.001−$641.20
short17 Jun 2024 66,676.9018 Jun 2024 66,476.901$200.00
long21 Jul 2024 67,140.0022 Jul 2024 68,165.401$1,025.38
short8 Aug 2024 55,133.809 Aug 2024 61,686.001−$6,552.24
long27 Aug 2024 62,834.0028 Aug 2024 59,415.001−$3,419.00
short7 Sep 2024 53,963.008 Sep 2024 54,160.901−$197.89
long22 Sep 2024 63,349.0023 Sep 2024 63,578.801$229.79
short12 Oct 2024 62,540.0013 Oct 2024 63,206.201−$666.24
long18 Oct 2024 67,421.8019 Oct 2024 68,428.001$1,006.21
short30 Dec 2024 93,738.2031 Dec 2024 92,792.101$946.14
long15 Jan 2025 96,560.9016 Jan 2025 100,497.001$3,936.50
short8 Feb 2025 96,506.809 Feb 2025 96,606.801−$100.00
long31 Mar 2025 82,390.001 Apr 2025 82,590.001$200.00
short8 Apr 2025 79,163.209 Apr 2025 76,322.401$2,840.82
long22 Apr 2025 87,516.2023 Apr 2025 93,443.001$5,926.77
short9 Jun 2025 105,734.0010 Jun 2025 110,263.001−$4,529.01
long20 Jun 2025 104,659.0021 Jun 2025 103,298.001−$1,360.61
short24 Jun 2025 105,334.0025 Jun 2025 106,083.001−$749.06
long5 Jul 2025 107,984.006 Jul 2025 108,198.001$213.87
short6 Aug 2025 114,130.007 Aug 2025 114,992.001−$862.52
long17 Aug 2025 117,381.0018 Aug 2025 117,281.001−$100.00
short27 Aug 2025 111,763.0028 Aug 2025 111,262.001$501.21
long15 Sep 2025 115,268.0016 Sep 2025 115,168.001−$100.00
short1 Oct 2025 114,049.002 Oct 2025 118,595.001−$4,546.05
long6 Oct 2025 123,482.007 Oct 2025 124,659.001$1,176.22
short20 Oct 2025 108,643.0021 Oct 2025 110,532.001−$1,889.32
long11 Dec 2025 92,015.4012 Dec 2025 92,513.401$498.00
short20 Dec 2025 88,136.9021 Dec 2025 88,360.901−$223.96
long7 Jan 2026 93,748.008 Jan 2026 91,364.201−$2,383.81
short29 Jan 2026 89,300.0030 Jan 2026 84,650.201$4,649.84
long10 Mar 2026 68,432.2011 Mar 2026 69,948.601$1,516.48
short31 Mar 2026 66,797.401 Apr 2026 68,284.501−$1,487.11
long14 Apr 2026 74,418.0015 Apr 2026 74,131.601−$286.45
short24 May 2026 76,752.0025 May 2026 77,065.001−$312.96
long14 Jul 2026 62,334.5015 Jul 2026 65,044.001$2,709.47
short5 Aug 2026 64,106.606 Aug 2026 64,665.201−$558.69
long20 Aug 2026 69,334.8021 Aug 2026 73,027.001$3,692.24
short18 Sep 2026 76,417.0019 Sep 2026 80,883.901−$4,466.85
long23 Sep 2026 86,208.6024 Sep 2026 84,397.601−$1,810.97
short16 Oct 2026 82,245.9017 Oct 2026 81,615.901$630.02

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
Stop loss and Take Profit in $$ example this page—v4 strategy−3.7%4314.1%
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 14 msYes 1,000 bars in 29 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// @description// //@version=4strategy("Stop loss and Take Profit in $$ example", overlay=true)// random entry conditionlongCondition = 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)moneyToSLPoints(money) =>    strategy.position_size !=0 ? (money / syminfo.pointvalue / abs(strategy.position_size)) / syminfo.mintick : nap = moneyToSLPoints(input(200, title = "Take Profit $$"))l = moneyToSLPoints(input(100, title = "Stop Loss $$"))strategy.exit("x", profit = p, loss = l)// debug plots for visualize SL & TP levelspointsToPrice(pp) =>    na(pp) ? na : strategy.position_avg_price + pp * sign(strategy.position_size) * syminfo.mintick    pp = plot(pointsToPrice(p), style = plot.style_linebr )lp = plot(pointsToPrice(-l), style = plot.style_linebr )avg = plot( strategy.position_avg_price, style = plot.style_linebr )fill(pp, avg, color = color.green)fill(avg, lp, color = color.red)