Key Concepts

Python and LEAN

Introduction

The LEAN open-source package powers the core functionality of QuantConnect. Strategies plugin to LEAN to request data, and process trades. LEAN is written in C# and accessed in python with a defined API. Most of these interactions are using native python types, with a few key exceptions. To comfortably use LEAN you should learn to recognize error messages when you hit a non-native API, and how to convert back to a native type.

Enumerable Types

When LEAN returns an enumerable, the object should be converted to a list() to be easily read by python classes. Popular API methods which return an enumerable are shown below:

Historical Data

QuantConnect also provides methods to request historical data that return a pandas dataframe, or an enumerable. If loading a large amount of data using an enumerable streams the results reducing RAM usage.

The enumerable types might look like the MemoizingEnumerable that is a raw C# type accessible in python.



You can directly enumerate this object with standard python for loops, or to convert it to a native type, wrap the object in the list():

Underlying Plugin Architecture

Your strategy code implements the QCAlgorithm class, which can communicate with the LEAN Engine. These method calls are done at the same speed as cython, directly invoking calls at the C layer of python. Your algorithm is imported as syntax checked, interpreted python.

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: