Pine Script v5 · Strategy
PCT Trailing Stoploss-Strategy
A Pine Script v5 strategy built on EMA (close, 14), Valuewhen (buyAlert, close) and Crossunder (close, alertSellValueLow). It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 9 settings; defaults include Stop Loss Loss: 1% = .01 .035, Trailing Stop Arm: 1%=.01 .0065 and Trailing Stop Trigger: 1%=.01 .003.
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 PCT Trailing Stoploss-Strategy repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did PCT Trailing Stoploss-Strategy do on BTCUSDT 1D with default settings?
- Not measured
- It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings; it opened a long position that was still open on the last bar, so there is no result to report. It closed no trades on the other markets either. A backtest, not investment advice.
- What alerts does PCT Trailing Stoploss-Strategy have?
- 2
- Buy; Sell.
- Does PCT Trailing Stoploss-Strategy 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: 0 before, 1 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 PCT Trailing Stoploss-Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
BTCUSDT · 1D · default settings
- EMA
- EMA
- EMA
- TSL Armed
- Buy
- Sell
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- 0.00%
- $0.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 0
- 0 won · 0 lost
- Profitable
- 0.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 1.16%
- −$11,607.00
One run with the author's default settings, $1,000,000 starting capital, commission as the script sets it. A long position was still open at the last bar. 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 stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
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.
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 |
|---|---|---|---|
| Sell Settings | |||
| Stop Loss Loss: 1% = .01 | .035 | float · min 0 | alertSellValueLow |
| Trailing Stop Arm: 1%=.01 | .0065 | float · min 0 | TSLTriggerValue beginTrail |
| Trailing Stop Trigger: 1%=.01 | .003 | float · min 0 | TSLStop |
| Trend Line Settings | |||
| ema 1 Length | 14 | int · min 1 | ema1 |
| ema 1 Source | close | source | ema1 |
| ema 2 Length | 22 | int · min 1 | ema2 |
| ema 2 Source | close | source | ema2 |
| ema 3 Length | 200 | int · min 1 | ema3 |
| ema 2 Source | close | source | ema3 |
What it calculates
- EMAema 1 Source (close), ema 1 Length (14)
ema1 · line 18 - EMAema 2 Source (close), ema 2 Length (22)
ema2 · line 22 - EMAema 2 Source (close), ema 3 Length (200)
ema3 · line 26 - ValuewhenbuyAlert, close, 0
alertBuyValue · line 44 - Crossunderclose, alertSellValueLow
Sell2 · line 90
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 Buy long | (longCondition) | On | 121 |
| Close sellAlert | (shortCondition) | On | 126 |
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 | Buy | 122 |
| (no title) | alert | Sell | 127 |
Does it repaint?
No
No repainting patterns in the source.
- 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 | 0.0% | +102.5% | 0 | 0% | — | 1.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | 0.0% | +5.5% | 0 | 0% | — | 0.2% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | 0.0% | +9.8% | 0 | 0% | — | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | 0.0% | +8.4% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | 0.0% | +16.7% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | 0.0% | +21.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | 0.0% | +148.3% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | 0.0% | +17.1% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
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 |
|---|---|---|---|---|---|
| PCT Trailing Stoploss-Strategy this page | — | v5 strategy | 0.0% | 0 | 1.2% |
| MACD 50x Leveraged Short Strategy with Real Equity by Noldo | ema valuewhen | v4 strategy | +15,109.2% | 39 | 2,974.0% |
| MACD 50x Leveraged Long Strategy Results with Real Equity by Noldo | ema valuewhen | v4 strategy | +40,225.0% | 38 | 1,581.6% |
| Yesterday's High v.17.07 by tumiza999 | valuewhen ema | v5 strategy | +10.1% | 127 | 1.8% |
| Sentiment Oscillator by dannylimardi | ema valuewhen | v4 strategy | +1.4% | 5 | 20.6% |
| Initial template by trading_mentalist | ema valuewhen | v4 strategy | No trades on this market | ||
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.
- 3 settings use the untyped input() (line 17, 21, 25). 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
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 17: input() → input.source()
- Line 21: input() → input.source()
- Line 25: input() → input.source()
⋯ema1Len = input.int(14, minval=1, title=" ema 1 Length", group="Trend Line Settings")-ema1Src = input(close, title="ema 1 Source", group="Trend Line Settings")+ema1Src = input.source(close, title="ema 1 Source", group="Trend Line Settings")ema1 = ta.ema(ema1Src, ema1Len)⋯ema2Len = input.int(22, minval=1, title=" ema 2 Length", group="Trend Line Settings")-ema2Src = input(close, title="ema 2 Source", group="Trend Line Settings")+ema2Src = input.source(close, title="ema 2 Source", group="Trend Line Settings")ema2 = ta.ema(ema2Src, ema2Len)⋯ema3Len = input.int(200, minval=1, title=" ema 3 Length", group="Trend Line Settings")-ema3Src = input(close, title="ema 2 Source", group="Trend Line Settings")+ema3Src = input.source(close, title="ema 2 Source", group="Trend Line Settings")ema3 = ta.ema(ema3Src, ema3Len)⋯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: 0 before, 1 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.
⋯// © Thumpyr-//@version=5+//@version=6/////////////////////////////////////////////////////////////////////////////////////////////⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 30 ms | Yes 1,000 bars in 51 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/// © Thumpyr//@version=5/////////////////////////////////////////////////////////////////////////////////////////////// Comment out Strategy Line and remove // from Indicator line to turn into Indicator //////// Do same for alertConidction at bottom ///////////////////////////////////////////////////////////////////////////////////////////////////strategy("PCT Trailing Stoploss-Strategy", shorttitle="PCT Trailing Stoploss- Strategy", overlay=true, margin_long=100, margin_short=100)//indicator(title="PCT Trailing Stoploss- Indicator", shorttitle="PCT Trailing Stoploss - Indicator", timeframe="", timeframe_gaps=true, overlay=true)//sellLow=input.float(.035, minval=0, title="Stop Loss Loss: 1% = .01", group="Sell Settings")trailStopArm=input.float(.0065, minval=0, title="Trailing Stop Arm: 1%=.01", group="Sell Settings")trailStopPct=input.float(.003, minval=0, title="Trailing Stop Trigger: 1%=.01 ", group="Sell Settings")/////////////////////////////////////////////////// Indicators ///////////////////////////////////////////////////ema1Len = input.int(14, minval=1, title=" ema 1 Length", group="Trend Line Settings")ema1Src = input(close, title="ema 1 Source", group="Trend Line Settings")ema1 = ta.ema(ema1Src, ema1Len)plot(ema1, title="EMA", color=color.blue)ema2Len = input.int(22, minval=1, title=" ema 2 Length", group="Trend Line Settings")ema2Src = input(close, title="ema 2 Source", group="Trend Line Settings")ema2 = ta.ema(ema2Src, ema2Len)plot(ema2, title="EMA", color=color.orange)ema3Len = input.int(200, minval=1, title=" ema 3 Length", group="Trend Line Settings")ema3Src = input(close, title="ema 2 Source", group="Trend Line Settings")ema3 = ta.ema(ema3Src, ema3Len)plot(ema3, title="EMA", color=color.gray)///////////////////////////////// Buy Conditions /////////////////////////////////alertBuy = ta.crossover(ema1,ema2) and close>ema3//////////////////////////////////////////////////////////////////////// Filter redundant Buy Signals if Sell has not happened ////////////////////////////////////////////////////////////////////////var lastsignal = 0showAlertBuy = 0if(alertBuy and lastsignal !=1) showAlertBuy := 1 lastsignal := 1buyAlert= showAlertBuy > 0////////////////////////////////////////////////////////////////////// Track Conditions at buy Signal //////////////////////////////////////////////////////////////////////alertBuyValue = ta.valuewhen(buyAlert, close,0)alertSellValueLow = alertBuyValue - (alertBuyValue*sellLow)///////////////////////////////////////////////////////////////// Trailing Stop /////////////////////////////////////////////////////////////////var TSLActive=0 //Check to see if TSL has been activatedvar TSLTriggerValue=0.0 //Initial and climbing value of TSLvar TSLStop = 0.0 //Sell Triggervar TSLRunning =0 //Continuously check each bar to raise TSL or not// Check if a Buy has been triggered and set initial value for TSL //if buyAlert TSLTriggerValue := alertBuyValue+(alertBuyValue*trailStopArm) TSLActive := 0 TSLRunning :=1 TSLStop := TSLTriggerValue - (TSLTriggerValue*trailStopPct) // Check that Buy has triggered and if Close has reached initial TSL// // Keeps from setting Sell Signal before TSL has been armed w/TSLActive//beginTrail=TSLRunning==1 and TSLActive==0 and close>alertBuyValue+(alertBuyValue*trailStopArm) and ta.crossover(close,TSLTriggerValue)if beginTrail TSLTriggerValue :=close TSLActive :=1 TSLStop :=TSLTriggerValue - (TSLTriggerValue*trailStopPct) // Continuously check if TSL needs to increase and set new value // runTrail= TSLActive==1 and (ta.crossover(close,TSLTriggerValue) or close>=TSLTriggerValue)if runTrail TSLTriggerValue :=close TSLStop :=TSLTriggerValue - (TSLTriggerValue*trailStopPct) // Verify that TSL is active and trigger when close cross below TSL Stop//TSL=TSLActive==1 and (ta.crossunder(close,TSLStop) or (close[1]>TSLStop and close<TSLStop)) // Plot point of inital arming of TSL//TSLTrigger=TSLActive==1 and TSLActive[1]==0plotshape(TSLTrigger, title='TSL Armed', location=location.abovebar, color=color.new(color.blue, 0), size=size.small, style=shape.cross, text='TSL Armed')////////////////////////////////////////////////////////////// Plots used for troubleshooting and verification of TSL ////////////////////////////////////////////////////////////////plot(TSLActive,"Trailing Stop", color=#f48fb1)//plot(TSLRunning,"Trailing Stop", color=#f48fb1)//plot(TSLTriggerValue,"Trailing Stop Trigger", color.new(color=#ec407a, transp = TSLRunning==1 ? 0 : 100))//plot(TSLStop,"Trailing Stop", color.new(color=#f48fb1, transp = TSLRunning==1 ? 0 : 100))/////////////////////////////////////////////////////////////////// Sell Conditions ///////////////////////////////////////////////////////////////////Sell1 = TSLSell2 = ta.crossunder(close,alertSellValueLow)alertSell= Sell1 or Sell2//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Remove Redundant Signals ////////////////////////////////////////////////////////////////showAlertSell = 0if(alertSell and lastsignal != -1) showAlertSell := 1 lastsignal := -1sellAlert= showAlertSell > 0if sellAlert TSLActive :=0 TSLRunning :=0/////////////////////////////////////////// Plot Buy and Sell Shapes on Chart ///////////////////////////////////////////plotshape(buyAlert, title='Buy', location=location.belowbar, color=color.new(color.green, 0), size=size.small, style=shape.triangleup, text='Buy')plotshape(sellAlert, title='Sell', location=location.abovebar, color=color.new(color.red, 0), size=size.small, style=shape.triangledown, text='Sell')/////////////////////////////////////////////////////////////////////////////////////////////// Remove // to setup for Indicator /////////////////////////////////////////////////////////////////////////////////////////////////Alerts//alertcondition(title='Buy Alert', condition=buyAlert, message='Buy Conditions are Met')//alertcondition(title='Sell Alert', condition=sellAlert, message='Sell Conditions are Met')///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Comment out this section if setup as Indicator ////////////////////////////////////////////////////////////////longCondition = buyAlertif (longCondition) strategy.entry("Buy", strategy.long) alert(message='Buy', freq=alert.freq_once_per_bar_close) shortCondition = sellAlertif (shortCondition) strategy.close_all(sellAlert,"Sell") alert(message='Sell', freq=alert.freq_once_per_bar_close)/////////////////////////////////////////////////////////////