This problem has its roots in NumPy.
def entered_long(df):
return buy_pattern(df) & (df.High > df.High.shift(1))
entered_long
is returning an array-like object. NumPy refuses to guess if an array is True or False:
In [48]: x = np.array([ True, True, True], dtype=bool)
In [49]: bool(x)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
To fix this, use any
or all
to specify what you mean for an array to be True:
def calc_position(df):
# sum of current positions + any new positions
if entered_long(df).any(): # or .all()
The any()
method will return True if any of the items in entered_long(df)
are True.
The all()
method will return True if all the items in entered_long(df)
are True.