Skip to content

Commit e8223d8

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

11 files changed

+1449
-790
lines changed

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):

doc/examples/Multiple Time Frames.ipynb

+176-123
Large diffs are not rendered by default.

doc/examples/Multiple Time Frames.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# extension: .py
66
# format_name: light
77
# format_version: '1.5'
8-
# jupytext_version: 1.3.0
8+
# jupytext_version: 1.5.1
99
# kernelspec:
1010
# display_name: Python 3
1111
# language: python
@@ -16,22 +16,21 @@
1616
# ============
1717
#
1818
# The best trading strategies relying on technical analysis take into account the price action on multiple time frames.
19-
# This tutorial will show how to do that with _backtesting.py_, offloading most of the work to
19+
# This tutorial will show how to do that with backtesting.py, offloading most of the work to
2020
# [pandas resampling](http://pandas.pydata.org/pandas-docs/stable/timeseries.html#resampling).
2121
# It is assumed you're already familiar with
22-
# [basic _backtesting.py_ usage](https://kernc.github.io/backtesting.py/doc/examples/Quick Start User Guide.html).
22+
# [basic usage](https://kernc.github.io/backtesting.py/doc/examples/Quick Start User Guide.html).
2323
#
2424
# We will put to the test this long-only, supposed
2525
# [400%-a-year trading strategy](http://jbmarwood.com/stock-trading-strategy-300/),
2626
# which uses daily and weekly
2727
# [relative strength index](https://en.wikipedia.org/wiki/Relative_strength_index)
2828
# (RSI) values and moving averages (MA).
2929
#
30-
# Let's introduce the two indicators we'll be using.
31-
# In practice, one can use functions from any indicator library, such as
32-
# [TA-Lib](https://github.com/mrjbq7/ta-lib),
30+
# In practice, one should use functions from an indicator library, such as
31+
# [TA-Lib](https://github.com/mrjbq7/ta-lib) or
3332
# [Tulipy](https://tulipindicators.org),
34-
# PyAlgoTrade, ...
33+
# but among us, let's introduce the two indicators we'll be using.
3534

3635
# +
3736
import pandas as pd
@@ -138,5 +137,10 @@ def next(self):
138137

139138
# Better. While the strategy doesn't perform as well as simple buy & hold, it does so with significantly lower exposure (time in market).
140139
#
141-
# In conclusion, to test strategies on multiple time frames, you need to pass in data in the lowest time frame, then resample it to higher time frames, apply the indicators, then resample back to the lower time frame, filling in the in-betweens.
140+
# In conclusion, to test strategies on multiple time frames, you need to pass in OHLC data in the lowest time frame, then resample it to higher time frames, apply the indicators, then resample back to the lower time frame, filling in the in-betweens.
142141
# Which is what the function [`backtesting.lib.resample_apply()`](https://kernc.github.io/backtesting.py/doc/backtesting/lib.html#backtesting.lib.resample_apply) does for you.
142+
143+
# Learn more by exploring further
144+
# [examples](https://kernc.github.io/backtesting.py/doc/backtesting/index.html#tutorials)
145+
# or find more framework options in the
146+
# [full API reference](https://kernc.github.io/backtesting.py/doc/backtesting/index.html#header-submodules).

0 commit comments

Comments
 (0)