**Milk Trader**, and kindly contributed to R-bloggers)

Every trader that looks at more than one market throughout the day will recognize that there is a certain symmetrical relationship between certain markets at certain times. The confounding thing about these intermarket relationships is that they are fleeting. Sometimes the Euro dances with the S&P 500, and sometimes Gold butts in. Standing in line to get their chance are Crude, US Bonds or the Yen. Statisticians dub this phenomenon correlation. And they’ve even gone to the trouble of representing it as a mathematical formula. Lucky for us that we don’t need to derive it every time we wish to see if there is something there or if it’s just our imagination.

Thinking it may be helpful to get a graphical representation about intermarket relationships, I decided to pre-process some data in Excel, save the file as a csv, and then run in through R to draw up some graphs. The basic idea is to match up the closing prices of two markets, calculate their Pearson correlation based on a 30-day lookback, and then plot the number. I did this first with two markets that I know must be correlated, the SPX and OEX. They must be correlated because the SPX (top 500 US companies) is essentially the OEX (top 100 US companies) plus 400. First I find at what date we can match these two datasets up with and I came up with 1984. Then I utilized the =Pearson (array1,array2) Excel function to create a vector that holds this data. Then it’s a simple plotting exercise in R after reading in the file. The R code is so simple, here it is:

################### Call packages

#### non required, just use the base

################### Read Data

SPX <- read.csv (“C:/R/TESTDATA/SPX30Y.csv”, header = TRUE, sep =”,”)

tail (SPX)

################## Define Vectors

COR <- SPX$X

################# Plot the sucker, with nice titles of course

hist(COR, main =”SPX/US Bond Correlation from 1977 to 2010″, xlab=”Pearson Correlation (30) at the End of Day”)

Some quick notes. I print the tail of the file with the tail (SPX) command because I did not label the correlation vector in the Excel file, so I need to know what R decided to call it so I could create an independent variable for manipulation purposes. In any case, behold below what we find.

Good, this is what we expected. The correlation is high most days during the years of data we examined. Now let’s turn our attention to the correlation between the S&P 500 and the famed OEX put/call ratio. This is a metric that has been touted in the past as having some predictive value, so let’s take a look-see.

**leave a comment**for the author, please follow the link and comment on their blog:

**Milk Trader**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...