Skip to content

Commit

Permalink
Add current time for LookupSymbols interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Molinero committed May 27, 2022
1 parent 3110707 commit c4696b8
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 31 deletions.
@@ -1,4 +1,4 @@
/*
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
Expand Down Expand Up @@ -32,9 +32,10 @@ public partial class TradierBrokerage
/// </summary>
/// <param name="symbol">Symbol to lookup</param>
/// <param name="includeExpired">Include expired contracts</param>
/// <param name="dateTimeUtc">The current time in utc</param>
/// <param name="securityCurrency">Expected security currency(if any)</param>
/// <returns>Enumerable of Symbols, that are associated with the provided Symbol</returns>
public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, string securityCurrency = null)
public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, DateTime dateTimeUtc, string securityCurrency = null)
{
if (symbol.SecurityType != SecurityType.Option)
{
Expand All @@ -47,7 +48,7 @@ public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, str

var symbols = new List<Symbol>();

var today = DateTime.UtcNow.ConvertFromUtc(TimeZones.NewYork).Date;
var today = dateTimeUtc.ConvertFromUtc(TimeZones.NewYork).Date;
symbols.AddRange(_algorithm.OptionChainProvider.GetOptionContractList(symbol.Underlying, today));

// Try to remove options contracts that have expired
Expand Down
3 changes: 2 additions & 1 deletion Common/Data/UniverseSelection/ContinuousContractUniverse.cs
Expand Up @@ -130,7 +130,8 @@ public IEnumerable<DateTime> GetTriggerTimes(DateTime startTimeUtc, DateTime end
var endTimeLocal = endTimeUtc.ConvertFromUtc(_security.Exchange.TimeZone);

return Time.EachTradeableDay(_security, startTimeLocal, endTimeLocal)
.Where(tradeableDay => tradeableDay >= startTimeLocal)
// in live trading selection happens on start see 'DataQueueFuturesChainUniverseDataCollectionEnumerator'
.Where(tradeableDay => _liveMode || tradeableDay >= startTimeLocal)
// in live trading we delay selection so that we make sure auxiliary data is ready
.Select(time => _liveMode ? time.Add(Time.LiveAuxiliaryDataOffset) : time);
}
Expand Down
6 changes: 4 additions & 2 deletions Common/Interfaces/IDataQueueUniverseProvider.cs
@@ -1,4 +1,4 @@
/*
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
Expand All @@ -13,6 +13,7 @@
* limitations under the License.
*/

using System;
using System.Collections.Generic;

namespace QuantConnect.Interfaces
Expand All @@ -28,9 +29,10 @@ public interface IDataQueueUniverseProvider
/// </summary>
/// <param name="symbol">Symbol to lookup</param>
/// <param name="includeExpired">Include expired contracts</param>
/// <param name="dateTimeUtc">The current time in utc</param>
/// <param name="securityCurrency">Expected security currency(if any)</param>
/// <returns>Enumerable of Symbols, that are associated with the provided Symbol</returns>
IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, string securityCurrency = null);
IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, DateTime dateTimeUtc, string securityCurrency = null);

