Pine Script v4 · Strategy
Trend Bounce [racer8]
A Pine Script v4 strategy built on Highest (high, 7), Lowest (low, 7) and SMA (close, 200). It has 6 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss. Each trade is one contract and pays no commission. 3 settings; defaults include Channel Length 7, Ma Length 200 and ATR Stop Loss 2.0.
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 Trend Bounce [racer8] repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 8 of 8 trades.
- How did Trend Bounce [racer8] do on BTCUSDT 1D with default settings?
- +2.36%
- From 26 Jan 2024 to 21 Oct 2026, over 22 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Trend Bounce [racer8] 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 Trend Bounce [racer8] use a stop loss?
- Yes
- strategy.exit sets a stop on line 35, 37.
- Does Trend Bounce [racer8] run in Zeal Terminal?
- Yes
- It compiles in 14 ms and runs 1,000 bars in 32 ms, in the browser.
BTCUSDT · 1D · default settings
- high
- low
- sma
- ATR Stop Loss Long
- ATR Stop Loss Short
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +2.36%
- $2,362.72
- Buy and hold
- +102.5%
- same period
- Closed trades
- 22
- 12 won · 10 lost
- Profitable
- 54.5%
- of closed trades
- Profit factor
- 1.04
- gross profit ÷ gross loss
- Max drawdown
- 32.20%
- −$32,204.90
One run with the author's default settings, $100,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.
-
Likely bug
sma(tr, 10) ignores the settings line 24
Line 24 hard-codes 10 where line 10 uses “Ma Length”. Changing those settings moves one band but not this one.
sma(tr,10)sma(tr, n2) -
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 highest → ta.highest, lowest → ta.lowest, sma → ta.sma, valuewhen → ta.valuewhen.
v4 → v5
VerifiedSame 22 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 7: highest → ta.highest
- Line 8: lowest → ta.lowest
- Line 10: sma → ta.sma
- Line 24: sma → ta.sma
- Line 24: tr → ta.tr
- Line 30: valuewhen → ta.valuewhen
- Line 31: valuewhen → ta.valuewhen
⋯// © racer8-//@version=4+//@version=5// This Algo Strategy Has Only 3 rules and 62% Win Rate (Youtube) ⋯nn = input(7,"Channel Length")-hi = highest(high,nn)+hi = ta.highest(high,nn)-lo = lowest(low,nn)+lo = ta.lowest(low,nn)n2 = input(200,"Ma Length")-ma = sma(close,n2)+ma = ta.sma(close,n2)if close>ma and close<lo[1]⋯//-----------------------------------------Stop Loss-------------------------------------------------------atr = sma(tr,10)[1]+atr = ta.sma(ta.tr,10)[1]bought = strategy.position_size[0] > strategy.position_size[1]⋯StopPrice_Short = strategy.position_avg_price + slm*atr // determines stop loss's price -FixedStopPrice_Long = valuewhen(bought,StopPrice_Long,0) // stores original StopPrice +FixedStopPrice_Long = ta.valuewhen(bought,StopPrice_Long,0) // stores original StopPrice -FixedStopPrice_Short = valuewhen(sold,StopPrice_Short,0) // stores original StopPrice +FixedStopPrice_Short = ta.valuewhen(sold,StopPrice_Short,0) // stores original StopPrice plot(FixedStopPrice_Long,"ATR Stop Loss Long",color=color.blue,linewidth=1,style=plot.style_cross)⋯Same 22 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 6: input() → input.int()
- Line 9: input() → input.int()
- Line 27: input() → input.float()
⋯strategy("Trend Bounce", overlay=true)-nn = input(7,"Channel Length")+nn = input.int(7,"Channel Length")hi = ta.highest(high,nn)lo = ta.lowest(low,nn)-n2 = input(200,"Ma Length")+n2 = input.int(200,"Ma Length")ma = ta.sma(close,n2)⋯sold = strategy.position_size[0] < strategy.position_size[1]-slm = input(2.0,"ATR Stop Loss",minval=0)+slm = input.float(2.0,"ATR Stop Loss",minval=0)StopPrice_Long = strategy.position_avg_price - slm*atr // determines stop loss's price ⋯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: 22 before, 28 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.
⋯// © racer8-//@version=5+//@version=6// This Algo Strategy Has Only 3 rules and 62% Win Rate (Youtube) ⋯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 |
|---|---|---|---|
| Channel Length | 7 | int | hi lo |
| Ma Length | 200 | int | ma |
| ATR Stop Loss | 2.0 | float · min 0 | StopPrice_Long StopPrice_Short |
What it calculates
- Highest (
highest,ta.highestin v5)high, Channel Length (7)hi · line 7 - Lowest (
lowest,ta.lowestin v5)low, Channel Length (7)lo · line 8 - SMA (
sma,ta.smain v5)close, Ma Length (200)ma · line 10 - SMA (
sma,ta.smain v5)tr, 10atr · line 24 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, StopPrice_Long, 0FixedStopPrice_Long · line 30 - Valuewhen (
valuewhen,ta.valuewhenin v5)sold, StopPrice_Short, 0FixedStopPrice_Short · line 31
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 | close>ma and close<lo[1] | On | 12 |
| Close Buy | close>hi[1] | On | 14 |
| Open Sell short | close<ma and close>hi[1] | On | 17 |
| Close Sell | close<lo[1] | On | 19 |
| Exit Stop stop | strategy.position_size > 0 | On | 35 |
| Exit Stop stop | strategy.position_size < 0 | On | 37 |
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 | +2.4% | +102.5% | 22 | 55% | 1.04 | 32.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +6.3% | +5.5% | 31 | 52% | 1.35 | 5.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +2.5% | +9.8% | 29 | 62% | 1.99 | 0.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.5% | +8.4% | 28 | 64% | 1.98 | 0.2% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.1% | +16.7% | 28 | 61% | 1.31 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.0% | +21.5% | 30 | 53% | 0.99 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.1% | +148.3% | 21 | 52% | 1.20 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 24 | 50% | 1.09 | 0.0% | 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 | 6 | 83% | $13,630.40 |
| Sideways market | 11 | 45% | −$9,580.30 |
| Falling market | 5 | 40% | −$1,687.42 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
33.4%
The 95th percentile of the worst drawdown when the same 22 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 15.6% to 33.4%; the tick is what actually happened, 27.0%. Order luck alone moves it this much.
All 22 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 14 Sep 2024 60,498.00 | 26 Sep 2024 65,436.90 | 12 | −$4,938.96 |
| long | 20 Dec 2024 97,461.90 | 4 Jan 2025 98,174.20 | 15 | $712.31 |
| long | 3 Feb 2025 97,700.60 | 25 Feb 2025 90,413.40 | 22 | −$7,287.19 |
| long | 26 Feb 2025 88,680.40 | 28 Feb 2025 82,699.00 | 2 | −$5,981.41 |
| short | 13 Apr 2025 85,276.90 | 25 Apr 2025 94,769.00 | 12 | −$9,492.13 |
| long | 30 May 2025 105,590.00 | 10 Jun 2025 110,263.00 | 11 | $4,673.27 |
| long | 22 Jun 2025 102,120.00 | 26 Jun 2025 107,341.00 | 4 | $5,220.57 |
| long | 2 Aug 2025 113,298.00 | 11 Aug 2025 119,294.00 | 9 | $5,996.35 |
| long | 19 Aug 2025 116,227.00 | 25 Aug 2025 110,145.00 | 6 | −$6,082.51 |
| long | 26 Aug 2025 110,112.00 | 11 Sep 2025 113,960.00 | 16 | $3,848.02 |
| long | 23 Sep 2025 112,651.00 | 25 Sep 2025 108,959.00 | 2 | −$3,691.93 |
| long | 26 Sep 2025 108,994.00 | 2 Oct 2025 118,595.00 | 6 | $9,600.50 |
| long | 11 Oct 2025 112,774.00 | 27 Oct 2025 114,559.00 | 16 | $1,784.92 |
| short | 4 Dec 2025 93,429.90 | 16 Dec 2025 86,432.10 | 12 | $6,997.87 |
| short | 5 Jan 2026 91,529.70 | 13 Jan 2026 95,322.00 | 8 | −$3,792.28 |
| short | 14 Jan 2026 95,414.00 | 21 Jan 2026 88,427.70 | 7 | $6,986.33 |
| short | 5 Mar 2026 72,666.80 | 23 Mar 2026 67,859.00 | 18 | $4,807.77 |
| short | 8 Apr 2026 71,924.20 | 17 Apr 2026 76,573.00 | 9 | −$4,648.79 |
| short | 18 Apr 2026 77,072.00 | 6 May 2026 82,124.00 | 18 | −$5,051.99 |
| short | 14 Jun 2026 64,458.00 | 25 Jun 2026 61,078.00 | 11 | $3,380.01 |
| short | 4 Jul 2026 62,583.30 | 19 Aug 2026 67,576.40 | 46 | −$4,993.14 |
| long | 11 Sep 2026 76,568.70 | 19 Sep 2026 80,883.90 | 8 | $4,315.13 |
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 |
|---|---|---|---|---|---|
| Trend Bounce [racer8] this page | — | v4 strategy | +2.4% | 22 | 32.2% |
| TradingGroundhog - Strategy & Fractal V1 by UnknownUnicorn27666852 | highest lowest valuewhen sma | v4 strategy | −2.9% | 8 | 5.9% |
| Buying Dip Strategy With Take Profit And Stop Loss [racer8] by racer8 | sma valuewhen lowest | v4 strategy | −22.3% | 34 | 34.7% |
| ChBrkOutStrategySMA by anshuanshu333 | sma highest lowest | v4 strategy | −4.5% | 42 | 20.9% |
| Swing Points Breakouts by tweakerID | lowest highest valuewhen | v4 strategy | +29.9% | 92 | 52.0% |
| KDJ Strategy @ionvolution by ionvolution | sma highest lowest | v5 strategy | No trades on this market | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 14 ms | Yes 1,000 bars in 32 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/// © racer8//@version=4// This Algo Strategy Has Only 3 rules and 62% Win Rate (Youtube) strategy("Trend Bounce", overlay=true)nn = input(7,"Channel Length")hi = highest(high,nn)lo = lowest(low,nn)n2 = input(200,"Ma Length")ma = sma(close,n2)if close>ma and close<lo[1] strategy.entry("Buy",strategy.long)if close>hi[1] strategy.close("Buy") if close<ma and close>hi[1] strategy.entry("Sell",strategy.short)if close<lo[1] strategy.close("Sell")plot(hi,"high",color=color.aqua)plot(lo,"low",color=color.aqua)plot(ma,"sma",color=color.yellow) //-----------------------------------------Stop Loss------------------------------------------------------atr = sma(tr,10)[1]bought = strategy.position_size[0] > strategy.position_size[1]sold = strategy.position_size[0] < strategy.position_size[1]slm = input(2.0,"ATR Stop Loss",minval=0)StopPrice_Long = strategy.position_avg_price - slm*atr // determines stop loss's price StopPrice_Short = strategy.position_avg_price + slm*atr // determines stop loss's price FixedStopPrice_Long = valuewhen(bought,StopPrice_Long,0) // stores original StopPrice FixedStopPrice_Short = valuewhen(sold,StopPrice_Short,0) // stores original StopPrice plot(FixedStopPrice_Long,"ATR Stop Loss Long",color=color.blue,linewidth=1,style=plot.style_cross)plot(FixedStopPrice_Short,"ATR Stop Loss Short",color=color.red,linewidth=1,style=plot.style_cross)if strategy.position_size > 0 strategy.exit(id="Stop", stop=FixedStopPrice_Long) // commands stop loss order to exit!if strategy.position_size < 0 strategy.exit(id="Stop", stop=FixedStopPrice_Short) // commands stop loss order to exit!