I'm trying to get started with running LEAN locally.
Is there documentation somewhere that features code samples and tutorials on how to do various things with LEAN? For example, I'm trying to figure out how to do the usual stuff like run backtests, connect to my brokerage, and place live trades.
I've checked the Bootcamp on QC but that doesn't seem to really directly teach the ins-n-outs of LEAN, when I try the github link all the documentation pages just point to empty HTML:
Documentation/99 LEAN Engine at master · QuantConnect/Documentation · GitHub
When I try the links on the website, they go to a 404 page:
LEAN Engine - QuantConnect.com
Where can I find a solid learning source for getting started from square 1 with LEAN?
Thank you
Fred Painchaud
Hi Borishu,
I'm also currently learning LEAN. I found these links useful:
Hope this helps,
Cheers,
Fred
Eric Schmidt
Borishu,
I am also just getting started. I also had a lot of problems getting started. I have not tried LEAN CLI. I am most interested in running from local source code because I have goals to do some other integration work that will be easier in local code. I agree with Fred that having access to all local code is a good way to learn because documentation is limited.
I now have the Visual Studio solution running in this way:
I am now able to run either the exampe C# algorithms or the example Python algorithms.
For code samples, look to the projects in the solution itself.
For example (my folder names), in C:\QuantWork\Lean1\Algorithm.CSharp:
BasicTemplateAlgorithm.cs
BasicTemplateAtreyuAlgorithm.cs
BasicTemplateCryptoAlgorithm.cs
BasicTemplateCryptoFrameworkAlgorithm.cs
BasicTemplateDailyAlgorithm.cs
BasicTemplateFillForwardAlgorithm.cs
BasicTemplateForexAlgorithm.cs
BasicTemplateFrameworkAlgorithm.cs
BasicTemplateFuturesAlgorithm.cs
BasicTemplateFuturesConsolidationAlgorithm.cs
BasicTemplateFuturesFrameworkAlgorithm.cs
BasicTemplateFuturesHistoryAlgorithm.cs
BasicTemplateHourlyAlgorithm.cs
BasicTemplateIndexAlgorithm.cs
BasicTemplateIndexOptionsAlgorithm.cs
BasicTemplateIndiaAlgorithm.cs
BasicTemplateIntrinioEconomicData.cs
BasicTemplateLibrary.cs
BasicTemplateMultiAssetAlgorithm.cs
BasicTemplateOptionEquityStrategyAlgorithm.cs
BasicTemplateOptionsAlgorithm.cs
BasicTemplateOptionsFilterUniverseAlgorithm.cs
BasicTemplateOptionsFrameworkAlgorithm.cs
BasicTemplateOptionsHistoryAlgorithm.cs
BasicTemplateOptionStrategyAlgorithm.cs
BasicTemplateOptionTradesAlgorithm.cs
[Benchmarks]
[bin]
BlackLittermanPortfolioOptimizationFrameworkAlgorithm.cs
BrokerageModelAlgorithm.cs
BubbleAlgorithm.cs
CallingRFromCSharp.cs
CancelOpenOrdersRegressionAlgorithm.cs
[CapacityTests]
CapmAlphaRankingFrameworkAlgorithm.cs
CfdTimeZonesRegressionAlgorithm.cs
ClassicRenkoConsolidatorAlgorithm.cs
CoarseFineFundamentalComboAlgorithm.cs
CoarseFineFundamentalRegressionAlgorithm.cs
CoarseFineOptionUniverseChainRegressionAlgorithm.cs
CoarseFundamentalTop3Algorithm.cs
CoarseNoLookAheadBiasAlgorithm.cs
CoarseSelectionTimeRegressionAlgorithm.cs
CompositeAlphaModelFrameworkAlgorithm.cs
CompositeRiskManagementModelFrameworkAlgorithm.cs
ConfidenceWeightedFrameworkAlgorithm.cs
ConsolidateRegressionAlgorithm.cs
ConstituentsQC500GeneratorAlgorithm.cs
ConstituentsUniverseDataGeneratorAlgorithm.cs
ConstituentsUniverseRegressionAlgorithm.cs
ConvertToFrameworkAlgorithm.cs
CustomBenchmarkAlgorithm.cs
CustomBenchmarkRegressionAlgorithm.cs
CustomBrokerageMessageHandlerAlgorithm.cs
CustomBuyingPowerModelAlgorithm.cs
CustomChartingAlgorithm.cs
CustomDataBitcoinAlgorithm.cs
CustomDataIndicatorExtensionsAlgorithm.cs
CustomDataNIFTYAlgorithm.cs
CustomDataPropertiesRegressionAlgorithm.cs
CustomDataRegressionAlgorithm.cs
CustomDataUniverseAlgorithm.cs
CustomDataUsingMapFileRegressionAlgorithm.cs
CustomFrameworkModelsAlgorithm.cs
CustomModelsAlgorithm.cs
CustomPartialFillModelAlgorithm.cs
CustomSecurityInitializerAlgorithm.cs
CustomUniverseSelectionRegressionAlgorithm.cs
CustomUniverseWithBenchmarkRegressionAlgorithm.cs
DailyAlgorithm.cs
DailyHistoryForDailyResolutionRegressionAlgorithm.cs
DailyHistoryForMinuteResolutionRegressionAlgorithm.cs
DailyResolutionSplitRegressionAlgorithm.cs
DataConsolidationAlgorithm.cs
DaylightSavingTimeHistoryRegressionAlgorithm.cs
DelistedFutureLiquidateRegressionAlgorithm.cs
DelistedIndexOptionDivestedRegression.cs
DelistingEventsAlgorithm.cs
DelistingFutureOptionRegressionAlgorithm.cs
DisplacedMovingAverageRibbon.cs
DividendAlgorithm.cs
DropboxBaseDataUniverseSelectionAlgorithm.cs
DropboxUniverseSelectionAlgorithm.cs
DuplicateSecurityWithBenchmarkRegressionAlgorithm.cs
DynamicSecurityDataRegressionAlgorithm.cs
EmaCrossFuturesFrontMonthAlgorithm.cs
EmaCrossUniverseSelectionAlgorithm.cs
EmaCrossUniverseSelectionFrameworkAlgorithm.cs
etc.
In C:\QuantWork\Lean1\Algorithm.Python:
BasicTemplateAlgorithm.py
BasicTemplateAtreyuAlgorithm.py
BasicTemplateCryptoAlgorithm.py
BasicTemplateDailyAlgorithm.py
BasicTemplateFillForwardAlgorithm.py
BasicTemplateForexAlgorithm.py
BasicTemplateFrameworkAlgorithm.py
BasicTemplateFuturesAlgorithm.py
BasicTemplateFuturesConsolidationAlgorithm.py
BasicTemplateFuturesFrameworkAlgorithm.py
BasicTemplateFuturesHistoryAlgorithm.py
BasicTemplateIndexAlgorithm.py
BasicTemplateIndexOptionsAlgorithm.py
BasicTemplateIntrinioEconomicData.py
BasicTemplateLibrary.py
BasicTemplateOptionEquityStrategyAlgorithm.py
BasicTemplateOptionsAlgorithm.py
BasicTemplateOptionsConsolidationAlgorithm.py
BasicTemplateOptionsFilterUniverseAlgorithm.py
BasicTemplateOptionsFrameworkAlgorithm.py
BasicTemplateOptionsHistoryAlgorithm.py
BasicTemplateOptionsPriceModel.py
BasicTemplateOptionStrategyAlgorithm.py
BasicTemplateOptionTradesAlgorithm.py
BlackLittermanPortfolioOptimizationFrameworkAlgorithm.py
BrokerageModelAlgorithm.py
BubbleAlgorithm.py
CapmAlphaRankingFrameworkAlgorithm.py
ClassicRenkoConsolidatorAlgorithm.py
CoarseFineFundamentalComboAlgorithm.py
CoarseFineFundamentalRegressionAlgorithm.py
CoarseFineOptionUniverseChainRegressionAlgorithm.py
CoarseFundamentalTop3Algorithm.py
CompositeAlphaModelFrameworkAlgorithm.py
CompositeRiskManagementModelFrameworkAlgorithm.py
ConfidenceWeightedFrameworkAlgorithm.py
ConsolidateRegressionAlgorithm.py
ConstituentsQC500GeneratorAlgorithm.py
ConstituentsUniverseRegressionAlgorithm.py
ConvertToFrameworkAlgorithm.py
CustomBenchmarkAlgorithm.py
CustomBuyingPowerModelAlgorithm.py
CustomChartingAlgorithm.py
CustomConsolidatorRegressionAlgorithm.py
CustomDataBitcoinAlgorithm.py
CustomDataIconicTypesAddDataRegressionAlgorithm.py
CustomDataIndicatorExtensionsAlgorithm.py
CustomDataLinkedIconicTypeAddDataCoarseSelectionRegressionAlgorithm.py
CustomDataLinkedIconicTypeAddDataOnSecuritiesChangedRegressionAlgorithm.py
CustomDataNIFTYAlgorithm.py
CustomDataPropertiesRegressionAlgorithm.py
CustomDataRegressionAlgorithm.py
CustomDataUniverseAlgorithm.py
CustomDataUsingMapFileRegressionAlgorithm.py
CustomIndicatorAlgorithm.py
CustomModelsAlgorithm.py
CustomPartialFillModelAlgorithm.py
CustomSecurityInitializerAlgorithm.py
CustomVolatilityModelAlgorithm.py
DailyAlgorithm.py
DataConsolidationAlgorithm.py
DelistingEventsAlgorithm.py
DisplacedMovingAverageRibbon.py
DividendAlgorithm.py
DropboxBaseDataUniverseSelectionAlgorithm.py
DropboxCoarseFineAlgorithm.py
DropboxUniverseSelectionAlgorithm.py
DynamicSecurityDataRegressionAlgorithm.py
EmaCrossFuturesFrontMonthAlgorithm.py
EmaCrossUniverseSelectionAlgorithm.py
EmaCrossUniverseSelectionFrameworkAlgorithm.py
ETFConstituentUniverseCompositeDelistingRegressionAlgorithm.py
ETFConstituentUniverseCompositeDelistingRegressionAlgorithmNoAddEquityETF.py
ETFConstituentUniverseFilterFunctionRegressionAlgorithm.py
ETFConstituentUniverseFrameworkRegressionAlgorithm.py
ETFConstituentUniverseMappedCompositeRegressionAlgorithm.py
ETFConstituentUniverseRSIAlphaModelAlgorithm.py
ETFGlobalRotationAlgorithm.py
ExpiryHelperAlphaModelFrameworkAlgorithm.py
ExtendedMarketTradingRegressionAlgorithm.py
FilteredIdentityAlgorithm.py
FilterUniverseRegressionAlgorithm.py
FinancialAdvisorDemoAlgorithm.py
FineFundamentalFilteredUniverseRegressionAlgorithm.py
FractionalQuantityRegressionAlgorithm.py
FutureOptionBuySellCallIntradayRegressionAlgorithm.py
FutureOptionCallITMExpiryRegressionAlgorithm.py
FutureOptionCallOTMExpiryRegressionAlgorithm.py
FutureOptionMultipleContractsInDifferentContractMonthsWithSameUnderlyingFutureRegressionAlgorithm.py
FutureOptionPutITMExpiryRegressionAlgorithm.py
etc.
So we have hundreds of example algorithms included in the Visual Studio project.
Also, it should be easy to develop an algorithm in the cloud using the QuantConnect web UI and then paste that code into a VS source code file to use it locally.
Look at the readme files on GitHub to see how to configure your config.json for each of C# or for Python.
Eric
Eric Schmidt
Be sure to set environment variable PYTHONNET_PYDLL
Mine is: C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\python37.dll
1 - Install Microsoft Visual Studio for use with both C# and Python
2 - Download from GitHub - Lean-master.zip
3 - Unzip to a folder location (mine is C:\QuantWork\Lean1\)
4 - Install and configure Python. (I have 3.7 which was installed as a part of Visual Studio)
Needed for API setup:
5 - For your user id and token, go to: `your account page <https://www.quantconnect.com/account>`_.
use: Request Email With Token and Your User-Id for API Requests
6 - Using pip from command line (from ../scripts location under Python installation):
pip install quantconnect -U
7 - Using pip from command line (from ../scripts location under Python installation):
pip install -e C:\QuantWork\Lean1\PythonToolbox
8 - Open Python Interpreter (Start menu link to Python.exe)
9 - Enter Python's interpreter and type the following commands (change to your actual userid and key as given by QuantConnect:
>>> from quantconnect.api import Api
>>> api = Api(123456, "e34skdjfh675jtud84idutk39dksjfue75fyd63ue65j5hdy7xmdjdusu234jdgs")
>>> p = api.list_projects()
>>> print(len(p['projects']))
By running the above commands one at a time in the interpreter, I was able to verify success. The final print showed me how many projects I have in the online environment.
10 - Edit file: C:\QuantWork\Lean1\Launcher\config.json (adjust location as needed)
Default comes configured for C#.
Change from:
//"algorithm-language": "CSharp",
to:
"algorithm-language": "Python",
When opening QuantConnect.Lean.sln in Visual Studio you will be prompted to install Python support. Should be automatic after responding to the prompt.
Fred Painchaud
See, even using Visual Studio Code instead of Visual Studio changes the details of what you need to do… refer to :
https://www.quantconnect.com/forum/discussion/10747/issues-in-running-python-algorithms-on-lean-locally/p1/comment-36934
So you basically need to be, say, “resourceful” 😊, to properly setup for local code building and execution.
I'm adding here some trick I did with Visual Studio Code, that can certainly be reproduced in Visual Studio and other IDEs:
F
Borishu
Thank you for all the help guys, the links and comments were very helpful.
I started to dive into the source code and think I finally have an understanding of how things flow, finishing up a few more bootcamp courses helped put other things into perspective as well.
Lesson 8 “The Algorithm Framework” in particular was very helpful in covering universe selection, risk management and order execution.
Fred Painchaud
On that note, also check out Algorithmic Trading A-Z in the Courses:
https://www.quantconnect.com/learning/course/5/algorithmic-trading-a-z-the-complete-course
It's also been helpful to me. Louis kinda thinks like I do…
Also check out Louis' YouTube Channel: https://www.youtube.com/c/TradeOptionsWithMe
F
Borishu
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!