/// <summary>
/// Returns whether selection can take place or not.
Expand Down
5 changes: 3 additions & 2 deletions Engine/DataFeeds/DataQueueHandlerManager.cs
Expand Up @@ -113,13 +113,14 @@ public void Dispose()
/// </summary>
/// <param name="symbol">Symbol to lookup</param>
/// <param name="includeExpired">Include expired contracts</param>
/// <param name="dateTimeUtc">The current time in utc</param>
/// <param name="securityCurrency">Expected security currency(if any)</param>
/// <returns>Enumerable of Symbols, that are associated with the provided Symbol</returns>
public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, string securityCurrency = null)
public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, DateTime dateTimeUtc, string securityCurrency = null)
{
foreach (var dataHandler in GetUniverseProviders())
{
var result = dataHandler.LookupSymbols(symbol, includeExpired, securityCurrency).ToList();
var result = dataHandler.LookupSymbols(symbol, includeExpired, dateTimeUtc, securityCurrency).ToList();
if (result.Any())
{
return result;
Expand Down
@@ -1,4 +1,4 @@
/*
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
Expand Down Expand Up @@ -94,12 +94,13 @@ public bool MoveNext()
return true;
}

var localTime = _timeProvider.GetUtcNow()
var utcTime = _timeProvider.GetUtcNow();
var localTime = utcTime
.RoundDown(_subscriptionRequest.Configuration.Increment)
.ConvertFromUtc(_subscriptionRequest.Configuration.ExchangeTimeZone);

// loading the list of futures contracts and converting them into zip entries
var symbols = _universeProvider.LookupSymbols(_subscriptionRequest.Security.Symbol, false);
var symbols = _universeProvider.LookupSymbols(_subscriptionRequest.Security.Symbol, false, utcTime);
var zipEntries = symbols.Select(x => new ZipEntryName { Time = localTime, Symbol = x } as BaseData).ToList();
var current = new FuturesChainUniverseDataCollection
{
Expand Down
@@ -1,4 +1,4 @@
/*
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
Expand Down Expand Up @@ -111,12 +111,13 @@ public bool MoveNext()
return true;
}

var localTime = _timeProvider.GetUtcNow()
var utcTime = _timeProvider.GetUtcNow();
var localTime = utcTime
.RoundDown(_subscriptionRequest.Configuration.Increment)
.ConvertFromUtc(_subscriptionRequest.Configuration.ExchangeTimeZone);

// loading the list of futures contracts and converting them into zip entries
var symbols = _universeProvider.LookupSymbols(_subscriptionRequest.Security.Symbol, false);
var symbols = _universeProvider.LookupSymbols(_subscriptionRequest.Security.Symbol, false, utcTime);
var zipEntries = symbols.Select(x => new ZipEntryName { Time = localTime, Symbol = x } as BaseData).ToList();
_currentData = new OptionChainUniverseDataCollection
{
Expand Down
4 changes: 2 additions & 2 deletions Engine/DataFeeds/LiveTradingDataFeed.cs
Expand Up @@ -353,7 +353,7 @@ private Subscription CreateUniverseSubscription(SubscriptionRequest request)

var symbolUniverse = GetUniverseProvider(SecurityType.Option);

var enumeratorFactory = new OptionChainUniverseSubscriptionEnumeratorFactory(configure, symbolUniverse, _timeProvider);
var enumeratorFactory = new OptionChainUniverseSubscriptionEnumeratorFactory(configure, symbolUniverse, _frontierTimeProvider);
enumerator = enumeratorFactory.CreateEnumerator(request, _dataProvider);

enumerator = new FrontierAwareEnumerator(enumerator, _frontierTimeProvider, tzOffsetProvider);
Expand All @@ -364,7 +364,7 @@ private Subscription CreateUniverseSubscription(SubscriptionRequest request)

var symbolUniverse = GetUniverseProvider(SecurityType.Option);

var enumeratorFactory = new FuturesChainUniverseSubscriptionEnumeratorFactory(symbolUniverse, _timeProvider);
var enumeratorFactory = new FuturesChainUniverseSubscriptionEnumeratorFactory(symbolUniverse, _frontierTimeProvider);
enumerator = enumeratorFactory.CreateEnumerator(request, _dataProvider);

enumerator = new FrontierAwareEnumerator(enumerator, _frontierTimeProvider, tzOffsetProvider);
Expand Down
10 changes: 9 additions & 1 deletion Engine/DataFeeds/Queues/FakeDataQueue.cs
Expand Up @@ -205,7 +205,15 @@ private TimeZoneOffsetProvider GetTimeZoneOffsetProvider(Symbol symbol)
return offsetProvider;
}

public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, string securityCurrency = null)
/// <summary>
/// Method returns a collection of Symbols that are available at the data source.
/// </summary>
/// <param name="symbol">Symbol to lookup</param>
/// <param name="includeExpired">Include expired contracts</param>
/// <param name="dateTimeUtc">The current time in utc</param>
/// <param name="securityCurrency">Expected security currency(if any)</param>
/// <returns>Enumerable of Symbols, that are associated with the provided Symbol</returns>
public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, DateTime dateTimeUtc, string securityCurrency = null)
{
yield break;
}
Expand Down
8 changes: 6 additions & 2 deletions Engine/HistoricalData/FakeHistoryProvider.cs
Expand Up @@ -52,7 +52,11 @@ public override void Initialize(HistoryProviderInitializeParameters parameters)
/// <returns>An enumerable of the slices of data covering the span specified in each request</returns>
public override IEnumerable<Slice> GetHistory(IEnumerable<HistoryRequest> requests, DateTimeZone sliceTimeZone)
{
var single = requests.Single();
var single = requests.SingleOrDefault();
if(single == null)
{
yield break;
}

var currentLocalTime = single.StartTimeLocal;
while (currentLocalTime < single.EndTimeLocal)
Expand Down Expand Up @@ -89,7 +93,7 @@ public override IEnumerable<Slice> GetHistory(IEnumerable<HistoryRequest> reques
}
else
{
throw new InvalidOperationException();
yield break;
}

yield return new Slice(data.EndTime, new BaseData[] { data }, data.EndTime.ConvertFromUtc(single.ExchangeHours.TimeZone));
Expand Down
2 changes: 1 addition & 1 deletion Tests/Engine/DataFeeds/DataQueueHandlerManagerTests.cs
Expand Up @@ -90,7 +90,7 @@ public void IsNotUniverseProvider()
{
var compositeDataQueueHandler = new DataQueueHandlerManager();
Assert.IsFalse(compositeDataQueueHandler.HasUniverseProvider);
Assert.Throws<NotSupportedException>(() => compositeDataQueueHandler.LookupSymbols(Symbols.ES_Future_Chain, false));
Assert.Throws<NotSupportedException>(() => compositeDataQueueHandler.LookupSymbols(Symbols.ES_Future_Chain, false, DateTime.UtcNow));
Assert.Throws<NotSupportedException>(() => compositeDataQueueHandler.CanPerformSelection());
compositeDataQueueHandler.Dispose();
}
Expand Down
@@ -1,4 +1,4 @@
/*
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
Expand Down Expand Up @@ -132,7 +132,7 @@ public TestDataQueueUniverseProvider(ITimeProvider timeProvider)
_timeProvider = timeProvider;
}

public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, string securityCurrency = null)
public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, DateTime dateTimeUtc, string securityCurrency = null)
{
TotalLookupCalls++;

Expand Down
Expand Up @@ -260,7 +260,7 @@ public TestDataQueueUniverseProvider(ITimeProvider timeProvider)
_timeProvider = timeProvider;
}

public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, string securityCurrency = null)
public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, DateTime dateTimeUtc, string securityCurrency = null)
{
TotalLookupCalls++;

Expand Down
@@ -1,4 +1,4 @@
/*
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
Expand Down Expand Up @@ -53,9 +53,10 @@ public class FuncDataQueueHandlerUniverseProvider : FuncDataQueueHandler, IDataQ
/// </summary>
/// <param name="symbol">Symbol to lookup</param>
/// <param name="includeExpired">Include expired contracts</param>
/// <param name="dateTimeUtc">The current time in utc</param>
/// <param name="securityCurrency">Expected security currency(if any)</param>
/// <returns></returns>
public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, string securityCurrency = null)
public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, DateTime dateTimeUtc, string securityCurrency = null)
{
return _lookupSymbolsFunction(symbol, includeExpired, securityCurrency);
}
Expand Down
8 changes: 5 additions & 3 deletions Tests/Engine/DataFeeds/IQFeedRealtimeDataFeedTests.cs
@@ -1,4 +1,4 @@
/*
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
Expand All @@ -14,6 +14,7 @@
*
*/

using System;
using NUnit.Framework;
using QuantConnect.Brokerages;
using QuantConnect.Interfaces;
Expand Down Expand Up @@ -41,8 +42,9 @@ public void IQFeedSanityCheckIfDataIsLoaded()
Assert.IsTrue(symbolUniverse.LookupSymbols("SPY", SecurityType.Option, false).Any());
Assert.IsTrue(symbolUniverse.LookupSymbols("SPY", SecurityType.Equity, false).Count() == 1);

Assert.IsTrue(lookup.LookupSymbols(Symbol.Create("SPY", SecurityType.Option, Market.USA), false).Any());
Assert.IsTrue(lookup.LookupSymbols(Symbol.Create("SPY", SecurityType.Equity, Market.USA), false).Count() == 1);
var now = DateTime.UtcNow;
Assert.IsTrue(lookup.LookupSymbols(Symbol.Create("SPY", SecurityType.Option, Market.USA), false, now).Any());
Assert.IsTrue(lookup.LookupSymbols(Symbol.Create("SPY", SecurityType.Equity, Market.USA), false, now).Count() == 1);

Assert.IsTrue(!string.IsNullOrEmpty(mapper.GetBrokerageSymbol(Symbols.SPY)));
Assert.IsTrue(mapper.GetLeanSymbol("SPY", SecurityType.Equity, "") != Symbol.Empty);
Expand Down
3 changes: 2 additions & 1 deletion ToolBox/IQFeed/IQFeedDataQueueHandler.cs
Expand Up @@ -358,9 +358,10 @@ public IEnumerable<Symbol> LookupSymbols(string lookupName, SecurityType securit
/// </summary>
/// <param name="symbol">Symbol to lookup</param>
/// <param name="includeExpired">Include expired contracts</param>
/// <param name="dateTimeUtc">The current time in utc</param>
/// <param name="securityCurrency">Expected security currency(if any)</param>
/// <returns>Symbol results</returns>
public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, string securityCurrency)
public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, DateTime dateTimeUtc, string securityCurrency = null)
{
return LookupSymbols(symbol.ID.Symbol, symbol.SecurityType, includeExpired, securityCurrency);
}
Expand Down
5 changes: 3 additions & 2 deletions ToolBox/IQFeed/IQFeedDataQueueUniverseProvider.cs
@@ -1,4 +1,4 @@
/*
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
Expand Down Expand Up @@ -190,9 +190,10 @@ public IEnumerable<Symbol> LookupSymbols(string lookupName, SecurityType securit
/// </summary>
/// <param name="symbol">Symbol to lookup</param>
/// <param name="includeExpired">Include expired contracts</param>
/// <param name="dateTimeUtc">The current time in utc</param>
/// <param name="securityCurrency">Expected security currency(if any)</param>
/// <returns>Symbol results</returns>
public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, string securityCurrency)
public IEnumerable<Symbol> LookupSymbols(Symbol symbol, bool includeExpired, DateTime dateTimeUtc, string securityCurrency = null)
{
return LookupSymbols(symbol.ID.Symbol, symbol.SecurityType, includeExpired, securityCurrency);
}
Expand Down

0 comments on commit c4696b8

Please sign in to comment.