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  public string Name { get; set; }
36 
37  /// <summary>
38  /// List of tags associated with the algorithm
39  /// </summary>
40  public ISet<string> Tags { get; set; }
41 
42  /// <summary>
43  /// The algorithm's account currency
44  /// </summary>
45  [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
46  public string AccountCurrency { get; set; }
47 
48  /// <summary>
49  /// The algorithm's brokerage model
50  /// </summary>
51  /// <remarks> Required to set the correct brokerage model on report generation.</remarks>
52  public BrokerageName Brokerage { get; set; }
53 
54  /// <summary>
55  /// The algorithm's account type
56  /// </summary>
57  /// <remarks> Required to set the correct brokerage model on report generation.</remarks>
58  public AccountType AccountType { get; set; }
59 
60  /// <summary>
61  /// The parameters used by the algorithm
62  /// </summary>
63  public IReadOnlyDictionary<string, string> Parameters { get; set; }
64 
65  /// <summary>
66  /// Backtest maximum end date
67  /// </summary>
68  public DateTime? OutOfSampleMaxEndDate { get; set; }
69 
70  /// <summary>
71  /// The backtest out of sample day count
72  /// </summary>
73  public int OutOfSampleDays { get; set; }
74 
75  /// <summary>
76  /// The backtest start date
77  /// </summary>
78  [JsonConverter(typeof(DateTimeJsonConverter), DateFormat.UI)]
79  public DateTime StartDate { get; set; }
80 
81  /// <summary>
82  /// The backtest end date
83  /// </summary>
84  [JsonConverter(typeof(DateTimeJsonConverter), DateFormat.UI)]
85  public DateTime EndDate { get; set; }
86 
87  /// <summary>
88  /// Number of trading days per year for Algorithm's portfolio statistics.
89  /// </summary>
90  public int TradingDaysPerYear { get; set; }
91 
92  /// <summary>
93  /// Initializes a new instance of the <see cref="AlgorithmConfiguration"/> class
94  /// </summary>
95  public AlgorithmConfiguration(string name, ISet<string> tags, string accountCurrency, BrokerageName brokerageName,
96  AccountType accountType, IReadOnlyDictionary<string, string> parameters, DateTime startDate, DateTime endDate,
97  DateTime? outOfSampleMaxEndDate, int outOfSampleDays = 0, int tradingDaysPerYear = 0)
98  {
99  Name = name;
100  Tags = tags;
101  OutOfSampleMaxEndDate = outOfSampleMaxEndDate;
102  TradingDaysPerYear = tradingDaysPerYear;
103  OutOfSampleDays = outOfSampleDays;
104  AccountCurrency = accountCurrency;
105  Brokerage = brokerageName;
106  AccountType = accountType;
107  Parameters = parameters;
108  StartDate = startDate;
109  EndDate = endDate;
110  }
111 
112  /// <summary>
113  /// Initializes a new empty instance of the <see cref="AlgorithmConfiguration"/> class
114  /// </summary>
116  {
117  // use default value for backwards compatibility
118  TradingDaysPerYear = 252;
119  }
120 
121  /// <summary>
122  /// Provides a convenience method for creating a <see cref="AlgorithmConfiguration"/> for a given algorithm.
123  /// </summary>
124  /// <param name="algorithm">Algorithm for which the configuration object is being created</param>
125  /// <param name="backtestNodePacket">The associated backtest node packet if any</param>
126  /// <returns>A new AlgorithmConfiguration object for the specified algorithm</returns>
127  public static AlgorithmConfiguration Create(IAlgorithm algorithm, BacktestNodePacket backtestNodePacket)
128  {
129  return new AlgorithmConfiguration(
130  algorithm.Name,
131  algorithm.Tags,
132  algorithm.AccountCurrency,
134  algorithm.BrokerageModel.AccountType,
135  algorithm.GetParameters(),
136  algorithm.StartDate,
137  algorithm.EndDate,
138  backtestNodePacket?.OutOfSampleMaxEndDate,
139  backtestNodePacket?.OutOfSampleDays ?? 0,
140  // use value = 252 like default for backwards compatibility
141  algorithm?.Settings?.TradingDaysPerYear ?? 252);
142  }
143  }
144 }