13.08.2022 Views

advanced-algorithmic-trading

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

172

set.seed(123)

## SIMULATED DATA

## Create a simulated random walk

z <- rep(0, 1000)

for (i in 2:1000) z[i] <- z[i-1] + rnorm(1)

## Create two non-stationary series based on the

## simulated random walk

p <- q <- rep(0, 1000)

p <- 0.3*z + rnorm(1000)

q <- 0.6*z + rnorm(1000)

## Perform a linear regression against the two

## simulated series in order to assess the hedge ratio

comb <- lm(p~q)

## FINANCIAL DATA - EWA/EWC

## Obtain EWA and EWC for dates corresponding to Chan (2013)

getSymbols("EWA", from="2006-04-26", to="2012-04-09")

getSymbols("EWC", from="2006-04-26", to="2012-04-09")

## Utilise the backwards-adjusted closing prices

ewaAdj = unclass(EWA$EWA.Adjusted)

ewcAdj = unclass(EWC$EWC.Adjusted)

## Plot the ETF backward-adjusted closing prices

plot(ewaAdj, type="l", xlim=c(0, 1500), ylim=c(5.0, 35.0),

xlab="April 26th 2006 to April 9th 2012",

ylab="ETF Backward-Adjusted Price in USD", col="blue")

par(new=T)

plot(ewcAdj, type="l", xlim=c(0, 1500), ylim=c(5.0, 35.0),

axes=F, xlab="", ylab="", col="red")

par(new=F)

## Plot a scatter graph of the ETF adjusted prices

plot(ewaAdj, ewcAdj, xlab="EWA Backward-Adjusted Prices",

ylab="EWC Backward-Adjusted Prices")

## Carry out linear regressions twice, swapping the dependent

## and independent variables each time, with zero drift

comb1 = lm(ewcAdj~ewaAdj)

comb2 = lm(ewaAdj~ewcAdj)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!