|
2 | 2 |
|
3 | 3 | Backtesting.py
|
4 | 4 | ==============
|
5 |
| -Backtest trading strategies with Python. |
6 |
| - |
7 | 5 | [](https://travis-ci.org/kernc/backtesting.py)
|
8 | 6 | [](https://codecov.io/gh/kernc/backtesting.py)
|
9 | 7 | [](https://pypi.org/project/backtesting)
|
10 | 8 |
|
| 9 | +Backtest trading strategies with Python. |
| 10 | + |
11 | 11 | [**Project website**](https://kernc.github.io/backtesting.py/)
|
12 | 12 |
|
13 |
| -[Documentation](https://kernc.github.io/backtesting.py/doc/backtesting/) |
| 13 | +[Documentation] |
| 14 | + |
| 15 | +[Documentation]:https://kernc.github.io/backtesting.py/doc/backtesting/ |
| 16 | + |
| 17 | + |
| 18 | +Installation |
| 19 | +------------ |
| 20 | + |
| 21 | +$ pip install backtesting |
| 22 | + |
| 23 | + |
| 24 | +Usage |
| 25 | +----- |
| 26 | +```python |
| 27 | +from backtestingimport Backtest, Strategy |
| 28 | +from backtesting.libimport crossover |
| 29 | + |
| 30 | +from backtesting.testimportSMA,GOOG |
| 31 | + |
| 32 | + |
| 33 | +classSmaCross(Strategy): |
| 34 | +definit(self): |
| 35 | + Close=self.data.Close |
| 36 | +self.ma1=self.I(SMA, Close,10) |
| 37 | +self.ma2=self.I(SMA, Close,20) |
| 38 | + |
| 39 | +defnext(self): |
| 40 | +if crossover(self.ma1,self.ma2): |
| 41 | +self.buy() |
| 42 | +elif crossover(self.ma2,self.ma1): |
| 43 | +self.sell() |
| 44 | + |
| 45 | + |
| 46 | +bt= Backtest(GOOG, SmaCross, |
| 47 | +cash=10000,commission=.002) |
| 48 | +bt.run() |
| 49 | +bt.plot() |
| 50 | +``` |
| 51 | + |
| 52 | +Results in: |
| 53 | + |
| 54 | +```text |
| 55 | +Start 2004-08-19 00:00:00 |
| 56 | +End 2013-03-01 00:00:00 |
| 57 | +Duration 3116 days 00:00:00 |
| 58 | +Exposure [%] 94.29 |
| 59 | +Equity Final [$] 69665.12 |
| 60 | +Equity Peak [$] 69722.15 |
| 61 | +Return [%] 596.65 |
| 62 | +Buy & Hold Return [%] 703.46 |
| 63 | +Max. Drawdown [%] -33.61 |
| 64 | +Avg. Drawdown [%] -5.68 |
| 65 | +Max. Drawdown Duration 689 days 00:00:00 |
| 66 | +Avg. Drawdown Duration 41 days 00:00:00 |
| 67 | +# Trades 93 |
| 68 | +Win Rate [%] 53.76 |
| 69 | +Best Trade [%] 56.98 |
| 70 | +Worst Trade [%] -17.03 |
| 71 | +Avg. Trade [%] 2.44 |
| 72 | +Max. Trade Duration 121 days 00:00:00 |
| 73 | +Avg. Trade Duration 32 days 00:00:00 |
| 74 | +Expectancy [%] 6.92 |
| 75 | +SQN 1.77 |
| 76 | +Sharpe Ratio 0.22 |
| 77 | +Sortino Ratio 0.54 |
| 78 | +Calmar Ratio 0.07 |
| 79 | +_strategy SmaCross |
| 80 | +``` |
| 81 | +[](https://kernc.github.io/backtesting.py/#example) |
| 82 | + |
| 83 | +Find more usage examples in the[documentation]. |
| 84 | + |
| 85 | +Features |
| 86 | +-------- |
| 87 | +* Simple, well-documented API |
| 88 | +* Blazing fast execution |
| 89 | +* Built-in optimizer |
| 90 | +* Library of composable base strategies and utilities |
| 91 | +* Indicator-library-agnostic |
| 92 | +* Supports_any_ financial instrument with candlestick data |
| 93 | +* Detailed results |
| 94 | +* Interactive visualizations |