blakc05
blakc05

Reputation: 3799

How to calculate returns from a vector of prices?

I have to calculate the return of a vector that gives a historical price series of a stock. The vector is of a form:

a <- c(10.25, 11.26, 14, 13.56) 

I need to calculate daily gain/loss (%) - i.e. what is the gain it has from 10.25 to 11.26 then from 11.26 to 14 etc.

Is there a function to calculate this automatically?

Upvotes: 10

Views: 57855

Answers (7)

andhherson
andhherson

Reputation: 37

You can do this:

(PRICE / lag(PRICE)-1) * 100

Upvotes: -1

Malick
Malick

Reputation: 6732

A more detailed example with multiple time series:

############ Vector  ############

vPrice <- (10.25, 11.26, 14, 13.56) 
n = length(vPrice)

#Log returns

log_ret <- diff(log(vPrice)) # or = log(vPrice[-1]/vPrice[-n]) because "..[-i]" removes the i'th item of the vector
log_ret

#Simple returns

simple_ret <- diff(vPrice)/vPrice[1:(n-1)] # or = diff(vPrice)/vPrice[-n]
simple_ret


############ Multiple Time series ############

head(EuStockMarkets)

mPrice <-  EuStockMarkets
n = dim(mPrice)[1] #Nb rows

log_ret <- diff(log(mPrice))
head(log_ret)

simple_ret <- diff(mPrice)/mPrice[1:(n-1),]
head(simple_ret)


#Total Returns

total_log_ret <- colSums(log_ret,2) #just a sum for log-returns
total_log_ret
total_Simple_ret <- apply(1+simple_ret, 2, prod)-1 # product of simple returns
total_Simple_ret

##################

#From simple to log returns 
all.equal(log(1+total_Simple_ret),total_log_ret) #should be true

#From Log to simple returns 
all.equal( total_Simple_ret,exp(total_log_ret)-1) #should be true

Upvotes: 2

vonjd
vonjd

Reputation: 4380

Another possibility is the ROC function of the TTR package:

library(TTR)
a <- c(10.25, 11.26, 14, 13.56)
ROC(a, type = "discrete")
## [1]          NA  0.09853659  0.24333925 -0.03142857

type = continuous (which is also the default) gives log-returns:

ROC(a)
## [1]          NA  0.09397892  0.21780071 -0.03193305

Upvotes: 0

Michael Ohlrogge
Michael Ohlrogge

Reputation: 10990

You can also use the exact relationship that returns are equal to the exponent of log returns minus one. Thus, if Prices contains your prices, the following will give you your returns:

Returns = exp(diff(log(Prices))) - 1

Note that this is an exact relationship, rather than the approximate relationship given in the answer by @PBS.

Upvotes: 3

PBS
PBS

Reputation: 17

ret<-diff(log(a))

This will give you the geometric returns - return follow a lognormal distribution (lower boundary is -100% since prices are always non-negative), so the ln(prices) follows a normal distribution (therefore you might see returns smaller than -1 or -100%).

For the "normal" range of returns, the difference between the [P(t+1)-P(t)]/P(t) and the LN(P(t+1)/P(t)) should be negligible. I hope this helps.

Upvotes: 0

Chase
Chase

Reputation: 69201

You may find the functions in quantmod relevant for your work:

> require(quantmod)
> Delt(a)
     Delt.1.arithmetic
[1,]                NA
[2,]        0.09853659
[3,]        0.24333925
[4,]       -0.03142857

Upvotes: 25

Andrie
Andrie

Reputation: 179468

Using your sample data, I think you mean the following:

a <- c(10.25, 11.26, 14, 13.56) 
> diff(a)/a[-length(a)]
[1]  0.09853659  0.24333925 -0.03142857

diff returns the vector of lagged differences and a[-length(a)] drops the last element of a.

Upvotes: 28

Related Questions