/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; namespace QuantConnect.Optimizer.Objectives { /// /// The optimization statistical target /// public class Target: Objective { /// /// Defines the direction of optimization, i.e. maximization or minimization /// public Extremum Extremum { get; set; } /// /// Current value /// [JsonIgnore] public decimal? Current { get; private set; } /// /// Fires when target complies specified value /// public event EventHandler Reached; /// /// Creates a new instance /// public Target(string target, Extremum extremum, decimal? targetValue): base(target, targetValue) { Extremum = extremum; } /// /// Creates a new instance /// public Target() { } /// /// Pretty representation of this optimization target /// public override string ToString() { return Messages.Target.ToString(this); } /// /// Check backtest result /// /// Backtest result json /// true if found a better solution; otherwise false public bool MoveAhead(string jsonBacktestResult) { if (string.IsNullOrEmpty(jsonBacktestResult)) { throw new ArgumentNullException(nameof(jsonBacktestResult), $"Target.MoveAhead(): {Messages.OptimizerObjectivesCommon.NullOrEmptyBacktestResult}"); } var token = GetTokenInJsonBacktest(jsonBacktestResult, Target); if (token == null) { return false; } var computedValue = token.Value().ToNormalizedDecimal(); if (!Current.HasValue || Extremum.Better(Current.Value, computedValue)) { Current = computedValue; return true; } return false; } /// /// Try comply target value /// public void CheckCompliance() { if (IsComplied()) { Reached?.Invoke(this, EventArgs.Empty); } } public static JToken GetTokenInJsonBacktest(string jsonBacktestResult, string target) { var jObject = JObject.Parse(jsonBacktestResult); var path = target.Replace("[", string.Empty, StringComparison.InvariantCultureIgnoreCase) .Replace("]", string.Empty, StringComparison.InvariantCultureIgnoreCase) .Replace("\'", string.Empty, StringComparison.InvariantCultureIgnoreCase).Split("."); JToken token = null; foreach (var key in path) { if (jObject.TryGetValue(key, StringComparison.OrdinalIgnoreCase, out token)) { if (token is not JValue) { jObject = token.ToObject(); } } else { return null; } } return token; } private bool IsComplied() => TargetValue.HasValue && Current.HasValue && (TargetValue.Value == Current.Value || Extremum.Better(TargetValue.Value, Current.Value)); } }