kernc / backtesting.py

:mag_right: :chart_with_upwards_trend: :snake: :moneybag: Backtest trading strategies in Python.
https://kernc.github.io/backtesting.py/
GNU Affero General Public License v3.0
5.44k stars 1.06k forks source link

Buy and Hold Return % #140

Closed headingup closed 4 years ago

headingup commented 4 years ago

Running the following:

from backtesting import Backtest, Strategy
from backtesting.lib import crossover
from backtesting.test import SMA
import priceHistory

class SmaCross(Strategy):
    def init(self):
        price = self.data.Close
        self.ma1 = self.I(SMA, price, 10)
        self.ma2 = self.I(SMA, price, 20)

    def next(self):
        if crossover(self.ma1, self.ma2):
            self.buy()
        elif crossover(self.ma2, self.ma1):
            self.sell()

symbol = 'BA'
ohlc = priceHistory.get_data(symbol, '2020-01-01', '2020-12-31')
print(ohlc)

bt = Backtest(ohlc, SmaCross, exclusive_orders=True)
stats = bt.run()
print(stats)

Produced the the following results:

                             Open    High     Low   Close    Volume
datetime                                                           
2020-01-01 23:00:00-07:00  328.55  333.35  327.70  333.32   4548221
2020-01-02 23:00:00-07:00  330.63  334.89  330.30  332.76   3878374
2020-01-05 23:00:00-07:00  329.30  334.86  327.88  333.74   5357973
2020-01-06 23:00:00-07:00  334.26  344.19  330.71  337.28   9942277
2020-01-07 23:00:00-07:00  332.40  334.03  329.60  331.37   8246250
...                           ...     ...     ...     ...       ...
2020-09-02 23:00:00-06:00  175.00  180.85  168.12  168.77  27799393
2020-09-03 23:00:00-06:00  171.31  172.83  164.00  171.05  18867266
2020-09-07 23:00:00-06:00  165.80  166.90  160.50  161.08  22622677
2020-09-08 23:00:00-06:00  161.61  163.06  157.00  160.78  21951401
2020-09-09 23:00:00-06:00  160.96  163.78  157.57  157.69  14532586

[175 rows x 5 columns]
Start                     2020-01-01 23:00...
End                       2020-09-09 23:00...
Duration                    251 days 23:00:00
Exposure Time [%]                     80.5714
Equity Final [$]                      11014.4
Equity Peak [$]                       13177.7
Return [%]                            10.1445
**_Buy & Hold Return [%]                 52.6911_**
Max. Drawdown [%]                    -38.0218
Avg. Drawdown [%]                    -21.4588
Max. Drawdown Duration      174 days 00:00:00
Avg. Drawdown Duration       70 days 00:00:00
Trades                                    7
Win Rate [%]                          42.8571
Best Trade [%]                        41.7413
Worst Trade [%]                      -20.0121
Avg. Trade [%]                       0.099488
Max. Trade Duration          49 days 00:00:00
Avg. Trade Duration          29 days 00:00:00
Profit Factor                         1.27312
Expectancy [%]                        15.1045
SQN                                0.00995988
Sharpe Ratio                       0.00470498
Sortino Ratio                       0.0133826
Calmar Ratio                        0.0026166
_strategy                            SmaCross
_equity_curve                             ...
_trades                      Size  EntryBa...
dtype: object

As you can see from the DataFrame of OHLC data for this year, BA is actually down on the year, not up 52.69%. How is Buy and Hold calculated?

kernc commented 4 years ago

It's Buy & Hold or Sell & Hold, whichever is greater: https://github.com/kernc/backtesting.py/blob/aa6830be2bc334cfa59e34053e24f7b99dcd7e1b/backtesting/backtesting.py#L1378

Duplicate of https://github.com/kernc/backtesting.py/issues/36.

headingup commented 4 years ago

Ah... sorry I missed #36... thanks for the response...

kernc commented 4 years ago

This was changed as of https://github.com/kernc/backtesting.py/commit/3045b64635fe9e5af51046b7bdf7cdfcbf375c1e.