Back

Import User-Created Python Modules (additional files to organize code) into main.py in web editor

from datetime import datetime, timedelta

Code such as the above works for external libraries. But doesn't seem to allow me to pull additional files I've defined in the web editor, such as pivots.py, into main.py.

Previous discussion question suggests an answer, but unsure how to "add to root level" as he discusses here. Assuming he is running the desktop implementation:

https://www.quantconnect.com/forum/discussion/3708/python-libraries-and-directories/p1

How does one go about doing this, or is it even possible? Previously the capability has been shut down:

https://www.quantconnect.com/forum/discussion/1920/can-python-projects-can-only-use-main-py-now/p1

 

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.


Hi Ian,

This is what I do when I want to create and include new libraries, adapted for Python:

  1. Open your main Python algo
  2. Click Add New Library in the left-hand column
  3. Click the Create a New Library link in the bottom-right corner
  4. Paste your library class code into this (or multiple) file, ideally renaming them away from main.py
  5. Go back to your main Python algo
  6. Again click Add New Library but this time select your new library

The library files will now appear inside your project but they will still be linked to your library. This is good in case you have multiple algos sharing the same library - any changes to the library files will propagate across to all other algos.

1

Thanks a lot Douglas!!

0

By playing with the Algorithm Wizard 2.0 I've discovered that this is quite straightforward, modules added are listed as additional tabs and imported in the standard way:

Keep code modular and import into main.py (if using Python) with: "from module import class/function as class/function" Omit the .py on the end of the module. I didn't do this the first time and thus couldn't successfully import. Noob mistake

--------------

Bonus, the documentation mentions creating virtual folders to organize code first paragraph here:

https://www.quantconnect.com/docs/key-concepts/developing-in-the-ide

That looks like:

Create a folder for projects by adding "Title/" in front of the file name in the IDE. Don't include the quotes. All projects with the same title will end up in a virtual folder by that title, allowing for better organization.

---------------

Both of the above are going to go a long way for me, personally, in keeping the code readible and maintainable

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