Skip to content

Commit

Permalink
Slice dynamic accessors ignore Quote for equities
Browse files Browse the repository at this point in the history
- Slice dynamic data accessors will ignore QuoteBars and
Ticks TickType.QuoteBars for equities. This is to make slice dynamic
accessors deterministic and avoid breaking backwards compatibility when
adding QuoteBars for equities.
  • Loading branch information
Martin-Molinero committed Apr 6, 2020
1 parent 499248f commit 5671a32
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Algorithm.Python/ConvertToFrameworkAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def OnData(self, data):
Args:
data: Slice object with your stock data'''
# wait for our indicator to be ready
if not self.macd.IsReady or data[self.symbol] is None: return
if not self.macd.IsReady or not data.ContainsKey(self.symbol) or data[self.symbol] is None: return

holding = self.Portfolio[self.symbol]

Expand Down
5 changes: 5 additions & 0 deletions Common/Data/Slice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,11 @@ private static DataDictionary<SymbolData> CreateDynamicDataDictionary(IEnumerabl
var allData = new DataDictionary<SymbolData>();
foreach (var datum in data)
{
if (datum.Symbol.ID.SecurityType == SecurityType.Equity &&
(datum.DataType == MarketDataType.QuoteBar || datum.DataType == MarketDataType.Tick && (datum as Tick).TickType == TickType.Quote))
{
continue;
}
SymbolData symbolData;
if (!allData.TryGetValue(datum.Symbol, out symbolData))
{
Expand Down
46 changes: 34 additions & 12 deletions Tests/Common/Data/SliceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,36 @@ public void AccessesBaseBySymbol()
[Test]
public void AccessesTradeBarBySymbol()
{
TradeBar tradeBar = new TradeBar {Symbol = Symbols.SPY, Time = DateTime.Now};
TradeBar tradeBar = new TradeBar { Symbol = Symbols.SPY, Time = DateTime.Now };
Slice slice = new Slice(DateTime.Now, new[] { tradeBar });

TradeBar data = slice[tradeBar.Symbol];

Assert.AreEqual(tradeBar, data);
}

[Test]
public void EquitiesIgnoreQuoteBars()
{
var quoteBar = new QuoteBar { Symbol = Symbols.SPY, Time = DateTime.Now };
var slice = new Slice(DateTime.Now, new[] { quoteBar });

Assert.IsFalse(slice.HasData);
Assert.IsTrue(slice.ToList().Count == 0);
Assert.IsFalse(slice.ContainsKey(Symbols.SPY));
Assert.Throws<KeyNotFoundException>(() => { var data = slice[Symbols.SPY]; });
Assert.AreEqual(0, slice.Count);

var tickQuoteBar = new Tick { Symbol = Symbols.SPY, Time = DateTime.Now, TickType = TickType.Quote };
slice = new Slice(DateTime.Now, new[] { tickQuoteBar });

Assert.IsFalse(slice.HasData);
Assert.IsTrue(slice.ToList().Count == 0);
Assert.IsFalse(slice.ContainsKey(Symbols.SPY));
Assert.Throws<KeyNotFoundException>(() => { var data = slice[Symbols.SPY]; });
Assert.AreEqual(0, slice.Count);
}

[Test]
public void AccessesTradeBarCollection()
{
Expand All @@ -87,8 +109,8 @@ public void AccessesTradeBarCollection()
[Test]
public void AccessesTicksBySymbol()
{
Tick tick1 = new Tick(DateTime.Now, Symbols.SPY, 1, 2);
Tick tick2 = new Tick(DateTime.Now, Symbols.SPY, 1.1m, 2.1m);
Tick tick1 = new Tick { Time = DateTime.Now, Symbol = Symbols.SPY, Value = 1m, Quantity = 2m };
Tick tick2 = new Tick { Time = DateTime.Now, Symbol = Symbols.SPY, Value = 1.1m, Quantity = 2.1m };
Slice slice = new Slice(DateTime.Now, new[] { tick1, tick2 });

List<Tick> data = slice[tick1.Symbol];
Expand All @@ -99,10 +121,10 @@ public void AccessesTicksBySymbol()
[Test]
public void AccessesTicksCollection()
{
Tick tick1 = new Tick(DateTime.Now, Symbols.SPY, 1, 2);
Tick tick2 = new Tick(DateTime.Now, Symbols.SPY, 1.1m, 2.1m);
Tick tick3 = new Tick(DateTime.Now, Symbols.AAPL, 1, 2);
Tick tick4 = new Tick(DateTime.Now, Symbols.AAPL, 1.1m, 2.1m);
Tick tick1 = new Tick { Time = DateTime.Now, Symbol = Symbols.SPY, Value = 1, Quantity = 2 };
Tick tick2 = new Tick { Time = DateTime.Now, Symbol = Symbols.SPY, Value = 1.1m, Quantity = 2.1m };
Tick tick3 = new Tick { Time = DateTime.Now, Symbol = Symbols.AAPL, Value = 1, Quantity = 2 };
Tick tick4 = new Tick { Time = DateTime.Now, Symbol = Symbols.AAPL, Value = 1.1m, Quantity = 2.1m };
Slice slice = new Slice(DateTime.Now, new[] { tick1, tick2, tick3, tick4 });

Ticks ticks = slice.Ticks;
Expand Down Expand Up @@ -198,7 +220,7 @@ from QuantConnect.Data.Custom import *
def Test(slice):
data = slice.Get(Quandl)
return data").GetAttr("Test");
var quandlSpy = new Quandl { Symbol = Symbols.SPY, Time = DateTime.Now, Value = 10};
var quandlSpy = new Quandl { Symbol = Symbols.SPY, Time = DateTime.Now, Value = 10 };
var quandlAapl = new Quandl { Symbol = Symbols.AAPL, Time = DateTime.Now, Value = 11 };
var slice = new Slice(DateTime.Now, new[] { quandlSpy, quandlAapl });

Expand Down Expand Up @@ -462,10 +484,10 @@ public void EnumeratorDoesNotThrowWithTicks()
{
var slice = new Slice(DateTime.Now, new[]
{
new Tick(DateTime.Now, Symbols.SPY, 1, 2),
new Tick(DateTime.Now, Symbols.SPY, 1.1m, 2.1m),
new Tick(DateTime.Now, Symbols.AAPL, 1, 2),
new Tick(DateTime.Now, Symbols.AAPL, 1.1m, 2.1m)
new Tick {Time = DateTime.Now, Symbol = Symbols.SPY, Value = 1, Quantity = 2},
new Tick{Time = DateTime.Now, Symbol = Symbols.SPY, Value = 1.1m, Quantity = 2.1m},
new Tick{Time = DateTime.Now, Symbol = Symbols.AAPL, Value = 1, Quantity = 2},
new Tick{Time = DateTime.Now, Symbol = Symbols.AAPL, Value = 1.1m, Quantity = 2.1m}
});

Assert.AreEqual(4, slice.Count());
Expand Down

0 comments on commit 5671a32

Please sign in to comment.