Skip to content

Commit

Permalink
Fix QuoteBar Non Time Based Consolidator Period (QuantConnect#6409)
Browse files Browse the repository at this point in the history
* Fix QuoteBar NonTimeBased Consolidator Period

- Fix QuoteBar non timebased consolidator period, that was accounting
  twice for the initial bar period. Updating unit tests

* Improve QuoteBarConsolidator assertion

- Assert quote bar consolidated time and endtime in unit tests
  • Loading branch information
Martin-Molinero authored Jun 17, 2022
1 parent e736b28 commit 740b40f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
9 changes: 7 additions & 2 deletions Common/Data/Consolidators/QuoteBarConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ protected override void AggregateBar(ref QuoteBar workingBar, QuoteBar data)
workingBar.Update(0, previous.Bid?.Close ?? 0, previous.Ask?.Close ?? 0, 0, previous.LastBidSize, previous.LastAskSize);
}
}
else if (!IsTimeBased)
{
// we should only increment the period after the first data we get, else we would be accouting twice for the inital bars period
// because in the `if` above we are already providing the `data.Period` as argument. See test 'AggregatesNewCountQuoteBarProperly' which assert period
workingBar.Period += data.Period;
}

// update the bid and ask
if (bid != null)
Expand Down Expand Up @@ -126,7 +132,6 @@ protected override void AggregateBar(ref QuoteBar workingBar, QuoteBar data)
}

workingBar.Value = data.Value;
if (!IsTimeBased) workingBar.Period += data.Period;
}
}
}
}
44 changes: 23 additions & 21 deletions Tests/Common/Data/QuoteBarConsolidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ public class QuoteBarConsolidatorTests
public void AggregatesNewCountQuoteBarProperly()
{
QuoteBar quoteBar = null;
var creator = new QuoteBarConsolidator(4);
using var creator = new QuoteBarConsolidator(4);
creator.DataConsolidated += (sender, args) =>
{
quoteBar = args;
};

var time = DateTime.Today;
var period = TimeSpan.FromMinutes(1);
var bar1 = new QuoteBar
{
Time = time,
Expand All @@ -44,49 +43,49 @@ public void AggregatesNewCountQuoteBarProperly()
Ask = null,
LastAskSize = 0,
Value = 1,
Period = period
Period = Time.OneMinute
};
creator.Update(bar1);
Assert.IsNull(quoteBar);

var bar2 = new QuoteBar
{
Time = time + TimeSpan.FromMinutes(1),
Time = bar1.EndTime,
Symbol = Symbols.SPY,
Bid = new Bar(1.1m, 2.2m, 0.9m, 2.1m),
LastBidSize = 3,
Ask = new Bar(2.2m, 4.4m, 3.3m, 3.3m),
LastAskSize = 0,
Value = 1,
Period = period
Period = Time.OneMinute
};
creator.Update(bar2);
Assert.IsNull(quoteBar);

var bar3 = new QuoteBar
{
Time = time + TimeSpan.FromMinutes(2),
Time = bar2.EndTime,
Symbol = Symbols.SPY,
Bid = new Bar(1, 2, 0.5m, 1.75m),
LastBidSize = 3,
Ask = null,
LastAskSize = 0,
Value = 1,
Period = period
Period = Time.OneMinute
};
creator.Update(bar3);
Assert.IsNull(quoteBar);

var bar4 = new QuoteBar
{
Time = time + TimeSpan.FromMinutes(3),
Time = bar3.EndTime,
Symbol = Symbols.SPY,
Bid = null,
LastBidSize = 0,
Ask = new Bar(1, 7, 0.5m, 4.4m),
LastAskSize = 4,
Value = 1,
Period = period
Period = Time.OneMinute
};
creator.Update(bar4);
Assert.IsNotNull(quoteBar);
Expand All @@ -102,20 +101,23 @@ public void AggregatesNewCountQuoteBarProperly()
Assert.AreEqual(bar3.LastBidSize, quoteBar.LastBidSize);
Assert.AreEqual(bar4.LastAskSize, quoteBar.LastAskSize);
Assert.AreEqual(bar1.Value, quoteBar.Value);

Assert.AreEqual(bar1.Time, quoteBar.Time);
Assert.AreEqual(bar4.EndTime, quoteBar.EndTime);
Assert.AreEqual(TimeSpan.FromMinutes(4), quoteBar.Period);
}

