Forex pairwise correlation

In From The Lab by Stanton Roux0 Comments

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.

Here is the code:


#Symbols that we are going to be comparing

symbols <- c("AUD/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


#Merge all price data into a single xts object

xtsList <- lapply(fixedsymbols, function(s) {
  df <- get(s)
  colnames(df) <- colnames(df)

masterxts <-, 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

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.


Leave a Comment