Libraries

Project Libraries

Introduction

Project libraries are QuantConnect projects you can merge into your project to avoid duplicating code files. If you have tools that you use across several projects, create a library.

Create Libraries

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

$ lean project-create "Library/MyLibrary"
Restoring packages in 'Library\MyLibrary' to provide local autocomplete
Restored successfully
Successfully created C# project 'Library/MyLibrary'
$ lean project-create "Library/MyLibrary"
Successfully created Python project 'Library/MyLibrary'

The library name can only contain letters (a-z), numbers (0-9), and underscores (_). The library name can't contain spaces or start with a number. To create a library directory, set the name to Library / directoryName / libraryName (for example, Library / Tools / Calculators).

The lean project-create command creates a new project based on your default programming language. To create a PythonC# library, add the --language csharp--language python option.

$ lean project-create "Library/MyLibrary" --language csharp
Successfully created C# project 'Library/MyLibrary'
Restoring packages in 'Library\MyLibrary' to provide local autocomplete
Restored successfully
Successfully created C# project 'Library/MyLibrary'
$ lean project-create "Library/MyLibrary" --language python
Successfully created Python project 'Library/MyLibrary'

Add Libraries

Follow these steps to add a library to your project:

  1. Open a terminal in your organization workspace that contains the library.
  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
    Restored successfully
    $ 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
  3. In your project files, add the library namespace to the top of the page.

    By default, the namespace is QuantConnect.

  4. In your project files, import the library class at the top of the page.
  5. using QuantConnect;
    from MyLibrary.main import MyLibrary

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

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

Rename Libraries

Follow these steps to rename a library:

  1. Open the organization workspace that contains the library.
  2. In the Library directory, rename the library project.
  3. The library name can only contain letters (a-z), numbers (0-9), and underscores (_). The library name can't contain spaces or start with a number. To create a library directory, set the name to Library / directoryName / libraryName (for example, Library / Tools / Calculators).

  4. If you have a copy of the library in QuantConnect Cloud, open a terminal in your organization workspace and push the library project.
  5. $ lean cloud push --project "Library/MySpecialLibrary"
    [1/1] Pushing 'Library\MySpecialLibrary'
    Renaming project in cloud from 'Library/MyLibrary' to 'Library/MySpecialLibrary'
    Successfully updated name, files, and libraries for 'Library/MyLibrary'

Remove Libraries

Follow these steps to remove a library from a project, open a terminal in your organization workspace that stores the project and then run lean library remove "<projectName>" "Library/<libraryName>".

$ lean library remove "My Project" "Library/MyLibrary"
Removing D:\qc\workspaces\Quant Organization\Library\MyLibrary from 'My Project\My Project.csproj'
Restoring packages in 'My Project'
  Determining projects to restore...
  Restored D:\qc\workspaces\Quant Organization\My Project\My Project.csproj (in 399 ms).
$ lean library remove "My Project" "Library/MyLibrary"

Delete Libraries

To delete a library, open a terminal in your organization workspace that contains the library and then run lean project-delete "Library/<libraryName>".

$ lean project-delete "Library/MyLibrary"
Successfully deleted project 'Library/MyLibrary'

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: