Pine Script v4 · Strategy
Buying Dip Strategy With Take Profit And Stop Loss [racer8]
A Pine Script v4 strategy built on SMA (tr, 10), Valuewhen (bought, TakePrice) and Lowest (low, 7). It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits use a stop loss and a profit target. Each trade is one contract and pays no commission. 4 settings; defaults include ATR Length 10, ATR Take Profit 2.0 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 Buying Dip Strategy With Take Profit And Stop Loss [racer8] repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 10 of 10 trades.
- How did Buying Dip Strategy With Take Profit And Stop Loss [racer8] do on BTCUSDT 1D with default settings?
- −22.29%
- From 26 Jan 2024 to 21 Oct 2026, over 34 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Buying Dip Strategy With Take Profit And Stop Loss [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 Buying Dip Strategy With Take Profit And Stop Loss [racer8] use a stop loss?
- Yes
- strategy.exit sets a stop on line 21.
- Does Buying Dip Strategy With Take Profit And Stop Loss [racer8] run in Zeal Terminal?
- Yes
- It compiles in 9 ms and runs 1,000 bars in 30 ms, in the browser.
BTCUSDT · 1D · default settings
- ATR Take Profit
- ATR Stop Loss
- Dip Line
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −22.29%
- −$22,288.50
- Buy and hold
- +102.5%
- same period
- Closed trades
- 34
- 14 won · 20 lost
- Profitable
- 41.2%
- of closed trades
- Profit factor
- 0.81
- gross profit ÷ gross loss
- Max drawdown
- 34.71%
- −$38,104.00
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.
-
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 sma → ta.sma, valuewhen → ta.valuewhen, lowest → ta.lowest.
v4 → v5
VerifiedSame 34 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: sma → ta.sma
- Line 6: tr → ta.tr
- Line 10: valuewhen → ta.valuewhen
- Line 14: valuewhen → ta.valuewhen
- Line 17: lowest → ta.lowest
⋯// © racer8-//@version=4+//@version=5strategy("BuyTheDip", overlay=true)atn = input(10, "ATR Length")-atr = sma(tr,atn)[1]+atr = ta.sma(ta.tr,atn)[1]bought = strategy.position_size[0] > strategy.position_size[1]⋯TakePrice = strategy.position_avg_price + tpm*atr // determines Take Profit's price -FixedTakePrice = valuewhen(bought,TakePrice,0) // stores original TakePrice +FixedTakePrice = ta.valuewhen(bought,TakePrice,0) // stores original TakePrice plot(FixedTakePrice,"ATR Take Profit",color=color.blue,linewidth=1,style=plot.style_cross)⋯StopPrice = strategy.position_avg_price - slm*atr // determines stop loss's price -FixedStopPrice = valuewhen(bought,StopPrice,0) // stores original StopPrice +FixedStopPrice = ta.valuewhen(bought,StopPrice,0) // stores original StopPrice plot(FixedStopPrice,"ATR Stop Loss",color=color.yellow,linewidth=1,style=plot.style_cross)n = input(7,"Dip Length")-Dip = lowest(low,n)+Dip = ta.lowest(low,n)if close<Dip[1]⋯Same 34 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 5: input() → input.int()
- Line 8: input() → input.float()
- Line 12: input() → input.float()
- Line 16: input() → input.int()
⋯strategy("BuyTheDip", overlay=true)-atn = input(10, "ATR Length")+atn = input.int(10, "ATR Length")atr = ta.sma(ta.tr,atn)[1]bought = strategy.position_size[0] > strategy.position_size[1]-tpm = input(2.0,"ATR Take Profit",minval=0)+tpm = input.float(2.0,"ATR Take Profit",minval=0)TakePrice = strategy.position_avg_price + tpm*atr // determines Take Profit's price ⋯plot(FixedTakePrice,"ATR Take Profit",color=color.blue,linewidth=1,style=plot.style_cross)-slm = input(2.0,"ATR Stop Loss",minval=0)+slm = input.float(2.0,"ATR Stop Loss",minval=0)StopPrice = strategy.position_avg_price - slm*atr // determines stop loss's price ⋯plot(FixedStopPrice,"ATR Stop Loss",color=color.yellow,linewidth=1,style=plot.style_cross)-n = input(7,"Dip Length")+n = input.int(7,"Dip Length")Dip = ta.lowest(low,n)⋯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: 34 before, 31 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=6strategy("BuyTheDip", 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 |
|---|---|---|---|
| ATR Length | 10 | int | atr |
| ATR Take Profit | 2.0 | float · min 0 | TakePrice |
| ATR Stop Loss | 2.0 | float · min 0 | StopPrice |
| Dip Length | 7 | int | Dip |
What it calculates
- SMA (
sma,ta.smain v5)tr, ATR Length (10)atr · line 6 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, TakePrice, 0FixedTakePrice · line 10 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, StopPrice, 0FixedStopPrice · line 14 - Lowest (
lowest,ta.lowestin v5)low, Dip Length (7)Dip · line 17
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<Dip[1] | On | 19 |
| Exit Stop stop and limit | strategy.position_size > 0 | On | 21 |
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 | −22.3% | +102.5% | 34 | 41% | 0.81 | 34.7% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +10.6% | +5.5% | 45 | 56% | 1.34 | 13.7% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −1.4% | +9.8% | 41 | 44% | 0.80 | 2.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.2% | +8.4% | 37 | 57% | 1.17 | 0.7% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.2% | +16.7% | 36 | 42% | 0.63 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.0% | +21.5% | 41 | 46% | 0.78 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.1% | +148.3% | 31 | 55% | 1.10 | 0.4% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 42 | 55% | 1.12 | 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 | 7 | 57% | $2,400.77 |
| Sideways market | 16 | 25% | −$41,342.30 |
| Falling market | 11 | 55% | $16,653.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
51.9%
The 95th percentile of the worst drawdown when the same 34 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 29.5% to 51.9%; the tick is what actually happened, 33.3%. Order luck alone moves it this much.
All 34 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 17 Mar 2024 65,300.60 | 1 May 2024 57,583.60 | 45 | −$7,717.02 |
| long | 2 May 2024 58,365.00 | 4 May 2024 63,776.10 | 2 | $5,411.12 |
| long | 12 Jun 2024 67,314.20 | 21 Jun 2024 63,591.90 | 9 | −$3,722.33 |
| long | 24 Jun 2024 63,210.00 | 28 Jun 2024 60,171.40 | 4 | −$3,038.64 |
| long | 5 Jul 2024 57,050.00 | 14 Jul 2024 61,156.20 | 9 | $4,106.14 |
| long | 3 Aug 2024 61,498.30 | 5 Aug 2024 56,182.10 | 2 | −$5,316.28 |
| long | 6 Aug 2024 54,018.80 | 8 Aug 2024 61,027.40 | 2 | $7,008.58 |
| long | 2 Sep 2024 57,301.80 | 6 Sep 2024 52,636.20 | 4 | −$4,665.58 |
| long | 7 Sep 2024 53,963.00 | 13 Sep 2024 58,845.50 | 6 | $4,882.52 |
| long | 2 Oct 2024 60,804.90 | 14 Oct 2024 64,958.20 | 12 | $4,153.25 |
| long | 20 Dec 2024 97,461.90 | 19 Jan 2025 106,128.00 | 30 | $8,666.40 |
| 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 |
| long | 10 Mar 2025 80,734.50 | 22 Apr 2025 92,883.60 | 43 | $12,149.20 |
| long | 30 May 2025 105,590.00 | 22 Jun 2025 99,189.20 | 23 | −$6,400.53 |
| long | 2 Aug 2025 113,298.00 | 10 Aug 2025 118,579.00 | 8 | $5,281.44 |
| long | 19 Aug 2025 116,227.00 | 25 Aug 2025 110,145.00 | 6 | −$6,082.51 |
| long | 26 Aug 2025 110,112.00 | 12 Sep 2025 116,450.00 | 17 | $6,338.03 |
| long | 23 Sep 2025 112,651.00 | 25 Sep 2025 108,959.00 | 2 | −$3,691.93 |
| long | 26 Sep 2025 108,994.00 | 29 Sep 2025 113,693.00 | 3 | $4,698.36 |
| long | 11 Oct 2025 112,774.00 | 4 Nov 2025 102,296.00 | 24 | −$10,478.70 |
| long | 5 Nov 2025 101,497.00 | 16 Nov 2025 93,488.40 | 11 | −$8,008.78 |
| long | 18 Nov 2025 92,215.10 | 21 Nov 2025 84,080.00 | 3 | −$8,135.11 |
| long | 22 Nov 2025 85,129.40 | 13 Jan 2026 95,151.80 | 52 | $10,022.40 |
| long | 21 Jan 2026 88,427.70 | 29 Jan 2026 83,394.80 | 8 | −$5,032.86 |
| long | 30 Jan 2026 84,650.20 | 31 Jan 2026 78,827.00 | 1 | −$5,823.14 |
| long | 1 Feb 2026 78,741.10 | 4 Feb 2026 72,141.30 | 3 | −$6,599.82 |
| long | 6 Feb 2026 62,909.90 | 4 Mar 2026 73,493.60 | 26 | $10,583.70 |
| long | 23 Mar 2026 67,859.00 | 9 Apr 2026 73,024.60 | 17 | $5,165.55 |
| long | 17 May 2026 78,148.10 | 28 May 2026 73,982.60 | 11 | −$4,165.47 |
| long | 29 May 2026 73,617.50 | 2 Jun 2026 69,915.80 | 4 | −$3,701.74 |
| long | 3 Jun 2026 66,760.80 | 4 Jun 2026 62,522.30 | 1 | −$4,238.57 |
| long | 5 Jun 2026 63,886.00 | 25 Jun 2026 58,771.80 | 20 | −$5,114.14 |
| long | 11 Sep 2026 76,568.70 | 18 Sep 2026 81,015.30 | 7 | $4,446.55 |
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 |
|---|---|---|---|---|---|
| Buying Dip Strategy With Take Profit And Stop Loss [racer8] this page | — | v4 strategy | −22.3% | 34 | 34.7% |
| Trend Bounce [racer8] by racer8 | lowest sma valuewhen | v4 strategy | +2.4% | 22 | 32.2% |
| polynomic_stop by IntelTrading | sma lowest | v4 strategy | −6.9% | 4 | 33.0% |
| Rocket Grid Algorithm - The Quant Science by thequantscience | sma valuewhen | v5 strategy | No trades on this market | ||
| TradingGroundhog - Strategy & Fractal V1 by UnknownUnicorn27666852 | lowest valuewhen sma | v4 strategy | −2.9% | 8 | 5.9% |
| ChBrkOutStrategySMA by anshuanshu333 | sma lowest | v4 strategy | −4.5% | 42 | 20.9% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 9 ms | Yes 1,000 bars in 30 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=4strategy("BuyTheDip", overlay=true)atn = input(10, "ATR Length")atr = sma(tr,atn)[1]bought = strategy.position_size[0] > strategy.position_size[1]tpm = input(2.0,"ATR Take Profit",minval=0)TakePrice = strategy.position_avg_price + tpm*atr // determines Take Profit's price FixedTakePrice = valuewhen(bought,TakePrice,0) // stores original TakePrice plot(FixedTakePrice,"ATR Take Profit",color=color.blue,linewidth=1,style=plot.style_cross)slm = input(2.0,"ATR Stop Loss",minval=0)StopPrice = strategy.position_avg_price - slm*atr // determines stop loss's price FixedStopPrice = valuewhen(bought,StopPrice,0) // stores original StopPrice plot(FixedStopPrice,"ATR Stop Loss",color=color.yellow,linewidth=1,style=plot.style_cross)n = input(7,"Dip Length")Dip = lowest(low,n)if close<Dip[1] strategy.entry("Buy",strategy.long)if strategy.position_size > 0 strategy.exit(id="Stop", stop=FixedStopPrice, limit=FixedTakePrice) // commands stop loss order to exit! plot(Dip,"Dip Line",color=color.aqua,linewidth=2) // ♡♡♡