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.

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/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  