如何在每日时间序列数据中获得每周的最高或最低最低值,然后使其成为每周时间序列?

I have an ohlc daily data for US stocks. I would like to derive a weekly timeseries from it and compute SMA and EMA. To be able to do that though, requirement is to create the weekly timeseries from the maximum high per week, and another weekly timeseries from the minimum low per week. After that I, would then compute their sma and ema then assign to every days of the week (one period forward). So, first problem first, how do I get the weekly from the daily using R (any package), or better if you can show me an algo for it, any language but preferred is Golang? Anyway, I can rewrite it in golang if needed.

   Date           High     Low     Week(High)   Week(Low)   WkSMAHigh 2DP     WkSMALow 2DP 
                                                         (one period forward)   
Dec 24 Fri         6        3           8            3            5.5              1.5       
Dec 23 Thu         7        5                                     5.5              1.5
Dec 22 Wed         8        5                                     5.5              1.5
Dec 21 Tue         4        4                                     5.5              1.5
Assume Holiday (Dec 20)
Dec 17 Fri         4        3           6            2           None
Dec 16 Thu         4        3
Dec 15 Wed         5        2
Dec 14 Tue         6        4
Dec 13 Mon         6        4
Dec 10 Fri         5        1           5            1           None
Dec 9 Thu          4        3
Dec 8 Wed          3        2
Assume Holiday (Dec 6 & 7)

I'd start by generating a column which specifies which week it is.

You could use the lubridate package to do this, that would require converting your dates into Date types. It has a function called week which returns the number of full 7 day periods that have passed since Jan 1st + 1. However I don't know if this data goes over several years or not. Plus I think there's a simpler way to do this.

The example I'll give below will simply do it by creating a column which just repeats an integer 7 times up to the length of your data frame.

Pretend your data frame is called ohlcData.

# Create a sequence 7 at a time all the way up to the end of the data frame
# I limit the sequence to the length nrow(ohlcData) so the rounding error
# doesn't make the vectors uneven lengths
ohlcData$Week <- rep(seq(1, ceiling(nrow(ohlcData)/7), each = 7)[1:nrow(ohlcData)]

With that created we can then go ahead and use the plyr package, which has a really useful function called ddply. This function applies a function to columns of data grouped by another column of data. In this case we will apply the max and min functions to your data based on its grouping by our new column Week.

library(plyr)

weekMax <- ddply(ohlcData[,c("Week", "High")], "Week", numcolwise(max))
weekMin <- ddply(ohlcData[,c("Week", "Low")], "Week", numcolwise(min))

That will then give you the min and max of each week. The dataframe returned for both weekMax and weekMin will have 2 columns, Week and the value. Combine these however you see fit. Perhaps weekExtreme <- cbind(weekMax, weekMin[,2]). If you want to be able to marry up date ranges to the week numbers it will just be every 7th date starting with whatever your first date was.