R-code

Here is some quick documentation for the code:
Code is designed to calculate and return vectors of expected prices and a range based on standard deviations fitting the data to the following model:
dx=A*t+B*dz; where dz=є√dt and є~N(0,sd^2)

Quick documentation for the functions:

calcSDreturn <- function(tick, t)
%% Calculates SD of returns based on the last t data points in the tick object, returns a single value

calcMeanreturn <- function(tick, t)
%% Calculates Mean of returns based on the last t data points in the tick object, returns a single value.

expectedValue <- function(tick,t,f)
%% Creates a vector of f expected prices, from starting the current day, projecting out into the future, based on the last t days of data.

upperBoundCI <- function(tick,t,sd,f)
%% Creates a vector of f expected prices, sd number of standard deviations above the mean, projected out f periods into the future, based on the last t days of data.

lowerBoundCI <- function(tick,t,sd,f)
%% Creates a vector of f expected prices, sd number of standard deviations below the mean, projected out f periods into the future, based on the last t days of data.

Once you have cut and paste the entire thing into the R console, you only need to select a ticker and run the last function. I have included examples with returns at the bottom:

install.packages(“quantmod”)
library(quantmod)

getSymbols(“SPY”)

calcSDreturn <- function(tick, t)
{
p<- as.double(tick[,4])
if (t > (length(p)-2))
{
stop(“The requested time period is longer than that of the data”)
}
rv<-vector()
for (i in 2:length(p))
{
rv[i]<-100*(log(p[i],base=10)-log(p[i-1],base=10))
}
r<-vector()
for (i in t:1)
{
r[i] <- rv[length(rv)-i+1]
}
sd<-sqrt(var(r))
return(sd)
return(rv)
}

calcMeanreturn <- function(tick, t)
{
p<- as.double(tick[,4])
if (t > (length(p)-2))
{
stop(“The requested time period is longer than that of the data”)
}
rv<-vector()
for (i in 2:length(p))
{
rv[i]<-100*(log(p[i],base=10)-log(p[i-1],base=10))
}
r<-vector()
for (i in t:1)
{
r[i] <- rv[length(rv)-i+1]
}
m<-mean(r)
return(m)
}

expectedValue <- function(tick,t,f)
{
p<- as.double(tick[,4])
a <- calcMeanreturn(tick, t)/t
x <-vector()
x[1]<-p[length(p)]
for(i in 2:f)
{
x[i]<-x[i-1]+a*x[i-1]
}
index<-last(index(tick))+(1:f)
z<-as.xts(x,index)
return(z)
}

upperBoundCI <- function(tick,t,sd,f)
{
p<- as.double(tick[,4])
a <- calcMeanreturn(tick, t)/t
b <- calcSDreturn(tick, t)/t
x<-vector()
x[1]<-p[length(p)]
for(i in 2:f)
{
x[i]<-x[i-1]+a*x[i-1]+(b*sd)*x[i-1]
}
index<-last(index(tick))+(1:f)
z<-as.xts(x,index)
return(z)
}

lowerBoundCI <- function(tick,t,sd,f)
{
p<- as.double(tick[,4])
a <- calcMeanreturn(tick, t)/t
b <- calcSDreturn(tick, t)/t
x<-vector()
x[1]<-p[length(p)]
for(i in 2:f)
{
x[i]<-x[i-1]+a*x[i-1]-(b*sd)*x[i-1]
}
index<-last(index(tick))+(1:f)
z<-as.xts(x,index)
return(z)
}

plotGBM<-function(tick,t,f,sd)
{
u<- upperBoundCI(tick,t,sd,f)
l<- lowerBoundCI(tick,t,sd,f)
p<- expectedValue(tick,t,f)
plot(c(p,l,u),type=’p’, main=”GBM Price Projection”, xlab=”Current day + t”, ylab=”Price”)
}

Example: I want to calculate the GBM model of returns in TLT for the next 20 trading days based on the last 60 trading days of data with a variance of one standard deviation from the mean:
getSymbols(“TLT”)
plotGBM(TLT,60,20,1)

This will return:
http://i.imgur.com/IsGu4.png

Using much more data for our projections will significantly lower the deviation:
plotGBM(TLT,300,20,1)

This will return:
http://i.imgur.com/1W2Qo.png

Several problems at the moment are:
1) I have not figured out how to do the indexing in a way that tracks the days the market is actually open, hence right now the x-axis is only an index of days out into the future rather than price on an actual trading day.
2) I have not been able to attach these plots to the chartSeries or any other candlestick plotting functions in R so right now I am stuck with just expected values verses those and underlying prices.

To adapt this code to any model you would like, you only need to change the formula used by the calcMeanreturn() and calcSDreturn() functions. Alternatively, if you wish to vary the way the model forecasts the stock prices you can change the loops in the expectedValue() and lowerBoundCI()/upperBoundCI() functions.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: