Buying dips with take profit and stop loss

Author: ChaoZhang, Date: 2023-11-23 16:50:01



This strategy buys the dip and takes profit for a high win rate by dynamically tracking the price lows, going long after price dips, and locking in profits and controlling risks through adaptive take profit and stop loss.


The core logic of this strategy is to use the ATR indicator to calculate dynamic take profit and stop loss positions. Specifically, a long signal is triggered when the closing price is below the lowest low of the past n days (set to 7 days in the code); during long positions, take profit and stop loss prices will be calculated dynamically based on the ATR indicator (set through ATR multiples) and displayed on the chart in real time. Profit taking or risk control can be achieved when price hits the take profit or stop loss points.

The strategy combines the simplest buying dip approach with the idea of dynamic stop loss/take profit to capture opportunities in a timely manner while controlling risks.


The main advantages of this strategy are:

  1. Using dynamic ATR indicators to set stop loss and take profit can adjust P/L levels based on market volatility, avoiding unnecessary losses or missing greater profit opportunities due to overly fixed stop loss/profit taking. This is the biggest highlight of the strategy.

  2. Buying dip strategies tend to have higher win rates during market consolidations when prices dip below support levels abnormally and likely to bounce back.

  3. Estimating take profit/stop loss ratios through ATR values is reasonable and can be flexibly set according to market conditions and personal risk tolerance.

  4. The code logic is simple and clear, easy to understand. Parameter settings are also intuitive. It is suitable as an exemplary strategy for learning.


The main risks of this strategy are:

  1. Unable to determine the amplitude and strength of the rebound after the dip. There is a risk that profit expectations fall short. This can be addressed by adjusting ATR parameters to set different take profit ranges.

  2. Risk of being trapped in losses when prices break supports and continue to fall, facing greater loss. This can be mitigated by reducing position sizing and lowering stop loss ATR multiplier to cap losses.

  3. Stop loss being too tight may also get knocked out unnecessarily. ATR multiples should be set more loosely to avoid unnecessary stop outs.

  4. Backtest overfit risks. Testing under different market conditions is necessary, with proper slippage/commission settings.


The strategy can be enhanced in the following aspects:

  1. Optimizing support level and signal determination. More sophisticated indicators like KDJ or Bollinger Bands can be used to judge reversal signals more reliably.

  2. Optimizing position sizing rules. Dynamically adjust position sizes based on market volatility etc.

  3. Implement trailing stop loss module. Tighten stops after prices advance by certain range, to lock in partial profits.

  4. Adding confluence filters. Enter long only if corresponding sector/the broad market also reaches support, verifying signal reliability.


This strategy captures mean-reversion opportunities through buying dips, with take profit/stop loss for risk control. Despite room for more sophistication, it is simple enough for beginners to understand and learn from. Further improvements can enhance robustness and adaptability.

start: 2022-11-16 00:00:00
end: 2023-11-22 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]

// This source code is subject to the terms of the Mozilla Public License 2.0 at
// © racer8
strategy("Buy-The-Dip", overlay=true)

atn = input(15, "ATR Period")
atr = sma(tr,atn)[1]
bought = strategy.position_size[0] > strategy.position_size[1]

slm = input(2.0,"ATR SL Multiple",minval=0)
StopPrice  = strategy.position_avg_price - slm*atr              // determines stop loss's price 
FixedStopPrice = valuewhen(bought,StopPrice,0)                  // stores original StopPrice  
plot(FixedStopPrice,"Stop Loss",,linewidth=2,style=plot.style_cross)

tpm = input(1.0,"ATR TP Multiple",minval=0)
TakePrice  = strategy.position_avg_price + tpm*atr              // determines Take Profit's price 
FixedTakePrice = valuewhen(bought,TakePrice,0)                  // stores original TakePrice  
plot(FixedTakePrice,"Take Profit",,linewidth=2,style=plot.style_cross)

nn = input(7,"Channel Length")
ll = lowest(low,nn)

if close<ll[1]
if strategy.position_size > 0
    strategy.exit(id="XL SL", stop=FixedStopPrice, limit=FixedTakePrice)    // commands stop loss order to exit!