OPEN-SOURCE SCRIPT
DCA Anchor (Weekly/Monthly/N Bars) [CHE]

What is Dollar-Cost Averaging (DCA)?
DCA is a position-building method where you invest a fixed amount at fixed intervals (e.g., weekly or monthly) regardless of price. Over time, this:
reduces timing risk (you don’t need to guess tops/bottoms),
smooths entry price by buying more units when price is low and fewer when price is high,
keeps decisions simple and repeatable.
Trade-offs:
You’ll never catch the exact bottom.
In strong uptrends, lump-sum can outperform.
Fees matter if you buy very frequently.
Simple math:
Qty bought at time t = `amount / price_t` (net of fees if fees are not “on top”).
Total qty = sum of all buys.
Average price (cost basis) = `total invested / total qty`.
Equity = `total qty last price`.
P\&L = `equity − total invested` (and `%` = `P&L / total invested`).
DCA Anchor (Weekly/Monthly/N Bars)
Purpose: automate scheduled DCA buys on chart data, optionally add extra buys on drawdowns, track stats, and fire alerts.
Core features
Schedules:
1. Every N bars,
2. Weekly (first bar of a new week),
3. Monthly (first bar of a new month).
A Start time input gates when the logic begins.
Fees model:
Fee on top: you pay `amount + fee` in cash; quantity = `amount / close`.
Fee from amount: fee is deducted from the amount; quantity is smaller, cash outlay equals `amount`.
Optional drawdown buys:
Trigger when `close ≤ avgCost (1 − ddPct/100)`.
Controls: drawdown % threshold, multiplier (extra size vs. base amount), and cooldown in bars.
State & metrics: tracks total invested, total quantity, average price, equity, P\&L (abs/%).
Visuals:
Line plot of Average Price.
Buy labels at execution bars (plan and drawdown).
Compact table (positionable) with key stats (trades, invested, qty, avg price, equity, P\&L).
Alerts:
Plan Buy (Bar Close) and Drawdown Buy (Bar Close) — robust, non-repainting.
Optional Intrabar Preview alerts for early heads-up (can fire before bar close).
How to use it (quick start)
1. Add to chart → Inputs:
Buy frequency: pick Every N bars, Weekly, or Monthly.
Start time: date from which buys may begin.
Buy amount: fixed cash per planned buy.
Fees % and Fee on top? to match your broker/exchange model.
(Optional) Enable drawdown buy, set threshold %, multiplier, and cooldown.
Toggle Show buy labels and Show stats table.
2. Alerts (recommended):
Use “DCA Plan Buy (Bar Close)” and/or “DCA Drawdown Buy (Bar Close)” with Once per bar close.
If you need early signals, enable Intrabar pre-alerts and add the two Intrabar Preview alerts with Once per bar.
3. Interpretation:
The yellow line is your average price.
Green/orange markers show plan buys and drawdown buys.
The table summarizes total trades, invested capital, quantity, average price, current equity, and P\&L.
Practical notes
All executions occur at bar close by default to avoid intrabar repainting.
Weekly/monthly roll depends on the symbol’s exchange calendar.
Backtest realism: no slippage, no partial fills. Fees are modeled as configured.
If you buy very frequently, consider higher “N” or weekly/monthly to keep fees under control.
If you want, I can tailor the defaults (amount, fee model, drawdown rules) to your typical markets and timeframes.
Disclaimer
No indicator guarantees profits. DCA Anchor (Weekly/Monthly/N Bars) [CHE] is a decision aid; always combine with solid risk management and your own judgment. Backtest, forward test, and size responsibly.
The content provided, including all code and materials, is strictly for educational and informational purposes only. It is not intended as, and should not be interpreted as, financial advice, a recommendation to buy or sell any financial instrument, or an offer of any financial product or service. All strategies, tools, and examples discussed are provided for illustrative purposes to demonstrate coding techniques and the functionality of Pine Script within a trading context.
Any results from strategies or tools provided are hypothetical, and past performance is not indicative of future results. Trading and investing involve high risk, including the potential loss of principal, and may not be suitable for all individuals. Before making any trading decisions, please consult with a qualified financial professional to understand the risks involved.
By using this script, you acknowledge and agree that any trading decisions are made solely at your discretion and risk.
Enhance your trading precision and confidence 🚀
Best regards
Chervolino
DCA is a position-building method where you invest a fixed amount at fixed intervals (e.g., weekly or monthly) regardless of price. Over time, this:
reduces timing risk (you don’t need to guess tops/bottoms),
smooths entry price by buying more units when price is low and fewer when price is high,
keeps decisions simple and repeatable.
Trade-offs:
You’ll never catch the exact bottom.
In strong uptrends, lump-sum can outperform.
Fees matter if you buy very frequently.
Simple math:
Qty bought at time t = `amount / price_t` (net of fees if fees are not “on top”).
Total qty = sum of all buys.
Average price (cost basis) = `total invested / total qty`.
Equity = `total qty last price`.
P\&L = `equity − total invested` (and `%` = `P&L / total invested`).
DCA Anchor (Weekly/Monthly/N Bars)
Purpose: automate scheduled DCA buys on chart data, optionally add extra buys on drawdowns, track stats, and fire alerts.
Core features
Schedules:
1. Every N bars,
2. Weekly (first bar of a new week),
3. Monthly (first bar of a new month).
A Start time input gates when the logic begins.
Fees model:
Fee on top: you pay `amount + fee` in cash; quantity = `amount / close`.
Fee from amount: fee is deducted from the amount; quantity is smaller, cash outlay equals `amount`.
Optional drawdown buys:
Trigger when `close ≤ avgCost (1 − ddPct/100)`.
Controls: drawdown % threshold, multiplier (extra size vs. base amount), and cooldown in bars.
State & metrics: tracks total invested, total quantity, average price, equity, P\&L (abs/%).
Visuals:
Line plot of Average Price.
Buy labels at execution bars (plan and drawdown).
Compact table (positionable) with key stats (trades, invested, qty, avg price, equity, P\&L).
Alerts:
Plan Buy (Bar Close) and Drawdown Buy (Bar Close) — robust, non-repainting.
Optional Intrabar Preview alerts for early heads-up (can fire before bar close).
How to use it (quick start)
1. Add to chart → Inputs:
Buy frequency: pick Every N bars, Weekly, or Monthly.
Start time: date from which buys may begin.
Buy amount: fixed cash per planned buy.
Fees % and Fee on top? to match your broker/exchange model.
(Optional) Enable drawdown buy, set threshold %, multiplier, and cooldown.
Toggle Show buy labels and Show stats table.
2. Alerts (recommended):
Use “DCA Plan Buy (Bar Close)” and/or “DCA Drawdown Buy (Bar Close)” with Once per bar close.
If you need early signals, enable Intrabar pre-alerts and add the two Intrabar Preview alerts with Once per bar.
3. Interpretation:
The yellow line is your average price.
Green/orange markers show plan buys and drawdown buys.
The table summarizes total trades, invested capital, quantity, average price, current equity, and P\&L.
Practical notes
All executions occur at bar close by default to avoid intrabar repainting.
Weekly/monthly roll depends on the symbol’s exchange calendar.
Backtest realism: no slippage, no partial fills. Fees are modeled as configured.
If you buy very frequently, consider higher “N” or weekly/monthly to keep fees under control.
If you want, I can tailor the defaults (amount, fee model, drawdown rules) to your typical markets and timeframes.
Disclaimer
No indicator guarantees profits. DCA Anchor (Weekly/Monthly/N Bars) [CHE] is a decision aid; always combine with solid risk management and your own judgment. Backtest, forward test, and size responsibly.
The content provided, including all code and materials, is strictly for educational and informational purposes only. It is not intended as, and should not be interpreted as, financial advice, a recommendation to buy or sell any financial instrument, or an offer of any financial product or service. All strategies, tools, and examples discussed are provided for illustrative purposes to demonstrate coding techniques and the functionality of Pine Script within a trading context.
Any results from strategies or tools provided are hypothetical, and past performance is not indicative of future results. Trading and investing involve high risk, including the potential loss of principal, and may not be suitable for all individuals. Before making any trading decisions, please consult with a qualified financial professional to understand the risks involved.
By using this script, you acknowledge and agree that any trading decisions are made solely at your discretion and risk.
Enhance your trading precision and confidence 🚀
Best regards
Chervolino
Skrip open-source
Dengan semangat TradingView yang sesungguhnya, penulis skrip ini telah menjadikannya sumber terbuka, sehingga para trader dapat meninjau dan memverifikasi fungsinya. Hormat untuk penulisnya! Meskipun anda dapat menggunakannya secara gratis, ingatlah bahwa penerbitan ulang kode tersebut tunduk pada Tata Tertib kami.
Pernyataan Penyangkalan
Informasi dan publikasi tidak dimaksudkan untuk menjadi, dan bukan merupakan saran keuangan, investasi, perdagangan, atau rekomendasi lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Persyaratan Penggunaan.
Skrip open-source
Dengan semangat TradingView yang sesungguhnya, penulis skrip ini telah menjadikannya sumber terbuka, sehingga para trader dapat meninjau dan memverifikasi fungsinya. Hormat untuk penulisnya! Meskipun anda dapat menggunakannya secara gratis, ingatlah bahwa penerbitan ulang kode tersebut tunduk pada Tata Tertib kami.
Pernyataan Penyangkalan
Informasi dan publikasi tidak dimaksudkan untuk menjadi, dan bukan merupakan saran keuangan, investasi, perdagangan, atau rekomendasi lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Persyaratan Penggunaan.