[R] One year return not year to date

Rasmus Liland jr@| @end|ng |rom po@teo@no
Sun Jul 26 18:44:16 CEST 2020


Hello,

On 2020-07-25 20:05 +0100, Rui Barradas wrote:
> Às 19:06 de 25/07/2020, Bert Gunter escreveu:
> > On Sat, Jul 25, 2020 at 8:41 AM Pedro páramo <percentil101 using gmail.com> wrote:
> > > Hi all,
> > > 
> > > I want to calculate interanual 
> > > (not year to date) variation of a 
> > > stock, I am able to obtain year to 
> > > date with annualReturn, but no way 
> > > to obtain the interanual increase. 
> > > It says me no numerical 
> > > output.
> > > 
> > > Can anyone guide me to obtain it.
> > > 
> > > library(PerformanceAnalytics)
> > > library(dplyr)
> > > library(tibble)
> > > library(lubridate)
> > > library(PerformanceAnalytics)
> > > library(quantmod)
> > > library(ggplot2)
> > > library(png)
> > > library(RCurl)
> > > 
> > > fecha<-Sys.Date()-365 #with this I obtain one year back beginning
> > > precio<-getSymbols("ACX.MC",from='fecha')

Why do you provide quantmod::getSymbols 
the word fecha in the from argument, 
instead of the variable created on the 
previous line?

> > > ret2<- na.omit(CalculateReturns(Cl(ACX.MC)))
> > > g<-annualReturn(ACX.MC) #This is year to date
> > > d<-CalculateReturns(Cl(ACX.MCC))
> > > aaa<-getQuote('ACX.MC',from='fecha')

The from argument is not a valid in 
getQuote.

> > > Lastprice<-aaa[1,2]
> > > 
> > > lo<-c(diff(log(aaa),250))  #With this I try  to obtain interanual year variation but error

Yes, as Rui says, log(aaa[,-1]) returns 
something here at least, but diffing on 
lag=250 just returns an empty list 
because the one-line aaa[,-1] df is only 
7 elements long.

> > > Hope you can help me
> > 
> > What is the error message? Knowing 
> > this may help others diagnose the 
> > problem.
> 
> Hello,
> 
> I believe the error message comes from 
> the last line, getQuote returns a 
> data.frame with a Date column and 
> log(aaa) will try to log a date.
> Error.
> log(aaa[-1]) works, maybe the OP wants 
> to revise the code and then come back 
> to this or other thread.

Yes, because the first column is a 
POSIXct:

	> str(aaa)
	'data.frame':   1 obs. of  8 variables:
	 $ Trade Time: POSIXct, format:  ...
	 $ Last      : num 7.14
	 $ Change    : num 0
	 $ % Change  : num 0
	 $ Open      : num 7
	 $ High      : num 7.16
	 $ Low       : num 6.99
	 $ Volume    : int 794567
	> aaa
	                Trade Time  Last Change
	ACX.MC 2020-07-24 17:35:10 7.136      0
	       % Change Open  High  Low Volume
	ACX.MC        0    7 7.162 6.99 794567

V

r

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200726/442f80ad/attachment.sig>


More information about the R-help mailing list