Libraries
Project Libraries
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:
- Open a terminal in one of your workspaces.
- 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
- In your project files, add the library namespace to the top of the page.
By default, the namespace is
QuantConnect
.using QuantConnect;
- 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:
- Open a terminal in one of your workspaces.
- 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
- 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>
. - In your project files, instantiate the library class and call its methods.
x = MyLibrary() value = x.Add(1, 2)