Truth Ambiguity Error in Backtesting.py Backtest

105 views Asked by At

I am trying to backtest a momentum strategy using Backtesting.py. I've gathered the data and computed indicator values using pandas_ta. I've defined short and long trading conditions. Now I just need Backtesting.py to run a backtest so that I can determine the performance of my strategy on historical data.

This notebook contains all the code plus the errors I am getting:

https://github.com/kbs-code/algo_trader/blob/master/backtests/backtestingdotpy_broad_market_ema_vortex.ipynb

The latest error is this one:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I've googled this type of error and tried to resolve it using parentheses, bitwise (and) and logical (and). I don't think my truth statement is illegal in Python but I could be wrong. I've also read Backtesting.py's documentation to understand the framework as best I can.

Any help would be appreciated.

1

There are 1 answers

3
enzo On BEST ANSWER

Both self.close and self.ema are DataFrame series. Comparing two series (e.g. self.close > self.ema) results in a boolean series:

a = pd.Series([1, 2, 3, 4])
b = pd.Series([3, 1, 4, 2])
print(a > b)    # Outputs pd.Series([False, True, False, True])

Using a boolean series in a if-statement (e.g. if a > b:) is ambiguous because Pandas does not know what you mean: should that expression evaluate to true if all the items are True? Of it should evaluate to true if at least one item is True?

Therefore, you need to use it with either .any() or .all():

if (a > b).all():  # If all items in `a` are greater than
    pass           # their corresponding items in `b`

if (a > b).any():  # If any item in `a` is greater than
    pass           # their corresponding item in `b`

This makes your intention clearer to the person who will read your code and for yourself.