bt - Flexible Backtesting for Python

What is bt?

bt is a flexible backtesting framework for Python used to test quantitative trading strategies. Backtesting is the process of testing a strategy over a given data set. This framework allows you to easily create strategies that mix and match different Algos. It aims to foster the creation of easily testable, re-usable and flexible blocks of strategy logic to facilitate the rapid development of complex trading strategies.

The goal: to save quants from re-inventing the wheel and let them focus on the important part of the job - strategy development.

bt is coded in Python and joins a vibrant and rich ecosystem for data analysis. Numerous libraries exist for machine learning, signal processing and statistics and can be leveraged to avoid re-inventing the wheel - something that happens all too often when using other languages that don’t have the same wealth of high-quality, open-source projects.

bt is built atop ffn - a financial function library for Python. Check it out!

A Quick Example

Here is a quick taste of bt:

import bt

A Simple Strategy Backtest

Let’s create a simple strategy. We will create a monthly rebalanced, long-only strategy where we place equal weights on each asset in our universe of assets.

First, we will download some data. By default, bt.get (alias for ffn.get) downloads the Adjusted Close from Yahoo! Finance. We will download some data starting on January 1, 2010 for the purposes of this demo.

# fetch some data
data = bt.get('spy,agg', start='2010-01-01')
print data.head()
                  spy        agg
2010-01-04  98.214371  84.963075
2010-01-05  98.474354  85.349609
2010-01-06  98.543685  85.300266
2010-01-07  98.959667  85.201575
2010-01-08  99.288981  85.250923

Once we have our data, we will create our strategy. The Strategy object contains the strategy logic by combining various Algos.

