LEAN Algorithmic Trading Engine

repository·master·Indexed 12 days ago

https://github.com/quantconnect/lean

A modular algorithmic trading engine for backtesting and live trading. It features a plugin architecture to decouple trading logic from infrastructure and supports Python and C# algorithms. Key capabilities include advanced position grouping for margin and risk modeling, buying power management via IPositionGroupBuyingPowerModel, and support for various asset classes including CFDs.

Tokens
29.9K
Snippets
75
Records
135
Agent score
95%

What's inside LEAN

  1. Overview of LEAN Options Data Formats

    master
    LEAN supports options data provided by AlgoSeek, covering the USA market. The data includes trade, quote, and openinterest types at a Minute resolution. Data is stored in compressed ZIP files containing multiple CSV entries categorized by option style (e.g., call/put), strike price, and expiration date.
  2. Overview of LEAN CFD Data Formats

    master

    QuantConnect provides Contracts for Difference (CFD) data sourced from Oanda. This data consists exclusively of Quote data (Bid/Ask) and is stored in ZIP files containing a single CSV.

    Supported Resolutions:

    • Tick
    • Second
    • Minute
    • Hour
    • Daily

    Supported Market: Oanda.

    Note: CFD data operates across multiple timezones. Users should verify the specific timezone for their asset using the market hours database.

  3. System Overview of the LEAN Engine

    master

    LEAN is an algorithmic trading engine that utilizes a plugin-based architecture to manage key infrastructure components. This modularity allows the engine to swap implementations for different environments (e.g., backtesting vs. live trading) without changing the core algorithm logic. The primary plugin categories include:

    • Result Processing: Manages messages from the algorithmic engine, routing them to destinations like a local GUI or a web interface.
    • Datafeed Sourcing: Handles data acquisition. In backtesting, it sources files from disk; in live trading, it connects to a real-time stream to generate data objects.
    • Transaction Processing: Manages order requests. It uses either internal fill models (for backtesting) or connects to an actual brokerage (for live trading) to process orders and update the algorithm's portfolio.
    • Realtime Event Management: Generates real-time events (e.g., end-of-day) and triggers callbacks. In backtesting, these events are mocked using simulated time.
    • Algorithm State Setup: Responsible for initializing the algorithm's state, including configuring cash, portfolio, and requested data.
  4. Understand LEAN US Equity Data Formats

    master

    QuantConnect provides US Equity data (market 'USA') with various resolutions: Tick, Second, Minute, Hour, and Daily.

    Key Data Characteristics

    • Timezone: US equity data is in the New York timezone. Specific market hours can be found in MarketHoursDatabase.json.
    • Price Format: Prices are stored in deci-cents. To convert deci-cents to dollars, divide prices by 10,000.
    • Data Sources: Post-2007 data is provided by AlgoSeek (trade and quote); pre-2007 trade data is provided by QuantQuote.
    • Filtering: While raw ticks are stored unfiltered, TradeBars and QuoteBars have suspicious ticks filtered out and consolidated.
  5. Understand LEAN Futures Data Formats

    master

    LEAN futures data is provided by AlgoSeek and includes trade, quote, and openinterest data. Data is stored in compressed ZIP files containing one or more CSV files.

    Supported resolutions:

    • Tick
    • Second
    • Minute

    Supported markets:

    • CBOT, CME, NYMEX, COMEX, CBOE, ICE

    tickType values used in file naming and schemas:

    • trade
    • quote
    • openinterest
  6. How to use Python Notebooks for Research

    master

    Python research notebooks use a setup script that automatically loads QuantBook libraries into the Python kernel.

    Setup:

    • Docker Environment: The script runs automatically.
    • Local Environment: You must manually call the setup script in the first cell of your notebook.

    Important Note on start.py: Python research is heavily dependent on the start.py script. It is responsible for assigning the core clr as the runtime for PythonNet and clr-loader. If you are running locally, you must call %run "start.py". Depending on your directory structure, you may need to use a relative path like %run "../start.py".

    For a practical implementation, refer to the KitchenSinkQuantBookTemplate.ipynb reference notebook.

    %run "start.py"
  7. How to use C# Notebooks for Research

    master

    To use C# for research, you must load the Initialize.csx setup script into your notebook. This script loads the QuantConnect libraries into your C# Kernel.

    Setup: In the first cell of your notebook, use the #load directive. In Docker environments, the file is typically one directory above the notebooks directory.

    Important Note on Namespaces: The latest C# research kernel does not support global using statements outside the notebook context. You must add all required Lean namespaces directly via using statements within your notebook cells.

    For a practical implementation, refer to the KitchenSinkCSharpQuantBookTemplate.ipynb reference notebook.

    #load "../Initialize.csx"
    
    using QuantConnect;
    using QuantConnect.Data;
    // ... other namespaces
  8. How PositionGroupKey works

    master

    A PositionGroupKey is a deterministic identifier for a position group. It is constructed from:

    1. The Symbol and UnitQuantity of all contained positions.
    2. The IPositionGroupBuyingPowerModel used by the group.

    Because the UnitQuantities are stored in an ImmutableSortedSet, the order of positions does not change the key (e.g., +100 GOOG; -1 GOOG CALL is identical to -1 GOOG CALL; +100 GOOG). This key is used to index into the PositionManager and other collections. It also acts as a template, as the key contains enough information to reconstruct an IPositionGroup.

    public bool IsDefaultGroup { get; }
    public IPositionGroupBuyingPowerModel BuyingPowerModel { get; }
    public IReadOnlyList<Tuple<Symbol, decimal>> UnitQuantities { get; }
  9. Understand LEAN data storage and file formats

    master
    LEAN uses an open, human-readable, flat-file data format independent of specific databases. Data is stored as compressed .zip files containing individual .csv or .json files. To optimize storage, LEAN only records new ticks and price changes; if there is no activity for a security, the price is omitted from the file.
  10. Understand the purpose of Position Groups

    master

    Position Groups allow algorithms to submit orders for a logical grouping of securities as a single action. This is primarily used to manage margin requirements and risk modeling for hedged or market-neutral strategies.

    By grouping securities (e.g., a covered call consisting of 100 shares of equity and 1 short option contract), the margin required for the group is often significantly lower than the sum of the individual parts. This feature enables LEAN to:

    1. Accurately model reduced margin requirements for hedged positions.
    2. Submit multi-leg orders so brokerages can process them as a single unit, which is often necessary when individual legs would otherwise fail margin checks.
  11. Manage positions and groups with PositionManager

    master

    The PositionManager is responsible for managing both individual positions and IPositionGroup collections. It functions similarly to SecurityPortfolioManager.

    Key behaviors:

    • Automatic Grouping: The manager ensures that all holdings are always grouped. Any holdings that do not match specific criteria are moved into the default SecurityPositionGroup (the 'group of last resort').
    • Event-Driven Updates: The manager listens to SecurityHolding.QuantityChanged events. After every fill event, it invokes the configured IPositionGroupResolver to recalculate the current set of groups.
    • Consistency: To ensure margin calculations and buying power models remain accurate, groups must be resolved immediately following any change in security holdings.