简体   繁体   中英

EWMA on group / bucket of data

I have a time series vector I need to create a trend off of.

I was trying to get the EMA function in R (through the TTR package - I am loading quantmod, and TTR is required) to run over a group of prices.

Is there a way to create an EMA estimate over a given amount of rows in the vector, so I have one EMA estimate for each bucket? Say I have a bucket size of 10 and I would like to estimate the trend between each bucket, just choosing open and close for the bucket negates a large amount of info, so I would like to have an estimate over the bucket, but with just one output.

Alteratively I tried averaging over the bucket length, but this doesn't produce the desired output.

Example of the vector I am using

dput(head(price, 200))
c(137, 137, 137, 137, 137, 137, 137, 137, 137, 136.9, 136.9, 
136.9, 137, 137, 137, 137, 137, 137.2, 137.2, 137.2, 137.1, 137.4, 
137.4, 137.4, 137.4, 137.4, 137.4, 137.1, 137, 137, 137, 137, 
137, 136.9, 136.9, 136.9, 136.9, 136.9, 136.7, 137, 137, 137, 
137, 136.7, 136.9, 136.9, 136.9, 136.5, 136.5, 136.9, 136.9, 
136.9, 137, 137, 137.3, 136.7, 137, 137, 137, 137.1, 137, 137, 
137.1, 137.1, 137, 137, 137.1, 137, 137, 136.8, 137, 137, 136.9, 
136.8, 136.7, 137.1, 137, 137, 137.1, 137, 137.1, 137.1, 136.7, 
136.7, 136.7, 136.7, 136.7, 136.8, 137, 137, 137, 136.9, 136.9, 
136.9, 136.9, 136.9, 136.9, 136.9, 136.9, 136.9, 136.9, 136.9, 
136.9, 136.9, 137, 136.9, 136.9, 136.9, 136.9, 136.9, 136.8, 
136.8, 136.9, 136.9, 136.9, 136.9, 137, 136.9, 136.9, 136.9, 
136.9, 136.9, 136.9, 137, 137, 137, 137, 137, 137, 137, 136.9, 
136.9, 136.9, 137, 137, 136.9, 137, 137.1, 137.1, 137.1, 137, 
137.2, 137.2, 137.2, 137.2, 137.2, 137.2, 137.1, 137.1, 136.9, 
136.8, 136.9, 136.9, 136.9, 136.9, 136.9, 137, 136.9, 137, 137, 
137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 136.9, 
137.1, 137.1, 137.1, 137.1, 137, 137, 137.1, 137.1, 137.1, 137.1, 
137.1, 137.1, 137, 137, 137, 137, 137, 137, 136.9, 136.9, 136.9, 
136.9, 136.9, 136.9, 136.9, 136.9, 136.9)
bucketsize=10

bucket=rep(1:ceiling(length(price)/bucketsize),each=bucketsize)

aggregate(price,list(bucket),mean)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM