Home Blog Genetic Algorithms
 

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!>

In (re)evaluating this project I should lay out some basic requirements for the Forex.  Here are hard and fast requirements:

  1. Back-testing of strategies
    • Use of 1-minute bars the bare minimum
    • Tick data back-testing preferable
  2. Unattended, automatic execution of trades
  3. Large number of technical indicators can be easily plugged in
  4. Should be able to communicate to me (via email or SMS) in real time as things happen, good or bad

Here are some desires, either bells and whistles or what will make this fun rather than work:

  1. Implementation (at least in part) using Scala and Java
  2. Graphs with technical indicators, for live data and back-testing
  3. Use of GA (Genetic Algorithms) to decide when to trade
  4. Should be able to receive communications from me (via email or SMS), in emergency situations.  For example, in the case of a catasrophic news event, I would like to be able to send an SMS to it indicating it should close all positions immediately and cease trading.
  5. Be portable across Forex brokers
  6. Use tools that are Open Source as much as possible

 

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