Skip to content

Commit 8196d0b

Browse files
authored
HistoryRequest parameters now defaults to the security configuration (#9209)
* Fix HistoryRequest DataMappingMode default to use security configuration * Solve review comments * Make HistoryRequest inherit existing subscription configuration values generically * Exclude any class that inherits from BaseChainUniverseData * Reuse existing filter for user configuration * Solve review comments * Normalize DataMappingMode * Minor fix
1 parent 3ee941f commit 8196d0b

File tree

2 files changed

+100
-9
lines changed

2 files changed

+100
-9
lines changed

Algorithm/QCAlgorithm.History.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,15 @@ private IEnumerable<SubscriptionDataConfig> GetMatchingSubscriptions(Symbol symb
12831283
}
12841284
else
12851285
{
1286+
// let's try to respect already added user settings, even if resolution/type don't match, like Tick vs Bars
1287+
var userConfigIfAny = subscriptions.FirstOrDefault(x => LeanData.IsCommonLeanDataType(x.Type) && !x.IsInternalFeed);
1288+
1289+
// Inherit values from existing subscriptions or use defaults
1290+
var extendedMarketHours = userConfigIfAny?.ExtendedMarketHours ?? UniverseSettings.ExtendedMarketHours;
1291+
var dataNormalizationMode = userConfigIfAny?.DataNormalizationMode ?? UniverseSettings.GetUniverseNormalizationModeOrDefault(symbol.SecurityType);
1292+
var dataMappingMode = userConfigIfAny?.DataMappingMode ?? UniverseSettings.GetUniverseMappingModeOrDefault(symbol.SecurityType, symbol.ID.Market);
1293+
var contractDepthOffset = userConfigIfAny?.ContractDepthOffset ?? (uint)Math.Abs(UniverseSettings.ContractDepthOffset);
1294+
12861295
// If type was specified and not a lean data type and also not abstract, we create a new subscription
12871296
if (type != null && !LeanData.IsCommonLeanDataType(type) && !type.IsAbstract)
12881297
{
@@ -1306,17 +1315,16 @@ private IEnumerable<SubscriptionDataConfig> GetMatchingSubscriptions(Symbol symb
13061315
entry.DataTimeZone,
13071316
entry.ExchangeHours.TimeZone,
13081317
UniverseSettings.FillForward,
1309-
UniverseSettings.ExtendedMarketHours,
1318+
extendedMarketHours,
13101319
true,
13111320
isCustom,
13121321
LeanData.GetCommonTickTypeForCommonDataTypes(dataType, symbol.SecurityType),
13131322
true,
1314-
UniverseSettings.GetUniverseNormalizationModeOrDefault(symbol.SecurityType))};
1323+
dataNormalizationMode,
1324+
dataMappingMode,
1325+
contractDepthOffset)};
13151326
}
13161327

1317-
// let's try to respect already added user settings, even if resolution/type don't match, like Tick vs Bars
1318-
var userConfigIfAny = subscriptions.FirstOrDefault(x => LeanData.IsCommonLeanDataType(x.Type) && !x.IsInternalFeed);
1319-
13201328
var res = GetResolution(symbol, resolution, type);
13211329
return SubscriptionManager
13221330
.LookupSubscriptionConfigDataTypes(symbol.SecurityType, res,
@@ -1337,14 +1345,14 @@ private IEnumerable<SubscriptionDataConfig> GetMatchingSubscriptions(Symbol symb
13371345
entry.DataTimeZone,
13381346
entry.ExchangeHours.TimeZone,
13391347
UniverseSettings.FillForward,
1340-
userConfigIfAny?.ExtendedMarketHours ?? UniverseSettings.ExtendedMarketHours,
1348+
extendedMarketHours,
13411349
true,
13421350
false,
13431351
x.Item2,
13441352
true,
1345-
userConfigIfAny?.DataNormalizationMode ?? UniverseSettings.GetUniverseNormalizationModeOrDefault(symbol.SecurityType),
1346-
userConfigIfAny?.DataMappingMode ?? UniverseSettings.GetUniverseMappingModeOrDefault(symbol.SecurityType, symbol.ID.Market),
1347-
userConfigIfAny?.ContractDepthOffset ?? (uint)Math.Abs(UniverseSettings.ContractDepthOffset));
1353+
dataNormalizationMode,
1354+
dataMappingMode,
1355+
contractDepthOffset);
13481356
})
13491357
// 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
13501358
.OrderByDescending(config => GetTickTypeOrder(config.SecurityType, config.TickType));

