Skip to content

Commit

Permalink
Add new unit test to CommonIndicatorTests
Browse files Browse the repository at this point in the history
  • Loading branch information
JosueNina committed Dec 31, 2024
1 parent 7f666b6 commit 1ad35dd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
22 changes: 22 additions & 0 deletions Tests/Indicators/AlphaIndicatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,5 +393,27 @@ public void NullRiskFreeRate()

}
}

[Test]
public override void TracksPreviousState()
{
var period = 5;
var indicator = new Alpha(Symbols.AAPL, Symbols.SPX, period);
var previousValue = indicator.Current.Value;

// Update the indicator and verify the previous values
for (var i = 1; i < 2 * period; i++)
{
var startTime = _reference.AddDays(1 + i);
var endTime = startTime.AddDays(1);
indicator.Update(new TradeBar() { Symbol = Symbols.AAPL, Low = 1, High = 2, Volume = 100, Close = 1000 + i * 10, Time = startTime, EndTime = endTime });
indicator.Update(new TradeBar() { Symbol = Symbols.SPX, Low = 1, High = 2, Volume = 100, Close = 1000 + (i * 15), Time = startTime, EndTime = endTime });
// Verify the previous value matches the indicator's previous value
Assert.AreEqual(previousValue, indicator.Previous.Value);

// Update previousValue to the current value for the next iteration
previousValue = indicator.Current.Value;
}
}
}
}
22 changes: 22 additions & 0 deletions Tests/Indicators/BetaIndicatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,27 @@ public void BetaWithDifferentTimeZones()
}
Assert.AreEqual(1, (double)indicator.Current.Value);
}

[Test]
public override void TracksPreviousState()
{
var period = 5;
var indicator = new Beta(Symbols.SPY, Symbols.AAPL, period);
var previousValue = indicator.Current.Value;

// Update the indicator and verify the previous values
for (var i = 1; i < 2 * period; i++)
{
var startTime = _reference.AddDays(1 + i);
var endTime = startTime.AddDays(1);
indicator.Update(new TradeBar() { Symbol = Symbols.SPY, Low = 1, High = 2, Volume = 100, Close = 1000 + i * 10, Time = startTime, EndTime = endTime });
indicator.Update(new TradeBar() { Symbol = Symbols.AAPL, Low = 1, High = 2, Volume = 100, Close = 1000 + (i * 15), Time = startTime, EndTime = endTime });
// Verify the previous value matches the indicator's previous value
Assert.AreEqual(previousValue, indicator.Previous.Value);

// Update previousValue to the current value for the next iteration
previousValue = indicator.Current.Value;
}
}
}
}
24 changes: 23 additions & 1 deletion Tests/Indicators/CommonIndicatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public virtual void TimeMovesForward()
var input = GetInput(startDate, i);
indicator.Update(input);
}

Assert.AreEqual(1, indicator.Samples);
}

Expand Down Expand Up @@ -142,6 +142,28 @@ indicator is BarIndicator ||
}
}

[Test]
public virtual void TracksPreviousState()
{
var indicator = CreateIndicator();
var period = (indicator as IIndicatorWarmUpPeriodProvider)?.WarmUpPeriod;

var startDate = new DateTime(2024, 1, 1);
var previousValue = indicator.Current.Value;

// Update the indicator and verify the previous values
for (var i = 0; i < 2 * period; i++)
{
indicator.Update(GetInput(startDate, i));

// Verify the previous value matches the indicator's previous value
Assert.AreEqual(previousValue, indicator.Previous.Value);

// Update previousValue to the current value for the next iteration
previousValue = indicator.Current.Value;
}
}

[Test]
public virtual void WorksWithLowValues()
{
Expand Down

0 comments on commit 1ad35dd

Please sign in to comment.