Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions Algorithm/QCAlgorithm.History.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,15 @@ private IEnumerable<SubscriptionDataConfig> GetMatchingSubscriptions(Symbol symb
}
else
{
// let's try to respect already added user settings, even if resolution/type don't match, like Tick vs Bars
var userConfigIfAny = subscriptions.FirstOrDefault(x => LeanData.IsCommonLeanDataType(x.Type) && !x.IsInternalFeed);

// Inherit values from existing subscriptions or use defaults
var extendedMarketHours = userConfigIfAny?.ExtendedMarketHours ?? UniverseSettings.ExtendedMarketHours;
var dataNormalizationMode = userConfigIfAny?.DataNormalizationMode ?? UniverseSettings.GetUniverseNormalizationModeOrDefault(symbol.SecurityType);
var dataMappingMode = userConfigIfAny?.DataMappingMode ?? UniverseSettings.GetUniverseMappingModeOrDefault(symbol.SecurityType, symbol.ID.Market);
var contractDepthOffset = userConfigIfAny?.ContractDepthOffset ?? (uint)Math.Abs(UniverseSettings.ContractDepthOffset);

// If type was specified and not a lean data type and also not abstract, we create a new subscription
if (type != null && !LeanData.IsCommonLeanDataType(type) && !type.IsAbstract)
{
Expand All @@ -1306,17 +1315,16 @@ private IEnumerable<SubscriptionDataConfig> GetMatchingSubscriptions(Symbol symb
entry.DataTimeZone,
entry.ExchangeHours.TimeZone,
UniverseSettings.FillForward,
UniverseSettings.ExtendedMarketHours,
extendedMarketHours,
true,
isCustom,
LeanData.GetCommonTickTypeForCommonDataTypes(dataType, symbol.SecurityType),
true,
UniverseSettings.GetUniverseNormalizationModeOrDefault(symbol.SecurityType))};
dataNormalizationMode,
dataMappingMode,
contractDepthOffset)};
}

// let's try to respect already added user settings, even if resolution/type don't match, like Tick vs Bars
var userConfigIfAny = subscriptions.FirstOrDefault(x => LeanData.IsCommonLeanDataType(x.Type) && !x.IsInternalFeed);

var res = GetResolution(symbol, resolution, type);
return SubscriptionManager
.LookupSubscriptionConfigDataTypes(symbol.SecurityType, res,
Expand All @@ -1337,14 +1345,14 @@ private IEnumerable<SubscriptionDataConfig> GetMatchingSubscriptions(Symbol symb
entry.DataTimeZone,
entry.ExchangeHours.TimeZone,
UniverseSettings.FillForward,
userConfigIfAny?.ExtendedMarketHours ?? UniverseSettings.ExtendedMarketHours,
extendedMarketHours,
true,
false,
x.Item2,
true,
userConfigIfAny?.DataNormalizationMode ?? UniverseSettings.GetUniverseNormalizationModeOrDefault(symbol.SecurityType),
userConfigIfAny?.DataMappingMode ?? UniverseSettings.GetUniverseMappingModeOrDefault(symbol.SecurityType, symbol.ID.Market),
userConfigIfAny?.ContractDepthOffset ?? (uint)Math.Abs(UniverseSettings.ContractDepthOffset));
dataNormalizationMode,
dataMappingMode,
contractDepthOffset);
})
// lets make sure to respect the order of the data types, if used on a history request will affect outcome when using pushthrough for example
.OrderByDescending(config => GetTickTypeOrder(config.SecurityType, config.TickType));
Expand Down
83 changes: 83 additions & 0 deletions Tests/Algorithm/AlgorithmHistoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3968,6 +3968,89 @@ public void DailyFuturesHistoryDoesNotIncludeSundaysAndReturnsCorrectSliceCountF
}
}

[TestCase(false)]
[TestCase(true)]
public void HistoryRequestUsesSecurityConfigOrExplicitValues(bool explicitParameters)
{
var start = new DateTime(2013, 10, 28);
var algorithm = GetAlgorithm(start);
var future = algorithm.AddFuture(
Futures.Indices.SP500EMini,
dataNormalizationMode: DataNormalizationMode.BackwardsRatio,
dataMappingMode: DataMappingMode.LastTradingDay,
contractDepthOffset: 0,
extendedMarketHours: true);

var customTestHistoryProvider = new CustomTestHistoryProvider();
algorithm.SetHistoryProvider(customTestHistoryProvider);
algorithm.HistoryProvider.Initialize(new HistoryProviderInitializeParameters(
null,
null,
_dataProvider,
_cacheProvider,
_mapFileProvider,
_factorFileProvider,
null,
false,
new DataPermissionManager(),
algorithm.ObjectStore,
algorithm.Settings));

List<SymbolChangedEvent> history;

if (!explicitParameters)
{
history = algorithm.History<SymbolChangedEvent>(
future.Symbol,
new DateTime(2007, 1, 1),
new DateTime(2012, 1, 1)).ToList();
}
else
{
history = algorithm.History<SymbolChangedEvent>(
future.Symbol,
new DateTime(2007, 1, 1),
new DateTime(2012, 1, 1),
dataNormalizationMode: DataNormalizationMode.Raw,
dataMappingMode: DataMappingMode.OpenInterest,
contractDepthOffset: 0,
extendedMarketHours: false).ToList();
}

Assert.AreEqual(1, customTestHistoryProvider.HistoryRequests.Count);
Assert.Greater(history.Count, 0);

var request = customTestHistoryProvider.HistoryRequests[0];

if (!explicitParameters)
{
// Without explicit parameters: uses values from security configuration
Assert.AreEqual(DataNormalizationMode.BackwardsRatio, request.DataNormalizationMode);
Assert.AreEqual(DataMappingMode.LastTradingDay, request.DataMappingMode);
Assert.AreEqual(true, request.IncludeExtendedMarketHours);
Assert.AreEqual(0, request.ContractDepthOffset);
}
else
{
// With explicit parameters: uses values from history request
Assert.AreEqual(DataNormalizationMode.Raw, request.DataNormalizationMode);
Assert.AreEqual(DataMappingMode.OpenInterest, request.DataMappingMode);
Assert.AreEqual(false, request.IncludeExtendedMarketHours);
Assert.AreEqual(0, request.ContractDepthOffset);
}
}

private class CustomTestHistoryProvider : SubscriptionDataReaderHistoryProvider
{
public List<HistoryRequest> HistoryRequests { get; } = new List<HistoryRequest>();

public override IEnumerable<Slice> GetHistory(IEnumerable<HistoryRequest> requests, DateTimeZone sliceTimeZone)
{
HistoryRequests.AddRange(requests);
return base.GetHistory(requests, sliceTimeZone);
}
}

public class CustomFundamentalTestData : BaseData
{
private static DateTime _currentDate;
Expand Down