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 you correctly, you want the number of days that have at least <code>n</code> positive days in a row before and itself included.</p>

<p>Similarly to what @Thang suggested, you can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rolling.html" rel="nofollow">rolling</a>:</p>

<pre><code>import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(10, 2), columns=["open", "close"])
# This just sets up random test data, for example:
#       open     close
# 0  0.997986  0.594789
# 1  0.052712  0.401275
# 2  0.895179  0.842259
# 3  0.747268  0.919169
# 4  0.113408  0.253440
# 5  0.199062  0.399003
# 6  0.436424  0.514781
# 7  0.180154  0.235816
# 8  0.750042  0.558278
# 9  0.840404  0.139869

positiveDays = df["close"]-df["open"] &gt; 0
# This will give you a series that is True for positive days:
# 0    False
# 1     True
# 2    False
# 3     True
# 4     True
# 5     True
# 6     True
# 7     True
# 8    False
# 9    False
# dtype: bool

daysToCheck = 3
positiveDays.rolling(daysToCheck).sum()&gt;daysToCheck-1
</code></pre>

<p>This will now give you a series, indicating for every day, whether it has been positive for <code>daysToCheck</code> number of days in a row:</p>

<pre><code>0    False
1    False
2    False
3    False
4    False
5     True
6     True
7     True
8    False
9    False
dtype: bool
</code></pre>

<p>Now you can use <code>(positiveDays.rolling(daysToCheck).sum()&gt;daysToCheck-1).sum()</code> to get the number of days (in the example <code>3</code>) that obey this, which is what you want, as far as I understand.</p>

python pandas dataframe