Tutorials

Code sharing

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# code sharing

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 the directory you ran lean init in.
  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 "My Project" for each LEAN project you want to share code from or to. This command adds the project in ./My Project to the solution.
    $ dotnet sln add "My Project"
    Project `My Project/My Project.csproj` added to the solution.
  4. Run dotnet add "Algorithm Project" reference "Library Project" to make the code in ./Library Project available in the project in ./Algorithm Project.
    $ 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 code sharing

To share code between Python projects, you can create library projects in the Library directory in to the directory you ran lean init in. If you have a Library/Logging project with a functions.py 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 the directory you ran lean init in.
  2. Run lean create-project --language python "Library/MyLibrary" to create a new library project named "MyLibrary".
    $ lean create-project --language python "Library/MyLibrary"
    Successfully created Python project 'Library/MyLibrary'
    After running this command, you can import its members using from MyLibrary.<file name without extension> import <member name> in any other Python project.

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

Did you find this page helpful?

Contribute to the documentation: