Pine Script v4 · Strategy
percentrank strategy
A Pine Script v4 strategy built on Percentrank (close, 10). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 5 settings; defaults include Source close, lookback - Период сравнения 10 and scale offset - смещение шкалы 50.
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 percentrank strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 21 of 21 trades.
- How did percentrank strategy do on BTCUSDT 1D with default settings?
- +45.01%
- From 26 Jan 2024 to 21 Oct 2026, over 75 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can percentrank strategy 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 percentrank strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does percentrank strategy run in Zeal Terminal?
- Yes
- It compiles in 12 ms and runs 1,000 bars in 24 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +45.01%
- $45,012.90
- Buy and hold
- +102.5%
- same period
- Closed trades
- 75
- 28 won · 47 lost
- Profitable
- 37.3%
- of closed trades
- Profit factor
- 1.30
- gross profit ÷ gross loss
- Max drawdown
- 30.67%
- −$34,802.40
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A short 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.
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 percentrank → ta.percentrank, crossunder → ta.crossunder, crossover → ta.crossover.
v4 → v5
VerifiedSame 75 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 6: input(type = input.integer) → input.int()
- Line 7: input(type = input.integer) → input.int()
- Line 8: input(type = input.integer) → input.int()
- Line 9: input(type = input.integer) → input.int()
- Line 10: percentrank → ta.percentrank
4 more edits
- Line 14: crossunder → ta.crossunder
- Line 17: crossover → ta.crossover
- Line 21: crossover → ta.crossover
- Line 24: crossunder → ta.crossunder
⋯// © Alex_Dyuk-//@version=4+//@version=5strategy(title="percentrank", shorttitle="percentrank")src = input(close, title="Source")-len = input(title="lookback - Период сравнения", type=input.integer, defval=10, minval=2)+len = input.int(title="lookback - Период сравнения", defval=10, minval=2)-scale = input(title="scale offset - смещение шкалы", type=input.integer, defval=50, minval=0, maxval=100)+scale = input.int(title="scale offset - смещение шкалы", defval=50, minval=0, maxval=100)-level_1 = input(title="sygnal line 1", type=input.integer, defval=30)+level_1 = input.int(title="sygnal line 1", defval=30)-level_2 = input(title="sygnal line 2", type=input.integer, defval=-30)+level_2 = input.int(title="sygnal line 2", defval=-30)-prank = percentrank(src,len)-scale+prank = ta.percentrank(src,len)-scaleplot(prank, style = plot.style_columns)⋯plot(level_1, style = plot.style_line, color = color.green)-longCondition = (crossunder(level_1, prank) == true)+longCondition = (ta.crossunder(level_1, prank) == true)if (longCondition) strategy.entry("Long", strategy.long)-longExitCondition = (crossover(level_2, prank) == true)+longExitCondition = (ta.crossover(level_2, prank) == true)if (longExitCondition)⋯ -shortCondition = (crossover(level_2, prank) == true)+shortCondition = (ta.crossover(level_2, prank) == true)if (shortCondition) strategy.entry("Short", strategy.short)-shortexitCondition = (crossunder(level_1, prank) == true)+shortexitCondition = (ta.crossunder(level_1, prank) == true)if (shortexitCondition)⋯Same 75 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 5: input() → input.source()
⋯strategy(title="percentrank", shorttitle="percentrank")-src = input(close, title="Source")+src = input.source(close, title="Source")len = input.int(title="lookback - Период сравнения", defval=10, minval=2)⋯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: 75 before, 96 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.
⋯// © Alex_Dyuk-//@version=5+//@version=6strategy(title="percentrank", shorttitle="percentrank")⋯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 |
|---|---|---|---|
| Source | close | source | prank |
| lookback - Период сравнения | 10 | int (written input.integer) · min 2 | prank |
| scale offset - смещение шкалы | 50 | int (written input.integer) · min 0 · max 100 | prank |
| sygnal line 1 | 30 | int (written input.integer) | longCondition shortexitCondition plot |
| sygnal line 2 | -30 | int (written input.integer) | longExitCondition shortCondition plot |
What it calculates
- Percentrank (
percentrank,ta.percentrankin v5)Source (close), lookback - Период сравнения (10)prank · line 10
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 | (longCondition) | On | 16 |
| Close Long | (longExitCondition) | On | 19 |
| Open Short short | (shortCondition) | On | 23 |
| Close Short | (shortexitCondition) | On | 26 |
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 | +45.0% | +102.5% | 75 | 37% | 1.30 | 30.7% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +6.8% | +5.5% | 94 | 31% | 1.14 | 10.8% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +2.2% | +9.8% | 73 | 38% | 1.27 | 2.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.2% | +8.4% | 88 | 26% | 0.90 | 0.9% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.0% | +16.7% | 77 | 39% | 0.98 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 70 | 36% | 1.43 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.4% | +148.3% | 85 | 33% | 0.77 | 0.7% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.2% | +17.1% | 76 | 39% | 1.62 | 0.1% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
Settings heatmap
Two settings that feed the entry signal, moved around their defaults. The best in-sample pair (lookback - Период сравнения 10, scale offset - смещение шкалы 35) made +59.8% before 26 Dec 2025 and +38.3% after it, against +25.1% for the defaults. 22% of the 64 combinations made money in-sample and 45% out-of-sample.
| scale offset - смещение шкалы ↓ · lookback - Период сравнения → | 5 | 6 | 7 | 9 | 10 | 12 | 14 | 17 |
|---|---|---|---|---|---|---|---|---|
| 25 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 30 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 35 | −22% | −35% | −28% | +24% | +60% | +26% | −13% | −66% |
| 43 | −22% | −18% | −34% | +16% | +28% | +44% | −5% | −25% |
| 50 | −16% | +20% | −21% | +18% | +20% | +31% | −7% | −12% |
| 60 | +11% | −1% | +21% | −24% | +7% | −20% | +28% | −13% |
| 70 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 85 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| scale offset - смещение шкалы ↓ · lookback - Период сравнения → | 5 | 6 | 7 | 9 | 10 | 12 | 14 | 17 |
|---|---|---|---|---|---|---|---|---|
| 25 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 30 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 35 | −1% | −12% | +5% | +27% | +38% | +39% | +37% | +32% |
| 43 | −1% | +16% | +24% | +33% | +38% | +37% | +33% | +37% |
| 50 | +16% | +13% | +26% | +35% | +25% | +28% | +25% | +29% |
| 60 | +22% | +13% | +14% | +3% | +5% | +5% | +15% | +18% |
| 70 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 85 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
Net P&L of trades opened in each period, as a share of starting capital. marks the defaults and the best in-sample cell. Hover a cell for its trade count. 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 | 27 | 26% | −$25,301.10 |
| Sideways market | 35 | 46% | $60,666.50 |
| Falling market | 13 | 38% | $9,647.53 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
41.0%
The 95th percentile of the worst drawdown when the same 75 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 19.6% to 41.0%; the tick is what actually happened, 28.3%. Order luck alone moves it this much.
All 75 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 8 Feb 2024 44,349.60 | 24 Feb 2024 50,744.20 | 16 | $6,394.55 |
| short | 24 Feb 2024 50,744.20 | 27 Feb 2024 54,476.50 | 3 | −$3,732.33 |
| long | 27 Feb 2024 54,476.50 | 17 Mar 2024 65,300.60 | 19 | $10,824.20 |
| short | 17 Mar 2024 65,300.60 | 26 Mar 2024 69,880.00 | 9 | −$4,579.36 |
| long | 26 Mar 2024 69,880.00 | 3 Apr 2024 65,464.00 | 8 | −$4,416.01 |
| short | 3 Apr 2024 65,464.00 | 9 Apr 2024 71,620.00 | 6 | −$6,156.01 |
| long | 9 Apr 2024 71,620.00 | 14 Apr 2024 63,924.50 | 5 | −$7,695.48 |
| short | 14 Apr 2024 63,924.50 | 23 Apr 2024 66,819.30 | 9 | −$2,894.80 |
| long | 23 Apr 2024 66,819.30 | 28 Apr 2024 63,462.00 | 5 | −$3,357.34 |
| short | 28 Apr 2024 63,462.00 | 6 May 2024 64,012.00 | 8 | −$550.02 |
| long | 6 May 2024 64,012.00 | 30 May 2024 67,652.40 | 24 | $3,640.41 |
| short | 30 May 2024 67,652.40 | 5 Jun 2024 70,537.80 | 6 | −$2,885.42 |
| long | 5 Jun 2024 70,537.80 | 12 Jun 2024 67,314.20 | 7 | −$3,223.60 |
| short | 12 Jun 2024 67,314.20 | 14 Jul 2024 59,204.00 | 32 | $8,110.22 |
| long | 14 Jul 2024 59,204.00 | 1 Aug 2024 64,628.00 | 18 | $5,424.00 |
| short | 1 Aug 2024 64,628.00 | 22 Aug 2024 61,156.00 | 21 | $3,471.98 |
| long | 22 Aug 2024 61,156.00 | 1 Sep 2024 58,974.00 | 10 | −$2,182.03 |
| short | 1 Sep 2024 58,974.00 | 13 Sep 2024 58,132.30 | 12 | $841.69 |
| long | 13 Sep 2024 58,132.30 | 2 Oct 2024 60,804.90 | 19 | $2,672.61 |
| short | 2 Oct 2024 60,804.90 | 12 Oct 2024 62,540.00 | 10 | −$1,735.07 |
| long | 12 Oct 2024 62,540.00 | 26 Oct 2024 66,698.30 | 14 | $4,158.33 |
| short | 26 Oct 2024 66,698.30 | 29 Oct 2024 69,962.20 | 3 | −$3,263.89 |
| long | 29 Oct 2024 69,962.20 | 22 Dec 2024 97,292.00 | 54 | $27,329.80 |
| short | 22 Dec 2024 97,292.00 | 5 Jan 2025 98,220.50 | 14 | −$928.51 |
| long | 5 Jan 2025 98,220.50 | 10 Jan 2025 92,552.50 | 5 | −$5,668.02 |
| short | 10 Jan 2025 92,552.50 | 16 Jan 2025 100,497.00 | 6 | −$7,944.86 |
| long | 16 Jan 2025 100,497.00 | 28 Jan 2025 102,083.00 | 12 | $1,585.48 |
| short | 28 Jan 2025 102,083.00 | 13 Feb 2025 97,870.00 | 16 | $4,212.83 |
| long | 13 Feb 2025 97,870.00 | 17 Feb 2025 96,118.10 | 4 | −$1,751.88 |
| short | 17 Feb 2025 96,118.10 | 21 Feb 2025 98,305.00 | 4 | −$2,186.89 |
| long | 21 Feb 2025 98,305.00 | 25 Feb 2025 91,552.90 | 4 | −$6,752.13 |
| short | 25 Feb 2025 91,552.90 | 20 Mar 2025 86,845.90 | 23 | $4,706.95 |
| long | 20 Mar 2025 86,845.90 | 30 Mar 2025 82,648.50 | 10 | −$4,197.40 |
| short | 30 Mar 2025 82,648.50 | 13 Apr 2025 85,276.90 | 14 | −$2,628.38 |
| long | 13 Apr 2025 85,276.90 | 30 May 2025 105,590.00 | 47 | $20,312.80 |
| short | 30 May 2025 105,590.00 | 9 Jun 2025 105,734.00 | 10 | −$144.26 |
| long | 9 Jun 2025 105,734.00 | 18 Jun 2025 104,551.00 | 9 | −$1,182.84 |
| short | 18 Jun 2025 104,551.00 | 25 Jun 2025 106,083.00 | 7 | −$1,531.83 |
| long | 25 Jun 2025 106,083.00 | 21 Jul 2025 117,265.00 | 26 | $11,182.10 |
| short | 21 Jul 2025 117,265.00 | 23 Jul 2025 119,954.00 | 2 | −$2,689.32 |
| long | 23 Jul 2025 119,954.00 | 1 Aug 2025 115,764.00 | 9 | −$4,190.36 |
| short | 1 Aug 2025 115,764.00 | 11 Aug 2025 119,294.00 | 10 | −$3,530.20 |
| long | 11 Aug 2025 119,294.00 | 19 Aug 2025 116,227.00 | 8 | −$3,067.22 |
| short | 19 Aug 2025 116,227.00 | 9 Sep 2025 112,065.00 | 21 | $4,161.82 |
| long | 9 Sep 2025 112,065.00 | 22 Sep 2025 115,232.00 | 13 | $3,167.06 |
| short | 22 Sep 2025 115,232.00 | 2 Oct 2025 118,595.00 | 10 | −$3,362.70 |
| long | 2 Oct 2025 118,595.00 | 11 Oct 2025 112,774.00 | 9 | −$5,820.50 |
| short | 11 Oct 2025 112,774.00 | 25 Oct 2025 111,005.00 | 14 | $1,769.59 |
| long | 25 Oct 2025 111,005.00 | 4 Nov 2025 106,583.00 | 10 | −$4,421.85 |
| short | 4 Nov 2025 106,583.00 | 3 Dec 2025 91,277.90 | 29 | $15,305.20 |
| long | 3 Dec 2025 91,277.90 | 7 Dec 2025 89,236.80 | 4 | −$2,041.08 |
| short | 7 Dec 2025 89,236.80 | 10 Dec 2025 92,678.80 | 3 | −$3,442.01 |
| long | 10 Dec 2025 92,678.80 | 15 Dec 2025 88,172.20 | 5 | −$4,506.65 |
| short | 15 Dec 2025 88,172.20 | 2 Jan 2026 88,839.10 | 18 | −$666.89 |
| long | 2 Jan 2026 88,839.10 | 21 Jan 2026 88,427.70 | 19 | −$411.39 |
| short | 21 Jan 2026 88,427.70 | 3 Mar 2026 68,830.10 | 41 | $19,597.60 |
| long | 3 Mar 2026 68,830.10 | 20 Mar 2026 69,930.00 | 17 | $1,099.95 |
| short | 20 Mar 2026 69,930.00 | 6 Apr 2026 69,034.20 | 17 | $895.83 |
| long | 6 Apr 2026 69,034.20 | 30 Apr 2026 75,780.00 | 24 | $6,745.82 |
| short | 30 Apr 2026 75,780.00 | 3 May 2026 78,686.80 | 3 | −$2,906.84 |
| long | 3 May 2026 78,686.80 | 14 May 2026 79,313.60 | 11 | $626.77 |
| short | 14 May 2026 79,313.60 | 14 Jun 2026 64,458.00 | 31 | $14,855.60 |
| long | 14 Jun 2026 64,458.00 | 22 Jun 2026 63,312.00 | 8 | −$1,146.01 |
| short | 22 Jun 2026 63,312.00 | 4 Jul 2026 62,583.30 | 12 | $728.74 |
| long | 4 Jul 2026 62,583.30 | 14 Jul 2026 62,334.50 | 10 | −$248.74 |
| short | 14 Jul 2026 62,334.50 | 15 Jul 2026 65,044.00 | 1 | −$2,709.47 |
| long | 15 Jul 2026 65,044.00 | 28 Jul 2026 63,755.90 | 13 | −$1,288.13 |
| short | 28 Jul 2026 63,755.90 | 8 Aug 2026 64,923.20 | 11 | −$1,167.34 |
| long | 8 Aug 2026 64,923.20 | 13 Aug 2026 63,480.00 | 5 | −$1,443.21 |
| short | 13 Aug 2026 63,480.00 | 20 Aug 2026 69,334.80 | 7 | −$5,854.79 |
| long | 20 Aug 2026 69,334.80 | 2 Sep 2026 77,439.00 | 13 | $8,104.23 |
| short | 2 Sep 2026 77,439.00 | 4 Sep 2026 81,270.40 | 2 | −$3,831.36 |
| long | 4 Sep 2026 81,270.40 | 11 Sep 2026 76,568.70 | 7 | −$4,701.64 |
| short | 11 Sep 2026 76,568.70 | 19 Sep 2026 80,883.90 | 8 | −$4,315.13 |
| long | 19 Sep 2026 80,883.90 | 8 Oct 2026 83,321.80 | 19 | $2,437.95 |
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 |
|---|---|---|---|---|---|
| percentrank strategy this page | — | v4 strategy | +45.0% | 75 | 30.7% |
| Connors RSI by Zeal Terminal | percentrank | v5 indicator | indicator | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 12 ms | Yes 1,000 bars in 24 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/// © Alex_Dyuk//@version=4strategy(title="percentrank", shorttitle="percentrank")src = input(close, title="Source")len = input(title="lookback - Период сравнения", type=input.integer, defval=10, minval=2)scale = input(title="scale offset - смещение шкалы", type=input.integer, defval=50, minval=0, maxval=100)level_1 = input(title="sygnal line 1", type=input.integer, defval=30)level_2 = input(title="sygnal line 2", type=input.integer, defval=-30)prank = percentrank(src,len)-scaleplot(prank, style = plot.style_columns)plot(level_2, style = plot.style_line, color = color.red)plot(level_1, style = plot.style_line, color = color.green)longCondition = (crossunder(level_1, prank) == true)if (longCondition) strategy.entry("Long", strategy.long)longExitCondition = (crossover(level_2, prank) == true)if (longExitCondition) strategy.close("Long") shortCondition = (crossover(level_2, prank) == true)if (shortCondition) strategy.entry("Short", strategy.short)shortexitCondition = (crossunder(level_1, prank) == true)if (shortexitCondition) strategy.close("Short")