Over the last weeks, we've made significant strides to standardize and streamline our Python API to PEP8 for better conformance with modern standards. This change makes our Python API more intuitive for native Python users and improves the readability of algorithms.
The change was done by improving the technology that bridges LEAN and Python strategies, enabling backward-compatible use of the former CamelCase API and the new snake_case API versions of methods. The change had a negligible speed impact.
To support client migration, we've ported the QuantConnect documentation to PEP8 so you can start writing your algorithm in the new API format immediately. The old API will continue to work as the underlying LEAN engine, and C# projects still use them. To migrate your strategy, use the following shortcuts for some examples:
# Before
# After
# Lowercase properties, underscore between words (snake case):
self.Portfolio[self.Securities[self.spy]].HoldingsValue
self.portfolio[self.securities[self.spy]].holdings_value
# Snake case parameters:
self.AddEquity("SPY", extendedMarketHours=True)
self.add_equity("SPY", extended_market_hours=True)
# Capitalized constants, CamelCase classes:
if securityChanges == SecurityChanges.None:
if security_changes == SecurityChanges.NONE
# Snake case methods:
def OnData(self, data):
def on_data(self, data):
# Lowercase methods, _prefix encouraged for private variables:
self.ema = self.EMA(symbol, 30, Resolution.Minute)
self._ema = self.ema(symbol, 20, Resolution.MINUTE)
# Snake case methods, and CamelCase classes:
self.AddUniverseSelection(ManualUniverseSelectionModel(symbols))
self.add_universe_selection(ManualUniverseSelectionModel(symbols))
Potential Name Conflicts
For some clients, there may be breaking changes to strategies that use the same variable names as the underlying C# API. For example, a lowercase "self.securities" was permitted before, but will now clash with the uppercase "self.Securities" collection. This may cause strange new errors in your project.
Selecting Previous LEAN Build
To delay migration to PEP8, you can select the old version of the LEAN engine using the LEAN Version selector. This will be kept open for the next month to give you time to migrate your strategy. To select the old LEAN version, navigate to your project panel's "LEAN Engine" section and select the "master v16356". By default, all new projects are created on the master branch of LEAN and will use pep8 formatting (master v16357+).
Migration Script
We've written a Python research notebook that ports code to the new PEP8 standard and attached it to the discussion thread below. You can automatically rewrite the methods to the new APIs using this notebook.
In addition, we've invested heavily in standardizing our REST API across all its functions and updating the documentation to reflect the features we support. The new REST documentation will be published in the coming weeks as we finalize and ship the updates. If you depend on our REST API, reach out so we can give you advanced notice about the updates.
Thank you for your continued support; we're grateful to serve a truly excellent community. Happy coding!
Team @ QuantConnect
Jared Broad
To use the automatic conversion script, clone the project below and open the research notebook. Once you've connected to the kernel paste in your code to convert and run all cells.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Ethan Hammond
Has anyone figured out how to get around the output limit on the notebook converter? I'm only get able to get ~40 lines at a time.
Ethan Hammond
I've included a few examples of unintentional changes/errors made by the conversion script noted above. Hopefully this will help speed up your debugging.
Jared Broad
Thanks Ethan! Sorry about the line limit, I don't think there's an easy way other than pasting in just 40 lines at a time for now. 2) should be corrected to "x.value.invested" but it'll work as you showed above as it is backward compatible.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Ethan Hammond
I'm not sure why, but it does not like the lowercase x.value or x.key.
Noel Jackson
Even when rolling back the lean engine version, my algorithm is broken. Can anyone help explain why?
Jared Broad
Can you please send in a support ticket? We'd be happy to help Noel Jackson
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Yuri Lopukhov
OptionRight.Put is converted to Optionright.put instead of OptionRight.PUT in this code:
However, this code is converted correctly:
I am using this from command line, just copy main.py and exceptions.py from the project to local files, and add this code on the bottom of main.py:
Then call
to convert any file without line limits.
But it would be easier if there was a built-in option to convert project to new format.
Kubid
Does this migration affect lean CLI that runs locally?
It seems Lean's documentation has not been updated with this new format.
For exmaple, for OrderEvent Class, lean's reference documentation still shows “OrderID” instread of “order_id”
https://www.lean.io/docs/v2/lean-engine/class-reference/classQuantConnect_1_1Orders_1_1OrderEvent.html
Thanks,
Andy
Ethan Hammond
On my production strategy, I rolled the lean version back to give myself some time to convert the whole project. Now I am having issues rolling into v16364. Is buying this upgrade the only way to run the PEP8 engine? I do not want to create a new project, as I would lose historical trends.
Jared Broad
You should be able to pin it without upgrading this month. We'll look into the issue first thing Monday, sorry about the hassle.
@kupid the change doesn't impact lean and we wouldn't change that page, but the order event docs in writing algorithms has been updated
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Wai
Is May 20, 2024 the cut-off date for the migration?
My backtest failed when I self.SetStartDate on or before May 20, 2024 at this line:
Instead, the line below works:
So, if self.SetStartDate is May 21, 2024 or thereafter, the first line works!
Jared Broad
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!