Skip to content

Commit

Permalink
Quote bar previous close and current open should be same
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Vishnu authored and Martin-Molinero committed Jun 23, 2020
1 parent 98cde34 commit 79a05eb
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Common/Data/Consolidators/TickQuoteBarConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace QuantConnect.Data.Consolidators
/// </summary>
public class TickQuoteBarConsolidator : PeriodCountConsolidatorBase<Tick, QuoteBar>
{
private Tick _previousTick;
/// <summary>
/// Initializes a new instance of the <see cref="TickQuoteBarConsolidator"/> class
/// </summary>
Expand Down Expand Up @@ -99,12 +100,19 @@ protected override void AggregateBar(ref QuoteBar workingBar, Tick data)
Ask = null
};

// sync last tick close price with this tick open price
if (_previousTick != null)
{
workingBar.Update(0, _previousTick.BidPrice, _previousTick.AskPrice, 0, _previousTick.BidSize, _previousTick.AskSize);
}

if (Period.HasValue) workingBar.Period = Period.Value;
}

// update the bid and ask
workingBar.Update(0, data.BidPrice, data.AskPrice, 0, data.BidSize, data.AskSize);
if (!Period.HasValue) workingBar.EndTime = GetRoundedBarTime(data.EndTime);
_previousTick = (Tick) data.Clone();
}
}
}
96 changes: 96 additions & 0 deletions Tests/Common/Data/TickQuoteBarConsolidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,101 @@ public void DoesNotConsolidateDifferentSymbols()
Exception ex = Assert.Throws<InvalidOperationException>(() => consolidator.Update(tick2));
Assert.IsTrue(ex.Message.Contains("is not the same"));
}

[Test]
public void LastCloseAndCurrentOpenPriceShouldBeSameConsolidatedOnCount()
{
QuoteBar quoteBar = null;
var creator = new TickQuoteBarConsolidator(1);
creator.DataConsolidated += (sender, args) =>
{
quoteBar = args;
};

var reference = DateTime.Today;
var tick1 = new Tick
{
Symbol = Symbols.SPY,
Time = reference,
TickType = TickType.Quote,
AskPrice = 25,
BidPrice = 24,

};

creator.Update(tick1);

var tick2 = new Tick
{
Symbol = Symbols.SPY,
Time = reference.AddSeconds(1),
TickType = TickType.Quote,
AskPrice = 36,
BidPrice = 35,
};

creator.Update(tick2);

Assert.AreEqual(tick1.AskPrice, quoteBar.Ask.Open, "Ask Open not equal to Previous Close");
Assert.AreEqual(tick1.BidPrice, quoteBar.Bid.Open, "Bid Open not equal to Previous Close");
Assert.AreEqual(tick2.AskPrice, quoteBar.Ask.Close, "Ask Close incorrect");
Assert.AreEqual(tick2.BidPrice, quoteBar.Bid.Close, "Bid Close incorrect");
}

[Test]
public void LastCloseAndCurrentOpenPriceShouldBeSameConsolidatedOnTimeSpan()
{
QuoteBar quoteBar = null;
var creator = new TickQuoteBarConsolidator(TimeSpan.FromMinutes(1));
creator.DataConsolidated += (sender, args) =>
{
quoteBar = args;
};

var reference = DateTime.Today;

// timeframe 1
var tick1 = new Tick
{
Symbol = Symbols.SPY,
Time = reference,
TickType = TickType.Quote,
AskPrice = 25,
BidPrice = 24,

};

creator.Update(tick1);

var tick2 = new Tick
{
Symbol = Symbols.SPY,
Time = reference.AddSeconds(1),
TickType = TickType.Quote,
AskPrice = 26,
BidPrice = 25,
};
creator.Update(tick2);

// timeframe 2
var tick3 = new Tick
{
Symbol = Symbols.SPY,
Time = reference.AddMinutes(1),
TickType = TickType.Quote,
AskPrice = 36,
BidPrice = 35,
};
creator.Update(tick3);


//force the consolidator to emit DataConsolidated
creator.Scan(reference.AddMinutes(2));

Assert.AreEqual(tick2.AskPrice, quoteBar.Ask.Open, "Ask Open not equal to Previous Close");
Assert.AreEqual(tick2.BidPrice, quoteBar.Bid.Open, "Bid Open not equal to Previous Close");
Assert.AreEqual(tick3.AskPrice, quoteBar.Ask.Close, "Ask Close incorrect");
Assert.AreEqual(tick3.BidPrice, quoteBar.Bid.Close, "Bid Close incorrect");
}
}
}

0 comments on commit 79a05eb

Please sign in to comment.