Libraries

Project Libraries

Introduction

The CLI makes it possible to share code between projects. This allows you to create library projects containing common code, and use this common code in your algorithm projects without having to copy it around.

C# Libraries

To create a C# library, open a terminal in one of your workspaces and then create a project in the Library directory.

$ lean project-create "Library/MyLibrary" --language csharp
Successfully created C# project 'Library/MyLibrary'

Follow these steps to use a C# library in a C# project:

  1. Open a terminal in one of your workspaces.
  2. Run lean library add "<projectName>" "Library/<libraryName>".
    $ lean library add "My Project" "Library/MyLibrary"
    Adding Lean CLI library D:\qc\lean-cli\Library\MyLibrary to project D:\qc\lean-cli\My Project
    Restoring packages in 'My Project' to provide local autocomplete: D:\qc\lean-cli\My Project, D:\qc\lean-cli\My Project\My Project.csproj, D:\qc\lean-cli
  3. In your project files, add the library namespace to the top of the page.

    By default, the namespace is QuantConnect.

    using QuantConnect;
    
  4. In your project files, instantiate the library class and call its methods.
    var x = new MyLibrary();
    var value = x.Add(1, 2);

Python Libraries

To create a Python library, open a terminal in one of your workspaces and then create a project in the Library directory.

$ lean project-create "Library/MyLibrary" --language python
Successfully created Python project 'Library/MyLibrary'

The library name can only contain letters (a-z), numbers (0-9), and underscores (_). The name can't contain spaces or start with a number.

Follow these steps to use a Python library in a Python project:

  1. Open a terminal in one of your workspaces.
  2. Run lean library add "<projectName>" "Library/<libraryName>".
    $ lean library add "My Project" "Library/MyLibrary"
    Adding Lean CLI library D:\qc\lean-cli\Library\MyLibrary to project D:\qc\lean-cli\Library Test
  3. In your project files, import the library class at the top of the page.
    from MyLibrary.main import MyLibrary
    

    In general, use from <libraryName>.<fileNameWithoutExtension> import <memberName>.

  4. In your project files, instantiate the library class and call its methods.
    x = MyLibrary()
    value = x.Add(1, 2)

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: