Skip to content

antoineprdhmm/dca_backtesting_framework

Repository files navigation

DCA Backtesting Framework

A Python framework to backtest Dollar Cost Averaging (DCA) strategies on historical stock data. Visualize how your portfolio would have performed through market crashes, downtrends, and recoveries.

Why I Built This

DCA is often presented as the holy grail of passive investing:

  • Stock going up? Great, you're making money.
  • Stock going down? Great, you're buying cheaper!

But it feels wrong watching the position turn increasingly red and continue to pour money in. This framework helps to test and visualise DCA with historical data.

Sample Scenarios

The framework includes pre-configured scenarios on real market events, like:

Stock Scenario What Happened
LVMH 2022–now Peak at €900 → crash to €435 → recovery to ~€600. Big fall with some intermediate recoveries.
Pernod Ricard 2023–now Steady decline from €217 to €73. A "falling knife" scenario.
S&P 500 1995–2010 Dot-com bubble, subprime crisis. Two major crashes in 15 years.

Features

  • 3 DCA strategies — fractional shares, fixed whole shares, or whole shares with cash buffer
  • Dividend tracking — accounts for dividends received during the investment period
  • Transaction fees — includes broker fees in performance calculations
  • Visual analysis — plots the average cost (PRU) vs market price over time

Installation

# Create and activate virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

Quick Start

# List all available scenarios
python main.py --list

# Run a specific scenario
python main.py lvmh_down_fractional

Available Strategies

Strategy Description Use Case
fractional Invest a fixed € amount, buy fractional shares Modern brokers
whole_fixed Buy a fixed number of whole shares Traditional brokers
whole_buffer Invest fixed € amount, buy whole shares only, keep remainder as buffer Traditional brokers

Creating Custom Scenarios

Edit scenarios.py to add your own:

"my_scenario": Scenario(
    name="My Custom Scenario",
    start_date=datetime(2020, 1, 1),
    end_date=datetime(2024, 12, 31),
    transaction_fees=1.5,          # € per transaction
    strategy="fractional",         # or "whole_fixed", "whole_buffer"
    config_params={
        "amount_to_invest": 200    # € per month (or "nb_shares_to_buy" for whole_fixed)
    },
    dataset_path="data/MY_STOCK.csv",
),

Using Your Own Data

Place a CSV file in the data/ folder with at least these columns:

Column Description
time Date in YYYY-MM-DD format
close Closing price
Dividends per share (common stock primary issue) Optional: dividend amount

Tip: I'm using TradingView csv exports. If you have a subscription, your can simply copy paste their CSV inside data/.

Output

The framework calculates and displays:

  • Total invested — including transaction fees
  • P&L — profit/loss in €
  • Return % — percentage return on investment
  • Chart — your average cost vs market price over time

License

MIT

About

Python framework to test your DCA strategy on historical data

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages