Lean  $LEAN_TAG$
Target.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 Newtonsoft.Json;
17 using Newtonsoft.Json.Linq;
18 using System;
19 
21 {
22  /// <summary>
23  /// The optimization statistical target
24  /// </summary>
25  public class Target: Objective
26  {
27  /// <summary>
28  /// Defines the direction of optimization, i.e. maximization or minimization
29  /// </summary>
30  public Extremum Extremum { get; set; }
31 
32  /// <summary>
33  /// Current value
34  /// </summary>
35  [JsonIgnore]
36  public decimal? Current { get; private set; }
37 
38  /// <summary>
39  /// Fires when target complies specified value
40  /// </summary>
41  public event EventHandler Reached;
42 
43  /// <summary>
44  /// Creates a new instance
45  /// </summary>
46  public Target(string target, Extremum extremum, decimal? targetValue): base(target, targetValue)
47  {
48  Extremum = extremum;
49  }
50 
51  /// <summary>
52  /// Creates a new instance
53  /// </summary>
54  public Target()
55  {
56 
57  }
58 
59  /// <summary>
60  /// Pretty representation of this optimization target
61  /// </summary>
62  public override string ToString()
63  {
64  return Messages.Target.ToString(this);
65  }
66 
67  /// <summary>
68  /// Check backtest result
69  /// </summary>
70  /// <param name="jsonBacktestResult">Backtest result json</param>
71  /// <returns>true if found a better solution; otherwise false</returns>
72  public bool MoveAhead(string jsonBacktestResult)
73  {
74  if (string.IsNullOrEmpty(jsonBacktestResult))
75  {
76  throw new ArgumentNullException(nameof(jsonBacktestResult), $"Target.MoveAhead(): {Messages.OptimizerObjectivesCommon.NullOrEmptyBacktestResult}");
77  }
78 
79  var token = GetTokenInJsonBacktest(jsonBacktestResult, Target);
80  if (token == null)
81  {
82  return false;
83  }
84  var computedValue = token.Value<string>().ToNormalizedDecimal();
85  if (!Current.HasValue || Extremum.Better(Current.Value, computedValue))
86  {
87  Current = computedValue;
88 
89  return true;
90  }
91 
92  return false;
93  }
94 
95  /// <summary>
96  /// Try comply target value
97  /// </summary>
98  public void CheckCompliance()
99  {
100  if (IsComplied())
101  {
102  Reached?.Invoke(this, EventArgs.Empty);
103  }
104  }
105 
106  public static JToken GetTokenInJsonBacktest(string jsonBacktestResult, string target)
107  {
108  var jObject = JObject.Parse(jsonBacktestResult);
109  var path = target.Replace("[", string.Empty, StringComparison.InvariantCultureIgnoreCase)
110  .Replace("]", string.Empty, StringComparison.InvariantCultureIgnoreCase)
111  .Replace("\'", string.Empty, StringComparison.InvariantCultureIgnoreCase).Split(".");
112  JToken token = null;
113  foreach (var key in path)
114  {
115  if (jObject.TryGetValue(key, StringComparison.OrdinalIgnoreCase, out token))
116  {
117  if (token is not JValue)
118  {
119  jObject = token.ToObject<JObject>();
120  }
121  }
122  else
123  {
124  return null;
125  }
126  }
127 
128  return token;
129  }
130 
131  private bool IsComplied() => TargetValue.HasValue && Current.HasValue && (TargetValue.Value == Current.Value || Extremum.Better(TargetValue.Value, Current.Value));
132  }
133 }