<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ivan Idris Blog</title>
	<atom:link href="http://ivanidris.net/wordpress/index.php/feed" rel="self" type="application/rss+xml" />
	<link>http://ivanidris.net/wordpress</link>
	<description>An adventure in Computology</description>
	<lastBuildDate>Sun, 05 Sep 2010 19:40:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Can we predict stock prices with Grails Finance?</title>
		<link>http://ivanidris.net/wordpress/index.php/2010/09/05/can-we-predict-stock-prices-with-grails-finance</link>
		<comments>http://ivanidris.net/wordpress/index.php/2010/09/05/can-we-predict-stock-prices-with-grails-finance#comments</comments>
		<pubDate>Sun, 05 Sep 2010 19:32:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://ivanidris.net/wordpress/index.php/2010/09/05/can-we-predict-stock-prices-with-grails-finance</guid>
		<description><![CDATA[Grails Finance 0.8 Yes, we can. Not always correctly, of course. One way to predict things, is to try to find the probability distribution for a phenomenon. Usually a distribution is charted as a histogram, so that is what I did. I have a lot of ground to cover today, so I will be skipping [...]]]></description>
			<content:encoded><![CDATA[<p><i>Grails Finance 0.8</i></p>
<p>Yes, we can. Not always correctly, of course. One way to predict things, is to try to find the probability distribution for a phenomenon. Usually a distribution is charted as a <a href="http://en.wikipedia.org/wiki/Histogram">histogram</a>, so that is what I did. I have a lot of ground to cover today, so I will be skipping some details here and there.</p>
<h2>Services</h2>
<p>I went a bit overboard with all kinds of Grails services. These are the services I made</p>
<ul>
<li>HistoricalQueryService &#8211; retrieves historical data from the database.</li>
<li>FrequenceService calculates frequencies.</li>
<li>SubtractService &#8211; subtracts vectors and calculates deltas (current &#8211; previous day).</li>
<li>LnService &#8211; calculates ln var and ln var &#8211; ln var previous day.</li>
</ul>
<h3>Historical query service</h3>
<p>The job of the historical query service is to get from the database historical data based on a symbol or symbols and a field name. For instance, <b>DIA</b> and <b>Close</b>. When two symbols are specified the service matches the data of the two instruments based on the <b>added</b> timestamp and subtracts the data. Here is the code</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">    ...
    <span style="color: #000000; font-weight: bold;">def</span> queryVals<span style="color: #66cc66;">&#40;</span>symbol,fieldName<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
       <span style="color: #000000; font-weight: bold;">def</span> instrument <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Instrument</span>.<span style="color: #006600;">findBySymbol</span><span style="color: #66cc66;">&#40;</span>symbol<span style="color: #66cc66;">&#41;</span>
       <span style="color: #000000; font-weight: bold;">def</span> field <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Field</span>.<span style="color: #006600;">findByName</span><span style="color: #66cc66;">&#40;</span>fieldName<span style="color: #66cc66;">&#41;</span>
       <span style="color: #000000; font-weight: bold;">def</span> fieldVals <span style="color: #66cc66;">=</span> FieldValue.<span style="color: #006600;">findAllByInstrumentAndField</span><span style="color: #66cc66;">&#40;</span>instrument, field<span style="color: #66cc66;">&#41;</span>
       <span style="color: #000000; font-weight: bold;">def</span> data <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
      fieldVals.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span>  data <span style="color: #66cc66;">&lt;&lt;</span> <span style="color: #aaaadd; font-weight: bold;">Double</span>.<span style="color: #006600;">valueOf</span><span style="color: #66cc66;">&#40;</span> it.<span style="color: #006600;">val</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">return</span> data
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> queryPairVals<span style="color: #66cc66;">&#40;</span>symbol, symbol2, fieldName<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> instrument <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Instrument</span>.<span style="color: #006600;">findBySymbol</span><span style="color: #66cc66;">&#40;</span>symbol<span style="color: #66cc66;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">def</span> instrument2 <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Instrument</span>.<span style="color: #006600;">findBySymbol</span><span style="color: #66cc66;">&#40;</span>symbol2<span style="color: #66cc66;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">def</span> field <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Field</span>.<span style="color: #006600;">findByName</span><span style="color: #66cc66;">&#40;</span>fieldName<span style="color: #66cc66;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">def</span> fieldVals <span style="color: #66cc66;">=</span> FieldValue.<span style="color: #006600;">findAllByInstrumentAndField</span><span style="color: #66cc66;">&#40;</span>instrument, field<span style="color: #66cc66;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">def</span> fieldVals2 <span style="color: #66cc66;">=</span> FieldValue.<span style="color: #006600;">findAllByInstrumentAndField</span><span style="color: #66cc66;">&#40;</span>instrument2, field<span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> data <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
   <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">;</span> i <span style="color: #66cc66;">&lt;</span> fieldVals.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;&amp;</span> i <span style="color: #66cc66;">&lt;</span> fieldVals2.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span> i<span style="color: #66cc66;">++</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> sameDateVal <span style="color: #66cc66;">=</span> fieldVals2.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> 
   <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>fieldVals<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">added</span> <span style="color: #66cc66;">==</span> it.<span style="color: #006600;">added</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> it.<span style="color: #006600;">val</span>
            <span style="color: #66cc66;">&#125;</span>
         <span style="color: #66cc66;">&#125;</span>
&nbsp;
         <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>sameDateVal <span style="color: #66cc66;">==</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">continue</span>   
&nbsp;
         data <span style="color: #66cc66;">&lt;&lt;</span> <span style="color: #aaaadd; font-weight: bold;">Double</span>.<span style="color: #006600;">valueOf</span><span style="color: #66cc66;">&#40;</span>fieldVals<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">val</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #aaaadd; font-weight: bold;">Double</span>.<span style="color: #006600;">valueOf</span><span style="color: #66cc66;">&#40;</span>sameDateVal<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">val</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">return</span> data
    <span style="color: #66cc66;">&#125;</span>
    ...</pre></td></tr></table></div>

<p>and here is the test code</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
50
51
52
53
54
55
56
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">...
    <span style="color: #993333;">void</span> testQueryVals<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> type <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> InstrumentType<span style="color: #66cc66;">&#40;</span>type:<span style="color: #ff0000;">&quot;type&quot;</span>, description:<span style="color: #ff0000;">&quot;desc&quot;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull <span style="color: #ff0000;">&quot;Type should not be null&quot;</span>, type.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">def</span> ds <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Datasource<span style="color: #66cc66;">&#40;</span>name:<span style="color: #ff0000;">&quot;DASOURCE&quot;</span>, description: <span style="color: #ff0000;">&quot;desc&quot;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull <span style="color: #ff0000;">&quot;DS should not be null&quot;</span>, ds.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">def</span> ins <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Instrument</span><span style="color: #66cc66;">&#40;</span>name: <span style="color: #ff0000;">&quot;TEST&quot;</span>, symbol: <span style="color: #ff0000;">&quot;TEST&quot;</span>, 
         source: ds, instrumentType : type<span style="color: #66cc66;">&#41;</span>
&nbsp;
      assertNotNull <span style="color: #ff0000;">&quot;Instrument should not be null&quot;</span>,ins.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">def</span> f <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Field</span><span style="color: #66cc66;">&#40;</span>name:<span style="color: #ff0000;">&quot;TOAST&quot;</span>, description: <span style="color: #ff0000;">&quot;desc&quot;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull <span style="color: #ff0000;">&quot;f should not be null&quot;</span>, f.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">def</span> v <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FieldValue<span style="color: #66cc66;">&#40;</span>instrument:ins, field:f,
         added: <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, val: <span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull <span style="color: #ff0000;">&quot;V should not be null&quot;</span>, v.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">def</span> vals <span style="color: #66cc66;">=</span> historicalQueryService.<span style="color: #006600;">queryVals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;TEST&quot;</span>, <span style="color: #ff0000;">&quot;TOAST&quot;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull <span style="color: #ff0000;">&quot;Vals should not be null&quot;</span>,vals
      assertEquals <span style="color: #ff0000;">&quot;There should be 1 value&quot;</span>, vals.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> , <span style="color: #cc66cc;">1</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #993333;">void</span> testQueryPairVals<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> type <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> InstrumentType<span style="color: #66cc66;">&#40;</span>type:<span style="color: #ff0000;">&quot;type&quot;</span>, description:<span style="color: #ff0000;">&quot;desc&quot;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull <span style="color: #ff0000;">&quot;Type should not be null&quot;</span>, type.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">def</span> ds <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Datasource<span style="color: #66cc66;">&#40;</span>name:<span style="color: #ff0000;">&quot;DASOURCE&quot;</span>, description: <span style="color: #ff0000;">&quot;desc&quot;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull <span style="color: #ff0000;">&quot;DS should not be null&quot;</span>, ds.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">def</span> ins <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Instrument</span><span style="color: #66cc66;">&#40;</span>name: <span style="color: #ff0000;">&quot;TEST&quot;</span>, symbol: <span style="color: #ff0000;">&quot;TEST&quot;</span>, 
         source: ds, instrumentType : type<span style="color: #66cc66;">&#41;</span>
      <span style="color: #000000; font-weight: bold;">def</span> ins2 <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Instrument</span><span style="color: #66cc66;">&#40;</span>name: <span style="color: #ff0000;">&quot;TEST2&quot;</span>, symbol: <span style="color: #ff0000;">&quot;TEST2&quot;</span>, 
         source: ds, instrumentType : type<span style="color: #66cc66;">&#41;</span>
&nbsp;
      assertNotNull <span style="color: #ff0000;">&quot;Instrument should not be null&quot;</span>,ins.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull <span style="color: #ff0000;">&quot;Instrument should not be null&quot;</span>,ins2.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">def</span> f <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Field</span><span style="color: #66cc66;">&#40;</span>name:<span style="color: #ff0000;">&quot;TOAST&quot;</span>, description: <span style="color: #ff0000;">&quot;desc&quot;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull <span style="color: #ff0000;">&quot;f should not be null&quot;</span>, f.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">def</span> now <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>   
      <span style="color: #000000; font-weight: bold;">def</span> v <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FieldValue<span style="color: #66cc66;">&#40;</span>instrument:ins, field:f,
         added: now, val: <span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #000000; font-weight: bold;">def</span> v2 <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FieldValue<span style="color: #66cc66;">&#40;</span>instrument:ins2, field:f,
         added: now, val: <span style="color: #ff0000;">&quot;3.0&quot;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull <span style="color: #ff0000;">&quot;V should not be null&quot;</span>, v.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull <span style="color: #ff0000;">&quot;V2 should not be null&quot;</span>, v2.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">def</span> vals <span style="color: #66cc66;">=</span> historicalQueryService.<span style="color: #006600;">queryPairVals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;TEST&quot;</span>, <span style="color: #ff0000;">&quot;TEST2&quot;</span>, <span style="color: #ff0000;">&quot;TOAST&quot;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull <span style="color: #ff0000;">&quot;Vals should not be null&quot;</span>,vals
      assertEquals <span style="color: #ff0000;">&quot;There should be 1 value&quot;</span>, vals.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> , <span style="color: #cc66cc;">1</span>
   <span style="color: #66cc66;">&#125;</span>
...</pre></td></tr></table></div>

<h3>Frequence service</h3>
<p>The frequence service defines a number of bins and counts the number of occurrences in each bin. I defined a Bin class as follows</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">...
   <span style="color: #993333;">double</span> <span style="color: #993399;">start</span>
   <span style="color: #993333;">double</span> end
   <span style="color: #993333;">double</span> mid
   <span style="color: #aaaadd; font-weight: bold;">String</span> label
&nbsp;
   Bin<span style="color: #66cc66;">&#40;</span>s, e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #993399;">start</span> <span style="color: #66cc66;">=</span> s
      end <span style="color: #66cc66;">=</span> e
      mid <span style="color: #66cc66;">=</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #993399;">start</span> <span style="color: #66cc66;">+</span> end<span style="color: #66cc66;">&#41;</span> / <span style="color: #cc66cc;">2</span> 
      label <span style="color: #66cc66;">=</span> sprintf<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'%.3g'</span>, mid<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#125;</span>
...</pre></td></tr></table></div>

<p>The service code is shown below. First I  calculate the size of bin, then I make a list of Bin objects. After that I check in which bin each input value falls and increase the count for the lucky bin. There is also a convenience method that reduces the x labels by a factor of million &#8211; this is handy for volumes.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
50
51
52
53
54
55
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">...
    <span style="color: #000000; font-weight: bold;">def</span> frequence<span style="color: #66cc66;">&#40;</span>input<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> <span style="color: #993399;">step</span> <span style="color: #66cc66;">=</span> calcStep<span style="color: #66cc66;">&#40;</span>input<span style="color: #66cc66;">&#41;</span>
      <span style="color: #000000; font-weight: bold;">def</span> bins <span style="color: #66cc66;">=</span> makeBins<span style="color: #66cc66;">&#40;</span>input.<span style="color: #663399;">min</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #993399;">step</span><span style="color: #66cc66;">&#41;</span> 
      <span style="color: #000000; font-weight: bold;">def</span> frequency <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>:<span style="color: #66cc66;">&#93;</span>
&nbsp;
      <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span> <span style="color: #993333;">int</span> i <span style="color: #b1b100;">in</span> 0..<span style="color: #66cc66;">&lt;</span>NBINS<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         frequency<span style="color: #66cc66;">&#91;</span> bins<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">label</span> <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">def</span> <span style="color: #663399;">size</span> <span style="color: #66cc66;">=</span> input.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      input.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> 
         <span style="color: #000000; font-weight: bold;">def</span> index <span style="color: #66cc66;">=</span> calcIndex<span style="color: #66cc66;">&#40;</span>it, bins<span style="color: #66cc66;">&#41;</span>
         frequency<span style="color: #66cc66;">&#91;</span> bins<span style="color: #66cc66;">&#91;</span>index<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">label</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">++</span>
      <span style="color: #66cc66;">&#125;</span>   
&nbsp;
      <span style="color: #000000; font-weight: bold;">return</span> frequency
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">def</span> makeBins<span style="color: #66cc66;">&#40;</span><span style="color: #663399;">min</span>, <span style="color: #993399;">step</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> bins <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
      <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span> <span style="color: #993333;">int</span> i <span style="color: #b1b100;">in</span> 0..<span style="color: #66cc66;">&lt;</span>NBINS<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         bins <span style="color: #66cc66;">&lt;&lt;</span> <span style="color: #000000; font-weight: bold;">new</span> Bin<span style="color: #66cc66;">&#40;</span><span style="color: #663399;">min</span> <span style="color: #66cc66;">+</span> i <span style="color: #66cc66;">*</span> <span style="color: #993399;">step</span>, <span style="color: #663399;">min</span> <span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span>i <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #993399;">step</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">return</span> bins
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">def</span> reduceKeys<span style="color: #66cc66;">&#40;</span>frequency<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> reduced <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>:<span style="color: #66cc66;">&#93;</span>
&nbsp;
      frequency.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> 
         key,value <span style="color: #66cc66;">-&gt;</span> 
            reduced<span style="color: #66cc66;">&#91;</span><span style="color: #aaaadd; font-weight: bold;">Math</span>.<span style="color: #006600;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Double</span>.<span style="color: #006600;">valueOf</span><span style="color: #66cc66;">&#40;</span>key<span style="color: #66cc66;">&#41;</span>/<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1000</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> value
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">return</span> reduced
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">def</span> calcIndex<span style="color: #66cc66;">&#40;</span>it, bins<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #b1b100;">in</span> 0..<span style="color: #66cc66;">&lt;</span>bins.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>it <span style="color: #66cc66;">&gt;=</span> bins<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #993399;">start</span> <span style="color: #66cc66;">&amp;&amp;</span> it <span style="color: #66cc66;">&lt;</span> bins<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">end</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> i
         <span style="color: #66cc66;">&#125;</span>
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">return</span> bins.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span> 
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">def</span> calcStep<span style="color: #66cc66;">&#40;</span>input<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span>input.<span style="color: #663399;">max</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> input.<span style="color: #663399;">min</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>/ NBINS
   <span style="color: #66cc66;">&#125;</span>
...</pre></td></tr></table></div>

<p>Here is some of the test code</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">...
   <span style="color: #993333;">void</span> testFrequence<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      assertNotNull frequenceService
      <span style="color: #000000; font-weight: bold;">def</span> frequencies <span style="color: #66cc66;">=</span> frequenceService.<span style="color: #006600;">frequence</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1.0</span>, <span style="color: #cc66cc;">1.5</span>, <span style="color: #cc66cc;">32.0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull frequencies
      assertEquals <span style="color: #ff0000;">&quot;Number of bins incorrect&quot;</span>, <span style="color: #cc66cc;">30</span>, frequencies.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>   
&nbsp;
      <span style="color: #000000; font-weight: bold;">def</span> store <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
&nbsp;
      frequencies.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> key,value <span style="color: #66cc66;">-&gt;</span> store <span style="color: #66cc66;">+=</span> value <span style="color: #66cc66;">&#125;</span>
&nbsp;
      assertEquals <span style="color: #cc66cc;">3</span>, store
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #993333;">void</span> testStep<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      assertEquals <span style="color: #cc66cc;">1.0</span>, frequenceService.<span style="color: #006600;">calcStep</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">30.0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#125;</span>
...</pre></td></tr></table></div>

<h3>Subtract service</h3>
<p>The subtract service subtracts vectors and calculates deltas (current &#8211; previous day).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">...
    <span style="color: #000000; font-weight: bold;">def</span> minPrev<span style="color: #66cc66;">&#40;</span>array<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> subtracted <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
      <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #b1b100;">in</span> 1..<span style="color: #66cc66;">&lt;</span>array.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         subtracted <span style="color: #66cc66;">&lt;&lt;</span> array<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">-</span> array<span style="color: #66cc66;">&#91;</span>i <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">return</span> subtracted
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">def</span> <span style="color: #663399;">min</span><span style="color: #66cc66;">&#40;</span>left, right<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> subtracted <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
      <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #b1b100;">in</span> 0..<span style="color: #66cc66;">&lt;</span>array.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         subtracted <span style="color: #66cc66;">&lt;&lt;</span> array<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">-</span> array<span style="color: #66cc66;">&#91;</span>i <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">return</span> subtracted
   <span style="color: #66cc66;">&#125;</span>
...
   <span style="color: #000000; font-weight: bold;">def</span> testMinPrevious<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> subtracted <span style="color: #66cc66;">=</span> subtractService.<span style="color: #006600;">minPrev</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull subtracted
      assertEquals <span style="color: #cc66cc;">2</span>, subtracted.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
      assertEquals <span style="color: #cc66cc;">1</span>, subtracted<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>
      assertEquals <span style="color: #cc66cc;">1</span>, subtracted<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>
   <span style="color: #66cc66;">&#125;</span>
...</pre></td></tr></table></div>

<h3>Ln service</h3>
<p>The Ln service calculates the <a href="http://en.wikipedia.org/wiki/Natural_logarithm">natural logarithm </a>of a variable vector and the difference of the ln of a variable and the ln of the previous variable value. There is a small problem with logarithms. You cannot take the logarithm of 0 or negative numbers. So I cheat a little by substituting negative numbers by their absolute value and avoiding 0.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">...
    <span style="color: #000000; font-weight: bold;">def</span> ln<span style="color: #66cc66;">&#40;</span>numbers<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> result <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
      numbers.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span>
         <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>it <span style="color: #66cc66;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> 
            result <span style="color: #66cc66;">&lt;&lt;</span> <span style="color: #aaaadd; font-weight: bold;">Math</span>.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Math</span>.<span style="color: #006600;">abs</span><span style="color: #66cc66;">&#40;</span>it<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">return</span> result
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> deltaLn<span style="color: #66cc66;">&#40;</span>numbers<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> result <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
      <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #b1b100;">in</span> 1..<span style="color: #66cc66;">&lt;</span>numbers.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>numbers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!=</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&amp;&amp;</span> numbers<span style="color: #66cc66;">&#91;</span>i <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
            result <span style="color: #66cc66;">&lt;&lt;</span> <span style="color: #aaaadd; font-weight: bold;">Math</span>.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Math</span>.<span style="color: #006600;">abs</span><span style="color: #66cc66;">&#40;</span>numbers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #aaaadd; font-weight: bold;">Math</span>.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Math</span>.<span style="color: #006600;">abs</span><span style="color: #66cc66;">&#40;</span>numbers<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">return</span> result
    <span style="color: #66cc66;">&#125;</span>
...</pre></td></tr></table></div>

<p>Some more tests</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">...
    <span style="color: #993333;">void</span> testLn<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> outcome <span style="color: #66cc66;">=</span> lnService.<span style="color: #006600;">ln</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull outcome
      assertEquals  <span style="color: #cc66cc;">2</span>, outcome.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
      assertEquals  <span style="color: #cc66cc;">0</span>, outcome<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #993333;">void</span> testDeltaLn<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> outcome <span style="color: #66cc66;">=</span> lnService.<span style="color: #006600;">deltaLn</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
      assertNotNull outcome
      assertEquals  <span style="color: #cc66cc;">1</span>, outcome.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
      assertEquals  <span style="color: #cc66cc;">0</span>, outcome<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>
   <span style="color: #66cc66;">&#125;</span>
...</pre></td></tr></table></div>

<h2>Controller</h2>
<p>Services on their own don&#8217;t do much, so we need a controller to use the services and communicate with the views.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
50
51
52
53
54
55
56
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">...
   <span style="color: #000000; font-weight: bold;">def</span> calcFrequence<span style="color: #66cc66;">&#40;</span>vals, fieldVar<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> frequence <span style="color: #66cc66;">=</span> frequenceService.<span style="color: #006600;">frequence</span><span style="color: #66cc66;">&#40;</span>vals<span style="color: #66cc66;">&#41;</span>
&nbsp;
      <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">fieldVar</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">'Volume'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         frequence <span style="color: #66cc66;">=</span> frequenceService.<span style="color: #006600;">reduceKeys</span><span style="color: #66cc66;">&#40;</span>frequence<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">return</span> frequence
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">def</span> calcDeltaFrequence<span style="color: #66cc66;">&#40;</span>vals<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> subtracted <span style="color: #66cc66;">=</span> subtractService.<span style="color: #006600;">minPrev</span><span style="color: #66cc66;">&#40;</span>vals<span style="color: #66cc66;">&#41;</span>
      <span style="color: #000000; font-weight: bold;">return</span> frequenceService.<span style="color: #006600;">frequence</span><span style="color: #66cc66;">&#40;</span>subtracted<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">def</span> calcLnFrequency<span style="color: #66cc66;">&#40;</span>vals<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> lned <span style="color: #66cc66;">=</span> lnService.<span style="color: #006600;">ln</span><span style="color: #66cc66;">&#40;</span>vals<span style="color: #66cc66;">&#41;</span>
      <span style="color: #000000; font-weight: bold;">return</span> frequenceService.<span style="color: #006600;">frequence</span><span style="color: #66cc66;">&#40;</span>lned<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">def</span> calcDeltaLnFrequency<span style="color: #66cc66;">&#40;</span>vals<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> deltaLned <span style="color: #66cc66;">=</span> lnService.<span style="color: #006600;">deltaLn</span><span style="color: #66cc66;">&#40;</span>vals<span style="color: #66cc66;">&#41;</span>
      <span style="color: #000000; font-weight: bold;">return</span> frequenceService.<span style="color: #006600;">frequence</span><span style="color: #66cc66;">&#40;</span>deltaLned<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">def</span> makeResponse<span style="color: #66cc66;">&#40;</span>vals<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> frequence <span style="color: #66cc66;">=</span> calcFrequence<span style="color: #66cc66;">&#40;</span>vals, params.<span style="color: #006600;">fieldVar</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">def</span> deltasFrequency <span style="color: #66cc66;">=</span> calcDeltaFrequence<span style="color: #66cc66;">&#40;</span>vals<span style="color: #66cc66;">&#41;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">def</span> lnFrequency <span style="color: #66cc66;">=</span> calcLnFrequency<span style="color: #66cc66;">&#40;</span>vals<span style="color: #66cc66;">&#41;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">def</span> deltaLnFrequency <span style="color: #66cc66;">=</span> calcDeltaLnFrequency<span style="color: #66cc66;">&#40;</span>vals<span style="color: #66cc66;">&#41;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#91;</span>data : frequence, 
         deltaData : deltasFrequency,
         lnData : lnFrequency,
         deltaLnData : deltaLnFrequency<span style="color: #66cc66;">&#93;</span>
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> index <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> 
      <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">fieldVar</span> <span style="color: #66cc66;">!=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">def</span> vals <span style="color: #66cc66;">=</span> historicalQueryService.<span style="color: #006600;">queryVals</span><span style="color: #66cc66;">&#40;</span> params.<span style="color: #006600;">select1</span>, params.<span style="color: #006600;">fieldVar</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
         <span style="color: #000000; font-weight: bold;">return</span> makeResponse<span style="color: #66cc66;">&#40;</span>vals<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#125;</span>
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> pairs <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> 
      <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">fieldVar</span> <span style="color: #66cc66;">!=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">def</span> vals <span style="color: #66cc66;">=</span> historicalQueryService.<span style="color: #006600;">queryPairVals</span><span style="color: #66cc66;">&#40;</span> 
            params.<span style="color: #006600;">select1</span>, params.<span style="color: #006600;">select2</span>, params.<span style="color: #006600;">fieldVar</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
         <span style="color: #000000; font-weight: bold;">return</span> makeResponse<span style="color: #66cc66;">&#40;</span>vals<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#125;</span>
   <span style="color: #66cc66;">&#125;</span>
...</pre></td></tr></table></div>

<h2>Views</h2>
<p>There are 2 views &#8211; the main view for a single symbol and a view for a pair of symbols.</p>
<h3>Main view</h3>
<p>The view consists of a form with 4 histograms beneath it. The form lets you choose a symbol and a field. The histograms are made with the <a href="http://www.grails.org/Google%2BChart%2BPlugin">Google Charts Grails plugin</a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>html<span style="color: #66cc66;">&gt;</span>
   <span style="color: #66cc66;">&lt;</span>head<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>title<span style="color: #66cc66;">&gt;</span>Historical data histogram<span style="color: #66cc66;">&lt;</span>/title<span style="color: #66cc66;">&gt;</span>
   <span style="color: #66cc66;">&lt;</span>/head<span style="color: #66cc66;">&gt;</span>
   <span style="color: #66cc66;">&lt;</span>body<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>table<span style="color: #66cc66;">&gt;</span>
         <span style="color: #66cc66;">&lt;</span>tr<span style="color: #66cc66;">&gt;</span>
            <span style="color: #66cc66;">&lt;</span>g:form<span style="color: #66cc66;">&gt;</span>
            <span style="color: #66cc66;">&lt;</span>td<span style="color: #66cc66;">&gt;</span>
                  <span style="color: #66cc66;">&lt;</span>g:select name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;select1&quot;</span> from<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${['DIA', 'SPY', 'GLD']}&quot;</span> value<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${params.select1}&quot;</span>/<span style="color: #66cc66;">&gt;</span>
            <span style="color: #66cc66;">&lt;</span>/td<span style="color: #66cc66;">&gt;</span> 
            <span style="color: #66cc66;">&lt;</span>td<span style="color: #66cc66;">&gt;</span>
               <span style="color: #66cc66;">&lt;</span>g:select name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;fieldVar&quot;</span> from<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${['Open', 'High', 'Low', 'Close', 'Volume']}&quot;</span> value<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${params.fieldVar}&quot;</span>/<span style="color: #66cc66;">&gt;</span>
            <span style="color: #66cc66;">&lt;</span>/td<span style="color: #66cc66;">&gt;</span>
            <span style="color: #66cc66;">&lt;</span>td<span style="color: #66cc66;">&gt;</span>
               <span style="color: #66cc66;">&lt;</span>g:actionSubmit name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;doChart&quot;</span> action<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;index&quot;</span> value<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Chart&quot;</span>/<span style="color: #66cc66;">&gt;</span>
            <span style="color: #66cc66;">&lt;</span>/td<span style="color: #66cc66;">&gt;</span>
            <span style="color: #66cc66;">&lt;</span>/g:form<span style="color: #66cc66;">&gt;&lt;</span>/td<span style="color: #66cc66;">&gt;</span>
         <span style="color: #66cc66;">&lt;</span>/tr<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>/table<span style="color: #66cc66;">&gt;</span>
&nbsp;
      <span style="color: #66cc66;">&lt;</span>g:<span style="color: #b1b100;">if</span> test<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${data != null}&quot;</span><span style="color: #66cc66;">&gt;</span>
           <span style="color: #66cc66;">&lt;</span>g:barChart type<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bvs&quot;</span>
                title<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Histogram ${params.select1} (${params.fieldVar})&quot;</span>
            <span style="color: #663399;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${[900,200]}&quot;</span> 
            colors<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${['00FF00']}&quot;</span> 
            fill<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${'bg,s,efefef' }&quot;</span>
            axes<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;x,y&quot;</span>
            axesLabels<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${
               [0:data.keySet(),
               1:[0,data.values().max()/2, data.values().max()]
               ]
                }&quot;</span>
            dataType<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;simple&quot;</span>
            data<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${data.values().asList()}&quot;</span>
            /<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>/g:if<span style="color: #66cc66;">&gt;</span>
&nbsp;
      <span style="color: #66cc66;">&lt;</span>br/<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&lt;</span>br/<span style="color: #66cc66;">&gt;</span>
&nbsp;
      <span style="color: #66cc66;">&lt;</span>g:<span style="color: #b1b100;">if</span> test<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${deltaData != null}&quot;</span><span style="color: #66cc66;">&gt;</span>
           <span style="color: #66cc66;">&lt;</span>g:barChart type<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bvs&quot;</span>
                title<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Histogram ${params.select1} (${params.fieldVar} - previous day ${params.fieldVar}) &quot;</span>
            <span style="color: #663399;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${[900,200]}&quot;</span> 
            colors<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${['00FF00']}&quot;</span> 
            fill<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${'bg,s,efefef' }&quot;</span>
            axes<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;x,y&quot;</span>
            axesLabels<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${
               [0:deltaData.keySet(),
               1:[0,deltaData.values().max()/2, deltaData.values().max()]
               ]
                }&quot;</span>
            dataType<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;simple&quot;</span>
            data<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${deltaData.values().asList()}&quot;</span>
            /<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>/g:if<span style="color: #66cc66;">&gt;</span>
&nbsp;
      <span style="color: #66cc66;">&lt;</span>br/<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&lt;</span>br/<span style="color: #66cc66;">&gt;</span>
&nbsp;
      <span style="color: #66cc66;">&lt;</span>g:<span style="color: #b1b100;">if</span> test<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${lnData != null}&quot;</span><span style="color: #66cc66;">&gt;</span>
           <span style="color: #66cc66;">&lt;</span>g:barChart type<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bvs&quot;</span>
                title<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Histogram ${params.select1} (Log ${params.fieldVar}) &quot;</span>
            <span style="color: #663399;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${[900,200]}&quot;</span> 
            colors<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${['00FF00']}&quot;</span> 
            fill<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${'bg,s,efefef' }&quot;</span>
            axes<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;x,y&quot;</span>
            axesLabels<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${
               [0:lnData.keySet(),
               1:[0,lnData.values().max()/2, lnData.values().max()]
               ]
                }&quot;</span>
            dataType<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;simple&quot;</span>
            data<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${lnData.values().asList()}&quot;</span>
            /<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>/g:if<span style="color: #66cc66;">&gt;</span>
&nbsp;
      <span style="color: #66cc66;">&lt;</span>br/<span style="color: #66cc66;">&gt;&lt;</span>br/<span style="color: #66cc66;">&gt;</span>
&nbsp;
      <span style="color: #66cc66;">&lt;</span>g:<span style="color: #b1b100;">if</span> test<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${deltaLnData != null}&quot;</span><span style="color: #66cc66;">&gt;</span>
           <span style="color: #66cc66;">&lt;</span>g:barChart type<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bvs&quot;</span>
                title<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Histogram ${params.select1} (Log ${params.fieldVar} - Log previous day ${params.fieldVar}) &quot;</span>
            <span style="color: #663399;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${[900,200]}&quot;</span> 
            colors<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${['00FF00']}&quot;</span> 
            fill<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${'bg,s,efefef' }&quot;</span>
            axes<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;x,y&quot;</span>
            axesLabels<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${
               [0:deltaLnData.keySet(),
               1:[0,deltaLnData.values().max()/2, deltaLnData.values().max()]
               ]
                }&quot;</span>
            dataType<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;simple&quot;</span>
            data<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${deltaLnData.values().asList()}&quot;</span>
            /<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>/g:if<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>/body<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>/html<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<h3>The pairs view</h3>
<p>The pairs view is almost a copy of the main view. The difference being that in this view you can specify an extra symbol.</p>
<h2>Result</h2>
<p>The result is displayed in the table below for GLD, SPY, DIA and their respective differences. The data comes from historical data of end of day OHLC prices and volume.</p>
<div align="center">
<table border="1">
<tr>
<td></td>
<td>Open</td>
<td>High</td>
<td>Low</td>
<td>Close</td>
<td>Volume</td>
</tr>
<tr>
<td>DIA</td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/DIA/DIA_Open.png" rel="lightbox[DIA]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/DIA/th_DIA_Open.png" border="0" alt="DIA Open" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/DIA/DIA_High.png" rel="lightbox[DIA]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/DIA/th_DIA_High.png" border="0" alt="DIA High" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/DIA/DIA_Low.png" rel="lightbox[DIA]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/DIA/th_DIA_Low.png" border="0" alt="DIA Low" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/DIA/DIA_Close.png" rel="lightbox[DIA]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/DIA/th_DIA_Close.png" border="0" alt="DIA Close" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/DIA/DIA_Volume.png" rel="lightbox[DIA]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/DIA/th_DIA_Volume.png" border="0" alt="DIA Volume" ></a></td>
</tr>
<tr>
<td>SPY</td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/SPY/SPY_Open.png" rel="lightbox[SPY]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/SPY/th_SPY_Open.png" border="0" alt="SPY Open" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/SPY/SPY_High.png" rel="lightbox[SPY]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/SPY/th_SPY_High.png" border="0" alt="SPY High" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/SPY/SPY_Low.png" rel="lightbox[SPY]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/SPY/th_SPY_Low.png" border="0" alt="SPY Low" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/SPY/SPY_Close.png" rel="lightbox[SPY]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/SPY/th_SPY_Close.png" border="0" alt="SPY Close" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/SPY/SPY_Volume.png" rel="lightbox[SPY]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/SPY/th_SPY_Volume.png" border="0" alt="SPY Volume" ></a></td>
</tr>
<tr>
<td>GLD</td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/GLD/GLD_Open.png" rel="lightbox[GLD]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/GLD/th_GLD_Open.png" border="0" alt="GLD Open" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/GLD/GLD_High.png" rel="lightbox[GLD]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/GLD/th_GLD_High.png" border="0" alt="GLD High" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/GLD/GLD_Low.png" rel="lightbox[GLD]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/GLD/th_GLD_Low.png" border="0" alt="GLD Low" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/GLD/GLD_Close.png" rel="lightbox[GLD]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/GLD/th_GLD_Close.png" border="0" alt="GLD Close" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/GLD/GLD_Volume.png" rel="lightbox[GLD]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/GLD/th_GLD_Volume.png" border="0" alt="GLD Volume" ></a></td>
</tr>
<tr>
<td>DIA &#8211; SPY</td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-SPY/DIA-SPY_Open.png" rel="lightbox[DIA-SPY]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-SPY/th_DIA-SPY_Open.png" border="0" alt="DIA-SPY Open" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-SPY/DIA-SPY_High.png" rel="lightbox[DIA-SPY]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-SPY/th_DIA-SPY_High.png" border="0" alt="DIA-SPY High" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-SPY/DIA-SPY_Low.png" rel="lightbox[DIA-SPY]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-SPY/th_DIA-SPY_Low.png" border="0" alt="DIA-SPY Low" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-SPY/DIA-SPY_Close.png" rel="lightbox[DIA-SPY]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-SPY/th_DIA-SPY_Close.png" border="0" alt="DIA-SPY Close" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-SPY/DIA-SPY_Volume.png" rel="lightbox[DIA-SPY]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-SPY/th_DIA-SPY_Volume.png" border="0" alt="DIA-SPY Volume" ></a></td>
</tr>
<tr>
<td>DIA &#8211; GLD</td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-GLD/DIA-GLD_Open.png" rel="lightbox[DIA-GLD]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-GLD/th_DIA-GLD_Open.png" border="0" alt="DIA-GLD Open" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-GLD/DIA-GLD_High.png" rel="lightbox[DIA-GLD]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-GLD/th_DIA-GLD_High.png" border="0" alt="DIA-GLD High" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-GLD/DIA-GLD_Low.png" rel="lightbox[DIA-GLD]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-GLD/th_DIA-GLD_Low.png" border="0" alt="DIA-GLD Low" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-GLD/DIA-GLD_Close.png" rel="lightbox[DIA-GLD]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-GLD/th_DIA-GLD_Close.png" border="0" alt="DIA-GLD Close" ></a></p>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-GLD/DIA-GLD_Volume.png" rel="lightbox[DIA-GLD]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/DIA-GLD/th_DIA-GLD_Volume.png" border="0" alt="DIA-GLD Volume" ></a></td>
</tr>
<tr>
<td>SPY &#8211; GLD</td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/SPY-GLD/SPY-GLD_Open.png" rel="lightbox[SPY-GLD]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/SPY-GLD/th_SPY-GLD_Open.png" border="0" alt="SPY-GLD Open" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/SPY-GLD/SPY-GLD_High.png" rel="lightbox[SPY-GLD]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/SPY-GLD/th_SPY-GLD_High.png" border="0" alt="SPY-GLD High" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/SPY-GLD/SPY-GLD_Low.png" rel="lightbox[SPY-GLD]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/SPY-GLD/th_SPY-GLD_Low.png" border="0" alt="SPY-GLD Low" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/SPY-GLD/SPY-GLD_Close.png" rel="lightbox[SPY-GLD]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/SPY-GLD/th_SPY-GLD_Close.png" border="0" alt="SPY-GLD Close" ></a></p>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails8/SPY-GLD/SPY-GLD_Volume.png" rel="lightbox[SPY-GLD]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails8/SPY-GLD/th_SPY-GLD_Volume.png" border="0" alt="SPY-GLD Volume" ></a></td>
</tr>
</table>
</div>
<h2>Conclusions</h2>
<p>Turns out that the distributions are pretty non random, almost Gaussian. So we may be able to predict stock prices, a little a bit more accurately than you would have expected.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://ivanidris.net/wordpress/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://ivanidris.net/wordpress/index.php/2010/09/05/can-we-predict-stock-prices-with-grails-finance/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grails Finance Services</title>
		<link>http://ivanidris.net/wordpress/index.php/2010/08/29/grails-finance-services</link>
		<comments>http://ivanidris.net/wordpress/index.php/2010/08/29/grails-finance-services#comments</comments>
		<pubDate>Sun, 29 Aug 2010 19:16:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://ivanidris.net/wordpress/index.php/2010/08/29/grails-finance-services</guid>
		<description><![CDATA[Grails Finance 0.7 I should have started with Grails services from the beginning. Services are just another layer in the architecture. Services can easily be injected, wherever you need them. I also ran into some trouble, because of upgrading to a new Grails version. Creating services So I did a Mac Ports upgrade, which included [...]]]></description>
			<content:encoded><![CDATA[<p><i>Grails Finance 0.7</i></p>
<p>I should have started with <a href="http://www.grails.org/Services">Grails services</a> from the beginning. Services are just another layer in the architecture. Services can easily be injected, wherever you need them. I also ran into some trouble, because of upgrading to a new Grails version.</p>
<h3>Creating services</h3>
<p>So I did a Mac Ports upgrade, which included upgrade of Grails to 1.3.4. When I tried to create a new service with</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails create-service simulation</pre></td></tr></table></div>

<p>Grails complained about a version mismatch, so I had to do</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails update</pre></td></tr></table></div>

<p>to fix that. The service</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> SimulationService <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> transactional <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">true</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> create<span style="color: #66cc66;">&#40;</span>name, description<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">def</span> simulation <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Simulation<span style="color: #66cc66;">&#40;</span>name, description<span style="color: #66cc66;">&#41;</span>
	simulation.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">return</span> Simulation.<span style="color: #663399;">get</span><span style="color: #66cc66;">&#40;</span>simulation.<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>can now be injected into the controller for example.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> SimulationController <span style="color: #66cc66;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">def</span> scaffold <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">true</span>
	<span style="color: #000000; font-weight: bold;">def</span> simulationService
&nbsp;
	<span style="color: #000000; font-weight: bold;">def</span> persist<span style="color: #66cc66;">&#40;</span>name, description<span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">def</span> simulation <span style="color: #66cc66;">=</span> simulationService.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>name, description<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h3>Testing services</h3>
<p>Grails created a unit test stub along with the service, but I also wanted an integration test.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails create-integration-test SimulationServiceIntegration</pre></td></tr></table></div>

<p>Gant then suddenly reported a <b>MissingPropertyException</b>. Very mysterious. It turns out this is a known bug/issue <a href="http://jira.codehaus.org/browse/GRAILS-6606">GRAILS-6606</a>. The fix was easy &#8211; adding the line</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> type <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;Tests&quot;</span></pre></td></tr></table></div>

<p>to <b>CreateIntegrationTest.groovy</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">...
<span style="color: #006600;">target</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'default'</span>: <span style="color: #ff0000;">&quot;Creates a new Grails integration test which loads the whole Grails environment when run&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    depends<span style="color: #66cc66;">&#40;</span>checkVersion, parseArguments<span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> type <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;Tests&quot;</span>
    promptForName<span style="color: #66cc66;">&#40;</span>type: <span style="color: #ff0000;">&quot;Integration test&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> name <span style="color: #66cc66;">=</span> argsMap<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;params&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>
    name <span style="color: #66cc66;">=</span> purgeRedundantArtifactSuffix<span style="color: #66cc66;">&#40;</span>name, type<span style="color: #66cc66;">&#41;</span>
    createIntegrationTest<span style="color: #66cc66;">&#40;</span>name: name, suffix: <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The bad news is, that there are bugs/issues in Grails 1.3.4. The good news is that, some of these are easy to fix. Here is an example test</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">   ...
   <span style="color: #000000; font-weight: bold;">def</span> simulationService
&nbsp;
    <span style="color: #993333;">void</span> testCreate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	assertNotNull simulationService
    <span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://en.wikipedia.org/wiki/Dependency_injection">Dependency Injection</a> rules.</p>
<p>You can run all the tests with</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails test-app</pre></td></tr></table></div>

<h3>Side topic: Levy distribution</h3>
<p>It is a well known fact that stock prices etc. do not conform to the <a href="http://en.wikipedia.org/wiki/Normal_distribution">normal distribution</a>. Apparently some people have the idea that the <a href="http://www.wilmottwiki.com/wiki/index.php/Levy_distribution">Levy distribution</a> is more appropriate. One of these people is <a href="http://ozrisk.net/2009/06/08/normal-curves-and-the-levy-distribution/">Mandelbrot</a> of the fractals. Mandelbrot recognizes four schools of thought</p>
<ol>
<li>The most popular, technical analysts school exploiting short term changes.</li>
<li>Random walk, statistically independent prices, but price changes are Gauss distributed.</li>
<li>Another random walk believers school, but price changes have infinite variance.</li>
<li>Random walk is a first order approximation, but there is also a second order!</li>
</ol>
<p>This <a href="http://www.automated-trading-system.com/why-trend-following-works-look-at-the-distribution/">blog</a> explains why the Levy distribution makes trend following profitable. Let the profits run and cut your losses. If you do it correctly, the fat tail of the distribution will make you a net profit.</p>
<h3>Random links of interest</h3>
<ul>
<li><a href="http://www.ibm.com/developerworks/java/library/j-jtp11225/">Plugging memory leaks with weak references</a></li>
<li><a href="http://www.ibm.com/developerworks/java/library/j-5things11/index.html">Command-line flags for the JVM</a></li>
<li><a href="http://finance.quicquid.org/">The Financial Markets Seismometer</a></li>
<li><a href="http://channel9.msdn.com/shows/Going%2BDeep/Lecture-Series-Erik-Meijer-Functional-Programming-Fundamentals-Chapter-1/">C9 lectures</a> &#8211; functional programming intro</li>
<li><a href="http://www.cloudera.com/resources/?type=Training">Cloudera Hadoop Training</a></li>
<li><a href="http://halfarsedagilemanifesto.org/">Half arsed agile manifesto</a></li>
</ul>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://ivanidris.net/wordpress/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://ivanidris.net/wordpress/index.php/2010/08/29/grails-finance-services/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Vim 7.3 released</title>
		<link>http://ivanidris.net/wordpress/index.php/2010/08/22/vim-7-3-released</link>
		<comments>http://ivanidris.net/wordpress/index.php/2010/08/22/vim-7-3-released#comments</comments>
		<pubDate>Sun, 22 Aug 2010 18:49:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[editors]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://ivanidris.net/wordpress/index.php/2010/08/22/vim-7-3-released</guid>
		<description><![CDATA[Breaking news Vim 7.3 has been released. Hooray, vim users rejoice! It has been two years, so it is about time for an upgrade. Change log The new features are Persistent undo and undo for reload Blowfish encryption, encryption of the swap file Conceal text Lua interface Python 3 interface 1 :help version-7.3 offers more [...]]]></description>
			<content:encoded><![CDATA[<p>Breaking news <a href="http://www.vim.org/">Vim 7.3</a> has been released. Hooray, vim users rejoice! It has been two years, so it is about time for an upgrade.</p>
<h3>Change log</h3>
<p>The new features are</p>
<ul>
<li>Persistent undo and undo for reload</li>
<li>Blowfish encryption, encryption of the swap file</li>
<li>Conceal text</li>
<li>Lua interface</li>
<li>Python 3 interface</li>
</ul>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #000000;">:</span>help version<span style="color: #000000;">-</span><span style="color: #000000; font-weight:bold;">7.3</span></pre></td></tr></table></div>

<p> offers more detailed information. So I downloaded the latest version of <a href="http://code.google.com/p/macvim/">MacVim</a> and installed vim with <a href="http://www.macports.org/">MacPorts</a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #c20cb9; font-weight: bold;">vim</span></pre></td></tr></table></div>

<p>I checked the enabled features with <b>:version</b> and </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">vim</span> <span style="color: #660033;">--version</span></pre></td></tr></table></div>

<p>I noticed that the Python 3 and Lua interface are disabled.</p>
<h3>Undo persistence</h3>
<p>Undo persistence is a new vim feature. You can find information about undo persistence with </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #000000;">:</span>help undo<span style="color: #000000;">-</span>persistence</pre></td></tr></table></div>

<p>In my .vimrc I put these lines</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="vim" style="font-family:monospace;">set undofile
set undodir=<span style="color: #000000;">/</span>tmp<span style="color: #000000;">/</span>undos</pre></td></tr></table></div>

<p>The first line turns on undo persistence, which is off by default. The second line specifies a directory where to store the undo files, otherwise vim would use the same directory where the edited file is.</p>
<h3>Bag of tips and tricks (pre 7.3)</h3>
<p>My bag of vim gems and tidbits is full of stuff I would like to share no matter what. I admit that the usefulness varies and some of this stuff you may never ever need.</p>
<h3>XML folding</h3>
<p>I think that XML folding is a really neat feature. The <a href="http://www.vim.org/scripts/script.php%3Fscript_id%3D301">xmledit vim plugin</a> lets you do this the easy way. Download the plugin, put it in the .vim/ftplugin directory and add these lines to .vimrc</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">let</span> g<span style="color: #000000;">:</span>xml_syntax_folding=<span style="color: #000000; font-weight:bold;">1</span>
au FileType xml setlocal foldmethod=syntax</pre></td></tr></table></div>

<p>If you open a XML file, it will be folded already. You can close and open folds with <b>zo</b> and <b>zc</b>.</p>
<h3>Save a file with sudo</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #000000;">:</span>w <span style="color: #000000;">!</span>sudo tee <span style="color: #000000;">%</span></pre></td></tr></table></div>

<p>The ! calls an external command &#8211; in this case tee. % is the vim register containing the name of the file.</p>
<h3>Encoding files</h3>
<p>Vim allows you to encode data with a password</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">vim</span> <span style="color: #660033;">-x</span> secretfile</pre></td></tr></table></div>

<p>Now vim asks you to enter an encryption key also known as a password and then you need to repeat it. Next time you try to open this file you will be prompted for your password. Of course, if you try viewing the file with other editors or cat for example, you will only see gibberish.</p>
<h3>MySQL shell vi mode</h3>
<p>I tested this under Ubuntu only, but in theory it should work on other operating systems as well. Apparently if MySQL is compiled with readline support, you can set vi mode for the MySQL CLI. Add this line to ~/.inputrc</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">set</span> keymap <span style="color: #c20cb9; font-weight: bold;">vi</span> 
<span style="color: #000000; font-weight: bold;">set</span> editing-mode <span style="color: #c20cb9; font-weight: bold;">vi</span></pre></td></tr></table></div>

<h3>Vim questions on Stack Overflow</h3>
<p>I assembled a list of vim questions on Stack Overflow. The answers contain lots of useful tips and ideas. I highly recommend going through them. The signal to noise ratio is really good.</p>
<ul>
<li><a href="http://stackoverflow.com/questions/21725/favorite-gvim-plugins-scripts">Favorite vim plugins</a></li>
<li><a href="http://stackoverflow.com/questions/826208/making-vim-ubiquitous">Making vim ubiquitous</a> apparently evince let you use hjkl as well</li>
<li><a href="http://stackoverflow.com/questions/164847/what-is-in-your-vimrc">What is in your vimrc</a></li>
<li><a href="http://stackoverflow.com/questions/726894/what-are-the-dark-corners-of-vim-your-mom-never-told-you-about">The dark corners of vim</a></li>
<li><a href="http://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim">Most productive shortcut with vim</a></li>
</ul>
<h3>Random links of interest</h3>
<p>As a free service here are some interesting links</p>
<ul>
<li><a href="http://www.riptano.com/blog/slides-and-videos-cassandra-summit-2010">Cassandra Summit 2010</a></li>
<li><a href="http://superuser.com/questions/52483/terminal-tips-and-tricks-for-mac-os-x">Terminal Tips and Tricks For Mac OS X</a></li>
<li><a href="http://www.ibm.com/developerworks/aix/library/au-vitips.html">vi tips and tricks</a></li>
</ul>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://ivanidris.net/wordpress/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://ivanidris.net/wordpress/index.php/2010/08/22/vim-7-3-released/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grails Finance 0.6 Testing</title>
		<link>http://ivanidris.net/wordpress/index.php/2010/08/15/grails-finance-0-6-testing</link>
		<comments>http://ivanidris.net/wordpress/index.php/2010/08/15/grails-finance-0-6-testing#comments</comments>
		<pubDate>Sun, 15 Aug 2010 18:56:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://ivanidris.net/wordpress/index.php/2010/08/15/grails-finance-0-6-testing</guid>
		<description><![CDATA[Testing, testing, 123 &#8230; Test a little, code a little, go home, eat a little, sleep a little. This is the lifestyle recommended by the League of Agile Methodology Experts. That and wearing t-shirts with agile slogans printed on them and matching pants. I would like to do that today, the testing part I mean. [...]]]></description>
			<content:encoded><![CDATA[<p><i>Testing, testing, 123 &#8230;</i></p>
<p>Test a little, code a little, go home, eat a little, sleep a little. This is the lifestyle recommended by the <a href="http://www.infosys.com/research/publications/Documents/adopting-agile-methodologies.pdf">League of Agile Methodology Experts</a>. That and wearing t-shirts with agile slogans printed on them and matching pants. I would like to do that today, the testing part I mean.</p>
<h3>Integration tests</h3>
<p>Grails make it easy to create integration tests. Let&#8217;s do that real quick</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails create-integration-test SimulationIntegration</pre></td></tr></table></div>

<p>I modified the test to create and delete a Simulation object on the fly.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">grails.test.*</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> SimulationIntegrationTests <span style="color: #000000; font-weight: bold;">extends</span> GrailsUnitTestCase <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> setUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">setUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> tearDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">tearDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #993333;">void</span> testSaveAndDelete<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">def</span> simulation <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Simulation<span style="color: #66cc66;">&#40;</span>name : <span style="color: #ff0000;">&quot;sim1&quot;</span>, description : <span style="color: #ff0000;">&quot; first test&quot;</span><span style="color: #66cc66;">&#41;</span>
		assertNotNull simulation.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">def</span> foundSimulation <span style="color: #66cc66;">=</span> Simulation.<span style="color: #663399;">get</span><span style="color: #66cc66;">&#40;</span>simulation.<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span>
		foundSimulation.<span style="color: #006600;">delete</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
		assertFalse Simulation.<span style="color: #006600;">exists</span><span style="color: #66cc66;">&#40;</span>foundSimulation.<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>You can run the test with</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails test-app <span style="color: #660033;">-integration</span></pre></td></tr></table></div>

<h3>Another integraion test</h3>
<p>The problem with having many domain classes is that you need many tests. LAME practicioners use the word little, because otherwise testing gets boring. If you have the code already as I do, then it becomes test a little, then test a little more, which is not recommended at all. Sigh, here is another test</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails create-integration-test SessionIntegration</pre></td></tr></table></div>

<p>The test adds a few sessions to the simulation. Note the use of the <a href="http://www.grails.org/DomainClass%2BDynamic%2BMethods">dynamic addTo method</a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">    <span style="color: #993333;">void</span> testFirstSimulation<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">def</span> simulation <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Simulation<span style="color: #66cc66;">&#40;</span>name : <span style="color: #ff0000;">&quot;sim1&quot;</span>, description : <span style="color: #ff0000;">&quot; first test&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">def</span> session1 <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Session<span style="color: #66cc66;">&#40;</span>description : <span style="color: #ff0000;">&quot;First session&quot;</span><span style="color: #66cc66;">&#41;</span>
		simulation.<span style="color: #006600;">addToSessions</span><span style="color: #66cc66;">&#40;</span> session1 <span style="color: #66cc66;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">def</span> session2 <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Session<span style="color: #66cc66;">&#40;</span>description : <span style="color: #ff0000;">&quot;Second session&quot;</span><span style="color: #66cc66;">&#41;</span>
		simulation.<span style="color: #006600;">addToSessions</span><span style="color: #66cc66;">&#40;</span> session2 <span style="color: #66cc66;">&#41;</span>
		assertEquals <span style="color: #cc66cc;">2</span>, Simulation.<span style="color: #663399;">get</span><span style="color: #66cc66;">&#40;</span>simulation.<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">sessions</span>.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">def</span> foundSimulation <span style="color: #66cc66;">=</span> Simulation.<span style="color: #663399;">get</span><span style="color: #66cc66;">&#40;</span>simulation.<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span>	
		<span style="color: #000000; font-weight: bold;">def</span> descriptions <span style="color: #66cc66;">=</span> foundSimulation.<span style="color: #006600;">sessions</span>.<span style="color: #663399;">collect</span> <span style="color: #66cc66;">&#123;</span> it.<span style="color: #006600;">description</span> <span style="color: #66cc66;">&#125;</span>
		assertEquals<span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'First session'</span>, <span style="color: #ff0000;">'Second session'</span><span style="color: #66cc66;">&#93;</span>, descriptions.<span style="color: #663399;">sort</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h3>Intermission</h3>
<p>Let&#8217;s get something to drink. I have been researching trading concepts and approaches lately. So this is my list of popular ideas:</p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Trend_following">Trend following</a> &#8211; determine when a market is trending and follow it until the trend vanishes.</li>
<li><a href="http://en.wikipedia.org/wiki/Hedge_(finance)">Hedging</a> &#8211; lower investment risk with short selling and/or derivatives.</li>
<li><a href="http://en.wikipedia.org/wiki/Fundamental_analysis">Fundamental analysis</a></li>
<li><a href="http://en.wikipedia.org/wiki/Value_investing">Value investing</a> &#8211; invest based on intrinsic value determination.</li>
<li><a href="http://www.investopedia.com/articles/trading/07/bollinger.asp">Band trading</a> &#8211; trade within a band.</li>
<li><a href="http://en.wikipedia.org/wiki/Spread_trade">Spreads</a> &#8211; buying and selling different legs of an option or future.</li>
<li><a href="http://en.wikipedia.org/wiki/Arbitrage">Arbitrage</a> &#8211; taking advantage of price differences between different markets.</li>
</ul>
<p>OK, back to the main program.</p>
<h3>Yet another integration test</h3>
<p>Yes, more tests, but being LAME is not easy.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">    <span style="color: #993333;">void</span> testSessionRunCreation<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">def</span> simulation <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Simulation<span style="color: #66cc66;">&#40;</span>name : <span style="color: #ff0000;">&quot;sim1&quot;</span>, description : <span style="color: #ff0000;">&quot; first test&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">def</span> session <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Session<span style="color: #66cc66;">&#40;</span>description : <span style="color: #ff0000;">&quot;First session&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">def</span> run1 <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SessionRun<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">def</span> param1 <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StrategyParameter<span style="color: #66cc66;">&#40;</span> name : <span style="color: #ff0000;">&quot;param1&quot;</span>, 
			description : <span style="color: #ff0000;">&quot;first parameter&quot;</span>, val : <span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #66cc66;">&#41;</span>
		run1.<span style="color: #006600;">addToParameters</span><span style="color: #66cc66;">&#40;</span> param1 <span style="color: #66cc66;">&#41;</span>
		session.<span style="color: #006600;">addToRuns</span><span style="color: #66cc66;">&#40;</span> run1 <span style="color: #66cc66;">&#41;</span>
		assertEquals <span style="color: #cc66cc;">1</span>, session.<span style="color: #006600;">runs</span>.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">def</span> run2 <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SessionRun<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">def</span> param2 <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StrategyParameter<span style="color: #66cc66;">&#40;</span> name : <span style="color: #ff0000;">&quot;param2&quot;</span>, 
			description : <span style="color: #ff0000;">&quot;second parameter&quot;</span>, val : <span style="color: #ff0000;">&quot;2.0&quot;</span><span style="color: #66cc66;">&#41;</span>
		run2.<span style="color: #006600;">addToParameters</span><span style="color: #66cc66;">&#40;</span> param2 <span style="color: #66cc66;">&#41;</span>
		session.<span style="color: #006600;">addToRuns</span><span style="color: #66cc66;">&#40;</span> run2 <span style="color: #66cc66;">&#41;</span>
		assertEquals <span style="color: #cc66cc;">2</span>, session.<span style="color: #006600;">runs</span>.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
		simulation.<span style="color: #006600;">addToSessions</span><span style="color: #66cc66;">&#40;</span> session <span style="color: #66cc66;">&#41;</span>
&nbsp;
		assertEquals <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'param1'</span> , <span style="color: #ff0000;">'param2'</span> <span style="color: #66cc66;">&#93;</span>,	
			simulation.<span style="color: #006600;">sessions</span>.<span style="color: #006600;">runs</span>.<span style="color: #006600;">parameters</span><span style="color: #66cc66;">*</span>.<span style="color: #006600;">name</span>.<span style="color: #663399;">flatten</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #663399;">sort</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">def</span> foundSimulation <span style="color: #66cc66;">=</span> Simulation.<span style="color: #663399;">get</span><span style="color: #66cc66;">&#40;</span>simulation.<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span>
		assertEquals <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'param1'</span> , <span style="color: #ff0000;">'param2'</span> <span style="color: #66cc66;">&#93;</span>,	
			foundSimulation.<span style="color: #006600;">sessions</span>.<span style="color: #006600;">runs</span>.<span style="color: #006600;">parameters</span><span style="color: #66cc66;">*</span>.<span style="color: #006600;">name</span>.<span style="color: #663399;">flatten</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #663399;">sort</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h3>Random links of interest</h3>
<p>I would like to apologize for the boring stuff above. Please accept these links of interest as compensation.</p>
<ul>
<li><a href="http://developer.nvidia.com/object/gpu_gems_2_home.html">GPU Gems 2 book</a> a free online book on GPU programming.</li>
<li><a href="http://www.gpgpu.org/w/index.php/Main_Page">GPGPU wiki</a></li>
<li><a href="http://www.youtube.com/user/StanfordUniversity#g/p">Stanford University</a> courses on YouTube.</li>
<li><a href="http://gpars.codehaus.org/">GPars</a> Groovy Parallel Systems.</li>
<li><a href="http://code.google.com/p/groovypptest/">Groovy++</a> statically typed extension of Groovy.</li>
</ul</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://ivanidris.net/wordpress/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://ivanidris.net/wordpress/index.php/2010/08/15/grails-finance-0-6-testing/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grails Finance 0.5</title>
		<link>http://ivanidris.net/wordpress/index.php/2010/08/08/grails-finance-0-5</link>
		<comments>http://ivanidris.net/wordpress/index.php/2010/08/08/grails-finance-0-5#comments</comments>
		<pubDate>Sun, 08 Aug 2010 17:45:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://ivanidris.net/wordpress/index.php/2010/08/08/grails-finance-0-5</guid>
		<description><![CDATA[Birth of a simulation engine Mmm, okay, so it&#8217;s back to the drawing board for Design Phase Deux. I want to run trading simulation sessions using different strategies. Each session will consist of different runs, where certain parameter values will be varied. Each run will have a P &#038; L, that will be stored for [...]]]></description>
			<content:encoded><![CDATA[<p><i>Birth of a simulation engine</i></p>
<p>Mmm, okay, so it&#8217;s back to the drawing board for Design Phase Deux. I want to run trading simulation sessions using different strategies. Each session will consist of different runs, where certain parameter values will be varied. Each run will have a P &#038; L, that will be stored for later analysis. The simulation engine will generate orders, which will have different status and type.</p>
<h4>Class diagram</h4>
<p>I did some <a href="http://vinci.org/uml/crc.html">CRC analysis</a> in my head and sketches on paper. So I made a preliminary draft design, which is obviously subject to change. Unfortunately, I don&#8217;t know how to draw UML diagrams in Vim. After trying out a few tools and getting increasingly annoyed I settled for <a href="http://www.netbeans.org/">Netbeans</a>, because its diagrams seemed prettiest. Netbeans 6.5 to be exact, not the latest version, but the version I already had installed. Here is the class diagram, some of the datatypes are incorrect and while writing about the design, I realized, that I forgot a few classes <img src='http://ivanidris.net/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div align="center">
<a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails5/SimulationEngine.png" rel="lightbox[SimulationEngine]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails5/th_SimulationEngine.png" border="0" alt="Simulation Engine" ></a>
</div>
<h4>Code generation</h4>
<p>Netbeans has the ability to generate code for you from UML, but it requires you to make a target project. Since I am not a Netbeans fan, I decided to generate the code myself. For that purpose I generated a HTML report from Netbeans. The script below parses the HTML files, calls the <a href="http://www.grails.org/">Grails</a> commands to generate controllers and domain classes and then appends variable declarations to the generated domain classes. This means that manual editing is still required.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">REPORTDIR</span>=~<span style="color: #000000; font-weight: bold;">/</span>NetBeansProjects<span style="color: #000000; font-weight: bold;">/</span>SimulationEngine<span style="color: #000000; font-weight: bold;">/</span>report<span style="color: #000000; font-weight: bold;">/</span>SimulationEngine
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> f <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #007800;">$REPORTDIR</span><span style="color: #000000; font-weight: bold;">/*</span>.html<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">do</span>
   <span style="color: #007800;">str</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;public class&quot;</span> <span style="color: #007800;">$f</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null<span style="color: #000000; font-weight: bold;">|</span>
   <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> Unnamed<span style="color: #000000; font-weight: bold;">|</span>
   <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;&lt;B&gt;&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$str</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> 
   <span style="color: #000000; font-weight: bold;">then</span>
      <span style="color: #007800;">class</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$str</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/.*&lt;B&gt;//g;s/&lt;\/B&gt;.*//g'</span><span style="color: #000000; font-weight: bold;">`</span>
      grails create-controller <span style="color: #007800;">$class</span>
      grails create-domain-class <span style="color: #007800;">$class</span>
      <span style="color: #007800;">varname</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">grep</span> private <span style="color: #007800;">$f</span><span style="color: #000000; font-weight: bold;">|</span>
      <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/.*html&quot;&gt;//g;s/&lt;\/A&gt;//g;s/&lt;B&gt;//g;s/&lt;\/B&gt;.*$//g'</span><span style="color: #000000; font-weight: bold;">`</span>
      <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;    <span style="color: #007800;">$varname</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> grails-app<span style="color: #000000; font-weight: bold;">/</span>domain<span style="color: #000000; font-weight: bold;">/</span>finance<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${class}</span>.groovy
   <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<h4>Manual editing</h4>
<p>The quick and dirty code generation script created code that doesn&#8217;t compile. I had to move the variable declarations to the correct place, fill in the <b>constraints</b>, <b>belongsTo</b> and <b>hasMany</b> values. Also it is good practice to have a <b>toString</b> implementation. The current stats are as follows</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails stats
&nbsp;
    +----------------------+-------+-------+
    <span style="color: #000000; font-weight: bold;">|</span> Name                 <span style="color: #000000; font-weight: bold;">|</span> Files <span style="color: #000000; font-weight: bold;">|</span>  LOC  <span style="color: #000000; font-weight: bold;">|</span>
    +----------------------+-------+-------+
    <span style="color: #000000; font-weight: bold;">|</span> Controllers          <span style="color: #000000; font-weight: bold;">|</span>    <span style="color: #000000;">15</span> <span style="color: #000000; font-weight: bold;">|</span>   <span style="color: #000000;">174</span> <span style="color: #000000; font-weight: bold;">|</span> 
    <span style="color: #000000; font-weight: bold;">|</span> Domain Classes       <span style="color: #000000; font-weight: bold;">|</span>    <span style="color: #000000;">14</span> <span style="color: #000000; font-weight: bold;">|</span>   <span style="color: #000000;">168</span> <span style="color: #000000; font-weight: bold;">|</span> 
    <span style="color: #000000; font-weight: bold;">|</span> Unit Tests           <span style="color: #000000; font-weight: bold;">|</span>    <span style="color: #000000;">29</span> <span style="color: #000000; font-weight: bold;">|</span>   <span style="color: #000000;">348</span> <span style="color: #000000; font-weight: bold;">|</span> 
    +----------------------+-------+-------+
    <span style="color: #000000; font-weight: bold;">|</span> Totals               <span style="color: #000000; font-weight: bold;">|</span>    <span style="color: #000000;">58</span> <span style="color: #000000; font-weight: bold;">|</span>   <span style="color: #000000;">690</span> <span style="color: #000000; font-weight: bold;">|</span> 
    +----------------------+-------+-------+</pre></td></tr></table></div>

<p>690 LOC, not bad for a SHH ( Secret Home Hackathon ).</p>
<h4>Random links of interest</h4>
<p>And &#8230; now something really, absolutely and completely different.</p>
<ul>
<li><a href="http://codingbat.com/">CodingBat</a> &#8211; webbased you can create an account here and track your progress.</li>
<li><a href="http://singpath.appspot.com/">SingPath</a> &#8211; also webbased, track progress by signing in with your Google account.</li>
<li><a href="http://bitbucket.org/gregmalcolm/python_koans/wiki/Home">Python koans</a> &#8211; you can download the exercises with Mercurial. The exercises are failing tests, that you need to make pass.</li>
<li><a href="http://days2010.scala-lang.org/node/136">Scala days 2010 videos</a></li>
<li><a href="http://vrapper.sourceforge.net/">Vrapper</a> &#8211; Eclipse vim plugin. Haven&#8217;t tried it out yet. TODO</li>
</ul>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://ivanidris.net/wordpress/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://ivanidris.net/wordpress/index.php/2010/08/08/grails-finance-0-5/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grails Finance 0.4</title>
		<link>http://ivanidris.net/wordpress/index.php/2010/08/01/grails-finance-0-4</link>
		<comments>http://ivanidris.net/wordpress/index.php/2010/08/01/grails-finance-0-4#comments</comments>
		<pubDate>Sun, 01 Aug 2010 17:12:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://ivanidris.net/wordpress/index.php/2010/08/01/grails-finance-0-4</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p><i>Approved by the League of Agile Methodology Experts</i></p>
<p>Like, let me go ahead and confirm the results of <a href="http://ivanidris.net/wordpress/index.php/2010/07/25/grails-finance-0-3">Grails Finance 0.3</a>. In statistics there is the notion of <a href="http://en.wikipedia.org/wiki/Correlation_and_dependence">correlation</a>. In the case of a non normal distribution, it seems most appropriate to calculate the <a href="http://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient">Spearmans correlation</a> 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 <a href="http://commons.apache.org/math/">Apache Commons Math</a> API to determine the correlation.</p>
<h4>Dependencies</h4>
<p>The whole world uses <a href="http://maven.apache.org/">Maven</a> to manage dependencies with the principle of <a href="http://en.wikipedia.org/wiki/Convention_over_configuration">convention over configuration</a>. <a href="http://www.grails.org/">Grails</a> also uses this principle, but with a different convention <img src='http://ivanidris.net/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  and less XML. If you really want to, you can integrate Grails and Maven using a Maven archetype or special plugins (see <a href="http://www.grails.org/doc/latest/guide/3. Configuration.html">Grails configuration</a>). Instead I chose to tweak <b>BuildConfig.groovy</b> &#8211; the <b>repositories</b> and <b>dependencies</b> blocks.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">   repositories <span style="color: #66cc66;">&#123;</span>
        grailsPlugins<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        grailsHome<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        grailsCentral<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// uncomment the below to enable remote dependency resolution</span>
        <span style="color: #808080; font-style: italic;">// from public Maven repositories</span>
        mavenLocal<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        mavenCentral<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        mavenRepo <span style="color: #ff0000;">&quot;http://snapshots.repository.codehaus.org&quot;</span>
        mavenRepo <span style="color: #ff0000;">&quot;http://repository.codehaus.org&quot;</span>
        mavenRepo <span style="color: #ff0000;">&quot;http://download.java.net/maven/2/&quot;</span>
        mavenRepo <span style="color: #ff0000;">&quot;http://repository.jboss.com/maven2/&quot;</span>
    <span style="color: #66cc66;">&#125;</span>
    dependencies <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.</span>
		runtime <span style="color: #ff0000;">'org.apache.commons:commons-math:2.1'</span>
        <span style="color: #808080; font-style: italic;">// runtime 'mysql:mysql-connector-java:5.1.5'</span>
    <span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>You can test the dependencies with</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails dependency-report</pre></td></tr></table></div>

<h4>Controller</h4>
<p>I added some logic in the controller to calculate correlations of a specified instrument pair for OHLC and volume.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">   <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.apache.commons.math.stat.correlation.SpearmansCorrelation</span>
&nbsp;
   ...
&nbsp;
   <span style="color: #000000; font-weight: bold;">def</span> correlation <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
	   <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">select1</span> <span style="color: #66cc66;">!=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
		   <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#91;</span>instruments : getGoogleInstruments<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,
			   open: correlate<span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">select1</span>, params.<span style="color: #006600;">select2</span>, <span style="color: #ff0000;">'Open'</span><span style="color: #66cc66;">&#41;</span>,
			   high: correlate<span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">select1</span>, params.<span style="color: #006600;">select2</span>, <span style="color: #ff0000;">'High'</span><span style="color: #66cc66;">&#41;</span>,
			   low: correlate<span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">select1</span>, params.<span style="color: #006600;">select2</span>, <span style="color: #ff0000;">'Low'</span><span style="color: #66cc66;">&#41;</span>,
			   <span style="color: #993399; font-weight: bold;">close</span>: correlate<span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">select1</span>, params.<span style="color: #006600;">select2</span>, <span style="color: #ff0000;">'Close'</span><span style="color: #66cc66;">&#41;</span>,
			   volume: correlate<span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">select1</span>, params.<span style="color: #006600;">select2</span>, <span style="color: #ff0000;">'Volume'</span><span style="color: #66cc66;">&#41;</span>
	   		<span style="color: #66cc66;">&#93;</span>
	   <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#91;</span>instruments : getGoogleInstruments<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	   <span style="color: #66cc66;">&#125;</span>
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">def</span> correlate<span style="color: #66cc66;">&#40;</span>symbol1, symbol2, fieldName<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	       <span style="color: #000000; font-weight: bold;">def</span> instrument <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Instrument</span>.<span style="color: #006600;">findBySymbol</span><span style="color: #66cc66;">&#40;</span>symbol1<span style="color: #66cc66;">&#41;</span>
	       <span style="color: #000000; font-weight: bold;">def</span> instrument2 <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Instrument</span>.<span style="color: #006600;">findBySymbol</span><span style="color: #66cc66;">&#40;</span>symbol2<span style="color: #66cc66;">&#41;</span>
		   <span style="color: #000000; font-weight: bold;">def</span> field <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Field</span>.<span style="color: #006600;">findByName</span><span style="color: #66cc66;">&#40;</span>fieldName<span style="color: #66cc66;">&#41;</span>
		   <span style="color: #000000; font-weight: bold;">def</span> firstData <span style="color: #66cc66;">=</span> getDataForInstrument<span style="color: #66cc66;">&#40;</span>instrument, field<span style="color: #66cc66;">&#41;</span>
		   <span style="color: #000000; font-weight: bold;">def</span> secondData <span style="color: #66cc66;">=</span> getDataForInstrument<span style="color: #66cc66;">&#40;</span>instrument2, field<span style="color: #66cc66;">&#41;</span>
&nbsp;
			 <span style="color: #000000; font-weight: bold;">def</span> data1 <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
			 <span style="color: #000000; font-weight: bold;">def</span> data2 <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
			 <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">def</span> added : firstData<span style="color: #66cc66;">?</span>.<span style="color: #006600;">keySet</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> secondData<span style="color: #66cc66;">?</span>.<span style="color: #006600;">keySet</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">?</span>.<span style="color: #CC0099;">contains</span><span style="color: #66cc66;">&#40;</span>added<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
					data1 <span style="color: #66cc66;">&lt;&lt;</span> <span style="color: #aaaadd; font-weight: bold;">Double</span>.<span style="color: #006600;">valueOf</span><span style="color: #66cc66;">&#40;</span>firstData<span style="color: #66cc66;">?</span>.<span style="color: #663399;">get</span><span style="color: #66cc66;">&#40;</span>added<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
					data2 <span style="color: #66cc66;">&lt;&lt;</span> <span style="color: #aaaadd; font-weight: bold;">Double</span>.<span style="color: #006600;">valueOf</span><span style="color: #66cc66;">&#40;</span>secondData<span style="color: #66cc66;">?</span>.<span style="color: #663399;">get</span><span style="color: #66cc66;">&#40;</span>added<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#125;</span>
		 <span style="color: #66cc66;">&#125;</span>
&nbsp;
		   <span style="color: #000000; font-weight: bold;">def</span> corr <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SpearmansCorrelation<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">correlation</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">double</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>data1.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, 
			   <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">double</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>data2.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h4>View</h4>
<p>The view displays a correlation table for an instrument pair.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
   &lt;head&gt;
      &lt;title&gt;Correlation Historical data&lt;/title&gt;
   &lt;/head&gt;
   &lt;body&gt;
      &lt;table&gt;
         &lt;tr&gt;
            &lt;g:form&gt;
            &lt;td&gt;
            		&lt;g:select name=&quot;select1&quot; from=&quot;${instruments.symbol}&quot; value=&quot;${params.select1}&quot;/&gt;
            &lt;/td&gt; 
            &lt;td&gt;
            		&lt;g:select name=&quot;select2&quot; from=&quot;${instruments.symbol}&quot; value=&quot;${params.select2}&quot;/&gt;
            	&lt;td&gt;
            &lt;td&gt;
               &lt;g:actionSubmit name=&quot;correlation&quot; action=&quot;correlation&quot; value=&quot;Correlate&quot;/&gt;
            &lt;/td&gt;
            &lt;/g:form&gt;&lt;/td&gt;
         &lt;/tr&gt;
      &lt;/table&gt; 
&nbsp;
     &lt;br&gt; 
     &lt;br&gt; 
&nbsp;
      &lt;g:if test=&quot;${open != null}&quot;&gt;
      	&lt;table border=&quot;1&quot;&gt;
      		&lt;tr&gt;
      			&lt;td&gt;Open&lt;/td&gt;
      			&lt;td&gt;${open}&lt;/td&gt;
      		&lt;/tr&gt;
      		&lt;tr&gt;
      			&lt;td&gt;High&lt;/td&gt;
      			&lt;td&gt;${high}&lt;/td&gt;
      		&lt;/tr&gt;
      		&lt;tr&gt;
      			&lt;td&gt;Low&lt;/td&gt;
      			&lt;td&gt;${low}&lt;/td&gt;
      		&lt;tr&gt;
      			&lt;td&gt;Close&lt;/td&gt;
      			&lt;td&gt;${close}&lt;/td&gt;
      		&lt;/tr&gt;
      		&lt;tr&gt;
      			&lt;td&gt;Volume&lt;/td&gt;
      			&lt;td&gt;${volume}&lt;/td&gt;
      		&lt;/tr&gt;
      	&lt;/table&gt;
      &lt;/g:if&gt;
   &lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<h4>Result</h4>
<p>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.</p>
<div align="center">
<a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails04/diaSpyCorrelation.png" rel="lightbox[Correlation]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails04/th_diaSpyCorrelation.png" border="0" alt="Photobucket" ></a><br />
<a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails04/diaGldCorrelation.png" rel="lightbox[Correlation]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails04/th_diaGldCorrelation.png" border="0" alt="Photobucket" ></a><br />
<a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails04/spyGldCorrelation.png" rel="lightbox[Correlation]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails04/th_spyGldCorrelation.png" border="0" alt="Photobucket" ></a>
</div>
<h4>Conclusion</h4>
<p>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 <a href="http://en.wikipedia.org/wiki/Backtesting">backtesting</a>. 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.</p>
<h4>Random links of interest</h4>
<p>And now for something completely different &#8211; 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.</p>
<ul>
<li><a href="http://code.google.com/events/io/2010/">Google IO 2010</a> Google&#8217;s yearly conference for developers.</li>
<li><a href="http://code.google.com/edu/">Google Code University</a>  contains complete courses and video lectures.</li>
<li><a href="http://www.khanacademy.org/">Khan Academy</a> haven&#8217;t checked it out, but it gets good press. TODO.</li>
<li><a href="http://lab.arc90.com/experiments/readability/">Readability</a> bookmarlet, well name says it all.</li>
<li><a href="http://www.springsource.com/training/freeonline">SpringSource free online training</a> not a lot there, but good quality.</li>
</ul>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://ivanidris.net/wordpress/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://ivanidris.net/wordpress/index.php/2010/08/01/grails-finance-0-4/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grails Finance 0.3</title>
		<link>http://ivanidris.net/wordpress/index.php/2010/07/25/grails-finance-0-3</link>
		<comments>http://ivanidris.net/wordpress/index.php/2010/07/25/grails-finance-0-3#comments</comments>
		<pubDate>Sun, 25 Jul 2010 19:59:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://ivanidris.net/wordpress/index.php/2010/07/25/grails-finance-0-3</guid>
		<description><![CDATA[So now that I have historical data in my database, it&#8217;s time to have a good look at it. One popular way is to represent the data in a chart. Creating graphs on your own from scratch is a lot of work. Luckily, there are a lot of freely available charting API&#8217;s out there. For [...]]]></description>
			<content:encoded><![CDATA[<p>So now that I have historical data in my database, it&#8217;s time to have a good look at it. One popular way is to represent the data in a chart. Creating graphs on your own from scratch is a lot of work. Luckily, there are a lot of freely available charting API&#8217;s out there. For some of those API&#8217;s <a href="http://www.grails.org/Plugins">Grails plugins</a> exist.</p>
<h4>Google Charts</h4>
<p><a href="http://code.google.com/apis/chart/">Google Charts</a> generates an image from an URL with parameters. The <a href="http://www.grails.org/Google%2BChart%2BPlugin">Google Chart Grails plugin</a> has a set of tags with, which you can specify what kind of chart you want. Install it</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails install-plugin google-chart</pre></td></tr></table></div>

<p>The testing procedure on the plugin information page did not work for me and it seems that the plugin does not support all the Google chart types. Other than that, everything seems to work fine.</p>
<h4>Controller</h4>
<p>I added two methods in the controller. The first one, gets historical end of day data sorted by date, for an instrument and a field. For instance, DIA and the high price field. The second method calls the first one for two different securities and the same field in order to compare the data in a chart. This method also produces a list of x axis labels.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">   <span style="color: #000000; font-weight: bold;">def</span> historicalChart <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
     <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">fieldVar</span> <span style="color: #66cc66;">==</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#91;</span>instruments : getGoogleInstruments<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
      <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">def</span> instrument <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Instrument</span>.<span style="color: #006600;">findBySymbol</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">select1</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #000000; font-weight: bold;">def</span> instrument2 <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Instrument</span>.<span style="color: #006600;">findBySymbol</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">select2</span><span style="color: #66cc66;">&#41;</span>
		 <span style="color: #000000; font-weight: bold;">def</span> field <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Field</span>.<span style="color: #006600;">findByName</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">fieldVar</span><span style="color: #66cc66;">&#41;</span>
		 <span style="color: #000000; font-weight: bold;">def</span> firstData <span style="color: #66cc66;">=</span> getDataForInstrument<span style="color: #66cc66;">&#40;</span>instrument, field<span style="color: #66cc66;">&#41;</span>
		 <span style="color: #000000; font-weight: bold;">def</span> secondData <span style="color: #66cc66;">=</span> getDataForInstrument<span style="color: #66cc66;">&#40;</span>instrument2, field<span style="color: #66cc66;">&#41;</span>
&nbsp;
		 <span style="color: #000000; font-weight: bold;">def</span> data1 <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>:<span style="color: #66cc66;">&#93;</span>
		 <span style="color: #000000; font-weight: bold;">def</span> data2 <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>:<span style="color: #66cc66;">&#93;</span>
		 <span style="color: #000000; font-weight: bold;">def</span> xLabels <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
		 <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">def</span> added : firstData<span style="color: #66cc66;">?</span>.<span style="color: #006600;">keySet</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> secondData<span style="color: #66cc66;">?</span>.<span style="color: #006600;">keySet</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">?</span>.<span style="color: #CC0099;">contains</span><span style="color: #66cc66;">&#40;</span>added<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">def</span> shortKey <span style="color: #66cc66;">=</span> added.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;MMM yy&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>xLabels.<span style="color: #CC0099;">contains</span><span style="color: #66cc66;">&#40;</span>shortKey<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
					xLabels <span style="color: #66cc66;">&lt;&lt;</span> shortKey	
				<span style="color: #66cc66;">&#125;</span>
&nbsp;
				data1<span style="color: #66cc66;">&#91;</span>added<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Double</span>.<span style="color: #006600;">valueOf</span><span style="color: #66cc66;">&#40;</span>firstData<span style="color: #66cc66;">?</span>.<span style="color: #663399;">get</span><span style="color: #66cc66;">&#40;</span>added<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
				data2<span style="color: #66cc66;">&#91;</span>added<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Double</span>.<span style="color: #006600;">valueOf</span><span style="color: #66cc66;">&#40;</span>secondData<span style="color: #66cc66;">?</span>.<span style="color: #663399;">get</span><span style="color: #66cc66;">&#40;</span>added<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#125;</span>
		 <span style="color: #66cc66;">&#125;</span>
&nbsp;
         <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#91;</span>instruments : getGoogleInstruments<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,
			 firstData : data1,
			 secondData : data2,
			 xLabels : xLabels<span style="color: #66cc66;">&#93;</span>
      <span style="color: #66cc66;">&#125;</span>
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">def</span> getDataForInstrument<span style="color: #66cc66;">&#40;</span>instrument, field<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">def</span> fieldVals <span style="color: #66cc66;">=</span> FieldValue.<span style="color: #006600;">findAllByInstrumentAndField</span><span style="color: #66cc66;">&#40;</span>instrument, field<span style="color: #66cc66;">&#41;</span>
		 <span style="color: #000000; font-weight: bold;">def</span> data <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>:<span style="color: #66cc66;">&#93;</span>
&nbsp;
		 <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">;</span> i <span style="color: #66cc66;">&lt;</span> fieldVals.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span> i<span style="color: #66cc66;">++</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			data<span style="color: #66cc66;">&#91;</span>fieldVals<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">added</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> fieldVals<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">val</span> 
		 <span style="color: #66cc66;">&#125;</span>
&nbsp;
		 <span style="color: #000000; font-weight: bold;">return</span> data.<span style="color: #663399;">sort</span> <span style="color: #66cc66;">&#123;</span> it.<span style="color: #006600;">key</span> <span style="color: #66cc66;">&#125;</span>
   <span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h4>The view</h4>
<p>The view for the chart consists of one GSP page. I put some logic in it, that possibly could have been better placed in the controller. The page consists roughly of two parts. The top part is a form, which lets you select two instruments and a field, such as a price to plot. The bottom part displays a line chart with a special tag.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
   &lt;head&gt;
      &lt;title&gt;Chart Historical data&lt;/title&gt;
   &lt;/head&gt;
   &lt;body&gt;
      &lt;table&gt;
         &lt;tr&gt;
            &lt;g:form&gt;
            &lt;td&gt;
            		&lt;g:select name=&quot;select1&quot; from=&quot;${instruments.symbol}&quot; value=&quot;${params.select1}&quot;/&gt;
            &lt;/td&gt; 
            &lt;td&gt;
            		&lt;g:select name=&quot;select2&quot; from=&quot;${instruments.symbol}&quot; value=&quot;${params.select2}&quot;/&gt;
            	&lt;td&gt;
            &lt;td&gt;
               &lt;g:select name=&quot;fieldVar&quot; from=&quot;${['Open', 'High', 'Low', 'Close', 'Volume']}&quot; value=&quot;${params.fieldVar}&quot;/&gt;
            &lt;/td&gt;
            &lt;td&gt;
               &lt;g:actionSubmit name=&quot;doChart&quot; action=&quot;historicalChart&quot; value=&quot;Chart&quot;/&gt;
            &lt;/td&gt;
            &lt;/g:form&gt;&lt;/td&gt;
         &lt;/tr&gt;
      &lt;/table&gt;
&nbsp;
       &lt;g:if test=&quot;${firstData != null}&quot;&gt;
       		&lt;% 
			   def firstMax = firstData?.values()?.max()
			   def secondMax = secondData?.values()?.max()
			   def yMax = Math.max(firstMax, secondMax)
&nbsp;
			   def priceData = [firstData?.values()?.asList(), 
				   secondData?.values()?.asList()]
			%&gt;
	        &lt;g:lineChart type=&quot;lc&quot; title=&quot;End of day (${params.fieldVar}) comparison&quot;
				size=&quot;${[600,200]}&quot; 
				colors=&quot;${['FF0000','0000FF']}&quot; 
				axes=&quot;x,y&quot; 
				lineStyles=&quot;${[[2,2,2],[2,8,4]]}&quot; 
				legend=&quot;${[ params.select1, params.select2 ]}&quot; 
				gridLines=&quot;${100.0/11},25&quot; 
				axesLabels=&quot;${[0:xLabels,1:[0,yMax/2,yMax]] }&quot;
				data=&quot;${priceData}&quot;
				dataType=&quot;simple&quot;  /&gt;
		&lt;/g:if&gt; 
   &lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<h4>Result</h4>
<p>The result of this little science project is a table of charts. I plotted DIA, a <a href="http://en.wikipedia.org/wiki/Dow_Jones_Industrial_Average">Dow Jones Industrial Average</a> tracker, SPY, a <a href="http://en.wikipedia.org/wiki/S%26P_500">Standard and Poors 500</a> tracker and GLD, a Gold Trust tracker against each other for OHLC and volume parameters. The underlying indices of the first two trackers have similarities, therefore I expect, that the trackers wil be also linked in some way. GLD was added for good measure, because as 9 out of 10 scientists will, tell you, gold is very shiny <img src='http://ivanidris.net/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .</p>
<div align="center">
<table border="1">
<tr align="center">
<td></td>
<td>DIA x SPY</td>
<td>DIA x GLD</td>
<td>SPY x GLD</td>
</tr>
<tr>
<td>Open</td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails03/diaSpyOpen.png" rel="lightbox[Open]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails03/th_diaSpyOpen.png" border="0" alt="DIA SPY Open" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails03/diaGldOpen.png" rel="lightbox[Open]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails03/th_diaGldOpen.png" border="0" alt="DIA GLD Open" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails03/spyGldOpen.png" rel="lightbox[Open]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails03/th_spyGldOpen.png" border="0" alt="SPY GLD Open" ></a></td>
</tr>
<tr>
<td>High</td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails03/diaSpyHigh.png" rel="lightbox[High]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails03/th_diaSpyHigh.png" border="0" alt="DIA SPY High" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails03/diaGldHigh.png" rel="lightbox[High]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails03/th_diaGldHigh.png" border="0" alt="DIA GLD High" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails03/spyGldHigh.png" rel="lightbox[High]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails03/th_spyGldHigh.png" border="0" alt="SPY GLD High" ></a></td>
</tr>
<tr>
<td>Low</td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails03/diaSpyLow.png" rel="lightbox[Low]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails03/th_diaSpyLow.png" border="0" alt="DIA SPY Low" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails03/diaGldLow.png" rel="lightbox[Low]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails03/th_diaGldLow.png" border="0" alt="DIA GLD Low" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails03/spyGldLow.png" rel="lightbox[Low]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails03/th_spyGldLow.png" border="0" alt="SPY GLD Low" ></a></td>
</tr>
<tr>
<td>Close</td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails03/diaSpyClose.png" rel="lightbox[Close]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails03/th_diaSpyClose.png" border="0" alt="DIA SPY Close" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails03/diaGldClose.png" rel="lightbox[Close]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails03/th_diaGldClose.png" border="0" alt="DIA GLD Close" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails03/spyGldClose.png" rel="lightbox[Close]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails03/th_spyGldClose.png" border="0" alt="SPY GLD Close" ></a></td>
</tr>
<tr>
<td>Volume</td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails03/diaSpyVolume.png" rel="lightbox[Volume]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails03/th_diaSpyVolume.png" border="0" alt="DIA SPY Volume" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails03/diaGldVolume.png" rel="lightbox[Volume]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails03/th_diaGldVolume.png" border="0" alt="DIA GLD Volume" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails03/spyGldVolume.png" rel="lightbox[Volume]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails03/th_spyGldVolume.png" border="0" alt="SPY GLD Volume" ></a></td>
</tr>
</table>
</div>
<h4>Conclusion</h4>
<p>As expected there seems to be a relation between the DIA and SPY prices. Volumes differ by orders of magnitudes, which makes it hard to see any pattern in the charts. I can not say much about GLD. Next time I will perform simple statistical analysis to quantify any possible correlation. Oh, yeah, <a href="http://www.grails.org/">Grails</a> is great and plugins too.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://ivanidris.net/wordpress/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://ivanidris.net/wordpress/index.php/2010/07/25/grails-finance-0-3/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grails Finance 0.2</title>
		<link>http://ivanidris.net/wordpress/index.php/2010/07/18/grails-finance-0-2</link>
		<comments>http://ivanidris.net/wordpress/index.php/2010/07/18/grails-finance-0-2#comments</comments>
		<pubDate>Sun, 18 Jul 2010 17:04:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://ivanidris.net/wordpress/index.php/2010/07/18/grails-finance-0-2</guid>
		<description><![CDATA[Last week I made a scaffolded Grails finance application. This week I will fill the database with historical price data from Google Finance. Model Changes I needed to change the model a bit. I changed the name of the DateTime field and added some instance variables with corresponding constraints. Here is the modified FieldValue domain [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I made a scaffolded <a href="http://www.grails.org/">Grails</a> finance application. This week I will fill the database with historical price data from <a href="http://www.google.com/finance">Google Finance</a>.</p>
<h4>Model Changes</h4>
<p>I needed to change the model a bit. I changed the name of the DateTime field and added some instance variables with corresponding constraints. Here is the modified FieldValue domain class</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">finance</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.joda.time.*</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.joda.time.contrib.hibernate.*</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> FieldValue <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> val
    DateTime added
    <span style="color: #aaaadd; font-weight: bold;">Instrument</span> instrument
    <span style="color: #aaaadd; font-weight: bold;">Field</span> field
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> constraints <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
      val<span style="color: #66cc66;">&#40;</span>blank:<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
      added<span style="color: #66cc66;">&#40;</span>blank:<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
      instrument<span style="color: #66cc66;">&#40;</span>blank:<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
      field<span style="color: #66cc66;">&#40;</span>blank:<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> belongsTo <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #aaaadd; font-weight: bold;">Instrument</span>, <span style="color: #aaaadd; font-weight: bold;">Field</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
    <span style="color: #aaaadd; font-weight: bold;">String</span> toString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #ff0000;">&quot;${val} ${added}&quot;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h4>The view</h4>
<p>I created a <a href="http://www.grails.org/Developer%2B-%2BGroovy%2BServer%2BPages">Groovy Server Page</a> (GSP) that displays in a table all the Google Finance instruments in the database and buttons for data retrieval. <a href="http://www.prototypejs.org/">Prototype</a> is used to perform AJAX calls to the controller. I hope you recognize the famous Spinner UI design pattern.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
   &lt;head&gt;
      &lt;title&gt;Retrieve EOD data from Google Finance&lt;/title&gt;
      &lt;g:javascript library=&quot;prototype&quot;/&gt;
   &lt;/head&gt;
   &lt;body&gt;
      &lt;table&gt;
         &lt;g:each var=&quot;instrument&quot; in=&quot;${instruments}&quot;&gt;
         &lt;tr&gt;
            &lt;td&gt;${instrument.symbol}&lt;/td&gt;
            &lt;td&gt;${instrument.name}&lt;/td&gt;
            &lt;td&gt;&lt;g:form action='retrieve'&gt;
            &lt;g:hiddenField name='symbol' value='${instrument.symbol}'/&gt;
            &lt;g:submitToRemote value=&quot;Get data&quot;
            url=&quot;[controller: 'fieldValue', action: 'retrieve']&quot;
            onLoading=&quot;showSpinner(true)&quot; 
            onComplete=&quot;showSpinner(false)&quot;/&gt;
            &lt;img id=&quot;spinner&quot; style=&quot;display: none&quot; src=&quot;&lt;g:createLinkTo dir='/images' file='spinner.gif'/&gt;&quot;/&gt;
            &lt;/g:form&gt;&lt;/td&gt;
         &lt;/tr&gt;
         &lt;/g:each&gt;
      &lt;/table&gt;
      &lt;g:javascript&gt;
            function showSpinner(visible) {
                $('spinner').style.display = visible ? &quot;inline&quot; : &quot;none&quot;
            }   
        &lt;/g:javascript&gt;
&nbsp;
   &lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>The page looks like this in <a href="http://www.apple.com/safari/">Safari</a></p>
<div align="center">
<a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails02/eodgoogle.png" rel="lightbox[eodgoogle]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails02/th_eodgoogle.png" border="0" alt="Instruments" ></a>
</div>
<h4>The controller</h4>
<p>The controller does all the hard work of querying for instruments and retrieving data in CSV format. The data consists of a Date, Open, HighLow, Close and Volume column. I installed the REST plugin for the HTTP communication part</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails install-plugin rest</pre></td></tr></table></div>

<p>The plugin injects amongst others the <a href="http://www.grails.org/plugin/rest">withAsyncHttp</a> method.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
50
51
52
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">finance</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">static</span> groovyx.<span style="color: #006600;">net</span>.<span style="color: #006600;">http</span>.<span style="color: #006600;">ContentType</span>.<span style="color: #aaaadd; font-weight: bold;">HTML</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.joda.time.*</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.joda.time.format.*</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.joda.time.contrib.hibernate.*</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> FieldValueController <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> scaffold <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">true</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> eodgoogle <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">def</span> instruments <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Instrument</span>.<span style="color: #663399;">findAll</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #663399;">findAll</span> <span style="color: #66cc66;">&#123;</span> 
         instrument <span style="color: #66cc66;">-&gt;</span> instrument.<span style="color: #006600;">source</span>.<span style="color: #006600;">name</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">'Google Finance'</span>
      <span style="color: #66cc66;">&#125;</span>
      <span style="color: #66cc66;">&#91;</span>instruments : instruments<span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">def</span> retrieve <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
      withAsyncHttp<span style="color: #66cc66;">&#40;</span>poolSize : <span style="color: #cc66cc;">4</span>, uri : <span style="color: #ff0000;">&quot;http://finance.google.com&quot;</span>, contentType : <span style="color: #aaaadd; font-weight: bold;">HTML</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
         <span style="color: #000000; font-weight: bold;">def</span> result <span style="color: #66cc66;">=</span> <span style="color: #663399;">get</span><span style="color: #66cc66;">&#40;</span>path:<span style="color: #ff0000;">'/finance/historical'</span>, query: <span style="color: #66cc66;">&#91;</span>q:params.<span style="color: #006600;">symbol</span>, output:<span style="color: #ff0000;">'csv'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
            resp, html <span style="color: #66cc66;">-&gt;</span>  <span style="color: #993399;">println</span> <span style="color: #ff0000;">' got async response!'</span> 
            <span style="color: #000000; font-weight: bold;">return</span> html
         <span style="color: #66cc66;">&#125;</span> 
&nbsp;
         <span style="color: #000000; font-weight: bold;">assert</span> result <span style="color: #000000; font-weight: bold;">instanceof</span> java.<span style="color: #006600;">util</span>.<span style="color: #006600;">concurrent</span>.<span style="color: #006600;">Future</span>
&nbsp;
         <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span> result.<span style="color: #006600;">done</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
            <span style="color: #aaaadd; font-weight: bold;">Thread</span>.<span style="color: #006600;">sleep</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2000</span><span style="color: #66cc66;">&#41;</span> 
         <span style="color: #66cc66;">&#125;</span>
&nbsp;
         <span style="color: #000000; font-weight: bold;">def</span> html <span style="color: #66cc66;">=</span> result.<span style="color: #663399;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
         <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>line <span style="color: #b1b100;">in</span> html.<span style="color: #006600;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>line.<span style="color: #CC0099;">contains</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Date'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
               <span style="color: #000000; font-weight: bold;">def</span> fields <span style="color: #66cc66;">=</span> line.<span style="color: #006600;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">','</span><span style="color: #66cc66;">&#41;</span>
               <span style="color: #000000; font-weight: bold;">def</span> dateTime <span style="color: #66cc66;">=</span> DateTimeFormat.<span style="color: #006600;">forPattern</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;dd-MMM-yy&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">parseDateTime</span><span style="color: #66cc66;">&#40;</span>fields<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
               <span style="color: #000000; font-weight: bold;">def</span> instrument <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Instrument</span>.<span style="color: #006600;">findBySymbol</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">symbol</span><span style="color: #66cc66;">&#41;</span>
               saveValueByFieldName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Open'</span>, dateTime, instrument, fields<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
               saveValueByFieldName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'High'</span>, dateTime, instrument, fields<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
               saveValueByFieldName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Low'</span>, dateTime, instrument, fields<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
               saveValueByFieldName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Close'</span>, dateTime, instrument, fields<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
               saveValueByFieldName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Volume'</span>, dateTime, instrument, fields<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#125;</span>
         <span style="color: #66cc66;">&#125;</span>
      <span style="color: #66cc66;">&#125;</span>
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">def</span> saveValueByFieldName<span style="color: #66cc66;">&#40;</span>fieldName, dateTime, instrument, val<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">def</span> mnemonic <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Field</span>.<span style="color: #006600;">findByName</span><span style="color: #66cc66;">&#40;</span>fieldName<span style="color: #66cc66;">&#41;</span>
         <span style="color: #000000; font-weight: bold;">def</span> fVal <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FieldValue<span style="color: #66cc66;">&#40;</span>added: dateTime, val: val, instrument: instrument, field:mnemonic<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>A screenshot of the historical values list is shown here below. Mmm, gold &#8230;</p>
<div align="center">
<a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails02/values.png" rel="lightbox[values]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails02/th_values.png" border="0" alt="Values" ></a>
</div>
<p>Notice that no effort is required to get pagination.</p>
<h4>Conclusion</h4>
<p>Grails is really perfect for secret home projects, especially for weekend ones. Next on the Grails finance roadmap is charting, some mathematical analysis, a REST webservice, lots of polishing and even more features, which will remain secret for now.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://ivanidris.net/wordpress/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://ivanidris.net/wordpress/index.php/2010/07/18/grails-finance-0-2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grails Finance 0.1</title>
		<link>http://ivanidris.net/wordpress/index.php/2010/07/11/grails-finance-0-1</link>
		<comments>http://ivanidris.net/wordpress/index.php/2010/07/11/grails-finance-0-1#comments</comments>
		<pubDate>Sun, 11 Jul 2010 20:23:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://ivanidris.net/wordpress/index.php/2010/07/11/grails-finance-0-1</guid>
		<description><![CDATA[Grails is an automagical RAD web application framework in the style of Ruby on Rails. Grails uses Groovy as its main language. You could also substitute it with Java, but that would be less fun. As an example I made a small web application for financial data. Installation In Mac Ports this command installs Grails [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.grails.org/">Grails</a> is an automagical RAD web application framework in the style of <a href="http://rubyonrails.org/">Ruby on Rails</a>. <a href="http://www.grails.org/">Grails</a> uses <a href="http://groovy.codehaus.org/">Groovy</a> as its main language. You could also substitute it with Java, but that would be less fun. As an example I made a small web application for financial data.</p>
<h4>Installation</h4>
<p>In <a href="http://www.macports.org/">Mac Ports</a> this command installs Grails</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #c20cb9; font-weight: bold;">install</span> grails</pre></td></tr></table></div>

<p>to be exact it installs version 1.3.2 Just this week version 1.3.3 was released. You still need to put Grails in your path</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GRAILS_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>java<span style="color: #000000; font-weight: bold;">/</span>grails
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PATH</span>=<span style="color: #007800;">$PATH</span>:<span style="color: #007800;">$GRAILS_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin</pre></td></tr></table></div>

<h4>Creating the app</h4>
<p>Grails lets you generate code and configuration files from the command line or if you are a wimp you can use an IDE instead. This command generates the application</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails create-app finance
<span style="color: #7a0874; font-weight: bold;">cd</span> finance</pre></td></tr></table></div>

<p>You can already run the application,</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails run-app</pre></td></tr></table></div>

<p>but of course, there is not much to see yet.</p>
<div align="center">
<table>
<tr>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails01/firstGrailsApp.png" rel="lightbox[InitialApp]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails01/th_firstGrailsApp.png" border="0" alt="Initial app" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails01/scaffoldedInitial.png" rel="lightbox[dInitialApp]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails01/th_scaffoldedInitial.png" border="0" alt="Create screen" ></a></td>
<td><a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails01/scaffoldedCreated.png" rel=="lightbox[InitialApp]" rel="lightbox[46]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails01/th_scaffoldedCreated.png" border="0" alt="Create screen" ></a></td>
</tr>
</table>
</div>
<h4>Grails MVC</h4>
<p>Grails reinvents <a href="http://en.wikipedia.org/wiki/Model-view-controller">MVC</a> and provides scripts for it. Here is how we can create a controller</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails create-controller finance
&nbsp;
Resolving dependencies...
Dependencies resolved <span style="color: #000000; font-weight: bold;">in</span> 1439ms.
Running script <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>java<span style="color: #000000; font-weight: bold;">/</span>grails<span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>CreateController.groovy
Environment <span style="color: #000000; font-weight: bold;">set</span> to development
    <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #c20cb9; font-weight: bold;">mkdir</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Created <span style="color: #c20cb9; font-weight: bold;">dir</span>: ... finance<span style="color: #000000; font-weight: bold;">/</span>grails-app<span style="color: #000000; font-weight: bold;">/</span>controllers<span style="color: #000000; font-weight: bold;">/</span>finance
Created Controller <span style="color: #000000; font-weight: bold;">for</span> Finance
    <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #c20cb9; font-weight: bold;">mkdir</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Created <span style="color: #c20cb9; font-weight: bold;">dir</span>: ... finance<span style="color: #000000; font-weight: bold;">/</span>grails-app<span style="color: #000000; font-weight: bold;">/</span>views<span style="color: #000000; font-weight: bold;">/</span>finance
  <span style="color: #7a0874; font-weight: bold;">&#91;</span>groovyc<span style="color: #7a0874; font-weight: bold;">&#93;</span> Compiling <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #c20cb9; font-weight: bold;">file</span> to <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>ivanidris<span style="color: #000000; font-weight: bold;">/</span>Projects<span style="color: #000000; font-weight: bold;">/</span>grails<span style="color: #000000; font-weight: bold;">/</span>finance<span style="color: #000000; font-weight: bold;">/</span>target<span style="color: #000000; font-weight: bold;">/</span>classes
    <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #c20cb9; font-weight: bold;">mkdir</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Created <span style="color: #c20cb9; font-weight: bold;">dir</span>: ... finance<span style="color: #000000; font-weight: bold;">/</span>test<span style="color: #000000; font-weight: bold;">/</span>unit<span style="color: #000000; font-weight: bold;">/</span>finance
Created Tests <span style="color: #000000; font-weight: bold;">for</span> Finance</pre></td></tr></table></div>

<p>Grails generates some tests for the lazy <a href="http://en.wikipedia.org/wiki/Agile_software_development">Agile</a> developer and a controller</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">finance</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> FinanceController <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> index <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Now we can create some entity model classes or as Grails calls them domain classes</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">grails create<span style="color: #66cc66;">-</span>domain<span style="color: #66cc66;">-</span><span style="color: #000000; font-weight: bold;">class</span> datasource</pre></td></tr></table></div>

<p>The datasource class will store information for the financial data sources. They will all be free, of course, since this is a home project.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">finance</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Datasource <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> name
    <span style="color: #aaaadd; font-weight: bold;">String</span> description
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> constraints <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> 
    <span style="color: #66cc66;">&#125;</span>   
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h4>Tweaking</h4>
<p>In grails-app/conf/DataSource.groovy you can find configuration settings for different environments such as test, development and production. I changed the development settings so that the database will be persisted to a file for now. Every time Grails notices a change in the code the server is restarted. I changed the <b>dbCreate</b> parameter otherwise with the initial value of <b>create-drop</b> the database would have been dropped.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">    development <span style="color: #66cc66;">&#123;</span>
        dataSource <span style="color: #66cc66;">&#123;</span>
            dbCreate <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;update&quot;</span> <span style="color: #808080; font-style: italic;">// one of 'create', 'create-drop','update'</span>
            url <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;jdbc:hsqldb:file:devDB;shutdown=true&quot;</span>
        <span style="color: #66cc66;">&#125;</span>   
    <span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h4>Scaffolding feature</h4>
<p>Scaffolding gives you basic CRUD screens for your domain classes. I noticed a feature, when I setup scaffolding for Datasource in the main controller.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">finance</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> FinanceController <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> scaffold <span style="color: #66cc66;">=</span> Datasource  
    <span style="color: #000000; font-weight: bold;">def</span> index <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">&#125;</span> 
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The following stack trace was produced</p>
<pre>
ERROR view.ScaffoldingViewResolver  - Error generating scaffolded view [/finance/index]: /opt/local/share/java/grails/src/grails/templates/scaffolding/index.gsp (No such file or directory)
java.io.FileNotFoundException: /opt/local/share/java/grails/src/grails/templates/scaffolding/index.gsp (No such file or directory)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(FileInputStream.java:106)
	at java.lang.Thread.run(Thread.java:637)
</pre>
<p>The error message although correct was not very helpful. The fix was to change the controller to this</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">finance</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> FinanceController <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> scaffold <span style="color: #66cc66;">=</span> Datasource  
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h4>Constraints</h4>
<p>You can define constraints for your domain classes. For instance, here <b>name</b> is required to be not null or blank. There are a few other possible constraints, such as defining a field to be an email address or URL.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">finance</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Datasource <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> name
    <span style="color: #aaaadd; font-weight: bold;">String</span> description
    <span style="color: #aaaadd; font-weight: bold;">Date</span> added <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> constraints <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> 
      name<span style="color: #66cc66;">&#40;</span>blank:<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
      description<span style="color: #66cc66;">&#40;</span>maxSize:<span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>   
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h4>More commands</h4>
<p>At a certain point you will want to make a WAR</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails war
&nbsp;
Done creating WAR  ... finance<span style="color: #000000; font-weight: bold;">/</span>target<span style="color: #000000; font-weight: bold;">/</span>finance-0.1.war</pre></td></tr></table></div>

<p>Also you might be interested in how many files and lines of code were written</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails stats
&nbsp;
    +----------------------+-------+-------+
    <span style="color: #000000; font-weight: bold;">|</span> Name                 <span style="color: #000000; font-weight: bold;">|</span> Files <span style="color: #000000; font-weight: bold;">|</span>  LOC  <span style="color: #000000; font-weight: bold;">|</span>
    +----------------------+-------+-------+
    <span style="color: #000000; font-weight: bold;">|</span> Controllers          <span style="color: #000000; font-weight: bold;">|</span>     <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span>     <span style="color: #000000;">4</span> <span style="color: #000000; font-weight: bold;">|</span> 
    <span style="color: #000000; font-weight: bold;">|</span> Domain Classes       <span style="color: #000000; font-weight: bold;">|</span>     <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span>    <span style="color: #000000;">10</span> <span style="color: #000000; font-weight: bold;">|</span> 
    <span style="color: #000000; font-weight: bold;">|</span> Unit Tests           <span style="color: #000000; font-weight: bold;">|</span>     <span style="color: #000000;">2</span> <span style="color: #000000; font-weight: bold;">|</span>    <span style="color: #000000;">24</span> <span style="color: #000000; font-weight: bold;">|</span> 
    +----------------------+-------+-------+
    <span style="color: #000000; font-weight: bold;">|</span> Totals               <span style="color: #000000; font-weight: bold;">|</span>     <span style="color: #000000;">4</span> <span style="color: #000000; font-weight: bold;">|</span>    <span style="color: #000000;">38</span> <span style="color: #000000; font-weight: bold;">|</span> 
    +----------------------+-------+-------+</pre></td></tr></table></div>

<p>Not bad for a few minutes work. Strange that the configuration files and views are not listed.</p>
<h4>Instrument type</h4>
<p>Now it&#8217;s time to get serious about the domain model. Well not production type serious, more secret home weekend project type serious <img src='http://ivanidris.net/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . First let&#8217;s define <b>InstrumentType</b>. Every instrument would have a type, unless of course the type is unknown. The 1:1 relation description is set in <b>belongsTo</b>. The <b>type</b> is set to be unique. It is recommended to have a <b>toString</b> implementation, otherwise Grails will display the classname and record id by default in the UI.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">finance</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> InstrumentType <span style="color: #66cc66;">&#123;</span>         
    <span style="color: #000000; font-weight: bold;">static</span> belongsTo <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Instrument</span>   
&nbsp;
    <span style="color: #aaaadd; font-weight: bold;">String</span> type                
    <span style="color: #aaaadd; font-weight: bold;">String</span> description         
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> constraints <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>     
      type<span style="color: #66cc66;">&#40;</span>unique:<span style="color: #000000; font-weight: bold;">true</span>, blank:<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>  
    <span style="color: #66cc66;">&#125;</span> 
&nbsp;
    <span style="color: #aaaadd; font-weight: bold;">String</span> toString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #ff0000;">&quot;${type}&quot;</span>
    <span style="color: #66cc66;">&#125;</span>   
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h4>Instrument</h4>
<p>The amazing thing about the financial industry, is that, there are no real identification standards. You would think, that for such a multi billion type business, some general scheme would exist. Each data vendor has their own symbology so for the same instrument we would have different records. One for each data source.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">finance</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #aaaadd; font-weight: bold;">Instrument</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> name
    <span style="color: #aaaadd; font-weight: bold;">String</span> symbol
    InstrumentType instrumentType
    <span style="color: #aaaadd; font-weight: bold;">Date</span> added <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    Datasource source
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> constraints <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
      name<span style="color: #66cc66;">&#40;</span>unique:<span style="color: #000000; font-weight: bold;">true</span>, blank:<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
      symbol<span style="color: #66cc66;">&#40;</span>unique:<span style="color: #000000; font-weight: bold;">true</span>, blank:<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
      source<span style="color: #66cc66;">&#40;</span>blank:<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> mapping <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
      instrumentType lazy:<span style="color: #000000; font-weight: bold;">false</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> hasMany <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span> <span style="color: #CC0099;">contains</span>:<span style="color: #aaaadd; font-weight: bold;">Instrument</span>, values:FieldValue<span style="color: #66cc66;">&#93;</span>
&nbsp;
    <span style="color: #aaaadd; font-weight: bold;">String</span> toString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #ff0000;">&quot;${name} ${symbol} ${source}&quot;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>We can have the instrument type be lazy fetched or in this case eagerly. <b>hasMany</b> here indicates that an instrument can contain other instruments and could have many values.</p>
<h4>Field</h4>
<p>A field could be an attribute that does not change a lot or a price. It might be useful to have a field type domain class as well, but I decided to keep things simple for now.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">finance</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #aaaadd; font-weight: bold;">Field</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> name
    <span style="color: #aaaadd; font-weight: bold;">String</span> description
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> constraints <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> 
       name<span style="color: #66cc66;">&#40;</span>blank:<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>   
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> hasMany <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span> values : FieldValue <span style="color: #66cc66;">&#93;</span>
&nbsp;
    <span style="color: #aaaadd; font-weight: bold;">String</span> toString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>        
      <span style="color: #ff0000;">&quot;${name}&quot;</span>
    <span style="color: #66cc66;">&#125;</span> 
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h4>FieldValue</h4>
<p>Value is a keyword I think, just like values is a <a href="http://www.1keydata.com/sql/sqlinsert.html">SQL keyword</a>. Value is not allowed by groovyc</p>
<pre>

Apparent variable 'Value' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
  [groovyc] You attempted to reference a variable in the binding or an instance variable from a static context.
  [groovyc] You misspelled a classname or statically imported field. Please check the spelling.
  [groovyc] You attempted to use a method 'Value' but left out brackets in a place not allowed by the grammar.
  [groovyc]  @ line 11, column 33.
  [groovyc]        static hasMany = [ values : Value ]
  [groovyc]                                    ^
  [groovyc]
  [groovyc] 1 error
</pre>
<p>For the field values we need a proper timestamp. <b>java.util.Date</b> is not good enough for that so we will need <a href="http://joda-time.sourceforge.net/">Jodatime</a>. Fortunately Grails has a whole plugin ecosystem, including a Jodatime plugin that we can install.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails install-plugin joda-time
grails install-joda-time-gorm-mappings
grails install-joda-time-templates</pre></td></tr></table></div>

<p>The value is stored as a String. This is not type safe, so it would have been better to have a value table for each data type.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">finance</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.joda.time.*</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.joda.time.contrib.hibernate.*</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> FieldValue <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> val 
    DateTime timestamp
    <span style="color: #000000; font-weight: bold;">static</span> constraints <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> 
      val<span style="color: #66cc66;">&#40;</span>blank:<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
      timestamp<span style="color: #66cc66;">&#40;</span>blank:<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>   
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> belongsTo <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #aaaadd; font-weight: bold;">Instrument</span>, <span style="color: #aaaadd; font-weight: bold;">Field</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
    <span style="color: #aaaadd; font-weight: bold;">String</span> toString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>        
      <span style="color: #ff0000;">&quot;${val} ${timestamp}&quot;</span>    
    <span style="color: #66cc66;">&#125;</span> 
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h4>Final scaffolding</h4>
<p>Now we need to run</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">grails create-controller Datasource
grails create-controller Field
grails create-controller FieldValue
grails create-controller Instrument
grails create-controller InstrumentType</pre></td></tr></table></div>

<p>to create a controller for each domain class. Then put def scaffold = true in each controller to have Grails create basic CRUD functionality.</p>
<div align="center">
<a href="http://s724.photobucket.com/albums/ww242/ivanidris/grails01/instrument.png" rel="lightbox[instrument]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/grails01/th_instrument.png" border="0" alt="Instrument list" ></a>
</div>
<h4>Conclusion</h4>
<p>Grails is really easy to use and definitely more fun than the Java alternatives. Whenever I have time, I will retrieve some data and populate the database with some values. The next step would be to do some analysis on the data.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://ivanidris.net/wordpress/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://ivanidris.net/wordpress/index.php/2010/07/11/grails-finance-0-1/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Short guide to Scala</title>
		<link>http://ivanidris.net/wordpress/index.php/2010/07/04/short-guide-to-scala</link>
		<comments>http://ivanidris.net/wordpress/index.php/2010/07/04/short-guide-to-scala#comments</comments>
		<pubDate>Sun, 04 Jul 2010 09:57:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://ivanidris.net/wordpress/index.php/2010/07/04/short-guide-to-scala</guid>
		<description><![CDATA[Dedicated to Scala Johansson What is Scala? Scala is a scalable language based on Java. It adds functional programming features to the normal Java OO. One of the main advantages of Scala is that you can write shorter programs than in Java and still benefit from the many Java libraries out there. For instance this [...]]]></description>
			<content:encoded><![CDATA[<p><i>Dedicated to Scala Johansson</i></p>
<p><br/></p>
<h3>What is Scala?</h3>
<p>Scala is a scalable language based on Java. It adds functional programming features to the normal Java OO. One of the main advantages of Scala is that you can write shorter programs than in Java and still benefit from the many Java libraries out there. For instance this code in Java</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AClass <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> myString<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">public</span> AClass<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> string<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myString</span> <span style="color: #339933;">=</span> string<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>becomes</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> AClass<span style="color: #F78811;">&#40;</span>myString<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>As you can see semicolons are optional. Strangely enough Scala is statically typed. In fact the type system is pretty complex. I guess it is a matter of opinion whether this is good or bad.</p>
<h3>Getting started</h3>
<h4>Variables</h4>
<p>There are two types of variables that you can define <b>val</b> and <b>var</b>. A val represents a constant value and is final. A var can change.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> greeting <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;Hi&quot;</span></pre></td></tr></table></div>

<p>Notice that you don&#8217;t have to specify the type. This is called type inference, because <b>greeting</b> was initialized with a String literal, Scala figured out that <b>greeting</b> is of type String.</p>
<h4>Functions</h4>
<p>Functions are defined with <b>def</b>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> min<span style="color: #F78811;">&#40;</span>i<span style="color: #000080;">:</span> Int, j<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Int <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
   <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>i <span style="color: #000080;">&lt;</span> j<span style="color: #F78811;">&#41;</span> x
      <span style="color: #0000ff; font-weight: bold;">else</span> j
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>This code is pretty self explanatory. All the Java types have an equivalent in Scala with similar name. The exception is <b>void</b> which matches <b>Unit</b> in Scala.</p>
<h4>Tuples</h4>
<p>Scala adds tupples to the Java collection classes. Tuples can contain different type of elements. In Java you would have to create a holder class for this data. The code below creates a tuple and prints its contents</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> myTuple <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span>, <span style="color: #6666FF;">&quot;Ivan&quot;</span><span style="color: #F78811;">&#41;</span>
println<span style="color: #F78811;">&#40;</span>myTuple.<span style="color: #000080;">_</span>1<span style="color: #F78811;">&#41;</span>
println<span style="color: #F78811;">&#40;</span>myTuple.<span style="color: #000080;">_</span>2<span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<h4>Simple scripts</h4>
<p>Here is a script that can display lines in a file or files like the <b>cat</b> command in Unix.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">#!</span>/bin/sh
exec scala <span style="color: #6666FF;">&quot;$0&quot;</span> <span style="color: #6666FF;">&quot;$@&quot;</span>
<span style="color: #000080;">!#</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">io</span>.<span style="color: #000000;">Source</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>args.<span style="color: #000000;">length</span> <span style="color: #000080;">&gt;</span> <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
   <span style="color: #0000ff; font-weight: bold;">for</span><span style="color: #F78811;">&#40;</span>arg <span style="color: #000080;">&lt;</span>- args<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>line <span style="color: #000080;">&lt;</span>- Source.<span style="color: #000000;">fromFile</span><span style="color: #F78811;">&#40;</span>arg<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">getLines</span><span style="color: #F78811;">&#41;</span>
         print<span style="color: #F78811;">&#40;</span>line<span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span>
   <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>You can run this as a shell script.</p>
<h3>Pure OO</h3>
<p>Scala classes cannot have static members. There are no primitives. Every value is an object. In Scala every method is an operator and vice versa. For instance we can define addition, multiplication and other operations for complex numbers.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> Complex<span style="color: #F78811;">&#40;</span>r <span style="color: #000080;">:</span> Double, i <span style="color: #000080;">:</span> Double<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">var</span> real <span style="color: #000080;">=</span> r
  <span style="color: #0000ff; font-weight: bold;">val</span> imaginary <span style="color: #000080;">=</span> i
  <span style="color: #0000ff; font-weight: bold;">val</span> modulus <span style="color: #000080;">=</span>  Math.<span style="color: #000000;">sqrt</span><span style="color: #F78811;">&#40;</span>r <span style="color: #000080;">*</span> r + i <span style="color: #000080;">*</span> i<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> +<span style="color: #F78811;">&#40;</span>that<span style="color: #000080;">:</span> Complex<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>Complex <span style="color: #000080;">=</span>
    <span style="color: #0000ff; font-weight: bold;">new</span> Complex<span style="color: #F78811;">&#40;</span>real + that.<span style="color: #000000;">real</span>, imaginary + that.<span style="color: #000000;">imaginary</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> -<span style="color: #F78811;">&#40;</span>that<span style="color: #000080;">:</span> Complex<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>Complex <span style="color: #000080;">=</span>
    <span style="color: #0000ff; font-weight: bold;">new</span> Complex<span style="color: #F78811;">&#40;</span>real - that.<span style="color: #000000;">real</span>, imaginary - that.<span style="color: #000000;">imaginary</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> <span style="color: #000080;">*</span> <span style="color: #F78811;">&#40;</span>that<span style="color: #000080;">:</span> Complex<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>Complex <span style="color: #000080;">=</span>
    <span style="color: #0000ff; font-weight: bold;">new</span> Complex<span style="color: #F78811;">&#40;</span>real <span style="color: #000080;">*</span> that.<span style="color: #000000;">real</span> - imaginary <span style="color: #000080;">*</span> that.<span style="color: #000000;">imaginary</span>,
      real <span style="color: #000080;">*</span> that.<span style="color: #000000;">imaginary</span> + imaginary <span style="color: #000080;">*</span> that.<span style="color: #000000;">real</span><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>and we can test it with JUnit or any other testing frameworks which is written in Java or Scala.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">junit</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> ComplexSuite <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> one <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Complex<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1.0</span>, <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> two <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Complex<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">2.0</span>, <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #000080;">@</span>Test
    <span style="color: #0000ff; font-weight: bold;">def</span> testAddition<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">val</span> sum <span style="color: #000080;">=</span> one + two
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span>sum.<span style="color: #000000;">real</span>, <span style="color: #F78811;">3.0</span>, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span>sum.<span style="color: #000000;">imaginary</span>, <span style="color: #F78811;">0</span>, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #000080;">@</span>Test
    <span style="color: #0000ff; font-weight: bold;">def</span> testSubtraction<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">val</span> difference <span style="color: #000080;">=</span> one - one
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span>difference.<span style="color: #000000;">real</span>, <span style="color: #F78811;">0</span>, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span>difference.<span style="color: #000000;">imaginary</span>, <span style="color: #F78811;">0</span>, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #000080;">@</span>Test
    <span style="color: #0000ff; font-weight: bold;">def</span> testMultiplication<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">val</span> product <span style="color: #000080;">=</span> one <span style="color: #000080;">*</span> two
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span>product.<span style="color: #000000;">real</span>, <span style="color: #F78811;">2.0</span>, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span>product.<span style="color: #000000;">imaginary</span>, <span style="color: #F78811;">0</span>, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #000080;">@</span>Test
    <span style="color: #0000ff; font-weight: bold;">def</span> testModulus<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span>one.<span style="color: #000000;">modulus</span>, <span style="color: #F78811;">1.0</span>, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span>two.<span style="color: #000000;">modulus</span>, <span style="color: #F78811;">2.0</span>, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Complex numbers have many applications &#8211; one of the them is fractals like the Mandelbrot set. These type of fractals are defined by a recursive formula, where you calculate the next complex number in a series by multiplying the current complex number you have by itself and adding a constant to it. If z is a complex number, you have the relation z * z + c, where c is a constant complex number. This can be graphed in the complex plane with horizontal real values axis and vertical imaginary values axis. I used the so called &#8220;escape time algorithm&#8221; to draw the fractal. The algorithm scans the points in a small region around the origin on a distance of about 2. Actually I used a square which was probably a little bit too big. Each of these points is used as the c value and is assigned a color based on the number of iterations it takes to escape the region. If it takes more than a predefined number of iterations to escape the pixel gets the default background color.</p>
<div align="center"<br />
<a href="http://s724.photobucket.com/albums/ww242/ivanidris/scala/fractalApp.png" rel="lightbox[Mandelbrot]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/scala/fractalApp.png" border="0" alt="Mandelbrot"></a>
</div>
<p>Here is the Maven pom.xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ComplexNumbers<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ComplexNumbers<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;repositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>scala-tools.org<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Scala-tools Maven2 Repository<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://scala-tools.org/repo-releases<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/repositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pluginRepositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pluginRepository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>scala-tools.org<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Scala-tools Maven2 Repository<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://scala-tools.org/repo-releases<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pluginRepository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pluginRepositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.7<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.scala-lang<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>scala-library<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.8.0.RC3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.scala-lang<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>scala-swing<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.8.0.RC3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pluginManagement<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.maven.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-compiler-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.0.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.scala-tools<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-scala-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.9.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pluginManagement<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.maven.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-compiler-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>compile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>compile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.scala-tools<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-scala-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>We can extend the Mandelbrot idea and use other integer powers instead of 2. The resulting fractals are called the Multibrot set. This calls for an ** operator and also division for negative powers.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;">  <span style="color: #0000ff; font-weight: bold;">def</span> / <span style="color: #F78811;">&#40;</span>that<span style="color: #000080;">:</span> Complex<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>Complex <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> denominator<span style="color: #000080;">:</span> Double <span style="color: #000080;">=</span> Math.<span style="color: #000000;">pow</span><span style="color: #F78811;">&#40;</span>that.<span style="color: #000000;">modulus</span>, <span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> realNumerator <span style="color: #000080;">=</span> real <span style="color: #000080;">*</span> that.<span style="color: #000000;">real</span> + imaginary <span style="color: #000080;">*</span> that.<span style="color: #000000;">imaginary</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> imagNumerator <span style="color: #000080;">=</span> imaginary <span style="color: #000080;">*</span> that.<span style="color: #000000;">real</span> - real <span style="color: #000080;">*</span> that.<span style="color: #000000;">imaginary</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">return</span> <span style="color: #0000ff; font-weight: bold;">new</span> Complex<span style="color: #F78811;">&#40;</span> realNumerator/ denominator, imagNumerator/ denominator<span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> <span style="color: #000080;">**</span> <span style="color: #F78811;">&#40;</span>n<span style="color: #000080;">:</span>Int<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>Complex <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">var</span> z <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Complex<span style="color: #F78811;">&#40;</span>r, i<span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">if</span><span style="color: #F78811;">&#40;</span>n <span style="color: #000080;">&gt;</span> <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">for</span><span style="color: #F78811;">&#40;</span>j <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">1</span> until n<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
          z <span style="color: #000080;">*=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Complex<span style="color: #F78811;">&#40;</span>r, i<span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span>
    <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">for</span><span style="color: #F78811;">&#40;</span>j <span style="color: #000080;">&lt;</span>- n until <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
         z /<span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Complex<span style="color: #F78811;">&#40;</span>r, i<span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">return</span> z
&nbsp;
  <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>As a side note the Moivre formula did not work properly although it should in theory. Since this is not production code I did not look further for better algorithms. The corresponding unit tests are here.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;">    <span style="color: #000080;">@</span>Test
    <span style="color: #0000ff; font-weight: bold;">def</span> testDivision<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">2</span>, two / one real, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span>, two / one imaginary, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #000080;">@</span>Test
    <span style="color: #0000ff; font-weight: bold;">def</span> testPower<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">val</span> i <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Complex<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0.0</span>, <span style="color: #F78811;">1.0</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #0000ff; font-weight: bold;">val</span> i2 <span style="color: #000080;">=</span> i <span style="color: #000080;">**</span> <span style="color: #F78811;">2</span>
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span>i2.<span style="color: #000000;">real</span>, -<span style="color: #F78811;">1</span>, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span>i2.<span style="color: #000000;">imaginary</span>, <span style="color: #F78811;">0</span>, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
&nbsp;
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span>, one <span style="color: #000080;">**</span> <span style="color: #F78811;">2</span> real, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span>, one <span style="color: #000080;">**</span> <span style="color: #F78811;">2</span> imaginary, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
&nbsp;
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">4</span>, two <span style="color: #000080;">**</span> <span style="color: #F78811;">2</span> real, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span>, two <span style="color: #000080;">**</span> <span style="color: #F78811;">2</span> imaginary, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
&nbsp;
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">8</span>, two <span style="color: #000080;">**</span> <span style="color: #F78811;">3</span> real, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
      Assert.<span style="color: #000000;">assertEquals</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span>, two <span style="color: #000080;">**</span> <span style="color: #F78811;">3</span> imaginary, <span style="color: #F78811;">0.1</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>I also changed the main application class a bit. Scala has lots of wrappers for Swing, with lots of improvements. Unfortunately, sometimes it gets confusing because the names of the wrapper classes are often the same as the ones from swing.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> java.<span style="color: #000000;">awt</span>.<span style="color: #000000;">image</span>.<span style="color: #000000;">BufferedImage</span>
<span style="color: #0000ff; font-weight: bold;">import</span> java.<span style="color: #000000;">awt</span>.<span style="color: #F78811;">&#123;</span>Toolkit, Color, Graphics<span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">import</span> java.<span style="color: #000000;">util</span>.<span style="color: #F78811;">&#123;</span>Arrays, TimerTask, Timer<span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">swing</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> event.<span style="color: #000000;">ActionEvent</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> FractalApp <span style="color: #0000ff; font-weight: bold;">extends</span> SimpleSwingApplication <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> DIM <span style="color: #000080;">=</span> <span style="color: #F78811;">400</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> MAX<span style="color: #000080;">_</span>ITERATION <span style="color: #000080;">=</span> <span style="color: #F78811;">300</span>
&nbsp;
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">def</span> top <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> MainFrame <span style="color: #F78811;">&#123;</span>
      title <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;Fractal App&quot;</span>
      preferredSize<span style="color: #000080;">_=</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> Dimension<span style="color: #F78811;">&#40;</span>DIM, DIM<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      contents <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Panel <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">var</span> img <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> BufferedImage<span style="color: #F78811;">&#40;</span>DIM,DIM,BufferedImage.<span style="color: #000000;">TYPE_INT_RGB</span><span style="color: #F78811;">&#41;</span>
        <span style="color: #0000ff; font-weight: bold;">this</span>.<span style="color: #000000;">peer</span>.<span style="color: #000000;">setDoubleBuffered</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span>
        <span style="color: #0000ff; font-weight: bold;">val</span> timer <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Timer<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span>
        timer.<span style="color: #000000;">schedule</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> ScheduleTask<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>, <span style="color: #F78811;">500</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span>
&nbsp;
&nbsp;
        <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> paintComponent<span style="color: #F78811;">&#40;</span>g <span style="color: #000080;">:</span> Graphics2D<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> Unit <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
          <span style="color: #0000ff; font-weight: bold;">super</span>.<span style="color: #000000;">paintComponent</span><span style="color: #F78811;">&#40;</span>g<span style="color: #F78811;">&#41;</span>
&nbsp;
          g.<span style="color: #000000;">drawImage</span><span style="color: #F78811;">&#40;</span>img, <span style="color: #0000ff; font-weight: bold;">null</span>, <span style="color: #F78811;">0</span>, <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span>
          Toolkit.<span style="color: #000000;">getDefaultToolkit</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">sync</span>
&nbsp;
          g.<span style="color: #000000;">dispose</span>
&nbsp;
        <span style="color: #F78811;">&#125;</span>
&nbsp;
        <span style="color: #0000ff; font-weight: bold;">class</span> ScheduleTask <span style="color: #0000ff; font-weight: bold;">extends</span> TimerTask <span style="color: #F78811;">&#123;</span>
&nbsp;
          <span style="color: #0000ff; font-weight: bold;">def</span> run<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>Unit <span style="color: #000080;">=</span><span style="color: #F78811;">&#123;</span>
&nbsp;
            <span style="color: #0000ff; font-weight: bold;">for</span><span style="color: #F78811;">&#40;</span>d <span style="color: #000080;">&lt;</span>- -<span style="color: #F78811;">9</span> to <span style="color: #F78811;">9</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
              img.<span style="color: #000000;">flush</span>
              <span style="color: #0000ff; font-weight: bold;">for</span><span style="color: #F78811;">&#40;</span>i <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">1</span> until DIM<span style="color: #000080;">;</span> j <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">1</span> until DIM<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
                <span style="color: #0000ff; font-weight: bold;">val</span> z0 <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Complex<span style="color: #F78811;">&#40;</span>-<span style="color: #F78811;">2.0</span> + i <span style="color: #000080;">*</span> <span style="color: #F78811;">4.0</span> / DIM , -<span style="color: #F78811;">2.0</span> + j <span style="color: #000080;">*</span> <span style="color: #F78811;">4.0</span> / DIM<span style="color: #F78811;">&#41;</span>
                <span style="color: #0000ff; font-weight: bold;">val</span> color <span style="color: #000080;">=</span> calculateColor<span style="color: #F78811;">&#40;</span>z0, <span style="color: #F78811;">0</span>, z0, d<span style="color: #F78811;">&#41;</span>
                img.<span style="color: #000000;">setRGB</span><span style="color: #F78811;">&#40;</span>i, j, color<span style="color: #F78811;">&#41;</span>
              <span style="color: #F78811;">&#125;</span>
&nbsp;
              System.<span style="color: #000000;">out</span>.<span style="color: #000000;">println</span><span style="color: #F78811;">&#40;</span>d<span style="color: #F78811;">&#41;</span>
              repaint<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span>
            <span style="color: #F78811;">&#125;</span>
          <span style="color: #F78811;">&#125;</span>
        <span style="color: #F78811;">&#125;</span>
      <span style="color: #F78811;">&#125;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">def</span> calculateColor<span style="color: #F78811;">&#40;</span>z<span style="color: #000080;">:</span>Complex, n<span style="color: #000080;">:</span>Int, z0<span style="color: #000080;">:</span>Complex, d<span style="color: #000080;">:</span>Int<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>Int <span style="color: #000080;">=</span><span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">if</span><span style="color: #F78811;">&#40;</span>n <span style="color: #000080;">==</span> MAX<span style="color: #000080;">_</span>ITERATION<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">return</span> Color.<span style="color: #000000;">BLACK</span>.<span style="color: #000000;">getRGB</span>
      <span style="color: #F78811;">&#125;</span>
&nbsp;
      <span style="color: #0000ff; font-weight: bold;">if</span><span style="color: #F78811;">&#40;</span>z.<span style="color: #000000;">modulus</span> <span style="color: #000080;">&gt;=</span> <span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">return</span> n <span style="color: #000080;">*</span> n <span style="color: #000080;">*</span> MAX<span style="color: #000080;">_</span>ITERATION <span style="color: #000080;">*</span> MAX<span style="color: #000080;">_</span>ITERATION
      <span style="color: #F78811;">&#125;</span>
&nbsp;
      calculateColor<span style="color: #F78811;">&#40;</span>z <span style="color: #000080;">**</span> d + z0, n + <span style="color: #F78811;">1</span>, z0, d<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Demo run for powers -10 to 10</p>
<div align="center"<br />
<a href="http://s724.photobucket.com/albums/ww242/ivanidris/scala/multibrotz.gif" rel="lightbox[Multibrot set]"><img src="http://i724.photobucket.com/albums/ww242/ivanidris/scala/multibrotz.gif" border="0" alt="Multibrotz"></a>
</div>
<h3>Conclusion</h3>
<p>Scala is a new JVM language that is worth looking into. It shows lots of promise and offers new possibilities such as functional programming. You can write short scripts with Scala, Swing applications or anything else that you could write in Java. Scala tries to be better then Java without being Java, however in a statically typed way unlike Ruby or Groovy. Scala is by no means mainstream currently, but it might be by the time Scala Johansson graduates from college, starts a career etcetera, etcetera &#8230;</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://ivanidris.net/wordpress/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://ivanidris.net/wordpress/index.php/2010/07/04/short-guide-to-scala/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
