Optimization

Project 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.

Configuring 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 of the config.json file. All keys and values of this object must be strings. Example:
    {
        "parameters": {
            "ema-fast": "10",
            "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 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 _slow;
    
            public override void Initialize()
            {
                SetStartDate(2020, 1, 1);
                SetCash(100000);
                AddEquity("SPY");
    
                var emaFast = GetParameter("ema-fast");
                var emaSlow = GetParameter("ema-slow");
    
                var fastPeriod = int.Parse(emaFast);
                var slowPeriod = int.Parse(emaSlow);
    
                _fast = EMA("SPY", fastPeriod);
                _slow = EMA("SPY", slowPeriod);
            }
        }
    }
    class ParameterizedAlgorithm(QCAlgorithm):
        def Initialize(self):
            self.SetStartDate(2020, 1, 1)
            self.SetCash(100000)
            self.AddEquity("SPY")
    
            ema_fast = self.GetParameter("ema-fast")
            ema_slow = self.GetParameter("ema-slow")
    
            fast_period = int(ema_fast)
            slow_period = int(ema_slow)
    
            self._fast = self.EMA("SPY", fast_period)
            self._slow = self.EMA("SPY", slow_period)

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

Did you find this page helpful?

Contribute to the documentation: