I recently ran into an issue that I think might be helpful to share with the broader community. I am currently running a live algorithm which I have developed over the past couple of years. I implemented this algorithm in C#, and because of this, the infrastructure I have been using is naturally in that language. I recently have been looking to add more sophisticated statistical techniques to improve my strategy, and have found the libraries in C# quite limiting. Because python is better suited for this with the libraries QuantConnect supports, I looked to transition my code to Python. For some reason, I could not seem to get my algorithm to translate properly, so I tackled this problem from a different angle: Implementing a python script within my C# algorithm.
In the attached backtest you will see just how I was able to do it. Now there may be a better way to do this, so feel free to share any input. QuantConnect uses Pythonnet for the C#->Python interpretation so understanding how that package works is helpful. Some things I noticed
- You will see that the python portion of the code is indented all the way to the left. For some reason, the python interpreter is extremely sensitive to tabs and spaces and it needs to be written exactly as you would a normal script, including the indents.
- Converting the history call from C# to python requires the call to be converted into a list so that it is possible to then convert that into a DataFrame in python. I could not get it to work otherwise. I looked into calling the history function from within the python script but that proved to be too much of a pain to debug.
- The function must be called as a dynamic type. It is possible to return any datatype in python, which you can then pass back into a python script (i.e. with how I pass the HMM model back through to update). Most data types convert pretty easily between the two languages. Check out the pythonnet documentation for more regarding conversions.
I would like to give a special thank you to Alex with the QuantConnect support team for giving me the initial C# code to properly write this up, as well as Jack who wrote up the original HMM python code. The original code can be found here: https://www.quantconnect.com/forum/discussion/6878/from-research-to-production-hidden-markov-models/p1
Lee Fig
This is outstanding work and of particular interest to me as at present I am only able to develop my Lean strategies in C#. But python is my language of choice. But I was wondering... from your persepective is the additional coding overhead incurred related to code interop between python and c# worth the effort? I mean, would it not be easier to code in just the one language...?
Chris Holley
I definitely agree, if you are starting out a new algorithm it is easier to choose one or the other, depending on what you want to accomplish. For me, I started with a rules-based strategy in C# as I had a lot more experience in that language. I only wanted to add this small feature to my algorithm, which is not available in C# that I can find. Rather than re-write the entire thing I found it easier to just add this bit of code in. In the future, if wanting to use statistical packages I will stick with python over C#.
Lee Fig
I am in the same position after having invested most of my time in developing c# based algorithms but have found myself developing predominantly in python these days. Initially I found making the leap a difficult decision because I try to avoid mixing different technology unless it can be avoided. Lean made a brilliant call architectually speaking when designing the framework to support python in its early days.
Chris Holley
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!