Skip to content
All library documents

Adding Monthly Contributions to a BT Backtest

Article Quant Q&A · Author: Mike Sell

Summary

The document shows how to model a dollar cost averaging schedule in the Python bt backtesting framework. The proposed strategy runs monthly, selects the available asset, adds a fixed capital contribution, assigns equal weights, and rebalances. The explanation emphasizes that adding capital alone does not allocate it into holdings: the capital flow algorithm needs a rebalance step, and rebalancing in turn needs portfolio weights.

An example applies the setup to ETH-USD and reports backtest performance statistics over the displayed sample period, including return, Sharpe ratio, and drawdown measures. Those figures describe this particular historical run; they do not establish that monthly investing will outperform a lump-sum investment or predict future crypto returns. The questioner’s broader plan to compare investment schedules and asset mixes is not evaluated in the answer, and the result depends on the data, dates, and framework configuration used.

Key ideas

  • A monthly run schedule can trigger recurring contributions in a bt strategy.
  • CapitalFlow adds funds, while weighting and rebalancing are needed to allocate them to holdings.
  • The example applies monthly contributions to an ETH-USD backtest.
  • Reported historical performance statistics are specific to the displayed backtest and do not establish future results.

Tags

Full text
# Include Dollar Cost Averaging Strategy in BT python


# Include Dollar Cost Averaging Strategy in BT python












I am using bt backtesting to test between an initial lump sum into 'ETH-USD' and a dollar cost average approach. I will then look into a different mix of equally weighted crypto.

What I like about bt is I can get stats including the sharpe ratio and max drawdown after running a backtest. I have this so far:

```
data = bt.get('ETH-USD', start='2018-01-01')

s2 = bt.Strategy('s2', 
[bt.algos.RunMonthly(),
bt.algos.SelectAll(), 
bt.algos.CapitalFlow(1000)])

# create a backtest and run it
test = bt.Backtest(s2, data)
res = bt.run(test)
```

However, I just don't know how to create my own algorithm to invest $1000 per month. I feel there should be a simple way to do this though.

## Answer by Clebson Derivan (score 2)

https://quant.stackexchange.com/a/66091

CapitaFlow Algo requires Rebalance:

> the capital will remain in the strategy until a re-allocation/rebalancement is made.

And Rebalance requires Weight if you include it on your strategy you might see the result:

```
data = bt.get('ETH-USD', start='2018-01-01')

s2 = bt.Strategy('s2', 
[bt.algos.RunMonthly(),
bt.algos.SelectAll(), 
bt.algos.CapitalFlow(1000),
bt.algos.WeighEqually(), 
bt.algos.Rebalance()])

# create a backtest and run it
test = bt.Backtest(s2, data)
res = bt.run(test)
```

output:

```
Stat                 s2
-------------------  ----------
Start                2017-12-31
End                  2021-07-18
Risk-free rate       0.00%

Total Return         152.86%
Daily Sharpe         0.64
Daily Sortino        1.04
CAGR                 29.91%
Max Drawdown         -93.95%
Calmar Ratio         0.32

MTD                  -7.45%
3m                   -9.70%
6m                   55.54%
YTD                  165.02%
1Y                   719.83%
3Y (ann.)            60.79%
5Y (ann.)            29.91%
10Y (ann.)           -
Since Incep. (ann.)  29.91%

Daily Sharpe         0.64
Daily Sortino        1.04
Daily Mean (ann.)    53.47%
Daily Vol (ann.)     83.01%
Daily Skew           -0.39
Daily Kurt           5.74
Best Day             25.94%
Worst Day            -42.34%

Monthly Sharpe       0.75
Monthly Sortino      1.84
Monthly Mean (ann.)  88.41%
Monthly Vol (ann.)   117.74%
Monthly Skew         0.49
Monthly Kurt         -0.56
Best Month           78.21%
Worst Month          -55.59%

Yearly Sharpe        0.56
Yearly Sortino       3.35
Yearly Mean          137.07%
Yearly Vol           244.13%
Yearly Skew          1.06
Yearly Kurt          0.43
Best Year            468.90%
Worst Year           -82.73%

Avg. Drawdown        -16.69%
Avg. Drawdown Days   89.71
Avg. Up Month        32.82%
Avg. Down Month      -21.90%
Win Year %           50.00%
Win 12m %            57.58%
​
```

Regards,

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.