In today’s post, I will be showing you how to do Forex pairwise correlation using quantmod and R.

The purpose of the post is not to explain how to trade correlations or the purpose of correlations but to show you how quickly one can get correlations in r using the cor() function.

In a nutshell, correlations work on a scale of -1 to +1. If the correlation is 1, then the pair is perfectly correlated and will move 100% in the same direction. If the correlation is -1, then the movement of the pair will be completely random.

Should you wish to learn more about correlation and how to trade it check out the following article.

http://www.investopedia.com/articles/forex/05/051905.asp

Here is the code:

require(quantmod) #Symbols that we are going to be comparing symbols <- c("AUD/JPY", "AUD/USD", "CHF/JPY", "EUR/CHF", "EUR/GBP", "EUR/JPY", "EUR/USD", "GBP/CHF", "GBP/JPY", "GBP/USD", "NZD/USD", "USD/CAD", "USD/CHF", "USD/JPY" ) #remove the forward slash so we can create a single xts object of all the prices fixedsymbols <- lapply(symbols, function(x) { gsub("/", "", x) }) #use quantmod to get prices from Oanda getSymbols(symbols,src="oanda") #Merge all price data into a single xts object xtsList <- lapply(fixedsymbols, function(s) { df <- get(s) colnames(df) <- colnames(df) return(df) }) masterxts <- do.call(merge, xtsList) # Convert prices to returns returns <- apply(masterxts, 2, function(x) diff(log(x))) # Plot all the pairwise relationships pairs(returns, main = "Pairwise return scatter plot") # Compute the pairwise correlations correlation_matrix <- cor(returns, use = "complete.obs")

Here is the output

Click her to download a PDF of the output

matrixpdf

As you can see R is extremely powerful when it come to performing complex calculations.

Also demonstrated in this post is the use of quantmod's GetSymbols() function to return the Forex data.

Note: Using the GetSymbols() function when the source is Oanda only returns the price. Should you wish to get the OHLC data please refer to the following post.

http://conceptlab.co.za/2017/01/04/getting-intraday-and-daily-forex-data/

Thanks