Libraries
Project Libraries
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:
- Open a terminal in your organization workspace that contains the library.
- 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
- In your project files, add the library namespace to the top of the page.
By default, the namespace is
QuantConnect
. - In your project files, import the library class at the top of the page.
- In your project files, instantiate the library class and call its methods.
using QuantConnect;
from MyLibrary.main import MyLibrary
In general, use from <libraryName>.<fileNameWithoutExtension> import <memberName>
.
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:
- Open the organization workspace that contains the library.
- In the Library directory, rename the library project.
- If you have a copy of the library in QuantConnect Cloud, open a terminal in your organization workspace and push the library project.
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).
$ 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'