Skip to content

Commit

Permalink
Fix: Insight constructors not setting correct values (QuantConnect#8058)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonabreul authored May 24, 2024
1 parent 9c1824c commit 40cfcfc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Common/Algorithm/Framework/Alphas/Insight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public void Cancel(DateTime utcTime)
/// <param name="direction">The predicted direction</param>
/// <param name="tag">The insight's tag containing additional information</param>
public Insight(Symbol symbol, TimeSpan period, InsightType type, InsightDirection direction, string tag = "")
: this(symbol, period, type, direction, null, null, tag)
: this(symbol, period, type, direction, null, null, null, null, tag)
{
}

Expand Down Expand Up @@ -221,7 +221,7 @@ public Insight(Symbol symbol, TimeSpan period, InsightType type, InsightDirectio
/// <param name="direction">The predicted direction</param>
/// <param name="tag">The insight's tag containing additional information</param>
public Insight(Symbol symbol, Func<DateTime, DateTime> expiryFunc, InsightType type, InsightDirection direction, string tag = "")
: this(symbol, expiryFunc, type, direction, null, null, tag)
: this(symbol, expiryFunc, type, direction, null, null, null, null, tag)
{
}

Expand Down
49 changes: 48 additions & 1 deletion Tests/Algorithm/Framework/InsightTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ private void SetPeriodAndCloseTimeUsingExpiryFuncOrDateTime(Insight insight, Dat

Assert.AreEqual(expected, insight.CloseTimeUtc);
}


[Test]
public void ComputeCloseTimeHandlesFractionalDays()
Expand Down Expand Up @@ -431,5 +431,52 @@ public void IsActiveUsesClosedIntervalSemantics()
Assert.IsTrue(insight.IsActive(insight.CloseTimeUtc));
Assert.IsFalse(insight.IsActive(insight.CloseTimeUtc.AddTicks(1)));
}

[Test]
public void ConstructorsSetsCorrectValues()
{
var tag = "Insight Tag";

Assert.Multiple(() =>
{
var insight1 = new Insight(Symbols.SPY, Time.OneDay, InsightType.Price, InsightDirection.Up, tag);
AssertInsigthValues(insight1, Symbols.SPY, Time.OneDay, InsightType.Price, InsightDirection.Up, null, null, null, null, tag,
default(DateTime), default(DateTime));

var insight2 = new Insight(Symbols.SPY, Time.OneDay, InsightType.Price, InsightDirection.Up, 1.0, 0.5, "Model", 0.25, tag);
AssertInsigthValues(insight2, Symbols.SPY, Time.OneDay, InsightType.Price, InsightDirection.Up, 1.0, 0.5, "Model", 0.25, tag,
default(DateTime), default(DateTime));

var insight3 = new Insight(Symbols.SPY, time => time.AddDays(1), InsightType.Price, InsightDirection.Up, tag);
AssertInsigthValues(insight3, Symbols.SPY, new TimeSpan(0), InsightType.Price, InsightDirection.Up, null, null, null, null, tag,
default(DateTime), default(DateTime));

var insight4 = new Insight(Symbols.SPY, time => time.AddDays(1), InsightType.Price, InsightDirection.Up, 1.0, 0.5, "Model", 0.25, tag);
AssertInsigthValues(insight4, Symbols.SPY, new TimeSpan(0), InsightType.Price, InsightDirection.Up, 1.0, 0.5, "Model", 0.25, tag,
default(DateTime), default(DateTime));

var generatedTime = new DateTime(2024, 05, 23);
var insight5 = new Insight(generatedTime, Symbols.SPY, Time.OneDay, InsightType.Price, InsightDirection.Up, 1.0, 0.5, "Model", 0.25, tag);
AssertInsigthValues(insight5, Symbols.SPY, Time.OneDay, InsightType.Price, InsightDirection.Up, 1.0, 0.5, "Model", 0.25, tag,
generatedTime, generatedTime + Time.OneDay);

});
}

private static void AssertInsigthValues(Insight insight, Symbol symbol, TimeSpan period, InsightType type, InsightDirection direction,
double? magnitude, double? confidence, string sourceModel, double? weight, string tag, DateTime generatedTimeUtc, DateTime closeTimeUtc)
{
Assert.AreEqual(symbol, insight.Symbol);
Assert.AreEqual(period, insight.Period);
Assert.AreEqual(type, insight.Type);
Assert.AreEqual(direction, insight.Direction);
Assert.AreEqual(magnitude, insight.Magnitude);
Assert.AreEqual(confidence, insight.Confidence);
Assert.AreEqual(sourceModel, insight.SourceModel);
Assert.AreEqual(weight, insight.Weight);
Assert.AreEqual(tag, insight.Tag);
Assert.AreEqual(generatedTimeUtc, insight.GeneratedTimeUtc);
Assert.AreEqual(closeTimeUtc, insight.CloseTimeUtc);
}
}
}

0 comments on commit 40cfcfc

Please sign in to comment.