Crossover error operator '-'
-
Using btalib crossover an error is shown:
Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/computation/expressions.py:200: UserWarning: evaluating in Python space because the '-' operator is not supported by numexpr/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/computation/expressions.py:200: UserWarning: evaluating in Python space because the '-' operator is not supported by numexpr for the bool dtype, use '^' instead
My code uses as input parameter a pandas dataframe:
if len(df)>50: sma50 = btalib.sma(df, period=50) df['sma50'] = btalib.sma(df, period=50).df if len(df)>200: sma200 = btalib.sma(df, period=200) df['sma200'] = sma200.df btalib.crossover(sma50, sma200)
-
Could you share a few rows of your dataframe please?
-
@run-out Here you got it (last columns are sma20 and sma50):
o h l c v d s symbol max52w min52w sma20 sma50
ts
2020-02-18 85.110069 85.527370 83.987318 84.245651 2872500 0.0 0 A NaN NaN NaN NaN
2020-02-19 84.156212 85.288892 82.914237 84.802040 4741300 0.0 0 A NaN NaN NaN NaN
2020-02-20 84.354941 84.444359 82.824826 83.798531 2543600 0.0 0 A NaN NaN NaN NaN
2020-02-21 83.361355 84.543710 82.864565 84.523842 1762500 0.0 0 A NaN NaN NaN NaN
2020-02-24 81.692144 82.059763 79.506263 79.983185 2919200 0.0 0 A NaN NaN NaN NaN
... ... ... ... ... ... ... .. ... ... ... ... ...
2020-11-24 118.959999 118.959999 111.430000 114.680000 5559900 0.0 0 A NaN NaN 108.683499 104.917219
2020-11-25 116.089996 116.959999 113.410004 114.349998 2862500 0.0 0 A NaN NaN 109.317999 105.209415
2020-11-27 113.769997 114.980003 112.940002 114.089996 983700 0.0 0 A NaN NaN 109.921499 105.502801
2020-11-30 113.889999 117.070000 113.669998 116.900002 3778400 0.0 0 A NaN NaN 110.661999 105.856778
2020-12-01 117.580002 118.379997 115.040001 115.360001 1753700 0.0 0 A NaN NaN 111.167999 106.204910 -
@Xavier-Escudero said in Crossover error operator '-':
btalib.crossover(sma50, sma200)
I took a stab at this and I'm getting the same error. I modified the code slightly:
df = pd.read_csv("/home/runout/projects/data/es/es18_20.csv") df.columns = ['Date', 'High', 'Low', 'Open', 'Close', 'Cumvol', 'Volume'] df = df.set_index('Date') df.index = pd.to_datetime(df.index) sma50 = btalib.sma(df, period=50) df['sma50'] = sma50.df sma200 = btalib.sma(df, period=200) df['sma200'] = btalib.sma(df, period=200).df df = df.dropna() df btalib.crossover(sma50, sma200).df
It works right up to the crossover point then errors. I get the same error if I just try using
open and close
to cross over.TypeError: numpy boolean subtract, the `-` operator, is not supported, use the bitwise_xor, the `^` operator, or the logical_xor function instead.
So I investigated this and it is a known problem. Here is an example of someone fixing this.
WillPastor commented on Apr 23, 2019 @zockerwurf @NathanielCustom I fixed this problem by changing a single character in dejavu/fingerprint.py and it worked fine on my Ubuntu VirtualBox VM with about 8 GB memory. The code that needs to be changed is on line 104 of fingerprint.py ... # Boolean mask of arr2D with True at peaks detected_peaks = local_max - eroded_background Needs to be changed to: # Boolean mask of arr2D with True at peaks detected_peaks = local_max ^ eroded_background
So I found the likely culprit in the btalib file
btalib/indicators/crossover.py
in the crossover class:class crossover(_crossbase): ''' This indicator gives a signal if the provided datas (2) cross up or down. - `1.0` if the 1st data crosses the 2nd data upwards - `-1.0` if the 1st data crosses the 2nd data downwards It does need to look into the current time index (0) and the previous time index (-1) of both the 1st and 2nd data Formula: - crossup = data0(-1) < data1(-1) and data0 > data1 - crossdown = data0(-1) > data1(-1) and data0 < data1 - crossover = crossup - crossdown ''' group = 'utils' alias = 'CrossOver' outputs = 'crossover' _updown = 0 def __init__(self): self.o.crossover = self._cup - self._cdown
The last line needs to change the
-
to^
.def __init__(self): self.o.crossover = self._cup ^ self._cdown
This seems to solve the problem but I have not bug tested it, and this need to be changed in the codebase me thinks.
In my final working version I ended up just using this:
btalib.crossover(sma50.df, sma200.df).df
-
I should have checked the pull request for bta-lib, it's a known and ignore issue.
-
@run-out Yes, now that you mention it I remember I saw the pull request, but I don't understand why it's not merged yet (it was commited on june).
-
@run-out Anyway, thanks for your support, it works!