Skip to content

Commit a95b259

Browse files
committed
DOC: Update docs and examples
1 parent 3dfa0c9 commit a95b259

12 files changed

+1474
-812
lines changed

README.md

+25-22
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ from backtesting.test import SMA, GOOG
3333

3434
class SmaCross(Strategy):
3535
def init(self):
36-
Close = self.data.Close
37-
self.ma1 = self.I(SMA, Close, 10)
38-
self.ma2 = self.I(SMA, Close, 20)
36+
price = self.data.Close
37+
self.ma1 = self.I(SMA, price, 10)
38+
self.ma2 = self.I(SMA, price, 20)
3939

4040
def next(self):
4141
if crossover(self.ma1, self.ma2):
@@ -44,8 +44,8 @@ class SmaCross(Strategy):
4444
self.sell()
4545

4646

47-
bt = Backtest(GOOG, SmaCross,
48-
cash=10000, commission=.002)
47+
bt = Backtest(GOOG, SmaCross, commission=.002,
48+
exclusive_orders=True)
4949
bt.run()
5050
bt.plot()
5151
```
@@ -56,30 +56,33 @@ Results in:
5656
Start 2004-08-19 00:00:00
5757
End 2013-03-01 00:00:00
5858
Duration 3116 days 00:00:00
59-
Exposure [%] 94.29
60-
Equity Final [$] 69665.12
61-
Equity Peak [$] 69722.15
62-
Return [%] 596.65
59+
Exposure Time [%] 94.27
60+
Equity Final [$] 68935.12
61+
Equity Peak [$] 68991.22
62+
Return [%] 589.35
6363
Buy & Hold Return [%] 703.46
64-
Max. Drawdown [%] -33.61
65-
Avg. Drawdown [%] -5.68
66-
Max. Drawdown Duration 689 days 00:00:00
64+
Max. Drawdown [%] -33.08
65+
Avg. Drawdown [%] -5.58
66+
Max. Drawdown Duration 688 days 00:00:00
6767
Avg. Drawdown Duration 41 days 00:00:00
6868
# Trades 93
6969
Win Rate [%] 53.76
70-
Best Trade [%] 56.98
71-
Worst Trade [%] -17.03
72-
Avg. Trade [%] 2.44
70+
Best Trade [%] 57.12
71+
Worst Trade [%] -16.63
72+
Avg. Trade [%] 1.96
7373
Max. Trade Duration 121 days 00:00:00
7474
Avg. Trade Duration 32 days 00:00:00
75-
Expectancy [%] 6.92
76-
SQN 1.77
77-
Sharpe Ratio 0.22
78-
Sortino Ratio 0.54
79-
Calmar Ratio 0.07
80-
_strategy SmaCross
75+
Profit Factor 2.13
76+
Expectancy [%] 6.91
77+
SQN 1.78
78+
Sharpe Ratio 0.18
79+
Sortino Ratio 0.44
80+
Calmar Ratio 0.06
81+
_strategy SmaCross(n1=10, n2=20)
82+
_equity_curve Equ...
83+
_trades Size EntryB...
8184
```
82-
[![plot of trading simulation](https://i.imgur.com/q6OSQD8.png)](https://kernc.github.io/backtesting.py/#example)
85+
[![plot of trading simulation](https://i.imgur.com/xRFNHfg.png)](https://kernc.github.io/backtesting.py/#example)
8386

8487
Find more usage examples in the [documentation].
8588

backtesting/__init__.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
"""
22
# Backtesting.py Documentation
33
4+
.. warning:: v0.2.0 breaking changes
5+
Version 0.2.0 introduced some **breaking API changes**. For quick ways to
6+
migrate existing 0.1.x code, see the implementing
7+
[pull request](https://github.com/kernc/backtesting.py/pull/47/).
8+
49
## Manuals
510
611
* [**Quick Start User Guide**](../examples/Quick Start User Guide.html)
@@ -11,7 +16,7 @@
1116
* [Multiple Time Frames](../examples/Multiple Time Frames.html)
1217
* [Parameter Heatmap](../examples/Parameter Heatmap.html)
1318
14-
These tutorials are also available as live notebooks:
19+
These tutorials are also available to test as live Jupyter notebooks:
1520
[![Binder](https://mybinder.org/badge_logo.svg)][binder]
1621
1722
[binder]: \
@@ -22,6 +27,11 @@
2227
2328
* (contributions welcome)
2429
30+
## FAQ
31+
32+
Potentially outdated answers to popular questions can be found on the
33+
[issue tracker](https://github.com/kernc/backtesting.py/issues?q=label%3Aquestion).
34+
2535
## License
2636
2737
This software is licensed under the terms of [AGPL 3.0]{: rel=license},

backtesting/backtesting.py

+41-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
"""
2-
Core backtesting data structures.
3-
Objects from this module can be imported from the top-level
2+
Core framework data structures.
3+
Objects from this module can also be imported from the top-level
44
module directly, e.g.
55
66
from backtesting import Backtest, Strategy
7+
8+
.. warning:: v0.2.0 breaking changes
9+
Version 0.2.0 introduced some **breaking API changes**. For quick ways to
10+
migrate existing 0.1.x code, see the implementing
11+
[pull request](https://github.com/kernc/backtesting.py/pull/47/).
712
"""
813
import multiprocessing as mp
914
import os
@@ -962,7 +967,7 @@ def __init__(self,
962967
To run the backtest using e.g. 50:1 leverge that your broker allows,
963968
set margin to `0.02` (1 / leverage).
964969
965-
If `trade_on_close` is `True`, market orders will be executed
970+
If `trade_on_close` is `True`, market orders will be filled
966971
with respect to the current bar's closing price instead of the
967972
next bar's open.
968973
@@ -1029,6 +1034,37 @@ def run(self, **kwargs) -> pd.Series:
10291034
Run the backtest. Returns `pd.Series` with results and statistics.
10301035
10311036
Keyword arguments are interpreted as strategy parameters.
1037+
1038+
>>> Backtest(GOOG, SmaCross).run()
1039+
Start 2004-08-19 00:00:00
1040+
End 2013-03-01 00:00:00
1041+
Duration 3116 days 00:00:00
1042+
Exposure Time [%] 93.9944
1043+
Equity Final [$] 51959.9
1044+
Equity Peak [$] 75787.4
1045+
Return [%] 419.599
1046+
Buy & Hold Return [%] 703.458
1047+
Max. Drawdown [%] -47.9801
1048+
Avg. Drawdown [%] -5.92585
1049+
Max. Drawdown Duration 584 days 00:00:00
1050+
Avg. Drawdown Duration 41 days 00:00:00
1051+
# Trades 65
1052+
Win Rate [%] 46.1538
1053+
Best Trade [%] 53.596
1054+
Worst Trade [%] -18.3989
1055+
Avg. Trade [%] 2.35371
1056+
Max. Trade Duration 183 days 00:00:00
1057+
Avg. Trade Duration 46 days 00:00:00
1058+
Profit Factor 2.08802
1059+
Expectancy [%] 8.79171
1060+
SQN 0.916893
1061+
Sharpe Ratio 0.179141
1062+
Sortino Ratio 0.55887
1063+
Calmar Ratio 0.049056
1064+
_strategy SmaCross
1065+
_equity_curve Eq...
1066+
_trades Size EntryB...
1067+
dtype: object
10321068
"""
10331069
data = _Data(self._data.copy(deep=False))
10341070
broker = self._broker(data=data) # type: _Broker
@@ -1160,7 +1196,7 @@ def __getattr__(self, item):
11601196
raise ValueError('No admissible parameter combinations to test')
11611197

11621198
if len(param_combos) > 300:
1163-
warnings.warn('Searching best of {} configurations.'.format(len(param_combos)),
1199+
warnings.warn('Searching for best of {} configurations.'.format(len(param_combos)),
11641200
stacklevel=2)
11651201

11661202
heatmap = pd.Series(np.nan,
@@ -1367,7 +1403,7 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
13671403
a separate drawdown graph section.
13681404
13691405
If `smooth_equity` is `True`, the equity graph will be
1370-
interpolated between points of cash-only positions,
1406+
interpolated between fixed points at trade closing times,
13711407
unaffected by any interim asset volatility.
13721408
13731409
If `relative_equity` is `True`, scale and label equity graph axis

backtesting/lib.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def resample_apply(rule: str,
175175
a time frame to resample `series` to.
176176
177177
[Pandas offset string]: \
178-
http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases
178+
http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases
179179
180180
`func` is the indicator function to apply on the resampled series.
181181
@@ -202,7 +202,7 @@ def init(self):
202202
self.sma = resample_apply(
203203
'D', SMA, self.data.Close, 10, plot=False)
204204
205-
This short snippet is roughly equivalent to:
205+
The above short snippet is roughly equivalent to:
206206
207207
class System(Strategy):
208208
def init(self):

0 commit comments

Comments
 (0)