Back

Developing Trading Strategies with Genetic Algorithms

Hi there,

Here is a Project where Genetic Algorithms were used to develop a trading strategy by combining a fixed subset of signals chained by logical operators.

The project uses the genetic algorithm library GeneticSharp integrated with LEAN by James Smith.

The best out-of-sample trading strategy developed by the genetic algorithm showed a Sharpe Ratio of 2.28 in trading of EURUSD with 25 trades in the out-of-sample period of January – April 2017 (attached).

But more important that the results itself, are the layout of a framework flexible enough to test a wide range of strategies and the proof of concept of what is possible with two powerful open sources tools as Lean and GeneticSharp.

Also, the Lean-centric framework has two very strong advantages:

  • The training evaluation can be as complex as needed (including ask-bid spread, fees, commissions, slippage model, risk management, etc.) to enhances the training by exposing the individuals to realistic environments.
  • The QCAlgorithm used by the genetic algorithm to evaluate the individuals can be used to trade in live paper mode and even in real trade. Therefore, a profitable set up developed by the genetic algorithm can be tested in real time or put to trade immediately.

Here’s a kind of paper where is detailed the technical side of the implementation and the statistical analysis of the training session.

Hope you enjoy it!

Update Backtest






The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.



      

I think it's fairly clear that this raises the bar in terms of the quality of submissions to QC. The sophistication of the analysis and the approach go beyond most algorithms that are shared amongst trading communities and genuinely sets the template for an effective means to promote the on-going evolution of community driven quant trading strategies.

This algorithm represents a next-generation trading pattern super-set that is focussed on harnessing the abundance of available processing power for alpha discovery with genetic algorithms and machine learning techniques. This pattern should transfer to essentially any security and should be highly adaptable to different timescales.

@JayJayD: It might be worthwhile considering how it would be possible to extend and codify this model, so that for instance, additional indicators could be supported as well as multi-level chaining of logical operations.

It might also be helpful to outline the theoretical basis and suggested reading for this approach for anyone interested.

5

@JayJayD: Can't view the PDF unfortunately, I will try accessing it again later.

@James Smith: The reason I didn't use the framework yet is simply because I'm using the cloud environment on QC - don't like dealing with data warehousing issues on my local machine. Are there any plans by QC to integrate your project with cloud environment?

I should say that curiously, I developed a genetic programming framework in Java prior to joining QC. However, I was analyzing daily bars and couldn't find much alpha in that case, I expect results to be much better with intraday data.

3

James Smith thank you very much for your kind words, I truly appreciate it!

Respect to add more indicators, in the SetTradingRule method you can see that the number of indicators can be easily changed. In this implementation wasn’t a variable because of the difficulty of making variable the number of genes in the chromosome. But once you define your chromosome in the optimization.json in SharpNeat, you can change the number of indicators, and thus the operators.

Respect to making hierarchies of indicators, a quick way it can be implemented is through the ITechnicalIndicatorSignal implementation.

So, for example, the CrossingMovingAverages implementation send a True signal only when there is an actual crossing of MA's (as well the oscillators, this explain the low trades in the out-of-sample period), but you can easily change the GetSignal method to make more like a flag like this:

public bool GetSignal()
{
var signal = false;
if (IsReady)
{
switch (_tradeRuleDirection)
{
case TradeRuleDirection.LongOnly:
signal = Signal == CrossingMovingAveragesSignals.Bullish;
break;

case TradeRuleDirection.ShortOnly:
signal = Signal == CrossingMovingAveragesSignals.Bearish;
break;
}
}
return signal;
}

In this example a long-only CrossingMovingAverages instance will send a True signal when the fast moving average is above the slow one.

In any case the ITechnicalIndicatorSignal implementation can be as complex as you need as long as it returns a signal after an event.

Petter Hansson, the document issue is fixed, thanks for report it.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Beautiful code, Jay! and really appreciate you sharing it with us!

One question though... (and this maybe because I haven't tested the code yet and the answer could be in it already) how do you optimize the parameters? do you adjust them by manual testing? or by analyzing the historical data? Thanks!

 

1

Thank you Patrick Star!

The optimization is made by GeneticSharp, using the integration James Smith made.

Please note that the attached backtest is a hard coded realization of a strategy developed by the GA. As is explained in the text, the algorithm used in the optimization process cannot be replicated in the QC platform because of the use of DynamicExpresso.

The mechanics is as follows:

  1. Every TradingRule parameters is a gene. And is feed to GeneticSharp through the optimization.json
  2. The GA calls the QCAlgorithm with the genes encoded in the parameter dictionary. Here you can see how the data form the dictionary is decoded and then instantiated as a trading strategy.
  3. Lean runs the QuantConnect algorithm with the parameters and returns the results to the GA.
  4. Once a whole generation is evaluated, the GA estimated the next generation and the cycle continues.
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


In terms of hierarchies of indicator signals, my understanding is it should be possible to allow, for instance, ITradingSignal to have a reference to a child ITradingSignal instance along with a contingent switch that allows recursion into the hierarchy of indicator signals. So for instance you could have:

totalSignal =  (parentSignal = EMA(240) > α AND RSI(240) < β) AND (childSignal = EMA(60) < γ)

Structuring this using recursion means you simply need to decide whether to branch into a child before reusing all the same logic to build the descendant. I believe one of the main problems with this approach is the complexity of the resultant models.

1

I can code almost anything but that's not the point. The real value is in creativity and ideas.. Looking at this project gives you a freash set of ideas and a different way of thinking. So thanks to both of you Jay and James. Keep it coming!

I am still slow in using LEAN locally but I will try to get it to work and learn GenericSharp and the integration part. I will ask more questions once I'm there :)

1

James Smith, now I understand what you mean and yes complexity can explode. I‘m pretty sure that the chromosome represetation of framework like that should be very challenging!

Patrick Star, I’ll happy to help!

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Fantastic stuff JayJayD and James. Spent half of yesterday getting Visual Studio up and running with the Genetic Sharp implementation and it works fantastically.

I only wish I didn't have to go and buy data for all my ideas I want to test.

I would have to say this is the only time I've ever wanted more than 4 cores.

0

One thing I'm itching to try is using different measures for fitness and seeing the results on out of sample data.

https://quantsketch.com/results_page.html#common-sense

Common Sense Ratio, Gain-to-Pain Ratio and Tail Ratio would be super interesting to play around with. I've peeked around the code and all I've managed to do so far is get lost. My C# is not strong enough.

0

Alright, so I've managed to figure out how to add the three ratios and use them as fitness measures. Just needed a bit of effort.

I'm not sure of the best way to share the changes to Lean, but if anyone is interested we can probably figure something out.

0

Tim Butler - We made all FX/CFD data free! :) 