[Test]
public void AggregatesNewTimeSpanQuoteBarProperly()
{
QuoteBar quoteBar = null;
var creator = new QuoteBarConsolidator(TimeSpan.FromMinutes(2));
using var creator = new QuoteBarConsolidator(TimeSpan.FromMinutes(2));
creator.DataConsolidated += (sender, args) =>
{
quoteBar = args;
};

var time = DateTime.Today;
var period = TimeSpan.FromMinutes(1);
var bar1 = new QuoteBar
{
Time = time,
Expand All @@ -125,36 +127,36 @@ public void AggregatesNewTimeSpanQuoteBarProperly()
Ask = null,
LastAskSize = 0,
Value = 1,
Period = period
Period = Time.OneMinute
};
creator.Update(bar1);
Assert.IsNull(quoteBar);

var bar2 = new QuoteBar
{
Time = time + TimeSpan.FromMinutes(1),
Time = bar1.EndTime,
Symbol = Symbols.SPY,
Bid = new Bar(1.1m, 2.2m, 0.9m, 2.1m),
LastBidSize = 3,
Ask = new Bar(2.2m, 4.4m, 3.3m, 3.3m),
LastAskSize = 0,
Value = 1,
Period = period
Period = Time.OneMinute
};
creator.Update(bar2);
Assert.IsNull(quoteBar);

// pushing another bar to force the fire
var bar3 = new QuoteBar
{
Time = time + TimeSpan.FromMinutes(2),
Time = bar2.EndTime,
Symbol = Symbols.SPY,
Bid = new Bar(1, 2, 0.5m, 1.75m),
LastBidSize = 3,
Ask = null,
LastAskSize = 0,
Value = 1,
Period = period
Period = Time.OneMinute
};
creator.Update(bar3);

Expand All @@ -164,7 +166,7 @@ public void AggregatesNewTimeSpanQuoteBarProperly()

Assert.AreEqual(bar1.Symbol, quoteBar.Symbol);
Assert.AreEqual(bar1.Time, quoteBar.Time);
Assert.AreEqual(time + TimeSpan.FromMinutes(2), quoteBar.EndTime);
Assert.AreEqual(bar2.EndTime, quoteBar.EndTime);
Assert.AreEqual(TimeSpan.FromMinutes(2), quoteBar.Period);

// Bid
Expand All @@ -187,7 +189,7 @@ public void AggregatesNewTimeSpanQuoteBarProperly()
[Test]
public void DoesNotConsolidateDifferentSymbols()
{
var consolidator = new QuoteBarConsolidator(2);
using var consolidator = new QuoteBarConsolidator(2);

var time = DateTime.Today;
var period = TimeSpan.FromMinutes(1);
Expand Down Expand Up @@ -219,14 +221,14 @@ public void DoesNotConsolidateDifferentSymbols()
consolidator.Update(bar1);

Exception ex = Assert.Throws<InvalidOperationException>(() => consolidator.Update(bar2));
Assert.IsTrue(ex.Message.Contains("is not the same"));
Assert.IsTrue(ex.Message.Contains("is not the same", StringComparison.InvariantCultureIgnoreCase));
}

[Test]
public void LastCloseAndCurrentOpenPriceShouldBeSameConsolidatedOnTimeSpan()
{
QuoteBar quoteBar = null;
var creator = new QuoteBarConsolidator(TimeSpan.FromMinutes(2));
using var creator = new QuoteBarConsolidator(TimeSpan.FromMinutes(2));
creator.DataConsolidated += (sender, args) =>
{
quoteBar = args;
Expand Down Expand Up @@ -290,4 +292,4 @@ public void LastCloseAndCurrentOpenPriceShouldBeSameConsolidatedOnTimeSpan()
Assert.AreEqual(quoteBar.Ask.Open, bar2.Ask.Close);
}
}
}
}

0 comments on commit 740b40f

Please sign in to comment.