🚀 AI One-Click Generation of quantconnect Strategy Code

How to Update Your Code for QuantConnect's PEP8 Property Naming (e.g., HoldingsValue to holdings_value)

quantconnect | Published on: 2025/12/9
The following content is generated by EasyQuant .

Problem Description

My script uses `self.Portfolio.HoldingsValue` and is throwing an error. What's the new way to write this?

Solution

Based on the QuantConnect PEP8 API migration, the naming convention has changed from CamelCase (C# style) to snake_case (Python standard).

Here is the correct way to write it:

Correct Syntax

You should change self.Portfolio to self.portfolio and HoldingsValue to holdings_value.

# Old (Deprecated)
current_value = self.Portfolio["SPY"].HoldingsValue

# New (PEP8)
current_value = self.portfolio["SPY"].holdings_value

Explanation of Changes

  1. Properties are now lowercase: self.Portfolio becomes self.portfolio.
  2. Attributes use snake_case: HoldingsValue becomes holdings_value.
  3. Collections: If you are accessing the securities collection, self.Securities becomes self.securities.

Common Portfolio Property Mappings

Here is a quick reference for other common portfolio properties you might need to update:

Old API (CamelCase) New API (snake_case) Description
self.Portfolio.Invested self.portfolio.invested Boolean indicating if there are any holdings.
self.Portfolio.Cash self.portfolio.cash The total cash in the account.
self.Portfolio.TotalPortfolioValue self.portfolio.total_portfolio_value Total value of cash + holdings.
self.Portfolio["SPY"].Quantity self.portfolio["SPY"].quantity The number of shares/contracts held.
self.Portfolio["SPY"].AveragePrice self.portfolio["SPY"].average_price The average entry price of the holding.
self.Portfolio["SPY"].UnrealizedProfit self.portfolio["SPY"].unrealized_profit Unrealized PnL for the specific asset.

Q&A: QuantConnect API Migration

Why is my QuantConnect code throwing attribute errors?

QuantConnect recently updated their Python API to follow PEP8 standards. This means variable and method names that previously used CamelCase (e.g., self.AddEquity, self.Portfolio) now use snake_case (e.g., self.add_equity, self.portfolio). Using the old capitalization will result in AttributeError.

How do I fix "AttributeError: 'QCAlgorithm' object has no attribute 'Securities'"?

Change self.Securities to self.securities. The capitalization of the property has changed to lowercase to comply with Python conventions.

Can I still use the old LEAN engine version?

Yes. If you do not wish to migrate your code immediately, you can select the legacy version of the LEAN engine in your project settings. Look for the "LEAN Engine" section and select "master v16356". However, new projects default to the PEP8 version (master v16357+).

How do I access specific security data in the new API?

Previously you might have used self.Securities["SPY"].Price. In the new API, use self.securities["SPY"].price. Ensure both the collection name and the property name are lowercased.