Pine Script (TradingView)
This is a TradingView Pine Script strategy example for this page concept. Paste it into the TradingView Pine Editor, add it to your chart, and run it in the Strategy Tester.
//@version=6
strategy("Dollar-Cost Averaging (DCA) Strategy", overlay=true)
dropPct = input.float(3.0, "Buy on Drop %")
rsiLen = input.int(14, "RSI Length")
rsiOversold = input.int(35, "RSI Oversold")
r = ta.rsi(close, rsiLen)
drop = (close - ta.highest(close, 10)) / ta.highest(close, 10) * 100
// DCA: buy when price drops the configured % OR RSI is oversold
dcaBuy = drop < -dropPct or r < rsiOversold
if dcaBuy and strategy.position_size == 0
strategy.entry("DCA", strategy.long)
// Take profit at +10% above average cost
if strategy.position_size > 0 and close > strategy.position_avg_price * 1.10
strategy.close("DCA")
plot(ta.ema(close, 50), "EMA 50", color=color.orange)