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:

  1. Open your project in your preferred editor.
  2. Open the project's config.json file.
  3. 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"
        }
    }
  4. Open your algorithm in the editor.
  5. Call QCAlgorithm.GetParameter(name) in your algorithm to retrieve the string value of a parameter and use that instead of constant values. For example, the ema-fast, ema-medium, and ema-slow parameters from step 3 can be used like this:
    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 emaFast = GetParameter("ema-fast");
                var emaMedium = GetParameter("ema-medium");
                var emaSlow = GetParameter("ema-slow");
    
                var fastPeriod = int.Parse(emaFast);
                var mediumPeriod = int.Parse(emaMedium);
                var slowPeriod = int.Parse(emaSlow);
    
                _fast = EMA("SPY", fastPeriod);
                _medium = EMA("SPY", mediumPeriod);
                _slow = EMA("SPY", slowPeriod);
            }
        }
    }
    class ParameterizedAlgorithm(QCAlgorithm):
        def Initialize(self) -> None:
            self.SetStartDate(2020, 1, 1)
            self.SetCash(100000)
            self.AddEquity("SPY")
    
            ema_fast = self.GetParameter("ema-fast")
            ema_medium = self.GetParameter("ema-medium")
            ema_slow = self.GetParameter("ema-slow")
    
            fast_period = int(ema_fast)
            medium_period = int(ema_medium)
            slow_period = int(ema_slow)
    
            self._fast = self.EMA("SPY", fast_period)
            self._medium = self.EMA("SPY", medium_period)
            self._slow = self.EMA("SPY", slow_period)

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: