Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Indicators/Beta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public class Beta : DualSymbolIndicator<IBaseDataBar>
public Beta(string name, Symbol targetSymbol, Symbol referenceSymbol, int period)
: base(name, targetSymbol, referenceSymbol, 2)
{
// Assert the period is greater than two, otherwise the beta can not be computed
// Assert the period is greater than one, otherwise the beta can not be computed
if (period < 2)
{
throw new ArgumentException($"Period parameter for Beta indicator must be greater than 2 but was {period}.");
throw new ArgumentException($"Period parameter for Beta indicator must be greater than 1 but was {period}.");
}

_targetReturns = new RollingWindow<double>(period);
Expand Down
2 changes: 1 addition & 1 deletion Indicators/Correlation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Correlation(string name, Symbol targetSymbol, Symbol referenceSymbol, int
: base(name, targetSymbol, referenceSymbol, period)
{
// Assert the period is greater than two, otherwise the correlation can not be computed
if (period < 2)
if (period < 3)
{
throw new ArgumentException($"Period parameter for Correlation indicator must be greater than 2 but was {period}");
}
Expand Down
4 changes: 2 additions & 2 deletions Indicators/Covariance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public class Covariance : DualSymbolIndicator<IBaseDataBar>
public Covariance(string name, Symbol targetSymbol, Symbol referenceSymbol, int period)
: base(name, targetSymbol, referenceSymbol, 2)
{
// Assert the period is greater than two, otherwise the covariance can not be computed
// Assert the period is greater than one, otherwise the covariance can not be computed
if (period < 2)
{
throw new ArgumentException($"Period parameter for Covariance indicator must be greater than 2 but was {period}.");
throw new ArgumentException($"Period parameter for Covariance indicator must be greater than 1 but was {period}.");
}

_targetReturns = new RollingWindow<double>(period);
Expand Down
18 changes: 18 additions & 0 deletions Tests/Indicators/BetaIndicatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,5 +312,23 @@ public override void IndicatorShouldHaveSymbolAfterUpdates()
Assert.AreEqual(Symbols.AAPL, indicator.Current.Symbol);
}
}

[Test]
public void PeriodBelowMinimumThrows()
{
// One return is not a sample, so the smallest period this can accept is two
var period = 1;

var exception = Assert.Throws<ArgumentException>(() => new Beta(Symbols.SPY, Symbols.AAPL, period));
Assert.That(exception.Message, Is.EqualTo($"Period parameter for Beta indicator must be greater than 1 but was {period}."));
}

[Test]
public void PeriodOfTwoIsAccepted()
{
// Two returns give a finite, well defined beta, unlike the correlation
// of two points, which is 1 or -1 whatever the data says
Assert.DoesNotThrow(() => new Beta(Symbols.SPY, Symbols.AAPL, 2));
}
}
}
12 changes: 12 additions & 0 deletions Tests/Indicators/CorrelationPearsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,17 @@ public void CorrelationWithDifferentTimeZones()
}
Assert.AreEqual(1, (double)indicator.Current.Value);
}

[Test]
public void PeriodBelowMinimumThrows()
{
// A two point window leaves no spread to measure: the correlation of two
// points is 1 or -1 whatever the data says, so three is the smallest period
// that carries information
var period = 2;

var exception = Assert.Throws<ArgumentException>(() => new Correlation(Symbols.SPY, Symbols.AAPL, period));
Assert.That(exception.Message, Is.EqualTo($"Period parameter for Correlation indicator must be greater than 2 but was {period}"));
}
}
}
18 changes: 18 additions & 0 deletions Tests/Indicators/CovarianceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,5 +287,23 @@ public override void IndicatorShouldHaveSymbolAfterUpdates()
Assert.AreEqual(Symbols.AAPL, indicator.Current.Symbol);
}
}

[Test]
public void PeriodBelowMinimumThrows()
{
// One return is not a sample, so the smallest period this can accept is two
var period = 1;

var exception = Assert.Throws<ArgumentException>(() => new Covariance(Symbols.SPY, Symbols.AAPL, period));
Assert.That(exception.Message, Is.EqualTo($"Period parameter for Covariance indicator must be greater than 1 but was {period}."));
}

[Test]
public void PeriodOfTwoIsAccepted()
{
// Two returns give a finite, well defined covariance, unlike the correlation
// of two points, which is 1 or -1 whatever the data says
Assert.DoesNotThrow(() => new Covariance(Symbols.SPY, Symbols.AAPL, 2));
}
}
}