Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisSzeto committed Jan 19, 2024
1 parent dc5f581 commit 2d774f4
Show file tree
Hide file tree
Showing 2 changed files with 396 additions and 6 deletions.
43 changes: 37 additions & 6 deletions Tests/Algorithm/AlgorithmIndicatorsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public void Setup()

_algorithm.SetDateTime(new DateTime(2013, 10, 11, 15, 0, 0));
_algorithm.AddEquity("SPY");
_option = _algorithm.AddOption("SPY").Symbol;
_option = Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Call, 450m, new DateTime(2023, 9, 1));
_algorithm.AddOptionContract(_option);
_algorithm.EnableAutomaticIndicatorWarmUp = true;
}

Expand Down Expand Up @@ -95,16 +96,16 @@ public void SharpeRatioIndicatorUsesAlgorithmsRiskFreeRateModelSetAfterIndicator
public void IVIndicatorUsesAlgorithmsRiskFreeRateModelSetAfterIndicatorRegistration()
{
// Register indicator
var sharpeRatio = _algorithm.IV(_option);
var iv = _algorithm.IV(_option);

// Setup risk free rate model
var interestRateProviderMock = new Mock<IRiskFreeInterestRateModel>();
var reference = new DateTime(2023, 11, 21, 10, 0, 0);
interestRateProviderMock.Setup(x => x.GetInterestRate(reference)).Verifiable();

// Update indicator
sharpeRatio.Update(new IndicatorDataPoint(_option, reference, 30m));
sharpeRatio.Update(new IndicatorDataPoint(Symbols.SPY, reference, 300m));
iv.Update(new IndicatorDataPoint(_option, reference, 30m));
iv.Update(new IndicatorDataPoint(Symbols.SPY, reference, 300m));

// Our interest rate provider shouldn't have been called yet since it's hasn't been set to the algorithm
interestRateProviderMock.Verify(x => x.GetInterestRate(reference), Times.Never);
Expand All @@ -113,11 +114,41 @@ public void IVIndicatorUsesAlgorithmsRiskFreeRateModelSetAfterIndicatorRegistrat
_algorithm.SetRiskFreeInterestRateModel(interestRateProviderMock.Object);

// Update indicator
sharpeRatio.Update(new IndicatorDataPoint(_option, reference, 30m));
sharpeRatio.Update(new IndicatorDataPoint(Symbols.SPY, reference, 300m));
iv.Update(new IndicatorDataPoint(_option, reference, 30m));
iv.Update(new IndicatorDataPoint(Symbols.SPY, reference, 300m));

// Our interest rate provider should have been called once by each update
interestRateProviderMock.Verify(x => x.GetInterestRate(reference), Times.Exactly(2));
}

[Test]
public void DeltaIndicatorUsesAlgorithmsRiskFreeRateModelSetAfterIndicatorRegistration()
{
// Register indicator
var delta = _algorithm.Delta(_option);

// Setup risk free rate model
var interestRateProviderMock = new Mock<IRiskFreeInterestRateModel>();
var reference = new DateTime(2023, 8, 1, 10, 0, 0);
interestRateProviderMock.Setup(x => x.GetInterestRate(reference)).Verifiable();

// Update indicator
delta.Update(new IndicatorDataPoint(_option, reference, 30m));
delta.Update(new IndicatorDataPoint(Symbols.SPY, reference, 300m));

// Our interest rate provider shouldn't have been called yet since it's hasn't been set to the algorithm
interestRateProviderMock.Verify(x => x.GetInterestRate(reference), Times.Never);

// Set the interest rate provider to the algorithm
_algorithm.SetRiskFreeInterestRateModel(interestRateProviderMock.Object);

// Update indicator
delta.Update(new IndicatorDataPoint(_option, reference.AddDays(1), 30m));
delta.Update(new IndicatorDataPoint(Symbols.SPY, reference.AddDays(1), 300m));

// Our interest rate provider should have been called once by each update
// 1 for delta, 2 for IV
interestRateProviderMock.Verify(x => x.GetInterestRate(reference.AddDays(1)), Times.Exactly(3));
}
}
}
Loading

0 comments on commit 2d774f4

Please sign in to comment.