Home Blog
 

So you want to use Genetic algorithms to do Forex trading, eh? :) Where should we start?  I'm going to use the specifics of JGAP (since that is the platform I've picked for my development) but the concepts should work with any genetic structure.

Let's start at the very beginning:  what exactly is the basis of what we are trying to implement? Is it the case that:

  1. we have a specific trading system or set of indicators that we think will do the trick when tweaked just so?  In this case the goal is to evolve a system with optimal parameters.
  2. we don't really have a specific system in mind.  We would then have a completely naive system, which starts from a large set of indicators and builds its own trading methods.

The first case, that of a specific trading system which is to be optimized, is the simpler case. Let's plan a Chromosome for a trivial trading system.  Define the trading system Genes as:

  1. a Long position is entered when a shorter-duration moving average crosses above a longer-duration MA, AND
  2. RSI rises from below a certain threshold to above a certain threshold
  3. No short positions will be taken
  4. position is closed when a fixed number of pips are gained, OR
  5. position is closed when a fixed number of pips are lost from the maximum advancement of the position (a trailing stop)

Gene 1 implements the cross of moving averages.  How many variables can we extract from that operation?

 
Component
Variables
shorter moving average bar length
  type (simple, weighted, exponentially smoothed)
longer moving average bar length
  type (simple, weighted, exponentially smoothed)

Really, it seems as though this should be something that is evolved on on its own. For a system of sufficient complexity the combinatorial expolsion of possibilities means that it would take much longer to find an optimal solution for any single gene within the whole.  So let's split this task out into a separate Chromosome.

Let's define the Moving Average Chromosome as having the four genes above.

Indicator variable range
ShorterMA bar length 5-15
Moving Average Chromosome
  Type simple, weighted, Smoothed EMA
LongerMA bar length 16-30
  type simple, weighted, Smoothed EM

Evolution of this should be tested by many runs through the Open Forex Platform backtesting framework.

But what is the fitness function? See The next post for that!  :)

Long time, no update!  :)

I have settled on a platform for general Forex development, Open Forex Platform (http://www.openforexplatform.com/).  It is open source, it has extensive backtesting and charting capabilites, it currently integrates with MetaTrader 4, and can be made to integrate with just about any broker or data source.  It is also a .NET platform.  So it is not perfect :)  Given that Scala will soon be natively appearing on .NET (see here) and that IKVM allows for importing most Java libraries, the .NET-ness of it is not a major hurdle.

I have been playing around with the Java Genetic Algorithms Package v3.4.4 (http://jgap.sourceforge.net/) via C#, from within Visual Studio 2008.  Using IKVM it was actually pretty simple.

  1. Download the latest JGAP source from SourceForge.net.  Get the "Full" distro
  2. I used Eclipse for the next few steps, not because its Maven integration is so great, but because of the FatJar plugin. FatJar allows you to build one single jar file for a project that includes all of the external dependencies usually found in other jar files. (As the plugin author notes, sometimes doing this can be a license violation.  Use your best judgement.) Other IDEs probably have an equivalent but I don't know what they are.  So I used Eclipse :)
  3. Import the JGAP project into your Eclipse workspace, via the pom.xml
  4. Build a fatJar of JGAP using the right-click context menu. Verify the all of the  Mine was named JGAP_fat.jar.
  5. Compile a .NET assembly using IKVM.  The command i used was ikvmc -target:library JGAP_fat.jar. The output is JGAP_fat.dll
  6. Create a new solution in Visual Studio
  7. Create a new C# project
  8. Add assembly references to the C# project, for JGAP_fat.dll and all of the dlls found in the IKVM bin directory
  9. Voila, that's it!

To test whether JGAP would work properly in the .NET environment, I reimplemented the JGAP tutorial (found here) in C#.

Here is the output of a few runs:

JGAPTest.exe 75
The best solution contained the following:
3 quarters.
0 dimes.
0 nickels.
0 pennies.
For a total of 75 cents in 3 coins.

JGAPTest.exe 99
The best solution contained the following:
3 quarters.
2 dimes.
0 nickels.
4 pennies.
For a total of 99 cents in 9 coins.

Here is the source code:

<Click the article header to read the rest!>

I attended the Scala LiftOff on Friday, in Reston VA.  It was a great learning experience!  There were some heavy hitters from the Scala community there, including the heaviest hitter: Martin Odersky

According to Odersky, Microsoft is working on a Scala compiler for .NET. This compiler should be out before the end of the year(2009).  This is very exciting news!

Why is this exciting (In a Forex trading context)?:  It provides a simple way to integrate my desire to do Forex trading in Scala.  I recently discovered an open source project called Open Forex Platform.  These fellows have worked out a programmatic trading platform that is capable of interfacing with MT4 (among other things).

There are many details missing but this is very promising!

Why?  Why not!

Source of hanoi.scala

object Hanoi {
  def main (args: Array[String]) : Unit = {
    var plates: Int = 4
    
    for { j <- args } plates = try { Integer.parseInt(j) } catch { case _ => 1 }
    val soln = hanoi(plates, "A", "C", "B")
    for { i <- soln }  Console.println(i)
  }

  def hanoi(n: Int, peg1: String, peg3: String, peg2: String) : List[String] = {
    n match {
      case 1 => List( "Move the plate from " + peg1 + " to " + peg3)
      case _ => hanoi(n - 1, peg1, peg2, peg3) :::
        List( "Move the plate from "+ peg1 + " to "+ peg3) :::
        hanoi(n - 1, peg2, peg3, peg1)
    }
  }
}

Compile and invoke as "scala Hanoi" or "scala Hanoi x" where x is the number of plates to solve.

More Googling has uncovered 2 new ways to invoke Java/Scala code with MetaTrader. Both are commercial products but licenses are reasonably priced.

The first takes an approach similiar to the one demonstrated in this blog posting.  The name of the product is JavaDllBuilder.  It compiles Java code into DLLs but uses a JNI translating layer to make the code available. 

The second is more interesting.  It is called DDE for Java, a DDE client for java (naturally!)  Using the approach of a Java program accessing MetaTrader through DDE I think I can take advantage all of the heavy lifting done by MetaTrader (indicator calculation, actual communication with the broker) and do all of the fun work in Scala!

Edit:  crap -- DDE is being dropped from MetaTrader 5.  Scratch that option!

Blog Navigation

The Latest

Scala on .NET

Posted by Eric Somdahl in Blog Entries

Towers of Hanoi in Scala

Posted by Eric Somdahl in Uncategorized
Joomla Templates by Joomlashack