Algorithm Framework

Overview

Introduction

Since QuantConnect was formed in 2013, the community has created more than 2 million algorithms. Although they all seek to achieve different results, they can all be abstracted into the same core features. The Algorithm Framework is our attempt to provide this well-defined structure to the community, encouraging good design and making your work more reusable. LEAN provides many pre-built Algorithm Framework models you can use in your algorithms.

Important Terminology

The following table defines important Algorithm Framework terminology:

TermDescription
Universe Selection modelA framework module that selects assets for your algorithm.
Alpha modelA framework module that generates trading signals on the assets in your universe.
InsightAn object that represents a trading signal. The Alpha model generates these objects for the Portfolio Construction model.
Portfolio Construction modelA framework module that determines position size targets based on the Insight objects it receives from the Alpha model.
PortfolioTargetAn object that represents the target position size for an asset in the universe. The Portfolio Construction model generates these objects for the Risk Management model.
Risk ManagementA framework module that manages market risks and ensures the algorithm remains within target parameters. This model adjusts the PortfolioTarget objects it receives from the Portfolio Construction model before they reach the Execution model.
ExecutionA framework module that places trades to reach the target risk-adjust portfolio based on the PortfolioTarget objects it receives from the Risk Management model.

Framework Advantages

We recommend you design strategies with the Algorithm Framework in most cases. The Algorithm Framework has many advantages over the classic algorithm design.

Pluggable Algorithm Modules

With the Algorithm Framework, your code can instantly utilize all modules built within the framework. The modules clip together in well-defined ways, which enable them to be shared and swapped interchangeably.

Focus On Your Strengths

If you write code in modules, you can focus on your strengths. If you are great at building universes, you can build Universe Selection modules. If you have risk management experience, you could write reusable risk monitoring technology.

Reduce Development By Using Community Modules

Easily share modules you've made between algorithms or pull in ones produced by the community. The strict separation of duties makes the Algorithm Framework perfect for reusing code between strategies.

System Architecture

The Algorithm Framework is built into the QCAlgorithm class, so your strategy can access all the normal methods you use for algorithm development. You should be able to copy and paste your algorithms across without any changes. You don't need a separate class.

The Algorithm Framework modules communicate w

The framework data output of each module flows into the following module. The assets that the Universe Selection model selects are fed into the Alpha model to generate trade signals (Insight objects). The Insight objects from the Alpha model are fed into the Portfolio Construction model to create PortfolioTarget objects, which contain the target number of units to hold for each asset. The PortfolioTarget objects from the Portfolio Construction model are fed into the Risk Management model to ensure the targets are within safe risk parameters and to adjust the PortfolioTarget objects if necessary. The PortfolioTarget objects from the Risk Management model are fed into the Execution model, which efficiently places trades to acquire the target portfolio.

The flow of data between the Algorithm Framework modules
public class MyFrameworkAlgorithm : QCAlgorithm 
{ public void Initialize() { SetUniverseSelection(); AddAlpha(); SetPortfolioConstruction(); SetExecution(); AddRiskManagement(); } }
class MyFrameworkAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self.SetUniverseSelection()
        self.AddAlpha()
        self.SetPortfolioConstruction()
        self.SetExecution()
        self.AddRiskManagement()

For simple strategies, it may seem like overkill to abstract out your algorithm concepts. However, even simple strategies can benefit from reusing the ecosystem of modules available in QuantConnect. Imagine pairing your EMA-cross Alpha model with a better execution system or plugging in an open-source trailing stop Risk Management model.

Separation of Concerns

The separation of concerns principle is an algorithm design principle where individual components are isolated and their responsibilities don't overlap. The Algorithm Framework is designed to uphold this design principle. The individual framework components shouldn't rely on the state of the other framework components in order to operate.

In the Algorithm Framework, the models should not communicate. The universe constituents and Insight objects should be the same, regardless of what you select for the Portfolio Construction, Risk Management, and Execution models. A common example of violating the separation of concerns principle is flattening an Insight in an Alpha model after the Risk Management model has liquidated the security in order to avoid re-buying the security. If your algorithm logic naturally violates the separation of concerns design principle, it's more appropriate to use the classic or hybrid design.

Framework vs Classic Design

Select the classic or Algorithm Framework design based on the needs of your algorithm. If your algorithm uses special order types or passes information between the different algorithm components, use the classic design. If you can reuse existing modules and the Algorithm Framework modules can work in isolation, use the Algorithm Framework design.

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: