Lean  $LEAN_TAG$
ParametersReportElement.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.Collections.Generic;
17 using System.Linq;
18 using System.Text.RegularExpressions;
19 
21 {
22  /// <summary>
23  /// Class for creating a two column table for the Algorithm's Parameters in a report
24  /// </summary>
26  {
27  private IReadOnlyDictionary<string, string> _parameters;
28  private readonly string _template;
29 
30  /// <summary>
31  /// Creates a two column table for the Algorithm's Parameters
32  /// </summary>
33  /// <param name="name">Name of the widget</param>
34  /// <param name="key">Location of injection</param>
35  /// <param name="backtestConfiguration">The configuration of the backtest algorithm</param>
36  /// <param name="liveConfiguration">The configuration of the live algorithm</param>
37  /// <param name="template">HTML template to use</param>
38  public ParametersReportElement(string name, string key, AlgorithmConfiguration backtestConfiguration, AlgorithmConfiguration liveConfiguration, string template)
39  {
40  Name = name;
41  Key = key;
42  _template = template;
43 
44  if (liveConfiguration != null)
45  {
46  _parameters = liveConfiguration.Parameters;
47  }
48  else if (backtestConfiguration != null)
49  {
50  _parameters = backtestConfiguration.Parameters;
51  }
52  else
53  {
54  // This case only happens for the unit tests, then we just create an empty dictionary
55  _parameters = new Dictionary<string, string>();
56  }
57  }
58 
59  /// <summary>
60  /// Generates a HTML two column table for the Algorithm's Parameters
61  /// </summary>
62  /// <returns>Returns a string representing a HTML two column table</returns>
63  public override string Render()
64  {
65  var items = new List<string>();
66  int parameterIndex;
67  var columns = (new Regex(@"{{\$KEY(\d+?)}}")).Matches(_template).Count;
68 
69  for (parameterIndex = 0; parameterIndex < _parameters.Count;)
70  {
71  var template = _template;
72  int column;
73  for (column = 0; column < columns; column++)
74  {
75  var currTemplateKey = "{{$KEY" + column.ToString() + "}}";
76  var currTemplateValue = "{{$VALUE" + column.ToString() + "}}";
77 
78  if (parameterIndex < _parameters.Count)
79  {
80  var parameter = _parameters.ElementAt(parameterIndex);
81  template = template.Replace(currTemplateKey, parameter.Key);
82  template = template.Replace(currTemplateValue, parameter.Value);
83  }
84  else
85  {
86  template = template.Replace(currTemplateKey, string.Empty);
87  template = template.Replace(currTemplateValue, string.Empty);
88  }
89 
90  parameterIndex++;
91  }
92 
93  if (column == 0)
94  {
95  parameterIndex++;
96  }
97 
98  items.Add(template);
99  }
100 
101  if (Key == ReportKey.ParametersPageStyle)
102  {
103  if (items.Count == 0)
104  {
105  return "display: none;";
106  }
107 
108  return string.Empty;
109  }
110 
111  var parameters= string.Join("\n", items);
112  return parameters;
113  }
114  }
115 }