Find rolling 52 week high on daily stock market data in pandas
<p>This seems like a simple question (and answer) but I'm having trouble with it.</p>

<p>The issue:</p>

<p>I have a pandas dataframe full of OHLC data. I want to find the rolling 52 week high throughout the dataframe.</p>

<p>My dataset is from yahoo.  You can pull the same data down with the folllowing code to get daily data:</p>

<pre><code>import pandas.io.data as web
df = web.DataReader('SPX', 'yahoo', start, end)
</code></pre>

<p>A tail of the data gives the output below:</p>

<pre><code>                 Open        High         Low       Close     Volume  
Date                                                                    
2016-07-15  216.779999  217.009995  215.309998  215.830002  107155400   
2016-07-18  215.970001  216.600006  215.669998  216.410004   58725900   
2016-07-19  215.919998  216.229996  215.630005  216.190002   54345700   
2016-07-20  216.190002  217.369995  216.190002  217.089996   58159500   
2016-07-21  216.960007  217.220001  215.750000  216.270004   66070000 
</code></pre>

<p>To get the 52 week high (rolling), I can run the following:</p>

<pre><code>df["52weekhigh"] = pd.rolling_max(df.High, window=200, min_periods=1)
</code></pre>

<p>I get the following (some col:</p>

<pre><code>                 High  52weekhigh
Date                              
2016-07-15  217.009995  217.009995
2016-07-18  216.600006  217.009995
2016-07-19  216.229996  217.009995
2016-07-20  217.369995  217.369995
2016-07-21  217.220001  217.369995
</code></pre>

<p>This gives me an values for the 52 week highs as new highs come in, but I'm not a fan of using 200 here. Should it be 200 or 201 or 220 (there are "approximately" 200 trading days in the year)?</p>

<p>I could resample the data to weekly to get the values, but then i can't easily get back to my original daily data (or can I?).</p>

<p>So...here's the question:</p>

<p>Is there a way to run rolling_max on pandas dataframes and set the window to '52 weeks' or something similar? If not, can anyone think of a better approach to this than the above?</p>