Quantopian's community platform is shutting down. Please read this post for more information and download your code.
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

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?