<jupyter_start><jupyter_text>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, :func:`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.<jupyter_code># fetch some data
data = bt.get('spy,agg', start='2010-01-01')
print data.head()<jupyter_output>spy        agg
Date                            
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<jupyter_text>Once we have our data, we will create our strategy. The :class:`Strategy ` object contains the strategy logic by combining various :class:`Algos `.<jupyter_code># create the strategy
s = bt.Strategy('s1', [bt.algos.RunMonthly(),
                       bt.algos.SelectAll(),
                       bt.algos.WeighEqually(),
                       bt.algos.Rebalance()])<jupyter_output><empty_output><jupyter_text>Finally, we will create a :class:`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.<jupyter_code># create a backtest and run it
test = bt.Backtest(s, data)
res = bt.run(test)<jupyter_output>s1
0%                          100%
[############################# ] | ETA: 00:00:00<jupyter_text>Now we can analyze the results of our backtest. The :class:`Result ` object is a thin wrapper around `ffn.GroupStats `__ that adds some helper methods.<jupyter_code># first let's see an equity curve
res.plot()
# ok and what about some stats?
res.display()
# ok and how does the return distribution look like?
res.plot_histogram()
# and just to make sure everything went along as planned, let's plot the security weights over time
res.plot_security_weights()<jupyter_output><empty_output><jupyter_text>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:<jupyter_code># create our new strategy
s2 = bt.Strategy('s2', [bt.algos.RunWeekly(),
                        bt.algos.SelectAll(),
                        bt.algos.WeighInvVol(),
                        bt.algos.Rebalance()])

# 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 = bt.run(test, test2)

res2.plot()
res2.display()<jupyter_output>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   [...]
{'fraction_non_alphanumeric': 0.12236396771674043, 'fraction_numerical': 0.08409268419682374, 'mean_word_length': 2.1312143439282805, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '637190', 'license': 'MIT', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1184, 'n_words': 406, 'path': 'docs/source/intro.ipynb', 'repo_name': 'ceaza/bt', 'size': '3841'}