0

i have problem with line

 if (value[1]['Longs']==1.0) & (self.df['Long_Market'].rolling(20).sum()==0):
                        self.long_market=1

I want it to forbid code from opening to many long positions, but i get the error

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

I would love to know is it a function that checks previous 20 cells for specified value (1) and if there is one, it goes to another

Heres whole function

    def generate_signals(self):

            self.df['Z-Score']=(self.df['Spread']-self.df['Spread'].rolling(window=self.ma).mean())/self.df['Spread'].rolling(window=self.ma).std()
            self.df['Prior Z-Score']=self.df['Z-Score'].shift(1)
            self.df['Longs']=(self.df['Z-Score']<=self.floor)*1.0
            self.df['Shorts']=(self.df['Z-Score']>=self.ceiling)*1.0
            self.df['Exit']=(np.abs(self.df['Z-Score'])<=self.exit_zscore)*1.0
            self.df['Long_Market']=0.0
            self.df['Short_Market']=0.0
            self.long_market=0
            self.short_market=0

            for i,value in enumerate(self.df.iterrows()):
                if (value[1]['Longs']==1.0) & (self.df['Long_Market'].rolling(20).sum()==0):
                        self.long_market=1
Piotrek
  • 11
  • 1
  • 1
    Possible duplicate of [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – BoarGules May 12 '19 at 08:54
  • It is the same problem but the proposed solution doesn't help with mine – Piotrek May 12 '19 at 09:15

1 Answers1

1

Problem

Your problem seems to be your if-statement. The if-statement requires some expression that can be evaluated as either True or False. Instead, what you are supplying is a series of True or False values:

The expression you are using consists of two parts:

  • part1 = (value[1]['Longs']==1.0): here everything seems fine, as it produces a single True or False

  • part2 = (self.df['Long_Market'].rolling(20).sum()==0): Here is the problem.

Explanation

To see why part2 is the problem, note that self.df['Long_Market'].rolling(20).sum() produces a series. With the following dummy data, it looks like this:

df = pd.DataFrame({'a': [1,2,3,4,5,6,7], 'b': [4,1,5,6,2,5,56]})

test = df['a'].rolling(3).sum()
>>> test
# 0     NaN
# 1     NaN
# 2     6.0
# 3     9.0
# 4    12.0
# 5    15.0
# 6    18.0

If you compare this to a single value, you get this:

>>> test == 12
# 0    False
# 1    False
# 2    False
# 3    False
# 4    True
# 5    False
# 6    False

If part1 is a single value, and part2 is a series of values, then part1 & part2 will be a series of values again:

>>> part1 & (test == 12)
# 0    False
# 1    False
# 2    False
# 3    False
# 4    True
# 5    False
# 6    False

And now we get to actual problem that I briefly introduced at the beginning of this post: You are supplying a pandas Series as the if-clause, but the if-clause requires a single value! What the error message is telling you, is that 'python' is trying to evaluate your series as a single value (as required by the if-clause), but doesn't know how to do that without further instructions.

Solution

If you are asking how to check if there are any zeroes in your rolling sum, then I recommend you replace part2 with:

(self.df['Long_Market'].rolling(20).sum() == 0).any()
KenHBS
  • 6,756
  • 6
  • 37
  • 52
  • Thank you for your help, unfortunetly I still get the error – Piotrek May 13 '19 at 09:55
  • ```ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''``` – Piotrek May 13 '19 at 09:56