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  public Target()
52  {
53 
54  }
55 
56  /// <summary>
57  /// Pretty representation of this optimization target
58  /// </summary>
59  public override string ToString()
60  {
61  return Messages.Target.ToString(this);
62  }
63 
64  /// <summary>
65  /// Check backtest result
66  /// </summary>
67  /// <param name="jsonBacktestResult">Backtest result json</param>
68  /// <returns>true if found a better solution; otherwise false</returns>
69  public bool MoveAhead(string jsonBacktestResult)
70  {
71  if (string.IsNullOrEmpty(jsonBacktestResult))
72  {
73  throw new ArgumentNullException(nameof(jsonBacktestResult), $"Target.MoveAhead(): {Messages.OptimizerObjectivesCommon.NullOrEmptyBacktestResult}");
74  }
75 
76  var token = JObject.Parse(jsonBacktestResult).SelectToken(Target);
77  if (token == null)
78  {
79  return false;
80  }
81  var computedValue = token.Value<string>().ToNormalizedDecimal();
82  if (!Current.HasValue || Extremum.Better(Current.Value, computedValue))
83  {
84  Current = computedValue;
85 
86  return true;
87  }
88 
89  return false;
90  }
91 
92  /// <summary>
93  /// Try comply target value
94  /// </summary>
95  public void CheckCompliance()
96  {
97  if (IsComplied())
98  {
99  Reached?.Invoke(this, EventArgs.Empty);
100  }
101  }
102 
103  private bool IsComplied() => TargetValue.HasValue && Current.HasValue && (TargetValue.Value == Current.Value || Extremum.Better(TargetValue.Value, Current.Value));
104  }
105 }