Skip to content

Commit

Permalink
Normalize backtesting and live future selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Molinero committed May 27, 2022
1 parent 3110707 commit 1fcff9f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
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
23 changes: 22 additions & 1 deletion Engine/DataFeeds/Queues/FakeDataQueue.cs
Expand Up @@ -37,6 +37,7 @@ public class FakeDataQueue : IDataQueueHandler, IDataQueueUniverseProvider
private readonly Random _random = new Random();

private readonly Timer _timer;
private readonly IOptionChainProvider _optionChainProvider;
private readonly EventBasedDataQueueHandlerSubscriptionManager _subscriptionManager;
private readonly IDataAggregator _aggregator;
private readonly MarketHoursDatabase _marketHoursDatabase;
Expand All @@ -62,6 +63,7 @@ public FakeDataQueue()
public FakeDataQueue(IDataAggregator dataAggregator)
{
_aggregator = dataAggregator;
_optionChainProvider = new LiveOptionChainProvider();
_marketHoursDatabase = MarketHoursDatabase.FromDataFolder();
_symbolExchangeTimeZones = new Dictionary<Symbol, TimeZoneOffsetProvider>();
_subscriptionManager = new EventBasedDataQueueHandlerSubscriptionManager();
Expand Down Expand Up @@ -205,9 +207,28 @@ private TimeZoneOffsetProvider GetTimeZoneOffsetProvider(Symbol symbol)
return offsetProvider;
}

/// <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="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)
{
yield break;
switch (symbol.SecurityType)
{
case SecurityType.Option:
case SecurityType.IndexOption:
case SecurityType.FutureOption:
foreach (var result in _optionChainProvider.GetOptionContractList(symbol.Underlying, DateTime.UtcNow.Date))
{
yield return result;
}
break;
default:
break;
}
}

public bool CanPerformSelection()
Expand Down
9 changes: 6 additions & 3 deletions Engine/HistoricalData/FakeHistoryProvider.cs
Expand Up @@ -13,7 +13,6 @@
* limitations under the License.
*/

using System;
using NodaTime;
using System.Linq;
using QuantConnect.Data;
Expand Down Expand Up @@ -52,7 +51,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.FirstOrDefault();
if(single == null)
{
yield break;
}

var currentLocalTime = single.StartTimeLocal;
while (currentLocalTime < single.EndTimeLocal)
Expand Down Expand Up @@ -89,7 +92,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

0 comments on commit 1fcff9f

Please sign in to comment.