View Full Version : Genetic Optimizer
Pete S
02-11-2008, 09:17 PM
Here's the first public version of my genetic optimizer for NT. I've published some documentation here: http://www.ov-trading.com/geneticoptimizer
There are some issues I know about, including:
1. If you have any non-int or -double params this will break
2. Somehow an extra 0-byte file is getting created if you use the GASaveAll option
3. If you use very small population sizes the random number generation doesn't work right
and I'm sure others I don't, but I've been using this myself for about 1 1/2 months now and it's really opened up possibilities of what I can do. If you're going to use this, please read the documentation carefully once before getting started.
I have not tested this on futures or forex at all. I don't know of any reason why it won't work, but I haven't tried it.
I am not an expert in programming GA algorithms by any stretch. What I used I learned from various web sites & hacking the code.
Update: you need to download both of these files if you are going to use this.
NinjaTrader_Dierk
02-11-2008, 10:08 PM
Excellent! Thanks for sharing.
PrTester
02-12-2008, 04:26 AM
I definitely will try this, awesome thanks for sharing
Pete S
02-12-2008, 05:48 AM
I see the download counts don't match; you need both of those files. This SQN.cs is required even if you have the old one I posted. This is explained more in the documentation.
zoltran
02-12-2008, 06:24 AM
Great !!
Thanks
rokafella
02-12-2008, 12:49 PM
WTFG Pete!
Rok
:D
whitmark
02-15-2008, 11:43 AM
Nice work Pete, will give it a go. Thanks for sharing your work in this area.
Regards,
Whitmark
whitmark
02-15-2008, 03:45 PM
Great work Pete! Please see my comments below:
I've created a small test strategy (MA Xover with Profit Target and Stop Loss) to test out the Genetic Optimizer, see attached. If I were to optimize this strategy via the default brute force method given the min/max/increment settings I've selected, I estimate it would have had to process 6.25 million iterations and have taken approximately 9 days to do so. Your GA optmizer, with default GA settings, obtained reasonable profitable results within 2.7 minutes underscoring the power of smart optimization methods.
The updates to the output window is a nice touch (see below). Very useful information including the iterations per second and a recap of the best results with parameter values:
ES ##-##: Genetic Optimizer start: 2/15/2008 4:36:22 PM, parameters: 8, population: 256, generations: 5, save all results: True
ES ##-##: completed generation 1/5, iterations per second: 7.7, max SQN so far: 2.12
ES ##-##: completed generation 2/5, iterations per second: 7.7, max SQN so far: 1.97
ES ##-##: completed generation 3/5, iterations per second: 7.9, max SQN so far: 2.15
ES ##-##: completed generation 4/5, iterations per second: 8.0, max SQN so far: 2.15
ES ##-##: completed generation 5/5, iterations per second: 7.9, max SQN so far: 2.22
ES ##-##: completed: 2/15/2008 4:39:04 PM, total iterations: 1280, total time (minutes): 2.7, best result:
GASaveAll: 1
FastPeriod: 47
SlowPeriod: 53
ProfitTgtTicks: 23
StopLssTicks: 25
I tried various combinations of maximum generations and population sizes and agree with your defaults as offering the best trade offs between search depth and breadth. Would be nice to also be able to modify the reproduction percentage and mutation rates from the parameters too.
Initially, I tried to segment the listing of GO parameters in the sample strategy by using a different [Category("GO Settings"] in the code but this did not work in the strategy. Plus I see that specific references are made in the GAOptimizer code to overwrite the default values. Would be nice not to comingle the GO settings and strategy parameters as you point out.
I was also working with file output recently in a separate indicator and was getting an extra 0 size file written out in addition to a populated text file. Sounds like this is worth a follow-up with NinjaSupport.
When I created a GA for another platform I had the benefit of using a third-party GO dll and just had to managed the inputs and outputs. Using another dll may have more bells and whistles, but I am very impressed you've successfully developed all of the operations directly in the GAOptimizer code. Did you consider any open source solutions as an alternative?
I suspect transforming non-integer parameters to and from what the GO can work with is high on the enhancement list. Do you have any other thoughts on what other enhancements you would like to make over and above what you have posted on the forum?
Thanks again, Pete, for sharing your work. Good to know such great work is being done just up the road in VA. I hope our friends at NT are taking note of the power a GO can bring to enhance their platform offering.
Regards,
Whitmark
NinjaTrader_Dierk
02-18-2008, 09:09 AM
Pete, could you please PM me at "dierk AT ninjatrader DOT com"? I would want to discuss some issues with you. Thanks
Pete S
02-18-2008, 09:18 AM
Mail sent. (I sent one on Saturday as well after I saw your PM.)
NinjaTrader_Dierk
02-18-2008, 09:20 AM
Guess I need to monitor my spam filter ...
Ragingbull
02-21-2008, 03:52 PM
Hi Pete,
Great job, you have effectively complete the "missing links" to be used with SQN. I 'll get back to you when I will have the beta version working.
Best reagrds,
Pete S
02-21-2008, 08:13 PM
Hey Whitmark, sorry it took so long to respond to your feedback.
I tried various combinations of maximum generations and population sizes and agree with your defaults as offering the best trade offs between search depth and breadth. Would be nice to also be able to modify the reproduction percentage and mutation rates from the parameters too.
The user has to use a certain amount of judgement in selecting these values. As I mentioned in my docs, if you have a simple strategy you can end up actually running more iterations than you would using brute force. I haven't come up with a specific rule of thumb for the pop size yet but I find myself using values between 100 and 200 for the most part.
Initially, I tried to segment the listing of GO parameters in the sample strategy by using a different [Category("GO Settings"] in the code but this did not work in the strategy. Plus I see that specific references are made in the GAOptimizer code to overwrite the default values. Would be nice not to comingle the GO settings and strategy parameters as you point out.
Yeah, this is hacky right now. I proposed something here: http://www.ninjatrader-support.com/vb/showthread.php?t=4790 and am chatting about it with Dierk offline.
I was also working with file output recently in a separate indicator and was getting an extra 0 size file written out in addition to a populated text file. Sounds like this is worth a follow-up with NinjaSupport.
I need to do a little more debugging here myself, I have done this in another case without any issues. I still think this is is my code.
When I created a GA for another platform I had the benefit of using a third-party GO dll and just had to managed the inputs and outputs. Using another dll may have more bells and whistles, but I am very impressed you've successfully developed all of the operations directly in the GAOptimizer code. Did you consider any open source solutions as an alternative?
To be honest, in my searching, I did not run across anything appropriate.
I suspect transforming non-integer parameters to and from what the GO can work with is high on the enhancement list. Do you have any other thoughts on what other enhancements you would like to make over and above what you have posted on the forum?
I don't have a good feel for what other people are using for parameter types. I have never, in about 8 months of doing this, used anything other than an int or double parameter. If people need this support I am willing to implement it. The only other significant thing I can think of at this point is a "screening mode", where the optimizer runs until a threshold SQN value is reached. This would be useful on the stock side for running a list of lets say 200 symbols as quickly as possible to come up with a list of candidates for refinement.
Pete S
02-21-2008, 08:16 PM
And by the way -- you can add the GA parameters into your UserDefinedMethods file so you don't need to put them in every strategy if you use the GA optimizer frequently.
whitmark
02-22-2008, 08:33 AM
Hi Pete. Thanks for your reply. Here are some additional thoughts for your consideration:
1. In the GAOptimization code, I did notice two CreateLog() calls in the code and no CloseLog() call which could be the reason for the extra output file. I didn't give it much more investigation than that.
2. It would be nice to accommodate decimal parameter values including decimal increment values since using decimal multiples of ATR, StdDev, Parabolic inputs, for example, are quite common. Given GA parameters can only have integer values if you wanted to optimize a parameter between 1 and 10 in increments of 0.1 the min and max values would need to be converted to 10 and 100 respectively before loading into the GA. When the parameter is used later in the strategy, it is simply divided by 10. If increments of 0.2 was required, the defined min/max values would be 5 and 50 and the actual parameter would be divided by 5 when it is used.
3. Yes, I can't imagine support would be needed for anything beyond int or double. Boolean parameters can be converted to int's easily enough as a part of the strategy design.
4. Would be nice to be able to seed the parameter values of the initial generation of the GA (vs random number generation) when you already know that value X is pretty good, through previous testing or trading, but you want to explore other min and max values. This would likely require the input GUI to be expanded to accommodate a Seed value as well as Min, Max, and Increment. Perhaps there is a way to use the strategy parameter defaults as the seed values.
5. Have you considered implementing a parameter stress test feature. If not, you might be interested in learning more about it and how it can promote more stable parameter selection.
Thanks again for your generous contribution. Feel free to incorporate any or none of the above as you see fit and contact me if you need any development assistance with this.
Regards,
Whitmark
Pete S
02-22-2008, 08:47 AM
2. It would be nice to accommodate decimal parameter values including decimal increment values since using decimal multiples of ATR, StdDev, Parabolic inputs, for example, are quite common. Given GA parameters can only have integer values if you wanted to optimize a parameter between 1 and 10 in increments of 0.1 the min and max values would need to be converted to 10 and 100 respectively before loading into the GA. When the parameter is used later in the strategy, it is simply divided by 10. If increments of 0.2 was required, the defined min/max values would be 5 and 50 and the actual parameter would be divided by 5 when it is used.
Doubles are supported. You just can't (yet) specify the increment. The code looks at the data type & rounds double parameters to 2 decimal places. You don't need to do what you are talking about regarding multiplying the params.
5. Have you considered implementing a parameter stress test feature. If not, you might be interested in learning more about it and how it can promote more stable parameter selection.
I'll look into this, if you have a link handy feel free to send it along.
whitmark
02-22-2008, 10:37 AM
Doubles are supported. You just can't (yet) specify the increment. The code looks at the data type & rounds double parameters to 2 decimal places. You don't need to do what you are talking about regarding multiplying the params.
Ah yes, I see now after taking a closer look at the code. The GA functions I have used in the past were developed as "integer only" functions, I assumed to standardize and simplify the incrementation/crossover processing since the inputs/increments could have varying degrees of precision. Since you are not beholden to a third-party function you have much more flexibility in how to handle.
I was first introduced to GA stress testing when using the Grail Genetic Optimizer for TS that is no longer being distributed. I'll pm you some information on the stress testing idea. I wanted to mention too that your fitness function threshold idea for screening multiple instruments would be very useful. I suspect the threshold value could be passed in as a strategy parameter.
Regards,
Whitmark
Ragingbull
07-26-2008, 06:03 AM
Hi Pete,
Since you did such a great job with SQN and the genetic optimizer, I come back to you with another important missing link in NT (to my knowledge): Money Management. This is for me the third pillar that should be implemented.
Still referring to Van Tharp, as I did for SQN, Money Management is critical to any serious trader in terms of defining the goal he wants to achieve and protecting his capital. Any strategy should include this feature because it is the one which by far will have the largest impact on the final gain. Combined with an optimization based on the SQN number, it is the best way to grow and protect your capital.
Basically, what should be achieved should go along these lines:
1. Beeing able to define a level of risk in terms of % of the trading capital. When I speak of capital here, I mean the capital evolving with time after each trade (going up and down according to the result of the previous trade).
Risk_Capital(%)= n(# of future contracts or stocks)*(Entry($)-Stoploss($))/Capital($)
A typical value for Risk_Capital is of the order of 1% or less. This could eventually be increased if you have a SQN above 4. Any value above 3% is considered as “financial suicide”.
By defining Risk_Capital, you will be able to define for each “next” trade the amount of contracts or stocks “n” as:
n(number of contracts or stocks)= round(Risk_Capital(%)*Capital($)/(Entry($)-Stoploss($))). n has to be greater than 1 or the trade can't be taken.
By doing this, you see that if you enter in a serie of losing trades, your Capital($) will decrease and n will decrease accordingly. Inversely, if your Capital increase, n will increase also. This is Money Management 101…
2. Beeing able to impose a minimum reward to risk ratio defined as:
RewardtoRisk= (MinimumProfitTarget($)-Entry($))/(Entry($)-Stoploss($))
RewardtoRisk should be higher than 2. Statistically, on a large number of trades, you should not enter a trade if you have not the opportunity to gain at minimum the double of your initial risk.
Defining the RewardtoRisk allow to calculate the MinimumProfitTarget($) as:
MinimumProfitTarget($)=Entry($) + RewardtoRisk(>2)*(Entry($)-Stoploss($))
Today, I don’t see this being clearly defined in NT, so I use the following to optimize strategies on ES:
Parameters
ProfitTarget: 24, 512,1
Stoploss: 1,12,1 (This allow a minimum RtoR of 2 with 24/12)
These two parameters are used in the window “Stops and Targets” window.
Very brutal and inefficient way to try to achieve a RtoR above 2 combined with the optimization of the StopLoss and the ProfitTarget.
Optimize
Optimize on: my system quality number
Optimizer: genetic optimizer
Order Handling
Entries per direction: 10
To allow more than 1 entry in a given direction but does not achieve any kind of Money Management.
Order Properties
Set order quantity: by strategy
In the end, in the Order Properties, could it be possible to select a “by Money Management” option that would allow to enter:
Risk_Capital(%): 0.1%, 1%, 0.1% (min, max, increment)
RewardtoRisk: 2, 10, 1 (min, max, increment)
They could be a constant or preferably parameters to be optimized.
“by Money Management” would compute “n” and “ProfitTarget” for each trade in function of Entry, Stoploss, RewardtoRisk, Risk_Capital and updated Capital.
Thanks for you feedback.
Best regards.
rrv1980
08-22-2008, 12:16 AM
This is such a great piece of work. Thank you for sharing. You are great! I cant believe that this GA did find the best set of paramter in almost 1/1000th of time it would have taken to run thru all the parameters.
Thanks to you, I can now really focus on making my strategies as flexible as possible and test it for almost any parameter. Do you ever intend to add support for boolean? I think that can be a great asset. And if you add Datetimem then it will help a crazy person like me to find out what is the most optimal time to trade in the market.
Kudos for the work!!
Rishi