https://www.quantconnect.com/tutorials/use-api-file-provider/
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hey Tim Butler!

I’m so glad you find this work interesting! James made such amazing work that the GA integrations is painless.

As Jared Broad pointed out, data is not a problem. But, as you noted, one of the main drawbacks of this project is the training requirements of computational power. Is the cost of evaluating the individuals in a very realistic environment (Lean).

In every GA problem, the fitness definition is single most important definition. The indicator you mention seems pretty interesting, I’d love to see them working! Maybe you can try making some kind of weighted average between the different indicators and use it as fitness.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi,

When i try to run this in my live Oanda account, when i try to log in i get an error that the Oanda states are not the same. do you know where that needs to be corrected?

0

Ryan Brickey - Sounds like your oAuth got confused -- please write a quick email to support@quantconnect.com and we'll reset it for you. 

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hey Ryan Brickey, I wouldn’t put much expectation in this algorithm in particular, is just a proof of concept, the important thing here is the framework, not the resultant algorithm.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi JayJay,

Awesome framework.

I was able to run it on amazon EC2, but it consumed too much resources. I will consider other cheaper alternatives.

I did some modifications to the original TradingStrategies, like including Bollinger Bands, so I share it.

I would like to implement multiple rules, and for each rule have hierarchical operators for indicators. I improved somehow the parameters for a single rule, but not able to configure the hierarchy of operators. Please share if you have any ideas on how to configure hierarchical operators.

Of course, the modified strategy is meant to be used for educational purposes.

0


Thanks Erik. I was impressed by JayJayD's work on genetic programming and have started to work on my own derivation of this here:

jameschch/GeneticTreeAlgorithm

I have made substantial changes to the structure, fixed bugs and added unit tests. I have also provided support for a logical tree of operators including OR, NOT, (x OR y) etc. I have also added support for ADX and now plan to integrate your Bollinger bands code.

0

Forgot to add; I've also improved performance significantly.

0

Hey Erick,

I’m so glad you found this work interesting! I’ll check the BB implementation.

You nailed it respect to its main weakness, the expensiveness of the training. If I’d deploy something like this project I’d use DigitalOcean, the VM aren’t that fast but are cheap and reliable; and the interface takes the pain away from IaaS.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Cool James. I will take look. I might migrate what I'm doing to yours baseline implementation.I was just about to create an implementation for turtle soup strategy, better to start from a good basis

0

Thanks Jay, I will take a look at that option.

James, here my version of your baseline project. I will try to merge with the changes I've done on the previous project. The code looks cleaner.

GIT GenericTree updates

0

and of course, the current BB strategy implementation there must be only for trading ranges

0

James, 

I updated some code with fixes and to make it compatible with QC 

0


Thanks Erik. I'm going to try to integrate your changes as soon as I can. I can work without a pull request but you can go that route if you prefer. I'm giving genetic programming using this setup a lot of attention so feel free to suggest improvements or report any issues. The number one thing that helps me out is getting a third-party opinion on things. I have made quite a lot of changes to this and the genetic optimizer project and am getting fairly pleasing results.

In terms of an optimization rig, I have an old 4 slot server capable of 24 cores that I obtained for basically peanuts. I don't know how the costs stack up over time against cloud compute. I imagine it would even out after a few weeks of running 24/7 as long as power costs aren't too high.

For live hosting of trading algorithms +1 for digitalocean.

0

James, I will check how to do a pull request. Not really familiar with that.

Next steps for me are the integration of additional signals in order of creating a few strategies. The additional signals I'm looking at is the Autochartist, integration with rest based NN services.

I will provide further feedback as soon as I progress using this framework, but immeditelly I think the configuration is rather verbose, I will change it into 2 steps, to make it more human friendly.

Thanks for this.

0

I've merged the Bollinger and Channel breakout from your fork. Seems like a great idea to allow a survival period for the approximate coincidence of signals. I'm wondering whether this could use QuantConnect.Indicators.RollingWindow?

You're right that the Optimizer configuration is unwieldy for this level of complexity. I may address that sometime soon. In the meantime I'm using a few scripts and tools to shift json around.

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed