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
- 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
VerifiedSame 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 ⋯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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Take Profit $$ | 200 | int | pp exit x plot |
| Stop Loss $$ | 100 | int | lp exit x plot |
What it calculates
- Crossover / SMA (
crossover,ta.crossoverin v5)sma(close, 14), sma(close, 28)longCondition · line 8 - Crossunder / SMA (
crossunder,ta.crossunderin 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open My Long Entry Id long | (longCondition) | On | 10 |
| Open My Short Entry Id short | (shortCondition) | On | 13 |
| Exit x stop and limit | every bar | On | 18 |
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.7% | +102.5% | 43 | 47% | 0.90 | 14.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −4.5% | +5.5% | 46 | 28% | 0.57 | 7.1% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.7% | +9.8% | 38 | 39% | 1.32 | 1.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.4% | +8.4% | 47 | 30% | 0.74 | 0.9% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.3% | +16.7% | 32 | 59% | 3.11 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.1% | +21.5% | 41 | 41% | 2.04 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.2% | +148.3% | 36 | 25% | 0.80 | 1.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.2% | +17.1% | 43 | 30% | 0.55 | 0.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 15 | 40% | −$6,612.10 |
| Sideways market | 18 | 56% | −$1,468.08 |
| Falling market | 10 | 40% | $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.
All 43 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 29 Mar 2024 70,780.60 | 30 Mar 2024 69,850.50 | 1 | $930.07 |
| long | 6 Apr 2024 67,820.60 | 7 Apr 2024 68,896.00 | 1 | $1,075.37 |
| short | 16 Apr 2024 63,420.00 | 17 Apr 2024 63,793.40 | 1 | −$373.41 |
| long | 19 May 2024 66,915.20 | 20 May 2024 66,274.00 | 1 | −$641.20 |
| short | 17 Jun 2024 66,676.90 | 18 Jun 2024 66,476.90 | 1 | $200.00 |
| long | 21 Jul 2024 67,140.00 | 22 Jul 2024 68,165.40 | 1 | $1,025.38 |
| short | 8 Aug 2024 55,133.80 | 9 Aug 2024 61,686.00 | 1 | −$6,552.24 |
| long | 27 Aug 2024 62,834.00 | 28 Aug 2024 59,415.00 | 1 | −$3,419.00 |
| short | 7 Sep 2024 53,963.00 | 8 Sep 2024 54,160.90 | 1 | −$197.89 |
| long | 22 Sep 2024 63,349.00 | 23 Sep 2024 63,578.80 | 1 | $229.79 |
| short | 12 Oct 2024 62,540.00 | 13 Oct 2024 63,206.20 | 1 | −$666.24 |
| long | 18 Oct 2024 67,421.80 | 19 Oct 2024 68,428.00 | 1 | $1,006.21 |
| short | 30 Dec 2024 93,738.20 | 31 Dec 2024 92,792.10 | 1 | $946.14 |
| long | 15 Jan 2025 96,560.90 | 16 Jan 2025 100,497.00 | 1 | $3,936.50 |
| short | 8 Feb 2025 96,506.80 | 9 Feb 2025 96,606.80 | 1 | −$100.00 |
| long | 31 Mar 2025 82,390.00 | 1 Apr 2025 82,590.00 | 1 | $200.00 |
| short | 8 Apr 2025 79,163.20 | 9 Apr 2025 76,322.40 | 1 | $2,840.82 |
| long | 22 Apr 2025 87,516.20 | 23 Apr 2025 93,443.00 | 1 | $5,926.77 |
| short | 9 Jun 2025 105,734.00 | 10 Jun 2025 110,263.00 | 1 | −$4,529.01 |
| long | 20 Jun 2025 104,659.00 | 21 Jun 2025 103,298.00 | 1 | −$1,360.61 |
| short | 24 Jun 2025 105,334.00 | 25 Jun 2025 106,083.00 | 1 | −$749.06 |
| long | 5 Jul 2025 107,984.00 | 6 Jul 2025 108,198.00 | 1 | $213.87 |
| short | 6 Aug 2025 114,130.00 | 7 Aug 2025 114,992.00 | 1 | −$862.52 |
| long | 17 Aug 2025 117,381.00 | 18 Aug 2025 117,281.00 | 1 | −$100.00 |
| short | 27 Aug 2025 111,763.00 | 28 Aug 2025 111,262.00 | 1 | $501.21 |
| long | 15 Sep 2025 115,268.00 | 16 Sep 2025 115,168.00 | 1 | −$100.00 |
| short | 1 Oct 2025 114,049.00 | 2 Oct 2025 118,595.00 | 1 | −$4,546.05 |
| long | 6 Oct 2025 123,482.00 | 7 Oct 2025 124,659.00 | 1 | $1,176.22 |
| short | 20 Oct 2025 108,643.00 | 21 Oct 2025 110,532.00 | 1 | −$1,889.32 |
| long | 11 Dec 2025 92,015.40 | 12 Dec 2025 92,513.40 | 1 | $498.00 |
| short | 20 Dec 2025 88,136.90 | 21 Dec 2025 88,360.90 | 1 | −$223.96 |
| long | 7 Jan 2026 93,748.00 | 8 Jan 2026 91,364.20 | 1 | −$2,383.81 |
| short | 29 Jan 2026 89,300.00 | 30 Jan 2026 84,650.20 | 1 | $4,649.84 |
| long | 10 Mar 2026 68,432.20 | 11 Mar 2026 69,948.60 | 1 | $1,516.48 |
| short | 31 Mar 2026 66,797.40 | 1 Apr 2026 68,284.50 | 1 | −$1,487.11 |
| long | 14 Apr 2026 74,418.00 | 15 Apr 2026 74,131.60 | 1 | −$286.45 |
| short | 24 May 2026 76,752.00 | 25 May 2026 77,065.00 | 1 | −$312.96 |
| long | 14 Jul 2026 62,334.50 | 15 Jul 2026 65,044.00 | 1 | $2,709.47 |
| short | 5 Aug 2026 64,106.60 | 6 Aug 2026 64,665.20 | 1 | −$558.69 |
| long | 20 Aug 2026 69,334.80 | 21 Aug 2026 73,027.00 | 1 | $3,692.24 |
| short | 18 Sep 2026 76,417.00 | 19 Sep 2026 80,883.90 | 1 | −$4,466.85 |
| long | 23 Sep 2026 86,208.60 | 24 Sep 2026 84,397.60 | 1 | −$1,810.97 |
| short | 16 Oct 2026 82,245.90 | 17 Oct 2026 81,615.90 | 1 | $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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Stop loss and Take Profit in $$ example this page | — | v4 strategy | −3.7% | 43 | 14.1% |
| 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 14 ms | Yes 1,000 bars in 29 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// @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)