While investigating some issues around a hedging EA, it has become apparent that I want a better, statistically coherent way of calculating correlation. Without a powerful statistical package and using MT, the results can be erroneous and not trustable.
Therefore, I started doing research on how it can be calculated outside of metatrader, with the aim of integrating this outside code to work within metatrader. Thanks to the python to MT interface by 7bit, we can do just that. Some may wonder, since I already have the MT to R interface, wy not use R. The answer is I've found a very simple way to do it in python instead that appears to be just as capable of measuring true correlation as a pro stats package like R or matlab.
I present sample code from a book I'm reading about NumPy, that statistical add on library for python. This example uses data from a spreadsheet, this can easily be altered to pull data directly from metatrader. First, I want to get this code working, there seems to be a problem with the publisher supplied code. Anyway, here it is. The code does a correlation analysis on returns from 2 stocks. I am pretty sure this can just as easily be applied to fx pairs in the same way.
If anyone sees a problem with the code, let me know. I will be posting more information from the book I'm reading, then work on how we can send the result back to metatrader using the custom interface.
Any help/guidance welcomed. Thanks.
For information on the Metatrader/python interface, see here: http://www.forexfactory.com/showthread.php?t=190534
Therefore, I started doing research on how it can be calculated outside of metatrader, with the aim of integrating this outside code to work within metatrader. Thanks to the python to MT interface by 7bit, we can do just that. Some may wonder, since I already have the MT to R interface, wy not use R. The answer is I've found a very simple way to do it in python instead that appears to be just as capable of measuring true correlation as a pro stats package like R or matlab.
I present sample code from a book I'm reading about NumPy, that statistical add on library for python. This example uses data from a spreadsheet, this can easily be altered to pull data directly from metatrader. First, I want to get this code working, there seems to be a problem with the publisher supplied code. Anyway, here it is. The code does a correlation analysis on returns from 2 stocks. I am pretty sure this can just as easily be applied to fx pairs in the same way.
If anyone sees a problem with the code, let me know. I will be posting more information from the book I'm reading, then work on how we can send the result back to metatrader using the custom interface.
Any help/guidance welcomed. Thanks.
Inserted Code
#!/usr/bin/python import numpy from pylab import * bhp = numpy.loadtxt('BHP.csv', delimiter=',', usecols=(6,), unpack=True) bhp_returns = numpy.diff(bhp) / bhp[ : -1] vale = numpy.loadtxt('VALE.csv', delimiter=',', usecols=(6,), unpack=True) vale_returns = numpy.diff(vale) / vale[ : -1] covariance = numpy.cov(bhp_returns, vale_returns) print "Covariance",covariance() print "Covariance diagonal", covariance.diagonal() print "Covariance trace", covariance.trace() print covariance/ (bhp_returns.std() * vale_returns.std()) print "Correlation coefficient", numpy.corrcoef(bhp_returns, vale_returns) difference = bhp - vale avg = numpy.mean(difference) dev = numpy.std(difference) print "Out of sync", numpy.abs(difference[-1] - avg) > 2 * dev t = numpy.arange(len(bhp_returns)) plot(t, bhp_returns, lw=1) plot(t, vale_returns, lw=2) show()
For information on the Metatrader/python interface, see here: http://www.forexfactory.com/showthread.php?t=190534
Skype: heliosphan187