Lean  $LEAN_TAG$
AlgorithmConfiguration.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using Newtonsoft.Json;
18 using QuantConnect.Util;
19 using QuantConnect.Packets;
22 using System.Collections.Generic;
23 
24 namespace QuantConnect
25 {
26  /// <summary>
27  /// This class includes algorithm configuration settings and parameters.
28  /// This is used to include configuration parameters in the result packet to be used for report generation.
29  /// </summary>
31  {
32  /// <summary>
33  /// The algorithm's name
34  /// </summary>
35  [JsonProperty(PropertyName = "Name")]
36  public string Name;
37 
38  /// <summary>
39  /// List of tags associated with the algorithm
40  /// </summary>
41  [JsonProperty(PropertyName = "Tags")]
42  public ISet<string> Tags;
43 
44  /// <summary>
45  /// The algorithm's account currency
46  /// </summary>
47  [JsonProperty(PropertyName = "AccountCurrency", NullValueHandling = NullValueHandling.Ignore)]
48  public string AccountCurrency;
49 
50  /// <summary>
51  /// The algorithm's brokerage model
52  /// </summary>
53  /// <remarks> Required to set the correct brokerage model on report generation.</remarks>
54  [JsonProperty(PropertyName = "Brokerage")]
56 
57  /// <summary>
58  /// The algorithm's account type
59  /// </summary>
60  /// <remarks> Required to set the correct brokerage model on report generation.</remarks>
61  [JsonProperty(PropertyName = "AccountType")]
63 
64  /// <summary>
65  /// The parameters used by the algorithm
66  /// </summary>
67  [JsonProperty(PropertyName = "Parameters")]
68  public IReadOnlyDictionary<string, string> Parameters;
69 
70  /// <summary>
71  /// Backtest maximum end date
72  /// </summary>
73  [JsonProperty(PropertyName = "OutOfSampleMaxEndDate")]
74  public DateTime? OutOfSampleMaxEndDate;
75 
76  /// <summary>
77  /// The backtest out of sample day count
78  /// </summary>
79  [JsonProperty(PropertyName = "OutOfSampleDays")]
80  public int OutOfSampleDays;
81 
82  /// <summary>
83  /// The backtest start date
84  /// </summary>
85  [JsonProperty(PropertyName = "StartDate")]
86  [JsonConverter(typeof(DateTimeJsonConverter), DateFormat.UI)]
87  public DateTime StartDate;
88 
89  /// <summary>
90  /// The backtest end date
91  /// </summary>
92  [JsonProperty(PropertyName = "EndDate")]
93  [JsonConverter(typeof(DateTimeJsonConverter), DateFormat.UI)]
94  public DateTime EndDate;
95 
96  /// <summary>
97  /// Number of trading days per year for Algorithm's portfolio statistics.
98  /// </summary>
99  [JsonProperty(PropertyName = "TradingDaysPerYear")]
100  public int TradingDaysPerYear;
101 
102  /// <summary>
103  /// Initializes a new instance of the <see cref="AlgorithmConfiguration"/> class
104  /// </summary>
105  public AlgorithmConfiguration(string name, ISet<string> tags, string accountCurrency, BrokerageName brokerageName,
106  AccountType accountType, IReadOnlyDictionary<string, string> parameters, DateTime startDate, DateTime endDate,
107  DateTime? outOfSampleMaxEndDate, int outOfSampleDays = 0, int tradingDaysPerYear = 0)
108  {
109  Name = name;
110  Tags = tags;
111  OutOfSampleMaxEndDate = outOfSampleMaxEndDate;
112  TradingDaysPerYear = tradingDaysPerYear;
113  OutOfSampleDays = outOfSampleDays;
114  AccountCurrency = accountCurrency;
115  BrokerageName = brokerageName;
116  AccountType = accountType;
117  Parameters = parameters;
118  StartDate = startDate;
119  EndDate = endDate;
120  }
121 
122  /// <summary>
123  /// Initializes a new empty instance of the <see cref="AlgorithmConfiguration"/> class
124  /// </summary>
126  {
127  // use default value for backwards compatibility
128  TradingDaysPerYear = 252;
129  }
130 
131  /// <summary>
132  /// Provides a convenience method for creating a <see cref="AlgorithmConfiguration"/> for a given algorithm.
133  /// </summary>
134  /// <param name="algorithm">Algorithm for which the configuration object is being created</param>
135  /// <param name="backtestNodePacket">The associated backtest node packet if any</param>
136  /// <returns>A new AlgorithmConfiguration object for the specified algorithm</returns>
137  public static AlgorithmConfiguration Create(IAlgorithm algorithm, BacktestNodePacket backtestNodePacket)
138  {
139  return new AlgorithmConfiguration(
140  algorithm.Name,
141  algorithm.Tags,
142  algorithm.AccountCurrency,
144  algorithm.BrokerageModel.AccountType,
145  algorithm.GetParameters(),
146  algorithm.StartDate,
147  algorithm.EndDate,
148  backtestNodePacket?.OutOfSampleMaxEndDate,
149  backtestNodePacket?.OutOfSampleDays ?? 0,
150  // use value = 252 like default for backwards compatibility
151  algorithm?.Settings?.TradingDaysPerYear ?? 252);
152  }
153  }
154 }