Grails Finance 0.4

This entry is part of 15 in the series Grails Finance

Approved by the League of Agile Methodology Experts

Like, let me go ahead and confirm the results of Grails Finance 0.3. In statistics there is the notion of correlation. In the case of a non normal distribution, it seems most appropriate to calculate the Spearmans correlation coefficient. The coefficient varies between -1 and 1. 0 corresponds to independence, the closer the coefficient is to -1 or 1 the more correlation there is. I will use the Apache Commons Math API to determine the correlation.

Dependencies

The whole world uses Maven to manage dependencies with the principle of convention over configuration. Grails also uses this principle, but with a different convention 🙂 and less XML. If you really want to, you can integrate Grails and Maven using a Maven archetype or special plugins (see Grails configuration). Instead I chose to tweak BuildConfig.groovy – the repositories and dependencies blocks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
   repositories {
        grailsPlugins()
        grailsHome()
        grailsCentral()
 
        // uncomment the below to enable remote dependency resolution
        // from public Maven repositories
        mavenLocal()
        mavenCentral()
        mavenRepo "http://snapshots.repository.codehaus.org"
        mavenRepo "http://repository.codehaus.org"
        mavenRepo "http://download.java.net/maven/2/"
        mavenRepo "http://repository.jboss.com/maven2/"
    }
    dependencies {
        // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
		runtime 'org.apache.commons:commons-math:2.1'
        // runtime 'mysql:mysql-connector-java:5.1.5'
    }

You can test the dependencies with

1
grails dependency-report

Controller

I added some logic in the controller to calculate correlations of a specified instrument pair for OHLC and volume.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
   import org.apache.commons.math.stat.correlation.SpearmansCorrelation
 
   ...
 
   def correlation = {
	   if(params.select1 != null) { 
		   return [instruments : getGoogleInstruments(),
			   open: correlate(params.select1, params.select2, 'Open'),
			   high: correlate(params.select1, params.select2, 'High'),
			   low: correlate(params.select1, params.select2, 'Low'),
			   close: correlate(params.select1, params.select2, 'Close'),
			   volume: correlate(params.select1, params.select2, 'Volume')
	   		]
	   } else {
         return [instruments : getGoogleInstruments()]
	   }
   }
 
   def correlate(symbol1, symbol2, fieldName) {
	       def instrument = Instrument.findBySymbol(symbol1)
	       def instrument2 = Instrument.findBySymbol(symbol2)
		   def field = Field.findByName(fieldName)
		   def firstData = getDataForInstrument(instrument, field)
		   def secondData = getDataForInstrument(instrument2, field)
 
			 def data1 = []
			 def data2 = []
 
			 for(def added : firstData?.keySet()){
				if( secondData?.keySet()?.contains(added)) {
					data1 << Double.valueOf(firstData?.get(added))
					data2 << Double.valueOf(secondData?.get(added))
				}
		 }
 
		   def corr = new SpearmansCorrelation().correlation((double[])data1.toArray(), 
			   (double[])data2.toArray())
   }

View

The view displays a correlation table for an instrument pair.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<html>
   <head>
      <title>Correlation Historical data</title>
   </head>
   <body>
      <table>
         <tr>
            <g:form>
            <td>
            		<g:select name="select1" from="${instruments.symbol}" value="${params.select1}"/>
            </td> 
            <td>
            		<g:select name="select2" from="${instruments.symbol}" value="${params.select2}"/>
            	<td>
            <td>
               <g:actionSubmit name="correlation" action="correlation" value="Correlate"/>
            </td>
            </g:form></td>
         </tr>
      </table> 
 
     <br> 
     <br> 
 
      <g:if test="${open != null}">
      	<table border="1">
      		<tr>
      			<td>Open</td>
      			<td>${open}</td>
      		</tr>
      		<tr>
      			<td>High</td>
      			<td>${high}</td>
      		</tr>
      		<tr>
      			<td>Low</td>
      			<td>${low}</td>
      		<tr>
      			<td>Close</td>
      			<td>${close}</td>
      		</tr>
      		<tr>
      			<td>Volume</td>
      			<td>${volume}</td>
      		</tr>
      	</table>
      </g:if>
   </body>
</html>

Result

The results confirm the expectations. DIA and SPY prices have strong correlation. The correlation with GLD is weaker. Also volumes have consistently lower correlations, but still higher than 0.

Photobucket
Photobucket
Photobucket

Conclusion

The results lead me to believe, that I have found some dependencies, whether they are real remains to be seen. One way to check that, is to do some backtesting. For that I will need to build a simulation engine, which will require a complete application redesign and refactoring. You might be wondering what the plans exactly are. As LAME practicioners tend to say, there are no plans, but certainly there are lots of hopes and dreams.

Random links of interest

And now for something completely different – a list of totally unrelated links of awesome importance and content. Probably. Anyway, if you have a link of relevance, that you want to add, feel free to share it in the comments.

Series Navigation
By the author of NumPy Beginner's Guide, NumPy Cookbook and Instant Pygame. If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
Share
This entry was posted in programming and tagged , , . Bookmark the permalink.