Using Python with C++ is extremely powerful and very efficient. I have C++ code which I use as a shared library that is imported in Python (for example FAST_PROGRAM.cpython-312-linux.so). I would then import this in my Python script via import FAST_PROGRAM(assuming it’s in the same directory as the script).
Does anyone know how you can do this in QuantConnect? How can one use Pybind11 and C++ in QuantConnect?
I simply cannot push the file i have compiled ... i get an error
Cannot push 'test_project': File exceeds the maximum size of 32,000 characters by using 630,249.
Mia Alissi
To integrate your C++ code with Python using Pybind11 in QuantConnect, you need to follow these steps:
Create a Shared Library: Use Pybind11 to create a shared library from your C++ code. This will allow you to call C++ functions from Python. Ensure that your compiled shared library file is within the size limit of 32,000 characters.
Optimize Your C++ Code: If your compiled file exceeds the size limit, consider breaking down your C++ code into smaller components or optimizing it to reduce its size.
Use QuantConnect Libraries: QuantConnect allows you to create project libraries that can be shared across multiple projects. You can create a new project specifically for your C++ library and then add it as a library to your main algorithm project.
Import the Library: Once your library is added to your project, you can import it into your algorithm using the appropriate import statements. Ensure that the library files are correctly referenced in your project.
Test the Integration: Run a simple algorithm that utilizes functions from your C++ library to ensure everything is working as expected.
Request New Libraries: If you need additional libraries that are not currently supported, you can request them through QuantConnect. The process takes 2-4 weeks for review and deployment.
Here's a basic example of how you might structure your algorithm to use a shared library:
Make sure to replace
MyLibrary.my_cpp_module
andMyCppFunction
with the actual names of your library and function.Sources: Shared Libraries - QuantConnect.com, Shared Libraries - QuantConnect.com, Libraries - QuantConnect.com
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.
Aryan Efty
Thank you for your quick response!
My issue is that my C++ code consists of a single (but big) function call (cant split it). The file size is 148 KB. I have already optimized the compilation to reduce the size and removed debug symbols to shrink it further.
That said, is there any way to bypass the file size constraint? A 150 KB shared library is really not that large …
Aryan Efty
Is there limitation imposed by the plan tier? In what payment tier can you use custom compiled libraries?
Louis Szeto
Hi Aryan
Indeed the plan tier will affect the file size limitation of a single file:
Best
Louis
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.
Aryan Efty
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!