R Correlation matrix for trading products - Matlab, R project and Python | futures io social day trading
futures io futures trading


R Correlation matrix for trading products
Updated: Views / Replies:1,687 / 4
Created: by Big Mike Attachments:1

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 
Thread Tools Search this Thread
 

R Correlation matrix for trading products

  #1 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,218 received

R Correlation matrix for trading products

Here is some code I wrote to generate a correlation matrix and plot for a list of symbols. In my example, I am pulling the data out of my database, but you could also use the quantmod library and getSymbols() function to get the data from a public source like Yahoo if you need to.

First thing I do is pull a meta data into a data frame:

> allTickers <- getAllMeta()

This is going to return 5,000+ tickers from my database, so next I just isolate the set I want:

> myTickers <- subset(allTickers,AvgDailyVolume>20000000)
> nrow(myTickers)
[1] 32

This gives us a subset (32 values) of just the tickers where the avg daily volume is >20M. Here is a random sample of that data:

 
Code
> myTickers[sample(nrow(myTickers), 5), ]
     Symbol       Date  Close AvgDailyVolume AvgDailyRange
1768     GM 2013-11-25  37.55       23496904       0.62200
1462    EWJ 2013-11-25  12.07       29128785       0.06497
2914    NOK 2013-11-25   8.10       30859016       0.14528
4590   ZNGA 2013-11-25   4.50       26274119       0.15775
3836    SPY 2013-11-25 180.63      104524325       1.08268
Now we run it through the bulkCorrelation() function, passing myTickers$Symbol as the symbol name list, and I'll specify 120 as the number of days I want in the series, and that I want to run this against my "daily" database of OHLCV data.

> system.time(mx <- bulkCorrelation(myTickers$Symbol,nbrdays=120,dbname="daily"))
user system elapsed
1.968 0.016 2.023

Not terrible, 2 seconds for 32 tickers, pulling 120 days of data from my database, calculating the rate of change and then running the correlation analysis. The result set is a matrix, in this case a 32x32. Here is what the matrix looks like:

Please register on futures.io to view futures trading content such as post attachment(s), image(s), and screenshot(s).


We can then plot this as a heatmap, which gives us:

> plotCorrelationMatrix(mx)

Please register on futures.io to view futures trading content such as post attachment(s), image(s), and screenshot(s).


The scale is from -1 (perfect negative correlation) to +1 (perfect positive correlation). The sweet spot is 0, no correlation.

If you run this against a large number of instruments the heatmap isn't too useful, so instead we can rely on the function getCorrelationMatrixMins() to give us the minimum correlation value for each pair, as follows:

> mxmin <- getCorrelationMatrixMins(mx)

Please register on futures.io to view futures trading content such as post attachment(s), image(s), and screenshot(s).


Now we have a list that shows us the best match for no correlation for each symbol.

Here are some other examples:

> mx <- bulkCorrelation(c("SPY","USO","@DX#","GLD","SLV","XLF","JNK"))

Please register on futures.io to view futures trading content such as post attachment(s), image(s), and screenshot(s).


> plotCorrelationMatrix(mx)

Please register on futures.io to view futures trading content such as post attachment(s), image(s), and screenshot(s).


> mxmin <- getCorrelationMatrixMins(mx)

Please register on futures.io to view futures trading content such as post attachment(s), image(s), and screenshot(s).


Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Attached Files
Register to download File Type: r R-CorrelationMatrixTrading.R (1.8 KB, 49 views)
Reply With Quote
The following 10 users say Thank You to Big Mike for this post:
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,218 received


The next step is to coordinate this list with NAICS codes, so that I can do a sector comparison.

I have already imported the NAICS codes into my database. I need to think about how to setup the fields, my first thought would be to group tickers by NAICS then do some sort of group comparison.

Let me know if you have thoughts.

Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote
The following 2 users say Thank You to Big Mike for this post:
 
  #4 (permalink)
Independent News Watchman
New York, New York
 
Futures Experience: Intermediate
Platform: MetaTrader
Favorite Futures: ES
 
Trader2016's Avatar
 
Posts: 141 since Feb 2016
Thanks: 20 given, 40 received

Following this is like looking into a big room with everything happening, but it is visually digitally scrambled to my eyes in there sort of the way that channels used to be on the TV when the station would barely tune in. I have a long ways to go before I understand this.

Reply With Quote
 
  #5 (permalink)
 Vendor: diversifyportfolio.com 
PTA, Gauteng
 
Futures Experience: Advanced
Platform: Self built + Sierra + TWS
Favorite Futures: Stocks and Options
 
DarkPoolTrading's Avatar
 
Posts: 1,036 since May 2012
Thanks: 1,244 given, 1,304 received


Trader2016 View Post
Following this is like looking into a big room with everything happening, but it is visually digitally scrambled to my eyes in there sort of the way that channels used to be on the TV when the station would barely tune in. I have a long ways to go before I understand this.

If it's the concept of correlation and how to use it in your trading that you're unsure of, here is a futures.io webinar that explains it: Diversification: Lower Risk, Smoother Returns

If it's the R code you're unsure of or if you're new to programming, you might want to rather consider Python. Python is great for analyzing time series data and continues to grow in popularity for finance (and other applications). Personally my eyes glaze over when looking at R code.

Diversification is the only free lunch
Reply With Quote
The following user says Thank You to DarkPoolTrading for this post:

Reply



futures io > > > > R Correlation matrix for trading products

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Linda Bradford Raschke: Reading The Tape

Elite only

Adam Grimes: TBA

Elite only

NinjaTrader: TBA

January

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
correlation matrix in C# for use in NT? drolles NinjaTrader 3 November 26th, 2013 08:31 PM
I like my trading products the way I like my women... addchild Elite Trading Journals 163 February 26th, 2013 08:12 PM
Inverse correlation between school education and trading? YJ78 Traders Hideout 30 December 12th, 2012 04:01 PM
Correlation trading multiple instruments Insearch NinjaTrader 5 July 20th, 2010 01:30 AM
The Matrix - Trinity Help! Big Mike Off-Topic 0 November 26th, 2009 12:53 AM


All times are GMT -4. The time now is 06:46 AM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts
Page generated 2017-12-12 in 0.09 seconds with 20 queries on phoenix via your IP 54.145.51.250