Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (4)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$80,663$202,527Equity from $100,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots.
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

Verified

Same 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]⋯

Clean v5

VerifiedDownload the v5 script

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.

SettingDefaultType and rangeFeeds
ATR Length10intatr
ATR Take Profit2.0float · min 0TakePrice
ATR Stop Loss2.0float · min 0StopPrice
Dip Length7intDip

What it calculates

  • SMA (sma, ta.sma in v5)tr, ATR Length (10)atr · line 6
  • Valuewhen (valuewhen, ta.valuewhen in v5)bought, TakePrice, 0FixedTakePrice · line 10
  • Valuewhen (valuewhen, ta.valuewhen in v5)bought, StopPrice, 0FixedStopPrice · line 14
  • Lowest (lowest, ta.lowest in 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.

atnATR Lengthsetting · 10lowlowprice datanDip Lengthsetting · 7tpmATR Take Profitsetting · 2.0slmATR Stop Losssetting · 2.0closecloseprice dataatrSMA→ atrFixedTakePriceValuewhen→ FixedTakePriceFixedStopPriceValuewhen→ FixedStopPriceDipLowest→ DipTakePriceTakePricevalueStopPriceStopPricevalueboughtboughtconditionentry:BuyOpen Buystrategy.entryexit:Stopexit Stopstrategy.exitplot:11ATR Take Profitplotplot:15ATR Stop Lossplotplot:23Dip Lineplot

Order rules

OrderFires whenBy defaultLine
Open Buy longclose<Dip[1]On19
Exit Stop stop and limitstrategy.position_size > 0On21

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.

10/10trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D−22.3%+102.5%3441%0.8134.7%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+10.6%+5.5%4556%1.3413.7%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−1.4%+9.8%4144%0.802.6%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+0.2%+8.4%3757%1.170.7%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−0.2%+16.7%3642%0.630.2%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h−0.0%+21.5%4146%0.780.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+0.1%+148.3%3155%1.100.4%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+0.0%+17.1%4255%1.120.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 barsTradesWonP&L
Rising market757%$2,400.77
Sideways market1625%−$41,342.30
Falling market1155%$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.

0%60%
All 34 trades
SideEntryExitBarsP&L
long17 Mar 2024 65,300.601 May 2024 57,583.6045−$7,717.02
long2 May 2024 58,365.004 May 2024 63,776.102$5,411.12
long12 Jun 2024 67,314.2021 Jun 2024 63,591.909−$3,722.33
long24 Jun 2024 63,210.0028 Jun 2024 60,171.404−$3,038.64
long5 Jul 2024 57,050.0014 Jul 2024 61,156.209$4,106.14
long3 Aug 2024 61,498.305 Aug 2024 56,182.102−$5,316.28
long6 Aug 2024 54,018.808 Aug 2024 61,027.402$7,008.58
long2 Sep 2024 57,301.806 Sep 2024 52,636.204−$4,665.58
long7 Sep 2024 53,963.0013 Sep 2024 58,845.506$4,882.52
long2 Oct 2024 60,804.9014 Oct 2024 64,958.2012$4,153.25
long20 Dec 2024 97,461.9019 Jan 2025 106,128.0030$8,666.40
long3 Feb 2025 97,700.6025 Feb 2025 90,413.4022−$7,287.19
long26 Feb 2025 88,680.4028 Feb 2025 82,699.002−$5,981.41
long10 Mar 2025 80,734.5022 Apr 2025 92,883.6043$12,149.20
long30 May 2025 105,590.0022 Jun 2025 99,189.2023−$6,400.53
long2 Aug 2025 113,298.0010 Aug 2025 118,579.008$5,281.44
long19 Aug 2025 116,227.0025 Aug 2025 110,145.006−$6,082.51
long26 Aug 2025 110,112.0012 Sep 2025 116,450.0017$6,338.03
long23 Sep 2025 112,651.0025 Sep 2025 108,959.002−$3,691.93
long26 Sep 2025 108,994.0029 Sep 2025 113,693.003$4,698.36
long11 Oct 2025 112,774.004 Nov 2025 102,296.0024−$10,478.70
long5 Nov 2025 101,497.0016 Nov 2025 93,488.4011−$8,008.78
long18 Nov 2025 92,215.1021 Nov 2025 84,080.003−$8,135.11
long22 Nov 2025 85,129.4013 Jan 2026 95,151.8052$10,022.40
long21 Jan 2026 88,427.7029 Jan 2026 83,394.808−$5,032.86
long30 Jan 2026 84,650.2031 Jan 2026 78,827.001−$5,823.14
long1 Feb 2026 78,741.104 Feb 2026 72,141.303−$6,599.82
long6 Feb 2026 62,909.904 Mar 2026 73,493.6026$10,583.70
long23 Mar 2026 67,859.009 Apr 2026 73,024.6017$5,165.55
long17 May 2026 78,148.1028 May 2026 73,982.6011−$4,165.47
long29 May 2026 73,617.502 Jun 2026 69,915.804−$3,701.74
long3 Jun 2026 66,760.804 Jun 2026 62,522.301−$4,238.57
long5 Jun 2026 63,886.0025 Jun 2026 58,771.8020−$5,114.14
long11 Sep 2026 76,568.7018 Sep 2026 81,015.307$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.

ScriptSharesTypeNetTradesMax drawdown
Buying Dip Strategy With Take Profit And Stop Loss [racer8] this page—v4 strategy−22.3%3434.7%
Trend Bounce [racer8] by racer8lowest sma valuewhenv4 strategy+2.4%2232.2%
polynomic_stop by IntelTradingsma lowestv4 strategy−6.9%433.0%
Rocket Grid Algorithm - The Quant Science by thequantsciencesma valuewhenv5 strategyNo trades on this market
TradingGroundhog - Strategy & Fractal V1 by UnknownUnicorn27666852lowest valuewhen smav4 strategy−2.9%85.9%
ChBrkOutStrategySMA by anshuanshu333sma lowestv4 strategy−4.5%4220.9%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 9 msYes 1,000 bars in 30 msNot measured
PineTSNo 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)    // ♡♡♡