Pine Script v5 · Strategy
Moving Stop-Loss mechanism + alerts to MT4-MT5
A Pine Script v5 strategy built on SMA or Stoch (smoothK), SMA (k, periodD) and Pivothigh (high, 1). 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 100000 contracts and pays 0.00003 commission. 2 settings; defaults include Take Profit Level 400.
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 Moving Stop-Loss mechanism + alerts to MT4-MT5 repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 62 of 62 trades.
- What alerts does Moving Stop-Loss mechanism + alerts to MT4-MT5 have?
- 4
- alertsyntax_golong; alertsyntax_goshort; alertsyntax_updatelongstoploss; alertsyntax_updateshortstoploss.
- Does Moving Stop-Loss mechanism + alerts to MT4-MT5 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: 191 before, 0 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 Moving Stop-Loss mechanism + alerts to MT4-MT5 use a stop loss?
- Yes
- strategy.exit sets a stop on line 53, 55.
- Does Moving Stop-Loss mechanism + alerts to MT4-MT5 run in Zeal Terminal?
- Yes
- It compiles in 37 ms and runs 1,000 bars in 41 ms, in the browser.
BTCUSDT · 1D · default settings
- stoploss_long
- stoploss_short
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −9,857,600.00%
- −$98,576,000.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 191
- 189 won · 2 lost
- Profitable
- 99.0%
- of closed trades
- Profit factor
- 0.43
- gross profit ÷ gross loss
- Max drawdown
- 4,436.83%
- −$213,012,000.00
One run with the author's default settings, $1,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.
-
Note
1 setting has no label
In the settings dialog TakeProfitLevel (line 14) 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.
-
Note
Uses an argument v6 removed
when = on line 52, 54. The upgraded version below moves each call inside an if block.
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 Level (no label in the script) | 400 | int | alertsyntax_golong alertsyntax_goshort exit XLong exit XShort |
| Enable Stoploss Modification Mechanism | true | bool | UpdateLongStopLoss UpdateShortStopLoss |
What it calculates
- SMA / Stochta.stoch(close, high, low, periodK), smoothK
k · line 19 - SMAk, periodD
d · line 20 - Pivothighhigh, 1, 1
piv_high · line 25 - Pivotlowlow, 1, 1
piv_low · line 26 - Valuewhenpiv_low, piv_low, 0
pl · line 29 - Valuewhenpiv_high, piv_high, 0
ph · line 30
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 Long long | GoLongta.crossover(k, d) and k < 80 | On | 52 |
| Exit XLong stop and limit | every bar | On | 53 |
| Open Short short | GoShortta.crossunder(k, d) and k > 20 | On | 54 |
| Exit XShort stop and limit | every bar | On | 55 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Alerts
| Alert | Call | Message | Line |
|---|---|---|---|
| (no title) | alert | alertsyntax_golong | 58 |
| (no title) | alert | alertsyntax_goshort | 61 |
| (no title) | alert | alertsyntax_updatelongstoploss | 64 |
| (no title) | alert | alertsyntax_updateshortstoploss | 67 |
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 | −9,857,600.0% | +102.5% | 191 | 99% | 0.43 | 4,436.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −30,526,100.0% | +5.5% | 170 | 97% | 0.18 | 2,676.9% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −2,014,700.0% | +9.8% | 188 | 95% | 0.78 | 6,509.5% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +1,571,500.0% | +8.4% | 191 | 87% | 1.31 | 358,800.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +343,800.0% | +16.7% | 188 | 65% | 1.08 | 190.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +331,800.0% | +21.5% | 169 | 39% | 1.30 | 60,400.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +2,082,500.0% | +148.3% | 193 | 86% | 1.46 | 64,000.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −795,200.0% | +17.1% | 174 | 53% | 0.82 | 733.7% | 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 | 70 | 99% | −$22,800,000.00 |
| Sideways market | 68 | 100% | $27,200,000.00 |
| Falling market | 53 | 98% | −$102,976,000.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
10,755,500.0%
The 95th percentile of the worst drawdown when the same 191 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 995.7% to 10,755,500.0%; the tick is what actually happened, 3,086.4%. Order luck alone moves it this much.
The first 150 of 191 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 16 Feb 2024 51,880.00 | 16 Feb 2024 51,876.00 | 0 | $400,000.00 |
| long | 27 Feb 2024 54,476.50 | 27 Feb 2024 54,480.50 | 0 | $400,000.00 |
| short | 2 Mar 2024 62,387.90 | 2 Mar 2024 62,383.90 | 0 | $400,000.00 |
| short | 6 Mar 2024 63,724.00 | 6 Mar 2024 63,720.00 | 0 | $400,000.00 |
| short | 15 Mar 2024 71,388.90 | 15 Mar 2024 71,384.90 | 0 | $400,000.00 |
| long | 24 Mar 2024 63,990.00 | 24 Mar 2024 63,994.00 | 0 | $400,000.00 |
| short | 3 Apr 2024 65,464.00 | 3 Apr 2024 65,460.00 | 0 | $400,000.00 |
| long | 7 Apr 2024 68,896.00 | 7 Apr 2024 68,900.00 | 0 | $400,000.00 |
| short | 13 Apr 2024 67,116.50 | 13 Apr 2024 67,112.50 | 0 | $400,000.00 |
| long | 20 Apr 2024 63,818.00 | 20 Apr 2024 63,822.00 | 0 | $400,000.00 |
| short | 28 Apr 2024 63,462.00 | 28 Apr 2024 63,458.00 | 0 | $400,000.00 |
| long | 29 Apr 2024 63,118.60 | 29 Apr 2024 63,122.60 | 0 | $400,000.00 |
| short | 30 Apr 2024 63,866.00 | 30 Apr 2024 64,370.00 | 0 | −$50,400,000.00 |
| long | 5 May 2024 63,892.00 | 5 May 2024 63,896.00 | 0 | $400,000.00 |
| short | 9 May 2024 61,193.00 | 9 May 2024 61,189.00 | 0 | $400,000.00 |
| long | 14 May 2024 62,940.10 | 14 May 2024 62,944.10 | 0 | $400,000.00 |
| short | 23 May 2024 69,166.60 | 23 May 2024 69,162.60 | 0 | $400,000.00 |
| long | 28 May 2024 69,436.40 | 28 May 2024 69,440.40 | 0 | $400,000.00 |
| short | 29 May 2024 68,398.40 | 29 May 2024 68,394.40 | 0 | $400,000.00 |
| long | 4 Jun 2024 68,809.90 | 4 Jun 2024 68,813.90 | 0 | $400,000.00 |
| short | 9 Jun 2024 69,310.50 | 9 Jun 2024 69,306.50 | 0 | $400,000.00 |
| long | 18 Jun 2024 66,504.30 | 18 Jun 2024 66,508.30 | 0 | $400,000.00 |
| long | 26 Jun 2024 61,806.00 | 26 Jun 2024 61,810.00 | 0 | $400,000.00 |
| short | 30 Jun 2024 60,986.70 | 30 Jun 2024 60,982.70 | 0 | $400,000.00 |
| long | 1 Jul 2024 62,772.00 | 1 Jul 2024 62,776.00 | 0 | $400,000.00 |
| short | 5 Jul 2024 57,050.00 | 5 Jul 2024 57,046.00 | 0 | $400,000.00 |
| long | 9 Jul 2024 56,714.60 | 9 Jul 2024 56,718.60 | 0 | $400,000.00 |
| short | 20 Jul 2024 66,660.00 | 20 Jul 2024 66,656.00 | 0 | $400,000.00 |
| short | 24 Jul 2024 65,936.00 | 24 Jul 2024 65,932.00 | 0 | $400,000.00 |
| short | 30 Jul 2024 66,784.70 | 30 Jul 2024 66,780.70 | 0 | $400,000.00 |
| long | 7 Aug 2024 56,022.00 | 7 Aug 2024 56,026.00 | 0 | $400,000.00 |
| short | 19 Aug 2024 58,427.30 | 19 Aug 2024 58,423.30 | 0 | $400,000.00 |
| long | 22 Aug 2024 61,156.00 | 22 Aug 2024 61,160.00 | 0 | $400,000.00 |
| short | 28 Aug 2024 59,415.00 | 28 Aug 2024 59,411.00 | 0 | $400,000.00 |
| long | 5 Sep 2024 57,970.90 | 5 Sep 2024 57,974.90 | 0 | $400,000.00 |
| long | 8 Sep 2024 54,160.90 | 8 Sep 2024 54,164.90 | 0 | $400,000.00 |
| short | 18 Sep 2024 60,314.00 | 18 Sep 2024 60,310.00 | 0 | $400,000.00 |
| short | 23 Sep 2024 63,578.80 | 23 Sep 2024 63,574.80 | 0 | $400,000.00 |
| short | 1 Oct 2024 63,327.60 | 1 Oct 2024 63,323.60 | 0 | $400,000.00 |
| long | 6 Oct 2024 62,058.00 | 6 Oct 2024 62,062.00 | 0 | $400,000.00 |
| short | 10 Oct 2024 60,636.00 | 10 Oct 2024 60,632.00 | 0 | $400,000.00 |
| long | 12 Oct 2024 62,540.00 | 12 Oct 2024 62,544.00 | 0 | $400,000.00 |
| short | 22 Oct 2024 67,377.50 | 22 Oct 2024 67,373.50 | 0 | $400,000.00 |
| long | 29 Oct 2024 69,962.20 | 29 Oct 2024 69,966.20 | 0 | $400,000.00 |
| short | 2 Nov 2024 69,496.00 | 2 Nov 2024 69,492.00 | 0 | $400,000.00 |
| long | 7 Nov 2024 75,572.00 | 7 Nov 2024 75,576.00 | 0 | $400,000.00 |
| short | 14 Nov 2024 90,375.20 | 14 Nov 2024 90,371.20 | 0 | $400,000.00 |
| short | 25 Nov 2024 97,900.10 | 25 Nov 2024 97,896.10 | 0 | $400,000.00 |
| long | 30 Nov 2024 97,460.00 | 1 Dec 2024 97,464.00 | 1 | $400,000.00 |
| short | 3 Dec 2024 95,840.60 | 3 Dec 2024 95,836.60 | 0 | $400,000.00 |
| long | 5 Dec 2024 98,587.30 | 5 Dec 2024 98,591.30 | 0 | $400,000.00 |
| short | 6 Dec 2024 96,945.60 | 6 Dec 2024 96,941.60 | 0 | $400,000.00 |
| long | 8 Dec 2024 99,832.00 | 8 Dec 2024 99,836.00 | 0 | $400,000.00 |
| short | 9 Dec 2024 101,110.00 | 9 Dec 2024 101,106.00 | 0 | $400,000.00 |
| long | 14 Dec 2024 101,424.00 | 14 Dec 2024 101,428.00 | 0 | $400,000.00 |
| short | 19 Dec 2024 100,204.00 | 19 Dec 2024 100,200.00 | 0 | $400,000.00 |
| long | 25 Dec 2024 98,663.60 | 25 Dec 2024 98,667.60 | 0 | $400,000.00 |
| short | 28 Dec 2024 94,299.00 | 28 Dec 2024 94,295.00 | 0 | $400,000.00 |
| long | 2 Jan 2025 94,591.80 | 2 Jan 2025 94,595.80 | 0 | $400,000.00 |
| short | 8 Jan 2025 96,954.60 | 8 Jan 2025 96,950.60 | 0 | $400,000.00 |
| long | 14 Jan 2025 94,536.10 | 14 Jan 2025 94,540.10 | 0 | $400,000.00 |
| short | 21 Jan 2025 102,260.00 | 21 Jan 2025 102,256.00 | 0 | $400,000.00 |
| long | 25 Jan 2025 104,871.00 | 25 Jan 2025 104,875.00 | 0 | $400,000.00 |
| short | 26 Jan 2025 104,747.00 | 26 Jan 2025 104,743.00 | 0 | $400,000.00 |
| long | 5 Feb 2025 97,763.10 | 5 Feb 2025 97,767.10 | 0 | $400,000.00 |
| short | 8 Feb 2025 96,506.80 | 8 Feb 2025 96,502.80 | 0 | $400,000.00 |
| long | 11 Feb 2025 97,430.80 | 11 Feb 2025 97,434.80 | 0 | $400,000.00 |
| short | 12 Feb 2025 95,778.20 | 12 Feb 2025 95,774.20 | 0 | $400,000.00 |
| long | 13 Feb 2025 97,870.00 | 13 Feb 2025 97,874.00 | 0 | $400,000.00 |
| short | 17 Feb 2025 96,118.10 | 17 Feb 2025 96,114.10 | 0 | $400,000.00 |
| long | 21 Feb 2025 98,305.00 | 21 Feb 2025 98,309.00 | 0 | $400,000.00 |
| short | 25 Feb 2025 91,552.90 | 25 Feb 2025 91,548.90 | 0 | $400,000.00 |
| long | 1 Mar 2025 84,349.90 | 1 Mar 2025 84,353.90 | 0 | $400,000.00 |
| short | 7 Mar 2025 89,931.90 | 7 Mar 2025 89,927.90 | 0 | $400,000.00 |
| long | 8 Mar 2025 86,801.70 | 8 Mar 2025 86,805.70 | 0 | $400,000.00 |
| short | 10 Mar 2025 80,734.50 | 10 Mar 2025 80,730.50 | 0 | $400,000.00 |
| long | 14 Mar 2025 81,115.80 | 14 Mar 2025 81,119.80 | 0 | $400,000.00 |
| short | 28 Mar 2025 87,232.00 | 28 Mar 2025 87,228.00 | 0 | $400,000.00 |
| long | 3 Apr 2025 82,516.30 | 3 Apr 2025 81,278.50 | 0 | −$123,776,000.00 |
| short | 6 Apr 2025 83,538.00 | 6 Apr 2025 83,534.00 | 0 | $400,000.00 |
| long | 8 Apr 2025 79,163.20 | 8 Apr 2025 79,167.20 | 0 | $400,000.00 |
| short | 9 Apr 2025 76,322.40 | 9 Apr 2025 76,318.40 | 0 | $400,000.00 |
| long | 10 Apr 2025 82,615.20 | 10 Apr 2025 82,619.20 | 0 | $400,000.00 |
| short | 27 Apr 2025 94,628.00 | 27 Apr 2025 94,624.00 | 0 | $400,000.00 |
| short | 4 May 2025 95,856.40 | 4 May 2025 95,852.40 | 0 | $400,000.00 |
| long | 9 May 2025 103,262.00 | 9 May 2025 103,266.00 | 0 | $400,000.00 |
| short | 13 May 2025 102,791.00 | 13 May 2025 102,787.00 | 0 | $400,000.00 |
| short | 24 May 2025 107,318.00 | 24 May 2025 107,314.00 | 0 | $400,000.00 |
| long | 28 May 2025 108,938.00 | 28 May 2025 108,942.00 | 0 | $400,000.00 |
| short | 30 May 2025 105,590.00 | 30 May 2025 105,586.00 | 0 | $400,000.00 |
| long | 4 Jun 2025 105,377.00 | 4 Jun 2025 105,381.00 | 0 | $400,000.00 |
| short | 5 Jun 2025 104,697.00 | 5 Jun 2025 104,693.00 | 0 | $400,000.00 |
| long | 8 Jun 2025 105,552.00 | 8 Jun 2025 105,556.00 | 0 | $400,000.00 |
| short | 14 Jun 2025 106,067.00 | 14 Jun 2025 106,063.00 | 0 | $400,000.00 |
| long | 24 Jun 2025 105,334.00 | 24 Jun 2025 105,338.00 | 0 | $400,000.00 |
| short | 2 Jul 2025 105,681.00 | 2 Jul 2025 105,677.00 | 0 | $400,000.00 |
| short | 4 Jul 2025 109,585.00 | 4 Jul 2025 109,581.00 | 0 | $400,000.00 |
| short | 7 Jul 2025 109,204.00 | 7 Jul 2025 109,200.00 | 0 | $400,000.00 |
| long | 10 Jul 2025 111,234.00 | 10 Jul 2025 111,238.00 | 0 | $400,000.00 |
| short | 15 Jul 2025 119,841.00 | 15 Jul 2025 119,837.00 | 0 | $400,000.00 |
| long | 23 Jul 2025 119,954.00 | 23 Jul 2025 119,958.00 | 0 | $400,000.00 |
| short | 24 Jul 2025 118,756.00 | 24 Jul 2025 118,752.00 | 0 | $400,000.00 |
| long | 28 Jul 2025 119,416.00 | 28 Jul 2025 119,420.00 | 0 | $400,000.00 |
| short | 1 Aug 2025 115,764.00 | 1 Aug 2025 115,760.00 | 0 | $400,000.00 |
| long | 5 Aug 2025 115,055.00 | 5 Aug 2025 115,059.00 | 0 | $400,000.00 |
| short | 15 Aug 2025 118,295.00 | 15 Aug 2025 118,291.00 | 0 | $400,000.00 |
| long | 24 Aug 2025 115,438.00 | 24 Aug 2025 115,442.00 | 0 | $400,000.00 |
| long | 29 Aug 2025 112,567.00 | 29 Aug 2025 112,571.00 | 0 | $400,000.00 |
| short | 31 Aug 2025 108,816.00 | 31 Aug 2025 108,812.00 | 0 | $400,000.00 |
| long | 3 Sep 2025 111,240.00 | 3 Sep 2025 111,244.00 | 0 | $400,000.00 |
| short | 15 Sep 2025 115,268.00 | 15 Sep 2025 115,264.00 | 0 | $400,000.00 |
| short | 20 Sep 2025 115,632.00 | 20 Sep 2025 115,628.00 | 0 | $400,000.00 |
| long | 29 Sep 2025 112,164.00 | 29 Sep 2025 112,168.00 | 0 | $400,000.00 |
| short | 7 Oct 2025 124,659.00 | 7 Oct 2025 124,655.00 | 0 | $400,000.00 |
| long | 16 Oct 2025 110,763.00 | 16 Oct 2025 110,767.00 | 0 | $400,000.00 |
| short | 17 Oct 2025 108,194.00 | 17 Oct 2025 108,190.00 | 0 | $400,000.00 |
| long | 21 Oct 2025 110,532.00 | 21 Oct 2025 110,536.00 | 0 | $400,000.00 |
| short | 30 Oct 2025 110,021.00 | 30 Oct 2025 110,017.00 | 0 | $400,000.00 |
| long | 9 Nov 2025 102,313.00 | 9 Nov 2025 102,317.00 | 0 | $400,000.00 |
| short | 14 Nov 2025 99,692.00 | 14 Nov 2025 99,688.00 | 0 | $400,000.00 |
| long | 19 Nov 2025 92,960.80 | 19 Nov 2025 92,964.80 | 0 | $400,000.00 |
| long | 22 Nov 2025 85,129.40 | 22 Nov 2025 85,133.40 | 0 | $400,000.00 |
| short | 2 Dec 2025 86,286.00 | 2 Dec 2025 86,282.00 | 0 | $400,000.00 |
| long | 3 Dec 2025 91,277.90 | 3 Dec 2025 91,281.90 | 0 | $400,000.00 |
| short | 7 Dec 2025 89,236.80 | 7 Dec 2025 89,232.80 | 0 | $400,000.00 |
| long | 10 Dec 2025 92,678.80 | 10 Dec 2025 92,682.80 | 0 | $400,000.00 |
| short | 14 Dec 2025 90,240.00 | 14 Dec 2025 90,236.00 | 0 | $400,000.00 |
| long | 20 Dec 2025 88,136.90 | 20 Dec 2025 88,140.90 | 0 | $400,000.00 |
| short | 25 Dec 2025 87,669.40 | 25 Dec 2025 87,665.40 | 0 | $400,000.00 |
| long | 26 Dec 2025 87,225.30 | 26 Dec 2025 87,229.30 | 0 | $400,000.00 |
| short | 1 Jan 2026 87,648.20 | 1 Jan 2026 87,644.20 | 0 | $400,000.00 |
| long | 3 Jan 2026 89,995.10 | 3 Jan 2026 89,999.10 | 0 | $400,000.00 |
| short | 8 Jan 2026 91,364.20 | 8 Jan 2026 91,360.20 | 0 | $400,000.00 |
| long | 14 Jan 2026 95,414.00 | 14 Jan 2026 95,418.00 | 0 | $400,000.00 |
| short | 18 Jan 2026 95,147.80 | 18 Jan 2026 95,143.80 | 0 | $400,000.00 |
| long | 25 Jan 2026 89,225.30 | 25 Jan 2026 89,229.30 | 0 | $400,000.00 |
| long | 28 Jan 2026 89,250.00 | 28 Jan 2026 89,254.00 | 0 | $400,000.00 |
| short | 1 Feb 2026 78,741.10 | 1 Feb 2026 78,737.10 | 0 | $400,000.00 |
| long | 3 Feb 2026 78,738.60 | 3 Feb 2026 78,742.60 | 0 | $400,000.00 |
| long | 7 Feb 2026 70,580.30 | 7 Feb 2026 70,584.30 | 0 | $400,000.00 |
| short | 12 Feb 2026 67,082.50 | 12 Feb 2026 67,078.50 | 0 | $400,000.00 |
| long | 14 Feb 2026 68,854.00 | 14 Feb 2026 68,858.00 | 0 | $400,000.00 |
| short | 20 Feb 2026 67,003.70 | 20 Feb 2026 66,999.70 | 0 | $400,000.00 |
| long | 26 Feb 2026 67,988.00 | 26 Feb 2026 67,992.00 | 0 | $400,000.00 |
| short | 2 Mar 2026 65,776.50 | 2 Mar 2026 65,772.50 | 0 | $400,000.00 |
| long | 3 Mar 2026 68,830.10 | 3 Mar 2026 68,834.10 | 0 | $400,000.00 |
| short | 7 Mar 2026 68,114.00 | 7 Mar 2026 68,110.00 | 0 | $400,000.00 |
| long | 11 Mar 2026 69,948.60 | 11 Mar 2026 69,952.60 | 0 | $400,000.00 |
| short | 19 Mar 2026 71,246.60 | 19 Mar 2026 71,242.60 | 0 | $400,000.00 |
| long | 26 Mar 2026 71,336.50 | 26 Mar 2026 71,340.50 | 0 | $400,000.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 |
|---|---|---|---|---|---|
| Moving Stop-Loss mechanism + alerts to MT4-MT5 this page | — | v5 strategy | −9,857,600.0% | 191 | 4,436.8% |
| Alert(), alertcondition() or strategy alerts- by Peter_O | sma pivothigh pivotlow valuewhen | v4 strategy | +7,640,000.0% | 191 | 919.9% |
| Quantitative Trend Strategy- Uptrend long by CheatCode1 | sma pivothigh pivotlow valuewhen | v5 strategy | +5.9% | 28 | 32.8% |
| TradingView Alerts to MT4 MT5 - Forex, indices, commodities by Peter_O | sma pivothigh pivotlow valuewhen | v4 strategy | −699,283.0% | 382 | 3,138.7% |
| Qullamaggie Breakout V2 by millerrh | sma pivothigh pivotlow valuewhen | v5 strategy | +35.4% | 33 | 16.9% |
| TradePro's 2 EMA + Stoch RSI + ATR Strategy by PtGambler | sma pivothigh pivotlow valuewhen | v5 strategy | −2.7% | 18 | 22.0% |
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.
- 2 strategy calls use the when = argument (line 52, 54). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- 2 settings use the untyped input() (line 14, 40). 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 191 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 14: input() → input.int()
- Line 40: input() → input.bool()
- Line 52: when = GoLong → if block
- Line 54: when = GoShort → if block
⋯// trade to modify.-TakeProfitLevel = input(400)+TakeProfitLevel = input.int(400)// **** Entries logic, based on Stoch **** {⋯// Stop-Loss Updating mechanism-enable_stoploss_mechanism = input(true, title='Enable Stoploss Modification Mechanism')+enable_stoploss_mechanism = input.bool(true, title='Enable Stoploss Modification Mechanism')UpdateLongStopLoss = strategy.position_size > 0 and strategy.position_size[1] > 0 and piv_low and pl != stoploss_long and not GoLong and enable_stoploss_mechanism⋯// } End of Pivot-points and stop-loss logic-strategy.entry('Long', strategy.long, when=GoLong)+if GoLong+ strategy.entry('Long', strategy.long)strategy.exit('XLong', from_entry='Long', stop=stoploss_long, profit=TakeProfitLevel)-strategy.entry('Short', strategy.short, when=GoShort)+if GoShort+ strategy.entry('Short', strategy.short)strategy.exit('XShort', from_entry='Short', stop=stoploss_short, profit=TakeProfitLevel)⋯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: 191 before, 0 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.
⋯// © Peter_O-//@version=5+//@version=6strategy(title='Moving Stop-Loss mechanism', commission_type=strategy.commission.cash_per_order, commission_value=0.00003, overlay=true, default_qty_value=100000, initial_capital=1000)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 37 ms | Yes 1,000 bars in 41 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/// © Peter_O//@version=5strategy(title='Moving Stop-Loss mechanism', commission_type=strategy.commission.cash_per_order, commission_value=0.00003, overlay=true, default_qty_value=100000, initial_capital=1000)// This script was created for educational purposes only and it is a spin-off of my previous script:// https://www.tradingview.com/script/9MJO3AgE-TradingView-Alerts-to-MT4-MT5-dynamic-variables-NON-REPAINTING/// This spin-off adds very often requested Moving Stop-Loss Mechanism - the logic here moves the stop-loss each time // a new pivot is detected.//// Last lines of the script include alert() function calls, with a syntax compatible with TradingConnector// for execution in Forex/indices/commodities/crypto markets via MetaTrader.// Please note that "tradeid=" variable must be passed with each alert, so that MetaTrader knows which// trade to modify.TakeProfitLevel = input(400)// **** Entries logic, based on Stoch **** {periodK = 13 //input(13, title="K", minval=1)periodD = 3 //input(3, title="D", minval=1)smoothK = 4 //input(4, title="Smooth", minval=1)k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)d = ta.sma(k, periodD)GoLong = ta.crossover(k, d) and k < 80GoShort = ta.crossunder(k, d) and k > 20// } End of entries logic// **** Pivot-points and stop-loss logic **** {piv_high = ta.pivothigh(high, 1, 1)piv_low = ta.pivotlow(low, 1, 1)var float stoploss_long = lowvar float stoploss_short = highpl = ta.valuewhen(piv_low, piv_low, 0)ph = ta.valuewhen(piv_high, piv_high, 0)if GoLong stoploss_long := low < pl ? low : pl stoploss_longif GoShort stoploss_short := high > ph ? high : ph stoploss_shortplot(stoploss_long, color=color.new(color.red, 0), title='stoploss_long')plot(stoploss_short, color=color.new(color.lime, 0), title='stoploss_short')// Stop-Loss Updating mechanismenable_stoploss_mechanism = input(true, title='Enable Stoploss Modification Mechanism')UpdateLongStopLoss = strategy.position_size > 0 and strategy.position_size[1] > 0 and piv_low and pl != stoploss_long and not GoLong and enable_stoploss_mechanismUpdateShortStopLoss = strategy.position_size < 0 and strategy.position_size[1] < 0 and piv_high and ph != stoploss_short and not GoShort and enable_stoploss_mechanismif UpdateLongStopLoss stoploss_long := pl stoploss_longif UpdateShortStopLoss stoploss_short := ph stoploss_shortplotshape(UpdateLongStopLoss ? stoploss_long[1] - 300 * syminfo.mintick : na, location=location.absolute, style=shape.labelup, color=color.new(color.lime, 0), textcolor=color.new(color.white, 0), text='SL\nmove')plotshape(UpdateShortStopLoss ? stoploss_short[1] + 300 * syminfo.mintick : na, location=location.absolute, style=shape.labeldown, color=color.new(color.red, 0), textcolor=color.new(color.black, 0), text='SL\nmove')// } End of Pivot-points and stop-loss logicstrategy.entry('Long', strategy.long, when=GoLong)strategy.exit('XLong', from_entry='Long', stop=stoploss_long, profit=TakeProfitLevel)strategy.entry('Short', strategy.short, when=GoShort)strategy.exit('XShort', from_entry='Short', stop=stoploss_short, profit=TakeProfitLevel)if GoLong alertsyntax_golong = 'long slprice=' + str.tostring(stoploss_long) + ' tp=' + str.tostring(TakeProfitLevel) alert(message=alertsyntax_golong, freq=alert.freq_once_per_bar_close)if GoShort alertsyntax_goshort = 'short slprice=' + str.tostring(stoploss_short) + ' tp=' + str.tostring(TakeProfitLevel) alert(message=alertsyntax_goshort, freq=alert.freq_once_per_bar_close)if UpdateLongStopLoss alertsyntax_updatelongstoploss = 'slmod slprice=' + str.tostring(stoploss_long) alert(message=alertsyntax_updatelongstoploss, freq=alert.freq_once_per_bar_close)if UpdateShortStopLoss alertsyntax_updateshortstoploss = 'slmod slprice=' + str.tostring(stoploss_short) alert(message=alertsyntax_updateshortstoploss, freq=alert.freq_once_per_bar_close)