Saturday, 7 September 2013

Calculating averages of 15 records ahead of the current record as a new column

Calculating averages of 15 records ahead of the current record as a new
column

I have 1 minute data for an equity as follows;
bidopen bidhigh bidlow bidclose bidvolume
currencypair
2007-03-30 16:01:00 1.9687 1.96900 1.9686 1.9686 877.40
2007-03-30 16:02:00 1.9686 1.96905 1.9686 1.9686 897.20
2007-03-30 16:03:00 1.9686 1.96900 1.9686 1.9690 1076.11
2007-03-30 16:04:00 1.9689 1.96910 1.9688 1.9690 849.70
2007-03-30 16:05:00 1.9690 1.96900 1.9688 1.9689 1402.80
I want to add an extra column. This column will:
take 15 records from this point onwards (including the current time)
from those 15 records get the maximum bidhigh and the minimum bidlow
calculate the difference of the high-low and use that value in the new column
I tried the following
def lookaheadmaxmin(df):
df2=df[:15]
high=df2['bidhigh'].max(axis=1)
low=df2['bidlow'].min(axis=1)
return high-low
then
df['newcolumn'] = map( lookaheadmaxmin, df[:15])
This errors. I'm pretty sure the 'df[:15]' in the map is the problem as I
don't know how to pass a slice of the current & future records to the
function
Essentially what i'm trying to do is determine how much price has moved
within a 15 minute moving window as follows:
So between; 16:00 - 16:15 - how much did price move? put this in the
column on the 16:00 record
16:01 - 16:16 - how much did price move? put this in the column on the
16:01 record
16:02 - 16:17 - how much did price move? put this in the column on the
16:02 record
16:03 - 16:18 - how much did price move? put this in the column on the
16:03 record
16:04 - 16:19 - how much did price move? put this in the column on the
16:04 record
16:05 - 16:20 - how much did price move? put this in the column on the
16:05 record

No comments:

Post a Comment