Grails Finance Services

This entry is part of 15 in the series Grails Finance

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 upgrade of Grails to 1.3.4. When I tried to create a new service with

1
grails create-service simulation

Grails complained about a version mismatch, so I had to do

1
grails update

to fix that. The service

1
2
3
4
5
6
7
8
9
10
11
class SimulationService {
 
    static transactional = true
 
    def create(name, description) {
	def simulation = new Simulation(name, description)
	simulation.save()
 
	return Simulation.get(simulation.id)
    }
}

can now be injected into the controller for example.

1
2
3
4
5
6
7
8
class SimulationController {
   def scaffold = true
	def simulationService
 
	def persist(name, description)  {
		def simulation = simulationService.save(name, description)
	}
}

Testing services

Grails created a unit test stub along with the service, but I also wanted an integration test.

1
grails create-integration-test SimulationServiceIntegration

Gant then suddenly reported a MissingPropertyException. Very mysterious. It turns out this is a known bug/issue GRAILS-6606. The fix was easy – adding the line

1
def type = "Tests"

to CreateIntegrationTest.groovy

1
2
3
4
5
6
7
8
9
10
11
...
target ('default': "Creates a new Grails integration test which loads the whole Grails environment when run") {
    depends(checkVersion, parseArguments)
 
    def type = "Tests"
    promptForName(type: "Integration test")
 
    def name = argsMap["params"][0]
    name = purgeRedundantArtifactSuffix(name, type)
    createIntegrationTest(name: name, suffix: "")
}

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

1
2
3
4
5
6
   ...
   def simulationService
 
    void testCreate() {
	assertNotNull simulationService
    }

Dependency Injection rules.

You can run all the tests with

1
grails test-app

Side topic: Levy distribution

It is a well known fact that stock prices etc. do not conform to the normal distribution. Apparently some people have the idea that the Levy distribution is more appropriate. One of these people is Mandelbrot of the fractals. Mandelbrot recognizes four schools of thought

  1. The most popular, technical analysts school exploiting short term changes.
  2. Random walk, statistically independent prices, but price changes are Gauss distributed.
  3. Another random walk believers school, but price changes have infinite variance.
  4. Random walk is a first order approximation, but there is also a second order!

This blog 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.

Random links of interest

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