You need quantmod & tseries in R to run this:
The script gets FX daily data from Oanda, does a simple linear regression to find the hedging ratios, and then uses the Augmented DF test to test for the P-value of mean reversion in the spread of the basket.
When I run it I get this:
EUR.USD coefficient is 1.
When I plot the spread the first 100 days look like this:
http://i.imgur.com/HdtFS.png
Surely something must be wrong. The holy grail shouldn't be so easy to find.
Can someone help me find what is wrong?
I tried backtesting on Dukascopy with the above coefficients as lot sizes of a basket, but I run into loses. And the spread has a different order of magnitude in dukascopy. Why is that?
Inserted Code
library(quantmod)
library(tseries)
pairs <- c(
"EUR/USD",
"GBP/USD",
"AUD/USD",
"USD/CAD",
"USD/CHF",
"NZD/USD"
)
name <- function(n) {
gsub("/","",n,fixed=TRUE)
}
getFrame <- function(p) {
result <- NULL
as.data.frame(lapply(p, function(x) {
if(!exists(name(x))) {
getSymbols(x, src="oanda")
}
if(is.null(result)) {
result <- get(name(x))
} else {
result <- merge(result, get(name(x)))
}
}))
}
isStationary <- function(frame) {
model <- lm(frame[,1] ~ as.matrix(frame[,-1]) + 0)
spread <- frame[,1] - rowSums(coef(model) * frame[, -1])
results <- adf.test(spread, alternative="stationary", k=10)
if(results$p.value < 0.05) {
coefficients <- coef(model)
names(coefficients) <- gsub("as.matrix.frame.......", "", names(coefficients))
plot(spread[1:100], type = "b")
cat("Minimum spread: ", min(spread), "\n")
cat("Maximum spread: ", max(spread), "\n")
cat("P-Value: ", results$p.value, "\n")
cat("Coeficients: \n")
print(coefficients)
}
}
frame <- getFrame(pairs)
isStationary(frame)
The script gets FX daily data from Oanda, does a simple linear regression to find the hedging ratios, and then uses the Augmented DF test to test for the P-value of mean reversion in the spread of the basket.
When I run it I get this:
Inserted Code
Minimum spread: -1.894506 Maximum spread: 2.176735 P-Value: 0.03781909 Coeficients: GBP.USD AUD.USD USD.CAD USD.CHF NZD.USD 0.59862816 0.48810239 -0.12900886 0.04337268 0.02713479
EUR.USD coefficient is 1.
When I plot the spread the first 100 days look like this:
http://i.imgur.com/HdtFS.png
Surely something must be wrong. The holy grail shouldn't be so easy to find.
Can someone help me find what is wrong?
I tried backtesting on Dukascopy with the above coefficients as lot sizes of a basket, but I run into loses. And the spread has a different order of magnitude in dukascopy. Why is that?