/* * 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 System; using Newtonsoft.Json; namespace QuantConnect.Algorithm.Framework.Alphas { /// /// Defines the scores given to a particular insight /// public class InsightScore { /// /// Gets the time these scores were last updated /// [JsonProperty] public DateTime UpdatedTimeUtc { get; private set; } /// /// Gets the direction score /// [JsonProperty] public double Direction { get; private set; } /// /// Gets the magnitude score /// [JsonProperty] public double Magnitude { get; private set; } /// /// Gets whether or not this is the insight's final score /// [JsonProperty] public bool IsFinalScore { get; private set; } /// /// Initializes a new, default instance of the class /// public InsightScore() { } /// /// Initializes a new instance of the class /// /// The insight direction score /// The insight percent change score /// The algorithm utc time these scores were computed public InsightScore(double direction, double magnitude, DateTime updatedTimeUtc) { Direction = direction; Magnitude = magnitude; UpdatedTimeUtc = updatedTimeUtc; } /// /// Sets the specified score type with the value /// /// The score type to be set, Direction/Magnitude /// The new value for the score /// The algorithm's utc time at which time the new score was computed public void SetScore(InsightScoreType type, double value, DateTime algorithmUtcTime) { if (IsFinalScore) return; UpdatedTimeUtc = algorithmUtcTime; switch (type) { case InsightScoreType.Direction: Direction = Math.Max(0, Math.Min(1, value)); break; case InsightScoreType.Magnitude: Magnitude = Math.Max(0, Math.Min(1, value)); break; default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } } /// /// Marks the score as finalized, preventing any further updates. /// /// The algorithm's utc time at which time these scores were finalized public void Finalize(DateTime algorithmUtcTime) { IsFinalScore = true; UpdatedTimeUtc = algorithmUtcTime; } /// /// Gets the specified score /// /// The type of score to get, Direction/Magnitude /// The requested score public double GetScore(InsightScoreType type) { switch (type) { case InsightScoreType.Direction: return Direction; case InsightScoreType.Magnitude: return Magnitude; default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } } /// Returns a string that represents the current object. /// A string that represents the current object. /// 2 public override string ToString() { return Messages.InsightScore.ToString(this); } } }