Project Libraries


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 share code between two C# projects, you need to add them to the same solution and add a project reference between the two projects. You can do this in your favorite C# editor, or in the terminal by following these steps:

  1. Open a terminal in your CLI root directory.
  2. Run dotnet new sln to bootstrap the solution. This command only needs to be run once.
    $ dotnet new sln
    The template "Solution File" was created successfully.
  3. Run dotnet sln add "<projectName>" for each LEAN project you want to share code from or to. This command adds the project in ./<projectName> to the solution.
    $ dotnet sln add "My Project"
    Project `My Project/My Project.csproj` added to the solution.
  4. Run dotnet add "<projectName>" reference "<libraryName>" to make the code in ./<libraryName> available in the project in ./<projectName>.
    $ dotnet add "Algorithm Project" reference "Library Project"
    Reference `..\Library Project\Library Project.csproj` added to the project.

Sharing code between C# projects has the following limitations:

  • The directory the solution is in must be one of the parent directories of the project directory.
  • Your projects cannot have references to any projects or files outside the solution directory.

Python Libraries

To share code between Python projects, you can create library projects in the Library directory in to your CLI root directory. For example, if you have a Library/Logging project with a file, you can import all its members in any other Python project using from Logging.functions import *. Follow these steps to create a new library project:

  1. Open a terminal in your CLI root directory.
  2. Run lean create-project --language python "Library/<libraryName>" to create a new library project named <libraryName>.
    $ lean create-project --language python "Library/MyLibrary"
    Successfully created Python project 'Library/MyLibrary'
    After running this command, you can import its members using from <libraryName>.<fileNameWithoutExtension> import <memberName> in any other Python project.

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: