Pine Script v4 · Strategy
Richard's Strategy
A Pine Script v4 strategy built on Highest (close[1], 20), Lowest (close[1], 20) and ATR (20). 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. 2 settings; defaults include stop loss var 4 and lenght 20.
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 Richard's 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 Richard's Strategy do on BTCUSDT 1D with default settings?
- +34.59%
- From 26 Jan 2024 to 21 Oct 2026, over 70 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Richard's 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 Richard's Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Richard's Strategy run in Zeal Terminal?
- Yes
- It compiles in 15 ms and runs 1,000 bars in 28 ms, in the browser.
BTCUSDT · 1D · default settings
- Shape
- Shape
- Shape
- Shape
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +34.59%
- $34,593.90
- Buy and hold
- +102.5%
- same period
- Closed trades
- 70
- 49 won · 21 lost
- Profitable
- 70.0%
- of closed trades
- Profit factor
- 1.30
- gross profit ÷ gross loss
- Max drawdown
- 26.29%
- −$30,111.70
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.
-
Note
Uses an argument v6 removed
when = on line 25. The upgraded version below moves each call inside an if block.
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 highest → ta.highest, lowest → ta.lowest, atr → ta.atr, ema → ta.ema, round → math.round.
- 1 strategy call uses the when = argument (line 25). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 70 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 9: highest → ta.highest
- Line 10: lowest → ta.lowest
- Line 11: lowest → ta.lowest
- Line 12: highest → ta.highest
- Line 14: atr → ta.atr
3 more edits
- Line 15: ema → ta.ema
- Line 28: round → math.round
- Line 36: round → math.round
⋯// © melodyera0822-//@version=4+//@version=5strategy("Richard Strategy", overlay=true)⋯// high_low-_20_day_highest = highest(nz(close[1]), lenght)+_20_day_highest = ta.highest(nz(close[1]), lenght)-_20_day_lowest = lowest(nz(close[1]), lenght)+_20_day_lowest = ta.lowest(nz(close[1]), lenght)-_10_day_low = lowest(nz(close[1]), lenght/2)+_10_day_low = ta.lowest(nz(close[1]), lenght/2)-_10_day_high = highest(nz(close[1]), lenght/2)+_10_day_high = ta.highest(nz(close[1]), lenght/2)//indicators-atr20 = atr(20)+atr20 = ta.atr(20)-ema_atr20 = ema(atr20,20)+ema_atr20 = ta.ema(atr20,20)//vars⋯ buy_sell := "buy"- stoploss := round(close - variable_for_stoploss * ema_atr20)+ stoploss := math.round(close - variable_for_stoploss * ema_atr20) ⋯ buy_sell := "sell"- stoploss := round(close - variable_for_stoploss * ema_atr20)+ stoploss := math.round(close - variable_for_stoploss * ema_atr20)if traded == "true"⋯Same 70 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 6: input() → input.int()
- Line 7: input() → input.int()
- Line 25: when = buyCon → if block
⋯// User input-variable_for_stoploss = input(4,title="stop loss var")+variable_for_stoploss = input.int(4,title="stop loss var")-lenght = input(20,title="lenght")+lenght = input.int(20,title="lenght")// high_low⋯if (buyCon)- strategy.entry("long", strategy.long, when = buyCon)+ if buyCon+ strategy.entry("long", strategy.long) traded := "true"⋯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: 70 before, 84 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.
⋯// © melodyera0822-//@version=5+//@version=6strategy("Richard Strategy", 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 |
|---|---|---|---|
| stop loss var | 4 | int | stoploss |
| lenght | 20 | int | _20_day_highest _20_day_lowest _10_day_low _10_day_high |
What it calculates
- Highest (
highest,ta.highestin v5)nz(close[1]), lenght (20)_20_day_highest · line 9 - Lowest (
lowest,ta.lowestin v5)nz(close[1]), lenght (20)_20_day_lowest · line 10 - Lowest (
lowest,ta.lowestin v5)nz(close[1]), lenght/2_10_day_low · line 11 - Highest (
highest,ta.highestin v5)nz(close[1]), lenght/2_10_day_high · line 12 - ATR (
atr,ta.atrin v5)20atr20 · line 14 - EMA (
ema,ta.emain v5)atr20, 20ema_atr20 · line 15
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 | buyConclose > _20_day_highest and traded == "false" | On | 25 |
| Open short short | (sellCon) | On | 33 |
| Close long | buy_sell == "buy" and ((close<stoploss)or(close<_10_day_low)) | On | 39 |
| Close short | buy_sell == "sell" and ((close>stoploss)or(close>_10_day_high)) | On | 44 |
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 | +34.6% | +102.5% | 70 | 70% | 1.30 | 26.3% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +7.1% | +5.5% | 72 | 72% | 1.21 | 11.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.3% | +9.8% | 74 | 74% | 0.95 | 3.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.1% | +8.4% | 64 | 61% | 0.93 | 0.6% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.2% | +16.7% | 76 | 79% | 1.75 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 72 | 65% | 1.12 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.0% | +148.3% | 70 | 61% | 1.03 | 0.5% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.1% | +17.1% | 70 | 66% | 1.60 | 0.1% | 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 | 19 | 68% | −$5,228.75 |
| Sideways market | 33 | 70% | $27,278.20 |
| Falling market | 18 | 72% | $12,544.40 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
38.6%
The 95th percentile of the worst drawdown when the same 70 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 18.6% to 38.6%; the tick is what actually happened, 25.4%. Order luck alone moves it this much.
All 70 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 15 Feb 2024 51,795.20 | 17 Mar 2024 65,300.60 | 31 | $13,505.50 |
| long | 9 Apr 2024 71,620.00 | 14 Apr 2024 63,924.50 | 5 | −$7,695.48 |
| short | 16 Apr 2024 63,420.00 | 18 Apr 2024 61,277.40 | 2 | $2,142.61 |
| short | 1 May 2024 60,672.00 | 2 May 2024 58,365.00 | 1 | $2,307.04 |
| long | 16 May 2024 66,206.50 | 1 Jun 2024 67,540.00 | 16 | $1,333.50 |
| short | 12 Jun 2024 67,314.20 | 14 Jun 2024 66,773.00 | 2 | $541.22 |
| short | 15 Jun 2024 66,044.00 | 19 Jun 2024 65,175.30 | 4 | $868.67 |
| short | 20 Jun 2024 64,974.40 | 21 Jun 2024 64,870.00 | 1 | $104.38 |
| short | 22 Jun 2024 64,143.60 | 24 Jun 2024 63,210.00 | 2 | $933.55 |
| short | 25 Jun 2024 60,293.30 | 4 Jul 2024 60,208.60 | 9 | $84.73 |
| short | 5 Jul 2024 57,050.00 | 6 Jul 2024 56,628.80 | 1 | $421.23 |
| short | 8 Jul 2024 55,857.80 | 16 Jul 2024 64,724.10 | 8 | −$8,866.25 |
| long | 16 Jul 2024 64,724.10 | 1 Aug 2024 64,628.00 | 16 | −$96.05 |
| short | 4 Aug 2024 60,698.00 | 5 Aug 2024 58,161.00 | 1 | $2,536.99 |
| short | 6 Aug 2024 54,018.80 | 24 Aug 2024 64,037.20 | 18 | −$10,018.40 |
| long | 24 Aug 2024 64,037.20 | 1 Sep 2024 58,974.00 | 8 | −$5,063.24 |
| short | 2 Sep 2024 57,301.80 | 6 Sep 2024 56,180.00 | 4 | $1,121.77 |
| short | 7 Sep 2024 53,963.00 | 19 Sep 2024 61,760.00 | 12 | −$7,797.01 |
| long | 19 Sep 2024 61,760.00 | 2 Oct 2024 60,804.90 | 13 | −$955.06 |
| short | 10 Oct 2024 60,636.00 | 11 Oct 2024 60,326.40 | 1 | $309.61 |
| long | 15 Oct 2024 66,084.00 | 22 Dec 2024 97,292.00 | 68 | $31,208.00 |
| short | 23 Dec 2024 95,186.30 | 24 Dec 2024 94,881.50 | 1 | $304.81 |
| short | 28 Dec 2024 94,299.00 | 30 Dec 2024 93,738.20 | 2 | $560.84 |
| short | 31 Dec 2024 92,792.10 | 10 Jan 2025 92,552.50 | 10 | $239.56 |
| long | 18 Jan 2025 104,077.00 | 2 Feb 2025 100,636.00 | 15 | −$3,441.81 |
| short | 6 Feb 2025 96,612.40 | 7 Feb 2025 96,554.40 | 1 | $58.09 |
| short | 8 Feb 2025 96,506.80 | 9 Feb 2025 96,444.80 | 1 | $62.05 |
| short | 12 Feb 2025 95,778.20 | 19 Feb 2025 95,671.70 | 7 | $106.47 |
| short | 25 Feb 2025 91,552.90 | 26 Feb 2025 88,680.40 | 1 | $2,872.49 |
| short | 27 Feb 2025 84,250.10 | 10 Mar 2025 80,734.50 | 11 | $3,515.61 |
| short | 11 Mar 2025 78,595.90 | 7 Apr 2025 78,430.00 | 27 | $165.86 |
| short | 9 Apr 2025 76,322.40 | 22 Apr 2025 87,516.20 | 13 | −$11,193.80 |
| long | 22 Apr 2025 87,516.20 | 31 May 2025 103,985.00 | 39 | $16,469.30 |
| short | 6 Jun 2025 101,509.00 | 23 Jun 2025 100,964.00 | 17 | $544.82 |
| long | 3 Jul 2025 108,850.00 | 1 Aug 2025 115,764.00 | 29 | $6,914.48 |
| short | 2 Aug 2025 113,298.00 | 3 Aug 2025 112,546.00 | 1 | $751.57 |
| long | 13 Aug 2025 120,134.00 | 19 Aug 2025 116,227.00 | 6 | −$3,907.04 |
| short | 22 Aug 2025 112,500.00 | 26 Aug 2025 110,112.00 | 4 | $2,388.02 |
| short | 30 Aug 2025 108,377.00 | 1 Sep 2025 108,246.00 | 2 | $131.04 |
| long | 13 Sep 2025 116,029.00 | 22 Sep 2025 115,232.00 | 9 | −$797.12 |
| short | 26 Sep 2025 108,994.00 | 2 Oct 2025 118,595.00 | 6 | −$9,600.50 |
| long | 2 Oct 2025 118,595.00 | 11 Oct 2025 112,774.00 | 9 | −$5,820.50 |
| short | 17 Oct 2025 108,194.00 | 18 Oct 2025 106,432.00 | 1 | $1,762.59 |
| short | 5 Nov 2025 101,497.00 | 7 Nov 2025 101,346.00 | 2 | $151.19 |
| short | 14 Nov 2025 99,692.00 | 15 Nov 2025 94,594.00 | 1 | $5,098.03 |
| short | 17 Nov 2025 94,261.40 | 18 Nov 2025 92,215.10 | 1 | $2,046.31 |
| short | 20 Nov 2025 91,555.00 | 21 Nov 2025 86,637.20 | 1 | $4,917.74 |
| short | 22 Nov 2025 85,129.40 | 23 Nov 2025 84,739.80 | 1 | $389.67 |
| short | 18 Dec 2025 86,243.20 | 19 Dec 2025 85,516.40 | 1 | $726.82 |
| long | 4 Jan 2026 90,628.00 | 21 Jan 2026 88,427.70 | 17 | −$2,200.35 |
| short | 26 Jan 2026 86,670.40 | 30 Jan 2026 84,650.20 | 4 | $2,020.20 |
| short | 31 Jan 2026 84,260.50 | 1 Feb 2026 78,741.10 | 1 | $5,519.40 |
| short | 2 Feb 2026 76,968.20 | 4 Feb 2026 75,770.20 | 2 | $1,198.01 |
| short | 5 Feb 2026 73,165.80 | 6 Feb 2026 62,909.90 | 1 | $10,256.00 |
| long | 5 Mar 2026 72,666.80 | 22 Mar 2026 68,918.10 | 17 | −$3,748.65 |
| short | 30 Mar 2026 66,010.90 | 8 Apr 2026 71,924.20 | 9 | −$5,913.29 |
| long | 8 Apr 2026 71,924.20 | 16 May 2026 79,113.20 | 38 | $7,188.98 |
| short | 23 May 2026 75,539.50 | 28 May 2026 74,449.30 | 5 | $1,090.19 |
| short | 29 May 2026 73,617.50 | 30 May 2026 73,460.80 | 1 | $156.74 |
| short | 2 Jun 2026 71,408.90 | 3 Jun 2026 66,760.80 | 1 | $4,648.06 |
| short | 4 Jun 2026 64,142.80 | 5 Jun 2026 63,886.00 | 1 | $256.76 |
| short | 6 Jun 2026 61,056.50 | 7 Jun 2026 60,884.60 | 1 | $171.85 |
| short | 26 Jun 2026 59,794.60 | 29 Jun 2026 59,577.00 | 3 | $217.63 |
| short | 1 Jul 2026 58,624.70 | 15 Jul 2026 65,044.00 | 14 | −$6,419.28 |
| long | 15 Jul 2026 65,044.00 | 28 Jul 2026 63,755.90 | 13 | −$1,288.13 |
| long | 20 Aug 2026 69,334.80 | 3 Sep 2026 77,340.00 | 14 | $8,005.23 |
| long | 4 Sep 2026 81,270.40 | 11 Sep 2026 76,568.70 | 7 | −$4,701.64 |
| short | 16 Sep 2026 75,644.50 | 22 Sep 2026 86,620.00 | 6 | −$10,975.50 |
| long | 22 Sep 2026 86,620.00 | 8 Oct 2026 83,321.80 | 16 | −$3,298.20 |
| short | 15 Oct 2026 81,682.00 | 17 Oct 2026 81,615.90 | 2 | $66.11 |
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 |
|---|---|---|---|---|---|
| Richard's Strategy this page | — | v4 strategy | +34.6% | 70 | 26.3% |
| Classic Long Term Trend Following System by Robrecht99 | ema highest lowest atr | v5 strategy | +2.9% | 5 | 1.1% |
| Strategy Myth-Busting #1 - UT Bot+STC+Hull [MYN] by myncrypto | atr ema lowest highest | v5 strategy | −30.7% | 10 | 63.5% |
| Strategy for UT Bot Alerts indicator by StrategiesForEveryone | ema atr highest lowest | v5 strategy | −4.5% | 42 | 15.7% |
| Simple and Profitable Scalping Strategy (ForexSignals TV) by kevinmck100 | ema atr highest lowest | v5 strategy | No trades on this market | ||
| ATR + STC + MA by Romedius | ema lowest highest atr | v5 strategy | −1.2% | 15 | 3.3% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 15 ms | Yes 1,000 bars in 28 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/// © melodyera0822//@version=4strategy("Richard Strategy", overlay=true)// User inputvariable_for_stoploss = input(4,title="stop loss var")lenght = input(20,title="lenght")// high_low_20_day_highest = highest(nz(close[1]), lenght)_20_day_lowest = lowest(nz(close[1]), lenght)_10_day_low = lowest(nz(close[1]), lenght/2)_10_day_high = highest(nz(close[1]), lenght/2)//indicatorsatr20 = atr(20)ema_atr20 = ema(atr20,20)//varsvar traded = "false"var buy_sell = "none"var buyExit = falsevar sellExit = falsevar stoploss = 0buyCon = close > _20_day_highest and traded == "false"plotshape(buyCon,style = shape.triangleup,location = location.belowbar, color = color.green )if (buyCon) strategy.entry("long", strategy.long, when = buyCon) traded := "true" buy_sell := "buy" stoploss := round(close - variable_for_stoploss * ema_atr20) sellCon = close < _20_day_lowest and traded == "false"plotshape(sellCon,style = shape.triangledown, color = color.red )if (sellCon) strategy.entry("short", strategy.short) traded := "true" buy_sell := "sell" stoploss := round(close - variable_for_stoploss * ema_atr20)if traded == "true" if buy_sell == "buy" and ((close<stoploss)or(close<_10_day_low)) strategy.close("long") buyExit := true traded := "false" if buy_sell == "sell" and ((close>stoploss)or(close>_10_day_high)) strategy.close("short") sellExit := true traded := "false" plotshape(buyExit,style = shape.triangleup,location = location.belowbar, color = color.yellow )buyExit := falseplotshape(sellExit,style = shape.triangledown, color = color.yellow )sellExit := false