MiniXC / simple-back

A simple daily python backtester that works out of the box.
Mozilla Public License 2.0
59 stars 12 forks source link

tutorial example not working with datetime error #31

Closed zorgilo closed 3 years ago

zorgilo commented 3 years ago

Hi, I get a strange error when running the introductory tutorial example in notebook and also in terminal: "KeyError: datetime.date(2019, 1, 2)" Any idea? Thanks GV I paste below the full report:

KeyError Traceback (most recent call last)

in 12 13 bt = builder.no_live_plot().build() ---> 14 for day, event, b in bt['2019-1-1':'2020-1-1']: 15 if event == 'open': 16 jnug_ma = b.prices['JNUG',-5:]['close'] /opt/anaconda3/lib/python3.7/site-packages/simple_back/backtester.py in __getitem__(self, date_range) 844 for date in self.dates: 845 self.datetimes += [ --> 846 sched.loc[date]["market_open"], 847 sched.loc[date]["market_close"], 848 ] /opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py in __getitem__(self, key) 877 878 maybe_callable = com.apply_if_callable(key, self.obj) --> 879 return self._getitem_axis(maybe_callable, axis=axis) 880 881 def _is_scalar_access(self, key: Tuple): /opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis) 1108 # fall thru to straight lookup 1109 self._validate_key(key, axis) -> 1110 return self._get_label(key, axis=axis) 1111 1112 def _get_slice_axis(self, slice_obj: slice, axis: int): /opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py in _get_label(self, label, axis) 1057 def _get_label(self, label, axis: int): 1058 # GH#5667 this will fail if the label is not present in the axis. -> 1059 return self.obj.xs(label, axis=axis) 1060 1061 def _handle_lowerdim_multi_index_axis0(self, tup: Tuple): /opt/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in xs(self, key, axis, level, drop_level) 3486 loc, new_index = self.index.get_loc_level(key, drop_level=drop_level) 3487 else: -> 3488 loc = self.index.get_loc(key) 3489 3490 if isinstance(loc, np.ndarray): /opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/datetimes.py in get_loc(self, key, method, tolerance) 620 else: 621 # unrecognized type --> 622 raise KeyError(key) 623 624 try: KeyError: datetime.date(2019, 1, 2)
MiniXC commented 3 years ago

Pandas recently changed their way dates are used as indexes. For now you can downgrade to pandas==1.0.5 and it should work. Keeping this open because this needs to be fixed in simple back of course

MiniXC commented 3 years ago

fixed in df7d7dc82d5aa02821d2b100085adaf5f7bd777f