bta-lib icon indicating copy to clipboard operation
bta-lib copied to clipboard

the macd indicator are giving errors

Open duckquant opened this issue 4 years ago • 3 comments

btalib.macd(df.close,pfast=12,pslow=26,psignal=9)


TypeError Traceback (most recent call last) ~/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in setitem(self, key, value) 1013 try: -> 1014 self._set_with_engine(key, value) 1015 except com.SettingWithCopyError:

~/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in _set_with_engine(self, key, value) 1053 try: -> 1054 self.index._engine.set_value(values, key, value) 1055 return

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.set_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.set_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

TypeError: 'slice(11, None, None)' is an invalid key

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last) in ----> 1 btalib.macd(df.close,pfast=12,pslow=26,psignal=9)

~/anaconda3/lib/python3.7/site-packages/btalib/indicator.py in call(cls, *args, **kwargs) 150 # Auto-call base classes 151 for b_init in reversed(list(dict.fromkeys(b.init for b in bases))): --> 152 b_init(self, *args, **kwargs) 153 154 # delete old aliases only meant for operational purposes

~/anaconda3/lib/python3.7/site-packages/btalib/indicators/macd.py in init(self) 38 39 def init(self): ---> 40 ma1 = self.p._ma(self.i0, period=self.p.pfast, **self._talibkw) 41 ma2 = self.p._ma(self.i0, period=self.p.pslow) 42 self.o.macd = ma1 - ma2

~/anaconda3/lib/python3.7/site-packages/btalib/indicator.py in call(cls, *args, **kwargs) 150 # Auto-call base classes 151 for b_init in reversed(list(dict.fromkeys(b.init for b in bases))): --> 152 b_init(self, *args, **kwargs) 153 154 # delete old aliases only meant for operational purposes

~/anaconda3/lib/python3.7/site-packages/btalib/indicators/ema.py in init(self, poffset) 37 def init(self, poffset=0): # see above for poffset 38 span, seed, poff = self.p.period, self.p._seed, poffset ---> 39 self.o.ema = self.i0._ewm(span=span, _seed=seed, _poffset=poff).mean()

~/anaconda3/lib/python3.7/site-packages/btalib/meta/lines.py in call_op(*args, **kwargs) 377 sargs.append(arg) 378 --> 379 result[self._minidx:] = r = op(*sargs, **kwargs) # run/store 380 result = result.astype(r.dtype, copy=False) 381 return self._line._clone(result, period=self._minperiod)

~/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in setitem(self, key, value) 1040 pass 1041 -> 1042 self._set_with(key, value) 1043 1044 if cacher_needs_updating:

~/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in _set_with(self, key, value) 1062 if isinstance(key, slice): 1063 indexer = self.index._convert_slice_indexer(key, kind="getitem") -> 1064 return self._set_values(indexer, value) 1065 1066 elif is_scalar(key) and not is_integer(key) and key not in self.index:

~/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in _set_values(self, key, value) 1109 if isinstance(key, Series): 1110 key = key._values -> 1111 self._data = self._data.setitem(indexer=key, value=value) 1112 self._maybe_update_cacher() 1113

~/anaconda3/lib/python3.7/site-packages/pandas/core/internals/managers.py in setitem(self, **kwargs) 559 560 def setitem(self, **kwargs): --> 561 return self.apply("setitem", **kwargs) 562 563 def putmask(self, **kwargs):

~/anaconda3/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, filter, **kwargs) 440 applied = b.apply(f, **kwargs) 441 else: --> 442 applied = getattr(b, f)(**kwargs) 443 result_blocks = _extend_blocks(applied, result_blocks) 444

~/anaconda3/lib/python3.7/site-packages/pandas/core/internals/blocks.py in setitem(self, indexer, value) 875 876 # length checking --> 877 check_setitem_lengths(indexer, value, values) 878 exact_match = ( 879 len(arr_value.shape)

~/anaconda3/lib/python3.7/site-packages/pandas/core/indexers.py in check_setitem_lengths(indexer, value, values) 122 if len(value) != length_of_indexer(indexer, values): 123 raise ValueError( --> 124 "cannot set using a slice indexer with a " 125 "different length than the value" 126 )

ValueError: cannot set using a slice indexer with a different length than the value

duckquant avatar Apr 08 '20 17:04 duckquant

I get the same king of errors (TypeError: 'slice(11, None, None)' is an invalid key AND ValueError: cannot set using a slice indexer with a different length than the value) using btalib.macd, btalib.ppo and btalib.ppofast.

sylvainrocheleau avatar Mar 11 '21 15:03 sylvainrocheleau

Hey, I had the same issue and it was driving me crazy. What fixed it for me was setting correcting the column layout of my initial pandas dataframe.

before my df had columns: (index), dtime, open, high, low, close, volume => resulted in the error above

after: I either deleted or set the "dtime" column as an index with " df.set_index('dtime',inplace=True) "

then it magically worked. I suppose it was really just the extra column bta-lib didn't like. I think in their docs it also mentions somewhere that the library only accepts the OHLCV format.

Hope that might help you as well :)

yunis99wirkus avatar Apr 06 '21 07:04 yunis99wirkus

a similar problem happened to me. was solved, by putting as input the correct parameters of each indicator, as described in the documentation. In my case it was like this and it worked for a long time like this: sar= btalib.sar(df) and when corrected it looked like this: sar= btalib.sar(df.high, df.low)

maiaufrrj avatar Jul 10 '22 16:07 maiaufrrj