This Pine script strategy implements a daily dollar-cost averaging approach on the TradingView platform, incorporating EMA touch signals to determine entry points. It follows the dollar-cost averaging methodology to make fixed-amount investments every day, spreading purchases over time to mitigate risk. The EMA crossovers then serve as the specific trigger for entries.
The strategy has the following key features:
Daily Dollar-Cost Averaging
EMAs for Entry Signals
Dynamic Stop Loss
Trade Count Limit
Specifically, every day the strategy invests a fixed amount and calculates the shares to buy based on the closing price. If the closing price crosses above any of the 5-, 10-, 20-day EMA etc., a buy signal is triggered. Once the accumulated trade count hits the 300 limit, no further buys will occur. Additionally, if the price closes below the 20-day SMA or reaches the preset exit date, all positions are cleared. The script also plots the EMAs on the price chart for visual analysis.
The advantages of this strategy include:
Risk Diversification
EMA Combination Avoids Pullbacks
Dynamic Stop Loss Controls Losses
Trade Limit Controls Risks
Intuitive EMA Visualization
Highly Customizable
The strategy also carries some risks to note:
Systemic Risks Still Exist
Fixed Investment Amount
EMAs Cannot React to Extreme Moves
Trade Limit Caps Profit Potential
Stop Loss Placement Requires Care
Further optimizations:
Dynamic Daily Investment Amount
Additional Entry Signals
Exponential Moving Averages
Dynamic Position Limit
Trailing Stop Loss
In summary, this EMA-combined daily DCA strategy realizes the concept of long-term periodic investments, spreading risks across multiple small entries compared to large one-time purchases. The EMAs help avoid short-term pullback risks to a certain extent, while the stop loss controls max loss. Still, black swan risks and the limitations of fixed investment size need to be kept in mind. These aspects provide future enhancement directions through parameter tuning and indicator combinations for building efficient yet stable quant strategies.
/*backtest start: 2024-01-08 00:00:00 end: 2024-01-15 00:00:00 period: 3m basePeriod: 1m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=4 strategy("Daily DCA Strategy with Touching EMAs", overlay=true, pyramiding=10000) // Customizable Parameters daily_investment = input(50000, title="Daily Investment") start_year = input(2022, title="Start Year") start_month = input(1, title="Start Month") start_day = input(1, title="Start Day") end_year = input(2023, title="End Year") end_month = input(12, title="End Month") end_day = input(1, title="End Day") trade_count_limit = input(10000, title="Pyramiding Limit") enable_sell = input(true, title="Enable Sell") start_date = timestamp(start_year, start_month, start_day) var int trade_count = 0 // Calculate the number of shares to buy based on the current closing price shares_to_buy = daily_investment / close // Check if a new day has started and after the start date isNewDay = dayofmonth != dayofmonth[1] and time >= start_date // Buy conditions based on EMA crossovers ema5_cross_above = crossover(close, ema(close, 5)) ema10_cross_above = crossover(close, ema(close, 10)) ema20_cross_above = crossover(close, ema(close, 20)) ema50_cross_above = crossover(close, ema(close, 50)) ema100_cross_above = crossover(close, ema(close, 100)) ema200_cross_above = crossover(close, ema(close, 200)) if isNewDay and (ema5_cross_above or ema10_cross_above or ema20_cross_above or ema50_cross_above or ema100_cross_above or ema200_cross_above) and trade_count < trade_count_limit strategy.entry("Buy", strategy.long, qty=shares_to_buy) trade_count := trade_count + 1 // Dynamic sell conditions (optional) sell_condition = true if enable_sell and sell_condition strategy.close_all() // EMA Ribbon for visualization plot(ema(close, 5), color=color.red, title="EMA 5") plot(ema(close, 10), color=color.orange, title="EMA 10") plot(ema(close, 20), color=color.yellow, title="EMA 20") plot(ema(close, 50), color=color.green, title="EMA 50") plot(ema(close, 100), color=color.blue, title="EMA 100") plot(ema(close, 200), color=color.purple, title="EMA 200")