Tests/Algorithm/AlgorithmHistoryTests.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3968,6 +3968,89 @@ public void DailyFuturesHistoryDoesNotIncludeSundaysAndReturnsCorrectSliceCountF
39683968
}
39693969
}
39703970

3971+
[TestCase(false)]
3972+
[TestCase(true)]
3973+
public void HistoryRequestUsesSecurityConfigOrExplicitValues(bool explicitParameters)
3974+
{
3975+
var start = new DateTime(2013, 10, 28);
3976+
var algorithm = GetAlgorithm(start);
3977+
var future = algorithm.AddFuture(
3978+
Futures.Indices.SP500EMini,
3979+
dataNormalizationMode: DataNormalizationMode.BackwardsRatio,
3980+
dataMappingMode: DataMappingMode.LastTradingDay,
3981+
contractDepthOffset: 0,
3982+
extendedMarketHours: true);
3983+
3984+
var customTestHistoryProvider = new CustomTestHistoryProvider();
3985+
algorithm.SetHistoryProvider(customTestHistoryProvider);
3986+
algorithm.HistoryProvider.Initialize(new HistoryProviderInitializeParameters(
3987+
null,
3988+
null,
3989+
_dataProvider,
3990+
_cacheProvider,
3991+
_mapFileProvider,
3992+
_factorFileProvider,
3993+
null,
3994+
false,
3995+
new DataPermissionManager(),
3996+
algorithm.ObjectStore,
3997+
algorithm.Settings));
3998+
3999+
List<SymbolChangedEvent> history;
4000+
4001+
if (!explicitParameters)
4002+
{
4003+
history = algorithm.History<SymbolChangedEvent>(
4004+
future.Symbol,
4005+
new DateTime(2007, 1, 1),
4006+
new DateTime(2012, 1, 1)).ToList();
4007+
}
4008+
else
4009+
{
4010+
history = algorithm.History<SymbolChangedEvent>(
4011+
future.Symbol,
4012+
new DateTime(2007, 1, 1),
4013+
new DateTime(2012, 1, 1),
4014+
dataNormalizationMode: DataNormalizationMode.Raw,
4015+
dataMappingMode: DataMappingMode.OpenInterest,
4016+
contractDepthOffset: 0,
4017+
extendedMarketHours: false).ToList();
4018+
}
4019+
4020+
Assert.AreEqual(1, customTestHistoryProvider.HistoryRequests.Count);
4021+
Assert.Greater(history.Count, 0);
4022+
4023+
var request = customTestHistoryProvider.HistoryRequests[0];
4024+
4025+
if (!explicitParameters)
4026+
{
4027+
// Without explicit parameters: uses values from security configuration
4028+
Assert.AreEqual(DataNormalizationMode.BackwardsRatio, request.DataNormalizationMode);
4029+
Assert.AreEqual(DataMappingMode.LastTradingDay, request.DataMappingMode);
4030+
Assert.AreEqual(true, request.IncludeExtendedMarketHours);
4031+
Assert.AreEqual(0, request.ContractDepthOffset);
4032+
}
4033+
else
4034+
{
4035+
// With explicit parameters: uses values from history request
4036+
Assert.AreEqual(DataNormalizationMode.Raw, request.DataNormalizationMode);
4037+
Assert.AreEqual(DataMappingMode.OpenInterest, request.DataMappingMode);
4038+
Assert.AreEqual(false, request.IncludeExtendedMarketHours);
4039+
Assert.AreEqual(0, request.ContractDepthOffset);
4040+
}
4041+
}
4042+
4043+
private class CustomTestHistoryProvider : SubscriptionDataReaderHistoryProvider
4044+
{
4045+
public List<HistoryRequest> HistoryRequests { get; } = new List<HistoryRequest>();
4046+
4047+
public override IEnumerable<Slice> GetHistory(IEnumerable<HistoryRequest> requests, DateTimeZone sliceTimeZone)
4048+
{
4049+
HistoryRequests.AddRange(requests);
4050+
return base.GetHistory(requests, sliceTimeZone);
4051+
}
4052+
}
4053+
39714054
public class CustomFundamentalTestData : BaseData
39724055
{
39734056
private static DateTime _currentDate;

0 commit comments

Comments
 (0)