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.18k stars 1.02k forks source link

KeyError: 'Duration' #7

Closed jakeeegarcia-alb closed 5 years ago

jakeeegarcia-alb commented 5 years ago

Hi!

I tried running the example as shown in your website for the simple case. No errors were seen in installing backtesting-0.1.0.

The whole code:

from backtesting import Backtest, Strategy
from backtesting.lib import crossover

from backtesting.test import SMA, GOOG

class SmaCross(Strategy):
    n1 = 10
    n2 = 30

    def init(self):
        self.sma1 = self.I(SMA, self.data.Close, self.n1)
        self.sma2 = self.I(SMA, self.data.Close, self.n2)

    def next(self):
        if crossover(self.sma1, self.sma2):
            self.buy()
        elif crossover(self.sma2, self.sma1):
            self.sell()

bt = Backtest(GOOG, SmaCross, cash=10000, commission=.002)

output = bt.run()
bt.plot()

The error seems to come out only when running this line

output = bt.run()

Expected Behavior

Actual Behavior

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/series.py", line 760, in _set_with_engine
    self.index._engine.set_value(values, key, value)
  File "pandas/index.pyx", line 121, in pandas.index.IndexEngine.set_value (pandas/index.c:3600)
  File "pandas/index.pyx", line 129, in pandas.index.IndexEngine.set_value (pandas/index.c:3426)
  File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)
  File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)
  File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)
KeyError: 'Duration'

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/indexes/base.py", line 1945, in get_loc
    return self._engine.get_loc(key)
  File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)
  File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)
  File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)
  File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)
KeyError: 'Duration'

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/series.py", line 701, in setitem
    self._set_with_engine(key, value)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/series.py", line 763, in _set_with_engine
    values[self.index.get_loc(key)] = value
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/indexes/base.py", line 1947, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)
  File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)
  File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)
  File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)
KeyError: 'Duration'

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/backtesting/backtesting.py", line 705, in run
    self._results = self._compute_stats(broker, strategy)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/backtesting/backtesting.py", line 876, in _compute_stats
    s['Duration'] = 0
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/series.py", line 753, in __setitem__
    setitem(key, value)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/series.py", line 729, in setitem
    self.loc[key] = value
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/indexing.py", line 132, in __setitem__
    self._setitem_with_indexer(indexer, value)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/indexing.py", line 340, in _setitem_with_indexer
    new_values])
TypeError: invalid type promotion

Steps to Reproduce

1. 2. 3.

Additional info

kernc commented 5 years ago

Seems a pandas issue. What version of pandas is this? :confused:

import pandas as pd
print(pd.__version__)
jakeeegarcia-alb commented 5 years ago

@kernc Hi! my version of pandas is this one import pandas as pd print(pd.__version__) 0.18.1

I do not have any other python versions or other modules installed either. Would you happen to know what is causing this?

kernc commented 5 years ago

Thanks! The required pandas version seems to be at least 0.20.0.

You can upgrade pandas with: pip install -U pandas.