[R] moving averages on specific interval and merge

Jim Green student.northwestern at gmail.com
Sat May 12 11:39:51 CEST 2012


Greetings!

I am using quantstrat and xts to do some intraday work and come up
with this problem. the xts object temp in the following example is
attached as and rda file.

> head(temp)
                   A.Open A.High  A.Low A.Close A.Volume
2012-02-01 08:29:00  42.47  43.76 41.410   43.76     2071
2012-02-01 09:30:00  43.38  43.38 42.970   43.15    40300
2012-02-01 09:31:00  43.14  43.28 43.130   43.28    14990
2012-02-01 09:32:00  43.27  43.37 43.270   43.37     3300
2012-02-01 09:33:00  43.37  43.50 43.370   43.48     3056
2012-02-01 09:34:00  43.49  43.50 43.396   43.44    10968

> tail(temp)
                    A.Open  A.High   A.Low A.Close A.Volume
2012-03-27 16:07:00 45.6650 45.6650 45.6650 45.6650      170
2012-03-27 16:08:00 45.6710 45.6710 45.6710 45.6710      474
2012-03-27 16:10:00 45.9131 45.9131 45.9131 45.9131     1800
2012-03-27 16:13:00 45.6952 45.6952 45.6952 45.6952      300
2012-03-27 16:15:00 45.9368 45.9368 45.9368 45.9368      791
2012-03-27 16:21:00 45.7000 45.7000 45.7000 45.7000    22000


I would like to calculate moving averages of minute volume for
specific minute and merge with the original minute ohlc data.

take 09:40:00 for example, calculate the average previous 10 days
volume between 09:39:00 to 09:40:00 and merge with exiting data.

ultimately I want to get an xts object with columns

Open   High   Low   Close   Volume   Average.Volume.at.current.interval
2012-03-27 16:07:00 45.6650 45.6650 45.6650 45.6650      170   177
2012-03-27 16:08:00 45.6710 45.6710 45.6710 45.6710      474  500
...
...
..
2012-03-27 16:21:00 45.7000 45.7000 45.7000 45.7000    22000 1000

any pointers are appreciated!

Jim.


More information about the R-help mailing list