Forex Howto

17 Oct, 2008

Genetic programming an automated trading agent

Posted by: Milton In: Automation| Neural Networks

Some years back I did some research into Genetic Programming and using it for automated trading. Needless to say it didn’t get the results I was looking for so I gave up relatively quickly. Since then I’ve looked at many other methods of automated trading and now I’m back to Genetic Programming. I’ve learnt more and think with some better input data and looking for the right problem to solve it will hopefully be more successful.

The idea with Genetic Programming is to evolve a computer program in the same way that evolution happens in the real world. So you start with a randomly generated population of which there will be some that are fitter than others, you keep those (survival of the fittest) and have them interbreed thereby passing on features and mutating as well as generating new ones randomly… do this over many many generations and hopefully you end up with a damn smart program which is an expert at trading.

I’ve set a spare computer to spend it’s hours evolving these trading agents, preliminary results appear to be on the way to successful.

Finding the problem to solve appears to be critical, my attempts at generating price prediction agents thus far have been less than spectacular. Generating buy/sell signals seems to be more successful.

Early days still, will update more in the future.

The current best program in the current run is:

(IFGT (- (SMA100[2])(- (/ (ATR5[1])(IFGT (- (4)(- (- (/ (/ (ATR10[2])(IFGT (1.810102)(- (/ (ATR20[3])(SMA50[2]))(* (HIGH[4])(/ (4)(/ (IFGTE (* (* (* (IFGT (ATR5[1])(- (/ (ATR20[3])(SMA50[2]))(IFGTE (HIGH[4])(/ (4)(/ (ATR20[3])(HIGH[1]))))))(SMA10[1]))(SMA10[3]))(5.492882))(CLOSE[2]))(SMA10[3])))))))(IFGT (4)(/ (SMA10[1])(ATR100[1]))))(* (/ (8.307099)(/ (2.057818)(/ (LOW[2])(ATR100[1]))))(ATR10[2])))(HIGH[4])))(2.057818)))(1.782779)))(* (/ (8.307099)(/ (2.057818)(/ (ATR10[1])(ATR100[1]))))(ATR10[2])))

Looks like gobbly gook though it is remarkably more understandable than Neural Networks which are basically just a black box. It will be interesting to see if I can generate a successful agent with GP. I may also try on the same data a Neural Network based agent. Neural Networks do have an advantage in that we can have multiple output nodes where we’re stuck with just one for GP.

Update: I thought I’d post the next fittest agent after the above one so that you can see how a program evolves

(IFGT (- (SMA100[2])(- (/ (ATR20[3])(IFGT (- (ATR20[3])(- (/ (ATR10[1])(IFGT (- (4)(- (- (/ (/ (ATR100[1])(IFGT (2.727904)(- (/ (ATR5[1])(SMA100[2]))(* (/ (/ (ATR10[1])(1.463929))(/ (IFGTE (/ (SMA10[3])(SMA100[2]))(ATR10[2]))(SMA10[3])))(/ (ATR10[1])(1.463929))))))(4))(* (ATR20[3])(/ (16.978725)(/ (1.395991)(/ (LOW[2])(ATR100[1]))))))(ATR20[3])))(ATR10[2])))(/ (/ (ATR10[1])(ATR100[1]))(ATR10[1]))))(SMA100[2])))(1.991408)))(* (/ (8.911259)(/ (2.029770)(/ (ATR10[1])(ATR100[1]))))(ATR10[1])))

8 Responses to "Genetic programming an automated trading agent"

1 | phx

November 18th, 2008 at 5:43 am

Avatar

Hi,

Could you be so kind and explain some things in the formula? I am programmer myself, I can also code MQL4, but I’m newbie in genetic programming.
* What does IFGT mean?
* What do signs *, - and / mean right after parentheses?

Thank you in advance,
Justas

2 | Milton

November 18th, 2008 at 10:11 am

Avatar

Hi phx

IFGT is my if greater than function returns 1 or zero, I also have some others such as IFGTE, IFLT and IFLTE which are if greater than or equal, if less than and if less than or equal.

* and / are standard multiply and divide operators.

The output is a little hard to read, it should be read as

“/ (ATR100[1])(2.727904)”

as

“ATR100[1]/2.727904″

3 | Ed

December 28th, 2008 at 10:42 pm

Avatar

Hi,
my name is Ed, I’m 40yo and also from Sydney. I have been a trader for about 10 years and am on a quest to reduce my screentime and increase my profitability also.
I have a project that is 75% of the way there however it is in need of a skilled mq4 programmer (which I am not)to get it fully automated. Currently the system is semi automated and is very profitable.
If you are interested in finding out more please shoot me an email.

4 | Pablo

March 4th, 2009 at 8:28 am

Avatar

How is this doing? Do you get any success ?

5 | Guilherme

April 25th, 2009 at 5:09 am

Avatar

Hi!
Just for understanding, when reading SMA100[2] and ATR20[1], does it means lag 2 function on the indicator SMA 100 peirods and lag 1 on Average True Range 20 periods?

Another question:

How do you get an exit signal? Just by waiting a reversal signal from the formula?

Good Luck!

6 | Milton

May 13th, 2009 at 10:29 pm

Avatar

Hi Pablo, I really haven’t progressed it very far which is unfortunate.

Hi Guilherme, Yes you understood that more or less correctly, just an index in an array 0 is the latest item.

You need to make at least 2 programs, one is for entries one for exits.

7 | Mark

June 17th, 2009 at 9:03 am

Avatar

What tools are you using? Have you achieved the results you expected yet?

8 | Andrew

August 13th, 2009 at 12:47 am

Avatar

Hi Milton,

Coincidentally I’ve been doing exactly the same as you (see my latest blog entry). I tried breeding traders for the FTSE and whilst they were fantastically profitable on the trained data, they were very poor on new unseen data. I think you need to be careful to tune the parameters so you end up with something that has general ability rather than something that is an expert on the limited data that you’ve trained it on. You also need to be careful that your data set represents a range of market conditions i.e. bull, bear, sideways, etc.

I notice that you are using prefix notation, I use Scheme which is really ideally suited to this. It would be good to keep in touch.

Regards

Andrew

Comment Form

About FOREX Howto

Forex Howto is a site that I setup some years ago, I neglected it for a couple of years and now it's back online.

It will have my progress as I explore the world of Forex trading and my experiences in trying to profit from it.

If you need to contact me, just leave a message on any of my posts and I'll be sure to read it.

About me: I'm a 31 year old aussie living in Sydney, Australia.

I work as a programmer and hope to someday use my programming skills and growing knowledge of FOREX and markets in general to run live automated trading systems (all going well they'll be profitable).

A couple of pages which might be of interest to new traders: