Skip to content

Commit

Permalink
Fixing minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
JosueNina committed Dec 18, 2024
1 parent 4e9581b commit b35326c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
28 changes: 10 additions & 18 deletions Indicators/Beta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class Beta : BarIndicator, IIndicatorWarmUpPeriodProvider
/// <summary>
/// Gets a flag indicating when the indicator is ready and fully initialized
/// </summary>
public override bool IsReady => (_targetReturns.Samples >= WarmUpPeriod - 1) && (_referenceReturns.Samples >= WarmUpPeriod - 1);
public override bool IsReady => _targetReturns.IsReady && _referenceReturns.IsReady;

/// <summary>
/// Creates a new Beta indicator with the specified name, target, reference,
Expand Down Expand Up @@ -128,11 +128,9 @@ public Beta(string name, Symbol targetSymbol, Symbol referenceSymbol, int period
_targetReturns = new RollingWindow<double>(period);
_referenceReturns = new RollingWindow<double>(period);
_beta = 0;

_targetTimeZone = MarketHoursDatabase.FromDataFolder()
.GetExchangeHours(_targetSymbol.ID.Market, _targetSymbol, _targetSymbol.ID.SecurityType).TimeZone;
_referenceTimeZone = MarketHoursDatabase.FromDataFolder()
.GetExchangeHours(_referenceSymbol.ID.Market, _referenceSymbol, _referenceSymbol.ID.SecurityType).TimeZone;
var dataFolder = MarketHoursDatabase.FromDataFolder();
_targetTimeZone = dataFolder.GetExchangeHours(_targetSymbol.ID.Market, _targetSymbol, _targetSymbol.ID.SecurityType).TimeZone;
_referenceTimeZone = dataFolder.GetExchangeHours(_referenceSymbol.ID.Market, _referenceSymbol, _referenceSymbol.ID.SecurityType).TimeZone;
_isTimezoneDifferent = _targetTimeZone != _referenceTimeZone;
}

Expand Down Expand Up @@ -175,11 +173,6 @@ public Beta(string name, int period, Symbol targetSymbol, Symbol referenceSymbol
/// <returns>The beta value of the target used in relation with the reference</returns>
protected override decimal ComputeNextValue(IBaseDataBar input)
{
if (input.Symbol != _targetSymbol && input.Symbol != _referenceSymbol)
{
throw new ArgumentException($"The given symbol {input.Symbol} was not {_targetSymbol} or {_referenceSymbol} symbol");
}

if (_previousInput == null)
{
_previousInput = input;
Expand All @@ -202,12 +195,7 @@ protected override decimal ComputeNextValue(IBaseDataBar input)
{
AddDataPoint(input);
AddDataPoint(_previousInput);

// Compute beta when both have at least "period" data points
if (IsReady)
{
ComputeBeta();
}
ComputeBeta();
}
_previousInput = input;
return _beta;
Expand Down Expand Up @@ -258,6 +246,10 @@ private void AddDataPoint(IBaseDataBar input)
_referenceReturns.Add(GetNewReturn(_referenceDataPoints));
}
}
else
{
throw new ArgumentException($"The given symbol {input.Symbol} was not {_targetSymbol} or {_referenceSymbol} symbol");
}
}

/// <summary>
Expand Down Expand Up @@ -291,9 +283,9 @@ private void ComputeBeta()
/// </summary>
public override void Reset()
{
_previousInput = null;
_targetDataPoints.Reset();
_referenceDataPoints.Reset();

_targetReturns.Reset();
_referenceReturns.Reset();
_beta = 0;
Expand Down
8 changes: 5 additions & 3 deletions Tests/Indicators/BetaIndicatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,13 @@ public void EqualBetaValue()

for (int i = 0; i < 3; i++)
{
indicator.Update(new TradeBar() { Symbol = Symbols.AAPL, Low = 1, High = 2, Volume = 100, Close = i + 1, Time = _reference.AddDays(1 + i) });
indicator.Update(new TradeBar() { Symbol = Symbols.SPX, Low = 1, High = 2, Volume = 100, Close = i + 1, Time = _reference.AddDays(1 + 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 = i + 1, Time = startTime, EndTime = endTime });
indicator.Update(new TradeBar() { Symbol = Symbols.SPX, Low = 1, High = 2, Volume = 100, Close = i + 1, Time = startTime, EndTime = endTime });
}

Assert.AreEqual(0, (double)indicator.Current.Value, 0.0001);
Assert.AreEqual(1, (double)indicator.Current.Value, 0.0001);
}

[Test]
Expand Down

0 comments on commit b35326c

Please sign in to comment.