[R] Moving average

Achim Zeileis Achim.Zeileis at uibk.ac.at
Wed Dec 31 14:58:06 CET 2014


On Tue, 30 Dec 2014, jim holtman wrote:

> Try this:
>
>> x <- read.csv(text = "Date,Open,High,Low,Close,Volume,Adj Close
> + 2014-12-26,162.27,163.09,162.01,162.34,1912200,162.34
> + 2014-12-24,162.88,162.99,161.61,161.82,1868100,161.82
> + 2014-12-23,162.23,162.90,161.61,162.24,4043300,162.24
> + 2014-12-22,158.33,161.91,158.33,161.44,4682500,161.44", as.is = TRUE)
>> require(lubridate)
>> x$Date <- ymd(x$Date)  # convert to a date field
>> x <- x[order(x$Date), ]  # sort by date
>> x$two_day <- filter(x$Close, c(0.5, 0.5))  # compute moving average
>> x
>        Date   Open   High    Low  Close  Volume Adj.Close two_day
> 4 2014-12-22 158.33 161.91 158.33 161.44 4682500    161.44  161.84
> 3 2014-12-23 162.23 162.90 161.61 162.24 4043300    162.24  162.03
> 2 2014-12-24 162.88 162.99 161.61 161.82 1868100    161.82  162.08
> 1 2014-12-26 162.27 163.09 162.01 162.34 1912200    162.34      NA

A canned approach for reading and filtering the data is also available in 
the "zoo" package. The read.zoo() function can directly create a "zoo" 
time series object with "Date" time index:

R> z <- read.zoo(text = "Date,Open,High,Low,Close,Volume,Adj Close
+  2014-12-26,162.27,163.09,162.01,162.34,1912200,162.34
+  2014-12-24,162.88,162.99,161.61,161.82,1868100,161.82
+  2014-12-23,162.23,162.90,161.61,162.24,4043300,162.24
+  2014-12-22,158.33,161.91,158.33,161.44,4682500,161.44",
+  header = TRUE, sep = ",", format = "%Y-%m-%d")

And then rollmean() can compute rolling means for all variables/columns:

R> rollmean(z, 2)
               Open    High    Low  Close  Volume Adj.Close
2014-12-22 160.280 162.405 159.97 161.84 4362900    161.84
2014-12-23 162.555 162.945 161.61 162.03 2955700    162.03
2014-12-24 162.575 163.040 161.81 162.08 1890150    162.08

You can additionally supply the fill = NA argument if you want trailing 
NAs for 2014-12-26.

>
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
>
> On Sun, Dec 28, 2014 at 8:31 AM, Rolf Edberg <rolfe at algonet.se> wrote:
>
>> Thank you for trying to help!!
>>
>>
>>
>> I am very new to the R code. So need help with every step.
>>
>>
>>
>> The goal is to use technical analysis on stock prices. Not only MA but if
>> I understand the principle with that I hope I can use the other techniques
>> as well.
>>
>>
>>
>> I found R-adamant but do not know how to use it.
>>
>>
>>
>> I downloaded 4 days of IBM prices from yahoo in a csv-file..
>>
>> I don?t know what ?dput? is.
>>
>>
>>
>> Here is the IBM prices in a text string:
>>
>> Date,Open,High,Low,Close,Volume,Adj Close
>>
>> 2014-12-26,162.27,163.09,162.01,162.34,1912200,162.34
>>
>> 2014-12-24,162.88,162.99,161.61,161.82,1868100,161.82
>>
>> 2014-12-23,162.23,162.90,161.61,162.24,4043300,162.24
>>
>> 2014-12-22,158.33,161.91,158.33,161.44,4682500,161.44
>>
>>
>>
>> I would like the date in sorted with the oldest at the top.
>>
>>
>>
>> I would like to add a column with the technical indicator, in this case
>> 2-days MA of Close.
>>
>>
>>
>> And I would like to have the result in a csv file. I will use the file in
>> another program.
>>
>>
>>
>> Thank you !!
>>
>>
>>
>> Rolf
>>
>>
>>
>> *From:* jim holtman [mailto:jholtman at gmail.com]
>> *Sent:* Sunday, December 28, 2014 4:45 PM
>> *To:* Rolf Edberg
>> *Cc:* R mailing list
>> *Subject:* Re: [R] Moving average
>>
>>
>>
>> could not read the data you posted; try 'dput' next time.
>>
>>
>>
>> If it is just a 2 day moving average, try the 'filter' function:
>>
>>
>>
>> > x <- 1:20
>>
>> > x
>>
>>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
>>
>> > filter(x, c(.5,.5))
>>
>> Time Series:
>>
>> Start = 1
>>
>> End = 20
>>
>> Frequency = 1
>>
>>  [1]  1.5  2.5  3.5  4.5  5.5  6.5  7.5  8.5  9.5 10.5 11.5 12.5 13.5 14.5
>> 15.5 16.5 17.5 18.5 19.5   NA
>>
>> >
>>
>>
>>
>>
>>
>> Jim Holtman
>> Data Munger Guru
>>
>> What is the problem that you are trying to solve?
>> Tell me what you want to do, not how you want to do it.
>>
>>
>>
>> On Sun, Dec 28, 2014 at 6:56 AM, Rolf Edberg <rolfe at algonet.se> wrote:
>>
>>
>>
>> How do I add a new column with 2-days moving average (from
>> r-adamant(https://github.com/TotallyBullshit/radamant)) on IBM prices in a
>> csv-file (ibm.csv) and then save all in a new csv file(ibm2.csv)?
>>
>>
>>
>>
>> Prices
>>
>>
>>
>>
>>
>> Date
>>
>> Open
>>
>> High
>>
>> Low
>>
>> Close
>>
>> Volume
>>
>> Adj Close*
>>
>>
>> Dec 26, 2014
>>
>> 162.27
>>
>> 163.09
>>
>> 162.01
>>
>> 162.34
>>
>> 1,912,200
>>
>> 162.34
>>
>>
>> Dec 24, 2014
>>
>> 162.88
>>
>> 162.99
>>
>> 161.61
>>
>> 161.82
>>
>> 1,868,100
>>
>> 161.82
>>
>>
>> Dec 23, 2014
>>
>> 162.23
>>
>> 162.90
>>
>> 161.61
>>
>> 162.24
>>
>> 4,043,300
>>
>> 162.24
>>
>>
>> Dec 22, 2014
>>
>> 158.33
>>
>> 161.91
>>
>> 158.33
>>
>> 161.44
>>
>> 4,682,500
>>
>> 161.44
>>
>>
>> Dec 19, 2014
>>
>> 157.49
>>
>> 160.41
>>
>> 157.49
>>
>> 158.51
>>
>> 8,864,900
>>
>> 158.51
>>
>>
>>
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list