# create the strategy
s = bt.Strategy('s1', [bt.algos.RunMonthly(),

Finally, we will create a Backtest, which is the logical combination of a strategy with a data set.

Once this is done, we can run the backtest and analyze the results.

# create a backtest and run it
test = bt.Backtest(s, data)
res =
0%                          100%
[############################# ] | ETA: 00:00:00

Now we can analyze the results of our backtest. The Result object is a thin wrapper around ffn.GroupStats that adds some helper methods.

# first let's see an equity curve
# ok and what about some stats?
Stat                 s1
-------------------  ----------
Start                2010-01-03
End                  2017-02-22
Risk-free rate       0.00%

Total Return         81.30%
Daily Sharpe         1.19
Daily Sortino        1.57
CAGR                 8.69%
Max Drawdown         -7.83%
Calmar Ratio         1.11

MTD                  2.08%
3m                   4.08%
6m                   3.26%
YTD                  3.11%
1Y                   12.04%
3Y (ann.)            6.82%
5Y (ann.)            8.12%
10Y (ann.)           8.69%
Since Incep. (ann.)  8.69%

Daily Sharpe         1.19
Daily Sortino        1.57
Daily Mean (ann.)    8.61%
Daily Vol (ann.)     7.23%
Daily Skew           -0.35
Daily Kurt           3.80
Best Day             2.48%
Worst Day            -3.11%

Monthly Sharpe       1.41
Monthly Sortino      2.61
Monthly Mean (ann.)  8.61%
Monthly Vol (ann.)   6.10%
Monthly Skew         0.01
Monthly Kurt         0.18
Best Month           5.69%
Worst Month          -3.39%

Yearly Sharpe        1.62
Yearly Sortino       -
Yearly Mean          7.25%
Yearly Vol           4.46%
Yearly Skew          0.15
Yearly Kurt          -0.71
Best Year            14.10%
Worst Year           1.17%

Avg. Drawdown        -0.79%
Avg. Drawdown Days   13.31
Avg. Up Month        1.64%
Avg. Down Month      -1.27%
Win Year %           100.00%
Win 12m %            96.00%
# ok and how does the return distribution look like?
# and just to make sure everything went along as planned, let's plot the security weights over time

Modifying a Strategy

Now what if we ran this strategy weekly and also used some risk parity style approach by using weights that are proportional to the inverse of each asset’s volatility? Well, all we have to do is plug in some different algos. See below:

# create our new strategy
s2 = bt.Strategy('s2', [bt.algos.RunWeekly(),

# now let's test it with the same data set. We will also compare it with our first backtest.
test2 = bt.Backtest(s2, data)
# we include test here to see the results side-by-side
res2 =, test2)

0%                          100%
[############################# ] | ETA: 00:00:00
Stat                 s1          s2
-------------------  ----------  ----------
Start                2010-01-03  2010-01-03
End                  2017-02-22  2017-02-22
Risk-free rate       0.00%       0.00%

Total Return         81.30%      40.79%
Daily Sharpe         1.19        1.45
Daily Sortino        1.57        2.00
CAGR                 8.69%       4.91%
Max Drawdown         -7.83%      -4.07%
Calmar Ratio         1.11        1.21

MTD                  2.08%       1.56%
3m                   4.08%       2.66%
6m                   3.26%       0.47%
YTD                  3.11%       2.27%
1Y                   12.04%      5.49%
3Y (ann.)            6.82%       3.97%
5Y (ann.)            8.12%       4.02%
10Y (ann.)           8.69%       4.91%
Since Incep. (ann.)  8.69%       4.91%

Daily Sharpe         1.19        1.45
Daily Sortino        1.57        2.00
Daily Mean (ann.)    8.61%       4.85%
Daily Vol (ann.)     7.23%       3.34%
Daily Skew           -0.35       -0.29
Daily Kurt           3.80        2.87
Best Day             2.48%       1.20%
Worst Day            -3.11%      -1.13%

Monthly Sharpe       1.41        1.68
Monthly Sortino      2.61        2.61
Monthly Mean (ann.)  8.61%       5.04%
Monthly Vol (ann.)   6.10%       3.00%
Monthly Skew         0.01        -0.59
Monthly Kurt         0.18        0.03
Best Month           5.69%       1.91%
Worst Month          -3.39%      -2.09%

Yearly Sharpe        1.62        1.61
Yearly Sortino       -           -
Yearly Mean          7.25%       4.08%
Yearly Vol           4.46%       2.53%
Yearly Skew          0.15        -0.45
Yearly Kurt          -0.71       -0.03
Best Year            14.10%      7.02%
Worst Year           1.17%       -0.13%

Avg. Drawdown        -0.79%      -0.40%
Avg. Drawdown Days   13.31       13.28
Avg. Up Month        1.64%       0.83%
Avg. Down Month      -1.27%      -0.69%
Win Year %           100.00%     85.71%
Win 12m %            96.00%      94.67%

As you can see, the strategy logic is easy to understand and more importantly, easy to modify. The idea of using simple, composable Algos to create strategies is one of the core building blocks of bt.


  • Tree Structure

    The tree structure facilitates the construction and composition of complex algorithmic trading strategies that are modular and re-usable. Furthermore, each tree Node has its own price index that can be used by Algos to determine a Node’s allocation.

  • Algorithm Stacks

    Algos and AlgoStacks are another core feature that facilitate the creation of modular and re-usable strategy logic. Due to their modularity, these logic blocks are also easier to test - an important step in building robust financial solutions.

  • Charting and Reporting

    bt also provides many useful charting functions that help visualize backtest results. We also plan to add more charts, tables and report formats in the future, such as automatically generated PDF reports.

  • Detailed Statistics

    Furthermore, bt calculates a bunch of stats relating to a backtest and offers a quick way to compare these various statistics across many different backtests via Results' display methods.


Future development efforts will focus on:

  • Speed

    Due to the flexible nature of bt, a trade-off had to be made between usability and performance. Usability will always be the priority, but we do wish to enhance the performance as much as possible.

  • Algos

    We will also be developing more algorithms as time goes on. We also encourage anyone to contribute their own algos as well.

  • Charting and Reporting

    This is another area we wish to constantly improve on as reporting is an important aspect of the job. Charting and reporting also facilitate finding bugs in strategy logic.