Generate code in 3 minutes
Enter your idea and AI generates runnable code.
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:
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
self.Portfolio becomes self.portfolio.HoldingsValue becomes holdings_value.self.Securities becomes self.securities.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. |
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.
Change self.Securities to self.securities. The capitalization of the property has changed to lowercase to comply with Python conventions.
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+).
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.