Optimization
Parameters
Introduction
Project parameters are parameters that are defined in your project's configuration file. These parameters are a replacement for constants in your algorithm and can be optimized using one of LEAN's optimization strategies either locally or in the cloud.
Configure Project Parameters
Follow these steps to make your algorithm use project parameters instead of constant values:
- Open your project in your preferred editor.
- Open the project's config.json file.
- Add the required parameters in the
parameters
property. All keys and values of this object must be strings. Example:{ "parameters": { "ema-fast": "10", "ema-medium": "30", "ema-slow": "50" } }
- Open your algorithm in the editor.
- Call
QCAlgorithm.GetParameter(name)
in your algorithm to retrieve the value of a parameter and use that instead of constant values.namespace QuantConnect.Algorithm.CSharp { public class ParameterizedAlgorithm : QCAlgorithm { private ExponentialMovingAverage _fast; private ExponentialMovingAverage _medium; private ExponentialMovingAverage _slow; public override void Initialize() { SetStartDate(2020, 1, 1); SetCash(100000); AddEquity("SPY"); var fastPeriod = GetParameter("ema-fast", 10); var mediumPeriod = GetParameter("ema-medium", 30); var slowPeriod = GetParameter("ema-slow", 50); _fast = EMA("SPY", fastPeriod); _medium = EMA("SPY", mediumPeriod); _slow = EMA("SPY", slowPeriod); } } }
class ParameterizedAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2020, 1, 1) self.set_cash(100000) self.add_equity("SPY") fast_period = self.get_parameter("ema-fast", 10) medium_period = self.get_parameter("ema-medium", 30) slow_period = self.get_parameter("ema-slow", 50) self._fast = self.ema("SPY", fast_period) self._medium = self.ema("SPY", medium_period) self._slow = self.ema("SPY", slow_period)