The Altair Community is migrating to a new platform to provide a better experience for you. In preparation for the migration, the Altair Community is on read-only mode from October 28 - November 6, 2024. Technical support via cases will continue to work as is. For any urgent requests from Students/Faculty members, please submit the form linked here

"Help with R for RM Pairs Model"

ronmacronmac Member Posts: 11 Contributor II
edited May 2019 in Help
I am trying to put together code to retrieve Yahoo Data and perform a cointegration analysis of two securities for Pair Trade analysis in RM. I have R code for performing the ADF test. Not being knowledgeable in R Code I need help in adjusting some parameters in this code. The way it is now all the historical data available is accessed. I want to be able to define the date ranges for the data to be retrieved. For example I want one run of the code for last years data then I want to tun the code again on this years data. I don't know how to input this in R. Can someone help me with this?
#http://quanttrader.info/public/testForCoint.html

library(zoo)            # Load the zoo package
library(tseries)        # Load the tseries package

# Read the CSV files into data frames
#
aapl <- read.csv("http://ichart.finance.yahoo.com/table.csv?s=GLD&ignore=.csv", stringsAsFactors=F)
adbe <- read.csv("http://ichart.finance.yahoo.com/table.csv?s=GDX&ignore=.csv", stringsAsFactors=F)

# The first column contains dates.  The as.Date
# function can convert strings into Date objects.
#
aapl_dates <- as.Date(aapl[,1])
adbe_dates <- as.Date(adbe[,1])

# The seventh column contains the adjusted close.
# We use the zoo function to create zoo objects from that data.
# The function takes two arguments: a vector of data and
# a vector of dates.
#
aapl <- zoo(aapl[,7], aapl_dates)
adbe <- zoo(adbe[,7], adbe_dates)

# The merge function can combine two zoo objects,
# computing either their intersection (all=FALSE)
# or union (all=TRUE).
#
t.zoo <- merge(aapl, adbe, all=FALSE)

# At this point, t.zoo is a zoo object with two columns: gld and gdx.
# Most statistical functions expect a data frame for input,
# so we create a data frame here.
#
t <- as.data.frame(t.zoo)

# Tell the user what dates are spanned by the data.
#
cat("Date range is", format(start(t.zoo)), "to", format(end(t.zoo)), "\n")

m <- lm(aapl ~ adbe + 0, data=t)
beta <- coef(m)[1]

cat("Assumed hedge ratio is", beta, "\n")

sprd <- t$aapl - beta*t$adbe
ht <- adf.test(sprd, alternative="stationary", k=0)

cat("ADF p-value is", ht$p.value, "\n")

if (ht$p.value < 0.05) {
    cat("The spread is likely mean-reverting\n")
} else {
    cat("The spread is not mean-reverting.\n")
}
Tagged:

Answers

  • B_B_ Member Posts: 70 Maven
    Try something like this:


    http://ichart.finance.yahoo.com/table.csv?s=GLD&;a=10&b=18&c=2006&d=11&e=2&f=2009&g=d&ignore=.csv


    start date is nov18 2006, end date is dec 2, 2009.


    If you go to the historical prices page for a yahoo symbol and enter your date range, then copy the url from the Download to Spreadsheet button, you should have the correct url to place in your read.csv get data line
Sign In or Register to comment.