I'm working with some data in a dataframe and I find myself writing code that includes this a lot:
def entry_signal(y):
conditions1 = [np.logical_and(y > 0, y.shift(1) < 0),np.logical_and(y < 0, y.shift(1) > 0)]
values1 = ['LongEntry','ShortEntry']
return np.select(conditions1, values1, '')
Essentially if the value crosses above 0 and the previous value is less than 0 than it should be true.
I tried to create a function that did this but I keep getting an error:
def cross_above(x,y):
if np.logical_and(x>y, x.shift(1)<y):
return True
else:
return False
I then tried to use it here:
def entry_signal(y):
conditions1 = [cross_above((y,0), y.shift(1) < 0),np.logical_and(y < 0, y.shift(1) > 0)]
values1 = ['LongEntry','ShortEntry']
return np.select(conditions1, values1, '')
But I keep getting an truth of value of a series is ambiguous. What am i doing wrong?