Back to Community
Futures Pairs Trading Error

Hi All,

Could someone tell me why I'm getting the following error and how to fix it?

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
There was a runtime error on line 60.

Thanks,

Rohit

Clone Algorithm
5
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 593eed6b213f07698ff50ca5
There was a runtime error.
2 responses

Your statements in lines 109 to 133 try to do a logical comparisons on 'context.X' and 'context.Y'. The problem is that these are pandas series objects and not simple real numbers. Not sure what you are trying to do but if you want to compare the latest value in each series you can use the [-1] index approach.

       # Below the variables context.X and context.Y are series.  
       # Python doesn't understand how to compare series like this  
       if zscore >= 1.0:  
            if context.X > context.Y:  
                target_weights[x_contract] = -0.166  
                target_weights[y_contract] = 0.166

       ....

       # If one wants to compare the last values in the series  
       # should maybe be something like this  
       if zscore >= 1.0:  
            if context.X[-1] > context.Y[-1]:  
                target_weights[x_contract] = -0.166  
                target_weights[y_contract] = 0.166


Hi Dan,

Thanks! that seemed to fix the issue, however now the algorithm is running but won't place any trades. Do you happen to know why that would be the case?

Clone Algorithm
5
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5940a0766c766a69f02c313b
There was a runtime error.