Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (2)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$-98,568,000$2,025Equity from $1,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
−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.

SettingDefaultType and rangeFeeds
Take Profit Level (no label in the script)400intalertsyntax_golong alertsyntax_goshort exit XLong exit XShort
Enable Stoploss Modification MechanismtrueboolUpdateLongStopLoss UpdateShortStopLoss

What it calculates

  • SMA / Stochta.stoch(close, high, low, periodK), smoothKk · line 19
  • SMAk, periodDd · line 20
  • Pivothighhigh, 1, 1piv_high · line 25
  • Pivotlowlow, 1, 1piv_low · line 26
  • Valuewhenpiv_low, piv_low, 0pl · line 29
  • Valuewhenpiv_high, piv_high, 0ph · 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.

closecloseprice datahighhighprice datalowlowprice dataTakeProfitLevelTake Profit Levelsetting · 400kSMA / Stoch→ kdSMA→ dpiv_highPivothigh→ piv_highpiv_lowPivotlow→ piv_lowplValuewhen→ plphValuewhen→ phstoploss_longstoploss_longvaluestoploss_shortstoploss_shortvaluealertsyntax_golongalertsyntax_golongvaluealertsyntax_goshortalertsyntax_goshortvaluealertsyntax_updatelongstoplossalertsyntax_updatelongs…valuealertsyntax_updateshortstoplossalertsyntax_updateshort…valueGoLongGoLongconditionGoShortGoShortconditionUpdateLongStopLossUpdateLongStopLossconditionUpdateShortStopLossUpdateShortStopLossconditionentry:LongOpen Longstrategy.entryexit:XLongexit XLongstrategy.exitentry:ShortOpen Shortstrategy.entryexit:XShortexit XShortstrategy.exitplot:37stoploss_longplotplot:38stoploss_shortplotplotshape:49plotshapeplotshapeplotshape:50plotshapeplotshapealert:58alertalertalert:61alertalertalert:64alertalertalert:67alertalert

Order rules

OrderFires whenBy defaultLine
Open Long longGoLongta.crossover(k, d) and k < 80On52
Exit XLong stop and limitevery barOn53
Open Short shortGoShortta.crossunder(k, d) and k > 20On54
Exit XShort stop and limitevery barOn55

Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.

Alerts

AlertCallMessageLine
(no title)alertalertsyntax_golong58
(no title)alertalertsyntax_goshort61
(no title)alertalertsyntax_updatelongstoploss64
(no title)alertalertsyntax_updateshortstoploss67

Does it repaint?

No

No repainting patterns in the source, and the live replay matched history.

62/62trades 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−9,857,600.0%+102.5%19199%0.434,436.8%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−30,526,100.0%+5.5%17097%0.182,676.9%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−2,014,700.0%+9.8%18895%0.786,509.5%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+1,571,500.0%+8.4%19187%1.31358,800.0%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+343,800.0%+16.7%18865%1.08190.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+331,800.0%+21.5%16939%1.3060,400.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+2,082,500.0%+148.3%19386%1.4664,000.0%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−795,200.0%+17.1%17453%0.82733.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 barsTradesWonP&L
Rising market7099%−$22,800,000.00
Sideways market68100%$27,200,000.00
Falling market5398%−$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.

