Skip to content

Commit

Permalink
Beta Indicator Supports QuoteBar (#7540)
Browse files Browse the repository at this point in the history
* Adds Unit Test to Assert Beta Accepts QuoteBar

Beta must support QuoteBar for Forex/CDF and QuoteBarConsolidators

* Beta Indicator Supports QuoteBar
  • Loading branch information
AlexCatarino authored Oct 27, 2023
1 parent 265fbf7 commit f5d4bc4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Algorithm/QCAlgorithm.Indicators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public BollingerBands BB(Symbol symbol, int period, decimal k, MovingAverageType
/// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
/// <returns>The Beta indicator for the given parameters</returns>
[DocumentationAttribute(Indicators)]
public Beta B(Symbol target, Symbol reference, int period, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
public Beta B(Symbol target, Symbol reference, int period, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
{
var name = CreateIndicatorName(QuantConnect.Symbol.None, $"B({period})", resolution);
var beta = new Beta(name, target, reference, period);
Expand Down
4 changes: 2 additions & 2 deletions Indicators/Beta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace QuantConnect.Indicators
/// It is common practice to use the SPX index as a benchmark of the overall reference market when it comes to Beta
/// calculations.
/// </summary>
public class Beta : TradeBarIndicator, IIndicatorWarmUpPeriodProvider
public class Beta : BarIndicator, IIndicatorWarmUpPeriodProvider
{
/// <summary>
/// RollingWindow to store the data points of the target symbol
Expand Down Expand Up @@ -140,7 +140,7 @@ public Beta(string name, int period, Symbol targetSymbol, Symbol referenceSymbol
/// <param name="input">The input value of this indicator on this time step.
/// It can be either from the target or the reference symbol</param>
/// <returns>The beta value of the target used in relation with the reference</returns>
protected override decimal ComputeNextValue(TradeBar input)
protected override decimal ComputeNextValue(IBaseDataBar input)
{
var inputSymbol = input.Symbol;
if (inputSymbol == _targetSymbol)
Expand Down
19 changes: 17 additions & 2 deletions Tests/Indicators/BetaIndicatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
namespace QuantConnect.Tests.Indicators
{
[TestFixture]
public class BetaIndicatorTests : CommonIndicatorTests<TradeBar>
public class BetaIndicatorTests : CommonIndicatorTests<IBaseDataBar>
{
protected override string TestFileName => "bi_datatest.csv";

protected override string TestColumnName => "Beta";

private DateTime _reference = new DateTime(2020, 1, 1);

protected override IndicatorBase<TradeBar> CreateIndicator()
protected override IndicatorBase<IBaseDataBar> CreateIndicator()
{
var indicator = new Beta("testBetaIndicator", "AMZN 2T", "SPX 2T", 5);
return indicator;
Expand Down Expand Up @@ -142,6 +142,21 @@ public override void AcceptsVolumeRenkoBarsAsInput()
secondVolumeRenkoConsolidator.Dispose();
}


[Test]
public void AcceptsQuoteBarsAsInput()
{
var indicator = new Beta("testBetaIndicator", Symbols.IBM, Symbols.SPY, 5);

for (var i = 10; i > 0; i--)
{
indicator.Update(new QuoteBar { Symbol = Symbols.IBM, Ask = new Bar(1, 2, 1, 500), Bid = new Bar(1, 2, 1, 500), Time = _reference.AddDays(1 + i) });
indicator.Update(new QuoteBar { Symbol = Symbols.SPY, Ask = new Bar(1, 2, 1, 500), Time = _reference.AddDays(1 + i) });
}

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

[Test]
public void EqualBetaValue()
{
Expand Down

0 comments on commit f5d4bc4

Please sign in to comment.