{"id":13430779,"url":"https://github.com/jameschch/LeanParameterOptimization","last_synced_at":"2025-03-16T06:31:15.158Z","repository":{"id":38078668,"uuid":"190770271","full_name":"jameschch/LeanParameterOptimization","owner":"jameschch","description":"Parameter Optimization for Lean Algorithms","archived":false,"fork":false,"pushed_at":"2022-12-08T08:03:01.000Z","size":8754,"stargazers_count":58,"open_issues_count":10,"forks_count":21,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-04-10T00:57:12.496Z","etag":null,"topics":["blazor","csharp","lean","lean-algorithms","optimization","sharpe-ratio","trading"],"latest_commit_sha":null,"homepage":"https://optimizers.ml","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jameschch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-07T15:47:27.000Z","updated_at":"2023-12-23T02:08:51.000Z","dependencies_parsed_at":"2023-01-24T21:45:49.184Z","dependency_job_id":null,"html_url":"https://github.com/jameschch/LeanParameterOptimization","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameschch%2FLeanParameterOptimization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameschch%2FLeanParameterOptimization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameschch%2FLeanParameterOptimization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameschch%2FLeanParameterOptimization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jameschch","download_url":"https://codeload.github.com/jameschch/LeanParameterOptimization/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221656385,"owners_count":16858759,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blazor","csharp","lean","lean-algorithms","optimization","sharpe-ratio","trading"],"created_at":"2024-07-31T02:00:57.657Z","updated_at":"2025-03-16T06:31:15.153Z","avatar_url":"https://github.com/jameschch.png","language":"C#","funding_links":[],"categories":["Sample Projects","C# #"],"sub_categories":["Machine Learning"],"readme":"﻿# LeanParameterOptimization\r\nParameter optimization for Lean Trading Algorithms\r\n\r\nThis toolset allows you to execute multiple parallel backtests using a local Lean clone. It is possible to configure several different optimization methods to fit your trading algorithm to an array of different success measures. \r\n\r\nYou must edit the config file [optimization.json](https://github.com/jameschch/LeanParameterOptimization/blob/master/Jtc.Optimization.LeanOptimizer/optimization.json) to define parameters and other settings. The parameter values are fed into the Lean config and can be accessed in an algorithm using the QuantConnect.Configuration.Config methods.\r\n\r\nAn example algorithm is provided here: [ParameterizedAlgorithm.cs](https://github.com/jameschch/LeanParameterOptimization/blob/master/Jtc.Optimization.LeanOptimizer.Example/ParameterizedAlgorithm.cs)\r\nand here: [ParameterizedSharedAppDomainAlgorithm.py](https://github.com/jameschch/LeanParameterOptimization/blob/master/Jtc.Optimization.LeanOptimizer.Example/ParameterizedSharedAppDomainAlgorithm.py)\r\n\r\n## Quickstart\r\n1. Clone Lean from [https://github.com/jameschch/Lean](https://github.com/jameschch/Lean).\r\n2. Clone [LeanParameterOptimization](https://github.com/jameschch/LeanParameterOptimization) so that it shares the same parent folder as the Lean clone.\r\n3. Edit the optimization.json file and enter the location of your trading algorithm in \"algorithmLocation\".\r\n4. Now enter the class name of your algorithm in \"algorithmTypeName\".\r\n5. Enter the location of your trade and quote bar data in the \"dataFolder\" setting.\r\n6. Configure the \"maxThreads\" to define the number of parallel backtests (ignored for Python).\r\n7. Set \"algorithmLanguage\" to CSharp or Python.\r\n8. Build and run the project \"Jtc.Optimization.Launcher.Legacy\"\r\n\r\n## Configuration\r\nFull documentation is provided in comments: [OptimizerConfiguration](https://github.com/jameschch/LeanParameterOptimization/blob/master/Jtc.Optimization.Objects/OptimizerConfiguration.cs)\r\n\r\nA few important options:\r\n\r\n### fitnessTypeName\r\n\r\n#### Genetic\r\nThe default OptimizerFitness is a simple Sharpe Ratio tournament. There is also CompoundingAnnualReturnFitness to maximize raw returns. It is possible to optimize any Lean statistic using ConfiguredFitness.\r\n\r\n#### SharpeMaximizer\r\nSpecifying the SharpeMaximizer fitness allows access to all of the optimization methods provided by the SharpLearning library. These include Random Search. Grid Search, Particle Swarm, Smac and several others.\r\n\r\n#### NFoldCrossSharpeMaximizer\r\nThe simple SharpeMaximizer has been extended in NFoldCrossSharpeMaximizer so that the success score is measured over N-fold periods. This will prevent overfitting to a single in-sample period. \r\n\r\n#### WalkForwardSharpeMaximizer\r\nAlso now available is an experimental release of N-fold Walk Forward optimization.\r\n\r\n#### WalkForwardWeightedMetricSharpeMaximizer\r\nAn attempt to extend the single iteration walk forward optimizer has now evolved into a specialized ensemble machine learning method.\r\n\r\nAn optimization algorithm must be selected as iteration parent. This optimizer will improve a weighted metric cost function composed of the following:\r\ni) The out-sample score of each walk forward period fold, which is derived from an optimization of the in-sample fold using a separately selected optimization algorithm.\r\nii) The standard deviation of the parameter search ranges (the genes) drawn from the best performer in each fold (the alpha).\r\n\r\nAfter each parent iteration, the fold optimization range is constrained with the highest and lowest parameter values of the fold alphas. The effect of this gradual tightening of constraints is analogous to synthesized annealing, allowing convergence on a minimized range of successful parameters.\r\n\r\nThe parent optimizer will stop early for cases in which all out-sample scores return a failure. This indicates that the in-sample does not generalize and can often be resolved with a longer optimization period.\r\n\r\n### useSharedAppDomain\r\nIf it possible to run each parallel backtest in it's own context, or in a single context. The latter option can be useful for training a machine learning model and tends to execute more quickly. For Python algorithms this setting is ignored as only a single context is supported.\r\n\r\n### minimumTrades\r\nSetting this value correctly will prevent fitting to a small number of high-success events that are unlikely to generalize. Any backtest not meeting the minimum trades will be ignored.\r\n\r\n### enableRunningDuplicateParameters\r\nWhen training a non-deterministic machine learning model (such as QLearning), this will allow executing the same parameters multiple times on a single period. By default, duplicate parameters are not executed more than once.\r\n\r\n## Optimizers\r\nThe optimizers support multiple parallel executions of a Lean algorithm as standard. Currently, the following methods are available:\r\n* Genetic Tournament\r\n* Random Search\r\n* Grid (exhaustive) Search\r\n* Particle Swarm\r\n* Bayesian\r\n* Globalized Bounded Nelder Mead\r\n* Smac\r\n\r\nThese methods can target several fitness and maximization goals:\r\n* N-Fold Cross-Validated Sharpe Ratio\r\n* N-Fold Cross-Validated Compounding Annual Return\r\n* Walk Forward Period Sharpe Ratio\r\n* Nested Cross-Validated Sharpe Ratio\r\n* Maximization and minimization of most Lean algorithm statistics (Sharpe Ratio, Alpha, Win Rate, etc)\r\n\r\n## User Interfaces\r\nNow also provided are several Blazor interfaces:\r\n\r\n* Optimization Config Editor - [https://optimizers.ml/config](https://optimizers.ml/config)\r\n\r\n![Config](https://raw.githubusercontent.com/jameschch/LeanParameterOptimization/master/config.png)\r\n\r\n* Optimization Results Chart - [https://optimizers.ml/chart](https://optimizers.ml/chart)\r\n\r\n![Chart](https://raw.githubusercontent.com/jameschch/LeanParameterOptimization/master/chart.png)\r\n\r\n* Algorithm Code Editor and Runner (C#, Javascript) - [https://optimizers.ml/codeeditor](https://optimizers.ml/codeeditor)\r\n\r\n## WIP\r\n* Python multi-threaded parallel optimization\r\n* Python running in browser\r\n* User supplied C# to wasm compile in browser\r\n* Genetic optimization in browser\r\n* Other optimization methods in browser\r\n* Script to modify base Lean clone with required changes\r\n\r\n## Issues\r\n* Logging to console for multiple contexts was broken during ,net5 upgrade\r\n* Specifc changes to base Lean code are necessary. Please use this fork: [https://github.com/jameschch/Lean](https://github.com/jameschch/Lean)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjameschch%2FLeanParameterOptimization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjameschch%2FLeanParameterOptimization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjameschch%2FLeanParameterOptimization/lists"}