0%12,368,825%
The first 150 of 191 trades
SideEntryExitBarsP&L
short16 Feb 2024 51,880.0016 Feb 2024 51,876.000$400,000.00
long27 Feb 2024 54,476.5027 Feb 2024 54,480.500$400,000.00
short2 Mar 2024 62,387.902 Mar 2024 62,383.900$400,000.00
short6 Mar 2024 63,724.006 Mar 2024 63,720.000$400,000.00
short15 Mar 2024 71,388.9015 Mar 2024 71,384.900$400,000.00
long24 Mar 2024 63,990.0024 Mar 2024 63,994.000$400,000.00
short3 Apr 2024 65,464.003 Apr 2024 65,460.000$400,000.00
long7 Apr 2024 68,896.007 Apr 2024 68,900.000$400,000.00
short13 Apr 2024 67,116.5013 Apr 2024 67,112.500$400,000.00
long20 Apr 2024 63,818.0020 Apr 2024 63,822.000$400,000.00
short28 Apr 2024 63,462.0028 Apr 2024 63,458.000$400,000.00
long29 Apr 2024 63,118.6029 Apr 2024 63,122.600$400,000.00
short30 Apr 2024 63,866.0030 Apr 2024 64,370.000−$50,400,000.00
long5 May 2024 63,892.005 May 2024 63,896.000$400,000.00
short9 May 2024 61,193.009 May 2024 61,189.000$400,000.00
long14 May 2024 62,940.1014 May 2024 62,944.100$400,000.00
short23 May 2024 69,166.6023 May 2024 69,162.600$400,000.00
long28 May 2024 69,436.4028 May 2024 69,440.400$400,000.00
short29 May 2024 68,398.4029 May 2024 68,394.400$400,000.00
long4 Jun 2024 68,809.904 Jun 2024 68,813.900$400,000.00
short9 Jun 2024 69,310.509 Jun 2024 69,306.500$400,000.00
long18 Jun 2024 66,504.3018 Jun 2024 66,508.300$400,000.00
long26 Jun 2024 61,806.0026 Jun 2024 61,810.000$400,000.00
short30 Jun 2024 60,986.7030 Jun 2024 60,982.700$400,000.00
long1 Jul 2024 62,772.001 Jul 2024 62,776.000$400,000.00
short5 Jul 2024 57,050.005 Jul 2024 57,046.000$400,000.00
long9 Jul 2024 56,714.609 Jul 2024 56,718.600$400,000.00
short20 Jul 2024 66,660.0020 Jul 2024 66,656.000$400,000.00
short24 Jul 2024 65,936.0024 Jul 2024 65,932.000$400,000.00
short30 Jul 2024 66,784.7030 Jul 2024 66,780.700$400,000.00
long7 Aug 2024 56,022.007 Aug 2024 56,026.000$400,000.00
short19 Aug 2024 58,427.3019 Aug 2024 58,423.300$400,000.00
long22 Aug 2024 61,156.0022 Aug 2024 61,160.000$400,000.00
short28 Aug 2024 59,415.0028 Aug 2024 59,411.000$400,000.00
long5 Sep 2024 57,970.905 Sep 2024 57,974.900$400,000.00
long8 Sep 2024 54,160.908 Sep 2024 54,164.900$400,000.00
short18 Sep 2024 60,314.0018 Sep 2024 60,310.000$400,000.00
short23 Sep 2024 63,578.8023 Sep 2024 63,574.800$400,000.00
short1 Oct 2024 63,327.601 Oct 2024 63,323.600$400,000.00
long6 Oct 2024 62,058.006 Oct 2024 62,062.000$400,000.00
short10 Oct 2024 60,636.0010 Oct 2024 60,632.000$400,000.00
long12 Oct 2024 62,540.0012 Oct 2024 62,544.000$400,000.00
short22 Oct 2024 67,377.5022 Oct 2024 67,373.500$400,000.00
long29 Oct 2024 69,962.2029 Oct 2024 69,966.200$400,000.00
short2 Nov 2024 69,496.002 Nov 2024 69,492.000$400,000.00
long7 Nov 2024 75,572.007 Nov 2024 75,576.000$400,000.00
short14 Nov 2024 90,375.2014 Nov 2024 90,371.200$400,000.00
short25 Nov 2024 97,900.1025 Nov 2024 97,896.100$400,000.00
long30 Nov 2024 97,460.001 Dec 2024 97,464.001$400,000.00
short3 Dec 2024 95,840.603 Dec 2024 95,836.600$400,000.00
long5 Dec 2024 98,587.305 Dec 2024 98,591.300$400,000.00
short6 Dec 2024 96,945.606 Dec 2024 96,941.600$400,000.00
long8 Dec 2024 99,832.008 Dec 2024 99,836.000$400,000.00
short9 Dec 2024 101,110.009 Dec 2024 101,106.000$400,000.00
long14 Dec 2024 101,424.0014 Dec 2024 101,428.000$400,000.00
short19 Dec 2024 100,204.0019 Dec 2024 100,200.000$400,000.00
long25 Dec 2024 98,663.6025 Dec 2024 98,667.600$400,000.00
short28 Dec 2024 94,299.0028 Dec 2024 94,295.000$400,000.00
long2 Jan 2025 94,591.802 Jan 2025 94,595.800$400,000.00
short8 Jan 2025 96,954.608 Jan 2025 96,950.600$400,000.00
long14 Jan 2025 94,536.1014 Jan 2025 94,540.100$400,000.00
short21 Jan 2025 102,260.0021 Jan 2025 102,256.000$400,000.00
long25 Jan 2025 104,871.0025 Jan 2025 104,875.000$400,000.00
short26 Jan 2025 104,747.0026 Jan 2025 104,743.000$400,000.00
long5 Feb 2025 97,763.105 Feb 2025 97,767.100$400,000.00
short8 Feb 2025 96,506.808 Feb 2025 96,502.800$400,000.00
long11 Feb 2025 97,430.8011 Feb 2025 97,434.800$400,000.00
short12 Feb 2025 95,778.2012 Feb 2025 95,774.200$400,000.00
long13 Feb 2025 97,870.0013 Feb 2025 97,874.000$400,000.00
short17 Feb 2025 96,118.1017 Feb 2025 96,114.100$400,000.00
long21 Feb 2025 98,305.0021 Feb 2025 98,309.000$400,000.00
short25 Feb 2025 91,552.9025 Feb 2025 91,548.900$400,000.00
long1 Mar 2025 84,349.901 Mar 2025 84,353.900$400,000.00
short7 Mar 2025 89,931.907 Mar 2025 89,927.900$400,000.00
long8 Mar 2025 86,801.708 Mar 2025 86,805.700$400,000.00
short10 Mar 2025 80,734.5010 Mar 2025 80,730.500$400,000.00
long14 Mar 2025 81,115.8014 Mar 2025 81,119.800$400,000.00
short28 Mar 2025 87,232.0028 Mar 2025 87,228.000$400,000.00
long3 Apr 2025 82,516.303 Apr 2025 81,278.500−$123,776,000.00
short6 Apr 2025 83,538.006 Apr 2025 83,534.000$400,000.00
long8 Apr 2025 79,163.208 Apr 2025 79,167.200$400,000.00
short9 Apr 2025 76,322.409 Apr 2025 76,318.400$400,000.00
long10 Apr 2025 82,615.2010 Apr 2025 82,619.200$400,000.00
short27 Apr 2025 94,628.0027 Apr 2025 94,624.000$400,000.00
short4 May 2025 95,856.404 May 2025 95,852.400$400,000.00
long9 May 2025 103,262.009 May 2025 103,266.000$400,000.00
short13 May 2025 102,791.0013 May 2025 102,787.000$400,000.00
short24 May 2025 107,318.0024 May 2025 107,314.000$400,000.00
long28 May 2025 108,938.0028 May 2025 108,942.000$400,000.00
short30 May 2025 105,590.0030 May 2025 105,586.000$400,000.00
long4 Jun 2025 105,377.004 Jun 2025 105,381.000$400,000.00
short5 Jun 2025 104,697.005 Jun 2025 104,693.000$400,000.00
long8 Jun 2025 105,552.008 Jun 2025 105,556.000$400,000.00
short14 Jun 2025 106,067.0014 Jun 2025 106,063.000$400,000.00
long24 Jun 2025 105,334.0024 Jun 2025 105,338.000$400,000.00
short2 Jul 2025 105,681.002 Jul 2025 105,677.000$400,000.00
short4 Jul 2025 109,585.004 Jul 2025 109,581.000$400,000.00
short7 Jul 2025 109,204.007 Jul 2025 109,200.000$400,000.00
long10 Jul 2025 111,234.0010 Jul 2025 111,238.000$400,000.00
short15 Jul 2025 119,841.0015 Jul 2025 119,837.000$400,000.00
long23 Jul 2025 119,954.0023 Jul 2025 119,958.000$400,000.00
short24 Jul 2025 118,756.0024 Jul 2025 118,752.000$400,000.00
long28 Jul 2025 119,416.0028 Jul 2025 119,420.000$400,000.00
short1 Aug 2025 115,764.001 Aug 2025 115,760.000$400,000.00
long5 Aug 2025 115,055.005 Aug 2025 115,059.000$400,000.00
short15 Aug 2025 118,295.0015 Aug 2025 118,291.000$400,000.00
long24 Aug 2025 115,438.0024 Aug 2025 115,442.000$400,000.00
long29 Aug 2025 112,567.0029 Aug 2025 112,571.000$400,000.00
short31 Aug 2025 108,816.0031 Aug 2025 108,812.000$400,000.00
long3 Sep 2025 111,240.003 Sep 2025 111,244.000$400,000.00
short15 Sep 2025 115,268.0015 Sep 2025 115,264.000$400,000.00
short20 Sep 2025 115,632.0020 Sep 2025 115,628.000$400,000.00
long29 Sep 2025 112,164.0029 Sep 2025 112,168.000$400,000.00
short7 Oct 2025 124,659.007 Oct 2025 124,655.000$400,000.00
long16 Oct 2025 110,763.0016 Oct 2025 110,767.000$400,000.00
short17 Oct 2025 108,194.0017 Oct 2025 108,190.000$400,000.00
long21 Oct 2025 110,532.0021 Oct 2025 110,536.000$400,000.00
short30 Oct 2025 110,021.0030 Oct 2025 110,017.000$400,000.00
long9 Nov 2025 102,313.009 Nov 2025 102,317.000$400,000.00
short14 Nov 2025 99,692.0014 Nov 2025 99,688.000$400,000.00
long19 Nov 2025 92,960.8019 Nov 2025 92,964.800$400,000.00
long22 Nov 2025 85,129.4022 Nov 2025 85,133.400$400,000.00
short2 Dec 2025 86,286.002 Dec 2025 86,282.000$400,000.00
long3 Dec 2025 91,277.903 Dec 2025 91,281.900$400,000.00
short7 Dec 2025 89,236.807 Dec 2025 89,232.800$400,000.00
long10 Dec 2025 92,678.8010 Dec 2025 92,682.800$400,000.00
short14 Dec 2025 90,240.0014 Dec 2025 90,236.000$400,000.00
long20 Dec 2025 88,136.9020 Dec 2025 88,140.900$400,000.00
short25 Dec 2025 87,669.4025 Dec 2025 87,665.400$400,000.00
long26 Dec 2025 87,225.3026 Dec 2025 87,229.300$400,000.00
short1 Jan 2026 87,648.201 Jan 2026 87,644.200$400,000.00
long3 Jan 2026 89,995.103 Jan 2026 89,999.100$400,000.00
short8 Jan 2026 91,364.208 Jan 2026 91,360.200$400,000.00
long14 Jan 2026 95,414.0014 Jan 2026 95,418.000$400,000.00
short18 Jan 2026 95,147.8018 Jan 2026 95,143.800$400,000.00
long25 Jan 2026 89,225.3025 Jan 2026 89,229.300$400,000.00
long28 Jan 2026 89,250.0028 Jan 2026 89,254.000$400,000.00
short1 Feb 2026 78,741.101 Feb 2026 78,737.100$400,000.00
long3 Feb 2026 78,738.603 Feb 2026 78,742.600$400,000.00
long7 Feb 2026 70,580.307 Feb 2026 70,584.300$400,000.00
short12 Feb 2026 67,082.5012 Feb 2026 67,078.500$400,000.00
long14 Feb 2026 68,854.0014 Feb 2026 68,858.000$400,000.00
short20 Feb 2026 67,003.7020 Feb 2026 66,999.700$400,000.00
long26 Feb 2026 67,988.0026 Feb 2026 67,992.000$400,000.00
short2 Mar 2026 65,776.502 Mar 2026 65,772.500$400,000.00
long3 Mar 2026 68,830.103 Mar 2026 68,834.100$400,000.00
short7 Mar 2026 68,114.007 Mar 2026 68,110.000$400,000.00
long11 Mar 2026 69,948.6011 Mar 2026 69,952.600$400,000.00
short19 Mar 2026 71,246.6019 Mar 2026 71,242.600$400,000.00
long26 Mar 2026 71,336.5026 Mar 2026 71,340.500$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.

ScriptSharesTypeNetTradesMax drawdown
Moving Stop-Loss mechanism + alerts to MT4-MT5 this page—v5 strategy−9,857,600.0%1914,436.8%
Alert(), alertcondition() or strategy alerts- by Peter_Osma pivothigh pivotlow valuewhenv4 strategy+7,640,000.0%191919.9%
Quantitative Trend Strategy- Uptrend long by CheatCode1sma pivothigh pivotlow valuewhenv5 strategy+5.9%2832.8%
TradingView Alerts to MT4 MT5 - Forex, indices, commodities by Peter_Osma pivothigh pivotlow valuewhenv4 strategy−699,283.0%3823,138.7%
Qullamaggie Breakout V2 by millerrhsma pivothigh pivotlow valuewhenv5 strategy+35.4%3316.9%
TradePro's 2 EMA + Stoch RSI + ATR Strategy by PtGamblersma pivothigh pivotlow valuewhenv5 strategy−2.7%1822.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.

Clean v5

VerifiedDownload the v5 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

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 37 msYes 1,000 bars in 41 msNot measured
PineTSNot testedNot testedNot 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)