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:
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.
Both
self.close
andself.ema
areDataFrame
series. Comparing two series (e.g.self.close > self.ema
) results in a boolean series: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()
:This makes your intention clearer to the person who will read your code and for yourself.