Lean  $LEAN_TAG$
Objective.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 System.Linq;
18 using System.Text.RegularExpressions;
19 using Newtonsoft.Json;
20 
22 {
23  /// <summary>
24  /// Base class for optimization <see cref="Objectives.Target"/> and <see cref="Constraint"/>
25  /// </summary>
26  public abstract class Objective
27  {
28  private readonly Regex _targetTemplate = new Regex("['(.+)']");
29  private string _target;
30 
31  /// <summary>
32  /// Target; property of json file we want to track
33  /// </summary>
34  public string Target
35  {
36  get => _target;
37  set
38  {
39  _target = value != null ? string.Join(".", value.Split('.').Select(s => _targetTemplate.Match(s).Success ? s : $"['{s}']")) : value;
40  }
41  }
42 
43  /// <summary>
44  /// Target value
45  /// </summary>
46  /// <remarks>For <see cref="Objectives.Target"/> if defined and backtest complies with the targets then finish optimization</remarks>
47  /// <remarks>For <see cref="Constraint"/> non optional, the value of the target constraint</remarks>
48  public decimal? TargetValue { get; set; }
49 
50  /// <summary>
51  /// Creates a new instance of Objective class
52  /// </summary>
53  protected Objective()
54  {
55 
56  }
57 
58  /// <summary>
59  /// Creates a new instance
60  /// </summary>
61  protected Objective(string target, decimal? targetValue)
62  {
63  if (string.IsNullOrEmpty(target))
64  {
65  throw new ArgumentNullException(nameof(target), Messages.Objective.NullOrEmptyObjective);
66  }
67 
68  var objective = target;
69  if (!objective.Contains('.', StringComparison.InvariantCulture))
70  {
71  // default path
72  objective = $"Statistics.{objective}";
73  }
74  // escape empty space in json path
75  Target = objective;
76  TargetValue = targetValue;
77  }
78 
79  #region Backwards Compatibility
80  /// <summary>
81  /// Target value
82  /// </summary>
83  /// <remarks>For <see cref="Objectives.Target"/> if defined and backtest complies with the targets then finish optimization</remarks>
84  /// <remarks>For <see cref="Constraint"/> non optional, the value of the target constraint</remarks>
85  [JsonProperty("target-value")]
86  public decimal? OldTargetValue
87  {
88  set
89  {
90  TargetValue = value;
91  }
92  get
93  {
94  return TargetValue;
95  }
96  }
97  #endregion
98  }
99 }