Counting how many times in a row the result of a sum is positive (or negative)
<p><strong>First Part</strong></p>

<blockquote>
  <p>I have a dataframe with finance data (33023 rows, here the link to the
  data: <a href="https://mab.to/Ssy3TelRs" rel="nofollow">https://mab.to/Ssy3TelRs</a>); df.open is the price of the title and
  df.close is the closing price.</p>
  
  <p>I have been trying to see how many times in a row the title closed
  with a gain and with a lost.</p>
  
  <p>The result that I'm looking for should tell me that the title was
  positive 2 days in a row x times, 3 days in a row y times, 4 days in a
  row z times and so forth.</p>
  
  <p>I have started with a for:</p>

<pre><code>for x in range(1,df.close.count()):   y = df.close[x]-df.open[x]
</code></pre>
  
  <p>and then unsuccessful series of if statements...</p>
  
  <p>Thank you for your help.</p>
  
  <p>CronosVirus00</p>
  
  <p>EDITS:</p>

<pre><code>&gt;&gt;&gt; df.head(7)
       data  ora     open      max      min    close  Unnamed: 6
0  20160801    0  1.11781  1.11781  1.11772  1.11773           0
1  20160801  100  1.11774  1.11779  1.11773  1.11777           0
2  20160801  200  1.11779  1.11800  1.11779  1.11795           0
3  20160801  300  1.11794  1.11801  1.11771  1.11771           0
4  20160801  400  1.11766  1.11772  1.11763  1.11772           0
5  20160801  500  1.11774  1.11798  1.11774  1.11796           0
6  20160801  600  1.11796  1.11796  1.11783  1.11783           0
</code></pre>
  
  <p>Ifs:</p>

<pre><code>for x in range(1,df.close.count()):   y = df.close[x]-df.open[x]  if y &gt; 0 :      green += 1      y = df.close[x+1] - df.close[x+1]
  twotimes += 1       if y &gt; 0 :          green += 1          y = df.close[x+2] -
</code></pre>
  
  <p>df.close[x+2]             threetimes += 1             if y > 0 :
                green += 1          y = df.close[x+3] - df.close[x+3]           fourtimes += 1</p>
  
  <p>FINAL SOLUTION</p>
  
  <p>Thank you all! And the end I did this:</p>

<pre><code>df['test'] = df.close - df.open &gt;0
green = df.test #days that it was positive

def gg(z):
    tot =green.count()
    giorni = range (1,z+1) # days in a row i wanna check
    for x in giorni:
        y = (green.rolling(x).sum()&gt;x-1).sum()
        print(x," ",y, " ", round((y/tot)*100,1),"%")

gg(5)
1   14850   45.0 %
2   6647   20.1 %
3   2980   9.0 %
4   1346   4.1 %
5   607   1.8 %
</code></pre>
</blockquote>

<p>If i understood your question correctly you can do it this way:</p>

<pre><code>In [76]: df.groupby((df.close.diff() &lt; 0).cumsum()).cumcount()
Out[76]:
0    0
1    1
2    2
3    0
4    1
5    2
6    0
7    0
dtype: int64
</code></pre>

<blockquote>
  <p>The result that I'm looking for should tell me that the title was
  positive 2 days in a row x times, 3 days in a row y times, 4 days in a
  row z times and so forth.</p>
</blockquote>

<pre><code>In [114]: df.groupby((df.close.diff() &lt; 0).cumsum()).cumcount().value_counts().to_frame('count')
Out[114]:
   count
0      4
2      2
1      2
</code></pre>

<p>Data set:</p>

<pre><code>In [78]: df
Out[78]:
       data  ora     open      max      min    close
0  20160801    0  1.11781  1.11781  1.11772  1.11773
1  20160801  100  1.11774  1.11779  1.11773  1.11777
2  20160801  200  1.11779  1.11800  1.11779  1.11795
3  20160801  300  1.11794  1.11801  1.11771  1.11771
4  20160801  400  1.11766  1.11772  1.11763  1.11772
5  20160801  500  1.11774  1.11798  1.11774  1.11796
6  20160801  600  1.11796  1.11796  1.11783  1.11783
7  20160801  700  1.11783  1.11799  1.11783  1.11780

In [80]: df.close.diff()
Out[80]:
0        NaN
1    0.00004
2    0.00018
3   -0.00024
4    0.00001
5    0.00024
6   -0.00013
7   -0.00003
Name: close, dtype: float64
</code></pre>

python pandas dataframe