/* * 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; using Newtonsoft.Json.Linq; using NUnit.Framework; using QuantConnect.Algorithm.Framework.Alphas; using QuantConnect.Algorithm.Framework.Alphas.Serialization; namespace QuantConnect.Tests.Algorithm.Framework.Alphas.Serialization { [TestFixture] public class InsightJsonConverterTests { [Test] public void DeserializesInsightWithoutScore() { var jObject = JObject.Parse(jsonNoScore); var result = JsonConvert.DeserializeObject(jsonNoScore); Assert.AreEqual(jObject["id"].Value(), result.Id.ToString("N")); Assert.AreEqual(jObject["source-model"].Value(), result.SourceModel); Assert.AreEqual(jObject["group-id"]?.Value(), result.GroupId?.ToString("N")); Assert.AreEqual(jObject["generated-time"].Value(), Time.DateTimeToUnixTimeStamp(result.GeneratedTimeUtc), 5e-4); Assert.AreEqual(jObject["close-time"].Value(), Time.DateTimeToUnixTimeStamp(result.CloseTimeUtc), 5e-4); Assert.AreEqual(jObject["symbol"].Value(), result.Symbol.ID.ToString()); Assert.AreEqual(jObject["ticker"].Value(), result.Symbol.Value); Assert.AreEqual(jObject["type"].Value(), result.Type.ToLower()); Assert.AreEqual(jObject["reference"].Value(), result.ReferenceValue); Assert.AreEqual(jObject["direction"].Value(), result.Direction.ToLower()); Assert.AreEqual(jObject["period"].Value(), result.Period.TotalSeconds); Assert.AreEqual(jObject["magnitude"].Value(), result.Magnitude); Assert.AreEqual(null, result.Confidence); // default values for scores Assert.AreEqual(false, result.Score.IsFinalScore); Assert.AreEqual(0, result.ReferenceValueFinal); Assert.AreEqual(0, result.Score.Magnitude); Assert.AreEqual(0, result.Score.Direction); } [Test] public void DeserializesInsightWithScore() { var jObject = JObject.Parse(jsonWithScore); var result = JsonConvert.DeserializeObject(jsonWithScore); Assert.AreEqual(jObject["id"].Value(), result.Id.ToString("N")); Assert.AreEqual(jObject["source-model"].Value(), result.SourceModel); Assert.AreEqual(jObject["group-id"]?.Value(), result.GroupId?.ToString("N")); Assert.AreEqual(jObject["generated-time"].Value(), Time.DateTimeToUnixTimeStamp(result.GeneratedTimeUtc), 5e-4); Assert.AreEqual(jObject["close-time"].Value(), Time.DateTimeToUnixTimeStamp(result.CloseTimeUtc), 5e-4); Assert.AreEqual(jObject["symbol"].Value(), result.Symbol.ID.ToString()); Assert.AreEqual(jObject["ticker"].Value(), result.Symbol.Value); Assert.AreEqual(jObject["type"].Value(), result.Type.ToLower()); Assert.AreEqual(jObject["reference"].Value(), result.ReferenceValue); Assert.AreEqual(jObject["direction"].Value(), result.Direction.ToLower()); Assert.AreEqual(jObject["period"].Value(), result.Period.TotalSeconds); Assert.AreEqual(jObject["magnitude"].Value(), result.Magnitude); Assert.AreEqual(null, result.Confidence); Assert.AreEqual(true, result.Score.IsFinalScore); Assert.AreEqual(jObject["score-magnitude"].Value(), result.Score.Magnitude); Assert.AreEqual(jObject["score-direction"].Value(), result.Score.Direction); Assert.AreEqual(jObject["reference-final"].Value(), result.ReferenceValueFinal); } [Test] public void SerializesInsightWithoutScore() { var jObject = JObject.Parse(jsonNoScore); var insight = Insight.FromSerializedInsight(new SerializedInsight { Id = jObject["id"].Value(), SourceModel = jObject["source-model"].Value(), GroupId = jObject["group-id"]?.Value(), GeneratedTime = jObject["generated-time"].Value(), CloseTime = jObject["close-time"].Value(), Symbol = jObject["symbol"].Value(), Ticker = jObject["ticker"].Value(), Type = (InsightType)Enum.Parse(typeof(InsightType), jObject["type"].Value(), true), ReferenceValue = jObject["reference"].Value(), Direction = (InsightDirection)Enum.Parse(typeof(InsightDirection), jObject["direction"].Value(), true), Period = jObject["period"].Value(), Magnitude = jObject["magnitude"].Value() }); var result = JsonConvert.SerializeObject(insight, Formatting.Indented); Assert.AreEqual(jsonNoScore, result); } [Test] public void SerializesInsightWithScore() { var jObject = JObject.Parse(jsonWithScore); var insight = Insight.FromSerializedInsight(new SerializedInsight { Id = jObject["id"].Value(), SourceModel = jObject["source-model"].Value(), GroupId = jObject["group-id"]?.Value(), GeneratedTime = jObject["generated-time"].Value(), CloseTime = jObject["close-time"].Value(), Symbol = jObject["symbol"].Value(), Ticker = jObject["ticker"].Value(), Type = (InsightType)Enum.Parse(typeof(InsightType), jObject["type"].Value(), true), ReferenceValue = jObject["reference"].Value(), Direction = (InsightDirection)Enum.Parse(typeof(InsightDirection), jObject["direction"].Value(), true), Period = jObject["period"].Value(), Magnitude = jObject["magnitude"].Value(), ScoreIsFinal = jObject["score-final"].Value(), ScoreMagnitude = jObject["score-magnitude"].Value(), ScoreDirection = jObject["score-direction"].Value(), EstimatedValue = jObject["estimated-value"].Value(), ReferenceValueFinal = jObject["reference-final"].Value() }); var result = JsonConvert.SerializeObject(insight, Formatting.Indented); Assert.AreEqual(jsonWithScore, result); } private const string jsonNoScore = @"{ ""id"": ""e02be50f56a8496b9ba995d19a904ada"", ""group-id"": null, ""source-model"": ""mySourceModel-1"", ""generated-time"": 1520711961.00055, ""close-time"": 1520711961.00055, ""symbol"": ""BTCUSD XJ"", ""ticker"": ""BTCUSD"", ""type"": ""price"", ""reference"": 9143.53, ""reference-final"": 0.0, ""direction"": ""up"", ""period"": 5.0, ""magnitude"": 0.025, ""confidence"": null, ""weight"": null, ""score-final"": false, ""score-magnitude"": 0.0, ""score-direction"": 0.0, ""estimated-value"": 0.0 }"; private const string jsonWithScore = @"{ ""id"": ""e02be50f56a8496b9ba995d19a904ada"", ""group-id"": ""a02be50f56a8496b9ba995d19a904ada"", ""source-model"": ""mySourceModel-1"", ""generated-time"": 1520711961.00055, ""close-time"": 1520711961.00055, ""symbol"": ""BTCUSD XJ"", ""ticker"": ""BTCUSD"", ""type"": ""price"", ""reference"": 9143.53, ""reference-final"": 9243.53, ""direction"": ""up"", ""period"": 5.0, ""magnitude"": 0.025, ""confidence"": null, ""weight"": null, ""score-final"": true, ""score-magnitude"": 1.0, ""score-direction"": 1.0, ""estimated-value"": 1113.2484 }"; } }