Skip to content

ValueError: ['End'] not contained in the index #21

Closed
@brunocapelao

Description

@brunocapelao

Hello,

I'm using the lib with a dataset like this:

image

and I have the following error:

C:\Users\capel\Anaconda3\lib\site-packages\backtesting\_util.py:32: FutureWarning: Converting timezone-aware DatetimeArray to timezone-naive ndarray with 'datetime64[ns]' dtype. In the future, this will return an ndarray with 'object' dtype where each element is a 'pandas.Timestamp' with the correct 'tz'.
	To accept the future behavior, pass 'dtype=object'.
	To keep the old behavior, pass 'dtype="datetime64[ns]"'.
  obj = np.asarray(array).view(cls)
C:\Users\capel\Anaconda3\lib\site-packages\backtesting\_util.py:23: FutureWarning: The default of the 'keep_tz' keyword will change to True in a future release. You can set 'keep_tz=True' to obtain the future behaviour and silence this warning.
  return df.index[:100].to_series().diff().median()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-b0362348a33a> in <module>
      1 backtest = Backtest(dados, BarStrategy, commission=.0075, cash=100)
----> 2 backtest.run()

~\Anaconda3\lib\site-packages\backtesting\backtesting.py in run(self, **kwargs)
    703                 strategy.next()
    704 
--> 705         self._results = self._compute_stats(broker, strategy)
    706         return self._results
    707 

~\Anaconda3\lib\site-packages\backtesting\backtesting.py in _compute_stats(self, broker, strategy)
    870         s = pd.Series()
    871         s['Start'] = df.index[0]
--> 872         s['End'] = df.index[-1]
    873         # Assigning Timedeltas needs the key to exist beforehand,
    874         # otherwise the value is interpreted as nanosec *int*. See:

~\Anaconda3\lib\site-packages\pandas\core\series.py in __setitem__(self, key, value)
   1037         # do the setitem
   1038         cacher_needs_updating = self._check_is_chained_assignment_possible()
-> 1039         setitem(key, value)
   1040         if cacher_needs_updating:
   1041             self._maybe_update_cacher()

~\Anaconda3\lib\site-packages\pandas\core\series.py in setitem(key, value)
   1033                     pass
   1034 
-> 1035             self._set_with(key, value)
   1036 
   1037         # do the setitem

~\Anaconda3\lib\site-packages\pandas\core\series.py in _set_with(self, key, value)
   1083                 self._set_values(key.astype(np.bool_), value)
   1084             else:
-> 1085                 self._set_labels(key, value)
   1086 
   1087     def _set_labels(self, key, value):

~\Anaconda3\lib\site-packages\pandas\core\series.py in _set_labels(self, key, value)
   1093         mask = indexer == -1
   1094         if mask.any():
-> 1095             raise ValueError('%s not contained in the index' % str(key[mask]))
   1096         self._set_values(indexer, value)
   1097 

ValueError: ['End'] not contained in the index

I suggest change this code to use the .loc method expressly, like this:

 871         s.loc['Start'] = df.index[0]
 872         s.loc['End'] = df.index[-1]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions