Reputation: 1918
I have a pandas dataframe with 1 minute stock data.
Close
2013-09-23 09:30:00 NaN
2013-09-23 09:31:00 8.2500
2013-09-23 09:32:00 8.2500
2013-09-23 09:33:00 8.2800
2013-09-23 09:34:00 8.2725
2013-09-23 09:35:00 8.2850
2013-09-23 09:36:00 8.2700
2013-09-23 09:37:00 8.2827
2013-09-23 09:38:00 8.3100
2013-09-23 09:39:00 8.3200
I'm trying to make a function that returns TRUE when a given close is a 5 minute high
Something like (pseudo code):
stockdata["close"].apply(lambda x: x == max of last 5 rows)
How would you do this?
Upvotes: 0
Views: 223
Reputation: 139162
You can check when your column values equal the maximum of the last five rows determined with the rolling_max
function (http://pandas.pydata.org/pandas-docs/stable/computation.html#moving-rolling-statistics-moments):
>>> stockdata["Close"] == pd.rolling_max(stockdata["Close"], 5)
2013-09-23 09:30:00 False
2013-09-23 09:31:00 False
2013-09-23 09:32:00 False
2013-09-23 09:33:00 False
2013-09-23 09:34:00 False
2013-09-23 09:35:00 True
2013-09-23 09:36:00 False
2013-09-23 09:37:00 False
2013-09-23 09:38:00 True
2013-09-23 09:39:00 True
dtype: bool
If your are not certain you data are always nicely 1-minutly (so the 5 rows are not always equal to 5 mins), you can set the freq
keyword in rolling_max to 1min
.
Upvotes: 3