Calculate BTC DCA Cost Basis in Pine Script Without Using a Simple Average
A DCA cost basis must be quantity weighted. Buying 0.01 BTC and 0.10 BTC at two prices cannot be represented by a simple average of those prices.
Build the chart or workflow
The core formula is total cost divided by total quantity. In Pine Script, inputs can store each price and quantity, sum price × quantity, divide by total quantity, and plot the resulting cost line.
How to interpret the result
If fees are charged in quote currency, add them to cost. If they are taken from BTC, reduce received quantity. The script must state which convention it uses.
Common failure modes
Manual inputs are useful for a small set of tranches and review, but they are not an exchange ledger. Sales, transfer costs, tax lots, and precision can materially change long-term basis.
A repeatable checklist
- Record every price and quantity
- use one fee convention
- reconcile total BTC
- spot-check against statements
- apply a consistent accounting method after sales
Can this be used as a standalone trading signal?
No. Treat it as one piece of context. Price structure, liquidity, execution cost, and a predefined invalidation point still decide whether a trade is justified.
When should the setup be checked again?
Recheck whenever the symbol, exchange feed, interval, session definition, or indicator input changes. Those choices can materially change what the chart shows.
Sources and verification
Product behavior and time settings were cross-checked against the following official TradingView material. Market interpretation and workflow notes are editorial guidance.