Skip to content
Open
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
12 changes: 9 additions & 3 deletions Algorithm/QCAlgorithm.History.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ private IEnumerable<HistoryRequest> GetFilterestRequests(IEnumerable<HistoryRequ
request.DataType, request.Symbol, request.Resolution, request.ExchangeHours,
request.DataTimeZone, request.FillForwardResolution, request.IncludeExtendedMarketHours,
request.IsCustomData, request.DataNormalizationMode, request.TickType, request.DataMappingMode,
request.ContractDepthOffset);
request.ContractDepthOffset, request.DataMappingModeDaysOffset, request.ContractMonthCycle);

if (!sentMessage)
{
Expand Down Expand Up @@ -1291,6 +1291,8 @@ private IEnumerable<SubscriptionDataConfig> GetMatchingSubscriptions(Symbol symb
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);
var dataMappingModeDaysOffset = userConfigIfAny?.DataMappingModeDaysOffset ?? UniverseSettings.DataMappingModeDaysOffset;
var contractMonthCycle = userConfigIfAny?.ContractMonthCycle ?? UniverseSettings.ContractMonthCycle;

// 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 Down Expand Up @@ -1322,7 +1324,9 @@ private IEnumerable<SubscriptionDataConfig> GetMatchingSubscriptions(Symbol symb
true,
dataNormalizationMode,
dataMappingMode,
contractDepthOffset)};
contractDepthOffset,
dataMappingModeDaysOffset: dataMappingModeDaysOffset,
contractMonthCycle: contractMonthCycle)};
}

var res = GetResolution(symbol, resolution, type);
Expand Down Expand Up @@ -1352,7 +1356,9 @@ private IEnumerable<SubscriptionDataConfig> GetMatchingSubscriptions(Symbol symb
true,
dataNormalizationMode,
dataMappingMode,
contractDepthOffset);
contractDepthOffset,
dataMappingModeDaysOffset: dataMappingModeDaysOffset,
contractMonthCycle: contractMonthCycle);
})
// 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
23 changes: 18 additions & 5 deletions Algorithm/QCAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1987,10 +1987,13 @@ public Security AddSecurity(SecurityType securityType, string ticker, Resolution
/// <param name="dataNormalizationMode">The price scaling mode to use for the security</param>
/// <param name="contractDepthOffset">The continuous contract desired offset from the current front month.
/// For example, 0 (default) will use the front month, 1 will use the back month contract</param>
/// <param name="dataMappingModeDaysOffset">The continuous contract mapping offset in tradeable days</param>
/// <param name="contractMonthCycle">Optional contract expiration months to use when walking continuous future contract depth</param>
/// <returns>The new Security that was added to the algorithm</returns>
[DocumentationAttribute(AddingData)]
public Security AddSecurity(Symbol symbol, Resolution? resolution = null, bool? fillForward = null, decimal leverage = Security.NullLeverage, bool? extendedMarketHours = null,
DataMappingMode? dataMappingMode = null, DataNormalizationMode? dataNormalizationMode = null, int contractDepthOffset = 0)
DataMappingMode? dataMappingMode = null, DataNormalizationMode? dataNormalizationMode = null, int contractDepthOffset = 0,
int dataMappingModeDaysOffset = 0, int[] contractMonthCycle = null)
{
// allow users to specify negative numbers, we get the abs of it
var contractOffset = (uint)Math.Abs(contractDepthOffset);
Expand Down Expand Up @@ -2030,7 +2033,9 @@ public Security AddSecurity(Symbol symbol, Resolution? resolution = null, bool?
extendedMarketHours.Value,
isFilteredSubscription,
dataNormalizationMode: dataNormalizationMode.Value,
contractDepthOffset: (uint)contractDepthOffset);
contractDepthOffset: (uint)contractDepthOffset,
dataMappingModeDaysOffset: dataMappingModeDaysOffset,
contractMonthCycle: contractMonthCycle);
}
else
{
Expand All @@ -2039,7 +2044,9 @@ public Security AddSecurity(Symbol symbol, Resolution? resolution = null, bool?
securityFillForward,
extendedMarketHours.Value,
isFilteredSubscription,
contractDepthOffset: (uint)contractDepthOffset);
contractDepthOffset: (uint)contractDepthOffset,
dataMappingModeDaysOffset: dataMappingModeDaysOffset,
contractMonthCycle: contractMonthCycle);
}

var security = Securities.CreateSecurity(symbol, configs, leverage);
Expand Down Expand Up @@ -2076,6 +2083,8 @@ public Security AddSecurity(Symbol symbol, Resolution? resolution = null, bool?
DataMappingMode = dataMappingMode ?? UniverseSettings.GetUniverseMappingModeOrDefault(symbol.SecurityType, symbol.ID.Market),
DataNormalizationMode = dataNormalizationMode ?? UniverseSettings.GetUniverseNormalizationModeOrDefault(symbol.SecurityType),
ContractDepthOffset = (int)contractOffset,
DataMappingModeDaysOffset = dataMappingModeDaysOffset,
ContractMonthCycle = contractMonthCycle,
SubscriptionDataTypes = dataTypes,
Asynchronous = UniverseSettings.Asynchronous
};
Expand Down Expand Up @@ -2213,11 +2222,14 @@ public Option AddOption(Symbol underlying, string targetOption, Resolution? reso
/// <param name="dataNormalizationMode">The price scaling mode to use for the continuous future contract</param>
/// <param name="contractDepthOffset">The continuous future contract desired offset from the current front month.
/// For example, 0 (default) will use the front month, 1 will use the back month contract</param>
/// <param name="dataMappingModeDaysOffset">The continuous contract mapping offset in tradeable days</param>
/// <param name="contractMonthCycle">Optional contract expiration months to use when walking continuous future contract depth</param>
/// <returns>The new <see cref="Future"/> security</returns>
[DocumentationAttribute(AddingData)]
public Future AddFuture(string ticker, Resolution? resolution = null, string market = null,
bool? fillForward = null, decimal leverage = Security.NullLeverage, bool? extendedMarketHours = null,
DataMappingMode? dataMappingMode = null, DataNormalizationMode? dataNormalizationMode = null, int contractDepthOffset = 0)
DataMappingMode? dataMappingMode = null, DataNormalizationMode? dataNormalizationMode = null, int contractDepthOffset = 0,
int dataMappingModeDaysOffset = 0, int[] contractMonthCycle = null)
{
market = GetMarket(market, ticker, SecurityType.Future);

Expand All @@ -2231,7 +2243,8 @@ public Future AddFuture(string ticker, Resolution? resolution = null, string mar
}

return (Future)AddSecurity(canonicalSymbol, resolution, fillForward, leverage, extendedMarketHours, dataMappingMode: dataMappingMode,
dataNormalizationMode: dataNormalizationMode, contractDepthOffset: contractDepthOffset);
dataNormalizationMode: dataNormalizationMode, contractDepthOffset: contractDepthOffset, dataMappingModeDaysOffset: dataMappingModeDaysOffset,
contractMonthCycle: contractMonthCycle);
}

/// <summary>
Expand Down
8 changes: 6 additions & 2 deletions AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,14 @@ public Security AddSecurity(SecurityType securityType, string symbol, Resolution
/// <param name="dataNormalizationMode">The price scaling mode to use for the security</param>
/// <param name="contractDepthOffset">The continuous contract desired offset from the current front month.
/// For example, 0 (default) will use the front month, 1 will use the back month contract</param>
/// <param name="dataMappingModeDaysOffset">The continuous contract mapping offset in tradeable days</param>
/// <param name="contractMonthCycle">Optional contract expiration months to use when walking continuous future contract depth</param>
/// <returns>The new Security that was added to the algorithm</returns>
public Security AddSecurity(Symbol symbol, Resolution? resolution = null, bool? fillForward = null, decimal leverage = Security.NullLeverage, bool? extendedMarketHours = null,
DataMappingMode? dataMappingMode = null, DataNormalizationMode? dataNormalizationMode = null, int contractDepthOffset = 0)
=> _baseAlgorithm.AddSecurity(symbol, resolution, fillForward, leverage, extendedMarketHours, dataMappingMode, dataNormalizationMode, contractDepthOffset);
DataMappingMode? dataMappingMode = null, DataNormalizationMode? dataNormalizationMode = null, int contractDepthOffset = 0,
int dataMappingModeDaysOffset = 0, int[] contractMonthCycle = null)
=> _baseAlgorithm.AddSecurity(symbol, resolution, fillForward, leverage, extendedMarketHours, dataMappingMode, dataNormalizationMode,
contractDepthOffset, dataMappingModeDaysOffset, contractMonthCycle);

/// <summary>
/// Creates and adds a new single <see cref="Future"/> contract to the algorithm
Expand Down
5 changes: 5 additions & 0 deletions Common/Data/Auxiliary/MapFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public MapFile(string permtick, IEnumerable<MapFileRow> data)
/// <returns>Symbol on this date.</returns>
public string GetMappedSymbol(DateTime searchDate, string defaultReturnValue = "", DataMappingMode? dataMappingMode = null)
{
if (dataMappingMode == DataMappingMode.TradingDaysBeforeExpiry)
{
dataMappingMode = DataMappingMode.LastTradingDay;
}

var mappedSymbol = defaultReturnValue;
//Iterate backwards to find the most recent factor:
for (var i = 0; i < _data.Count; i++)
Expand Down
22 changes: 19 additions & 3 deletions Common/Data/HistoryRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ public bool IncludeExtendedMarketHours
/// </summary>
public uint ContractDepthOffset { get; set; }

/// <summary>
/// The continuous contract mapping offset in tradeable days
/// </summary>
public int DataMappingModeDaysOffset { get; set; }

/// <summary>
/// Optional contract expiration months to use when walking continuous future contract depth
/// </summary>
public IReadOnlyList<int> ContractMonthCycle { get; set; }

/// <summary>
/// Gets the tradable days specified by this request, in the security's data time zone
/// </summary>
Expand Down Expand Up @@ -137,7 +147,9 @@ public HistoryRequest(DateTime startTimeUtc,
DataNormalizationMode dataNormalizationMode,
TickType tickType,
DataMappingMode dataMappingMode = DataMappingMode.OpenInterest,
uint contractDepthOffset = 0)
uint contractDepthOffset = 0,
int dataMappingModeDaysOffset = 0,
IReadOnlyList<int> contractMonthCycle = null)
: base(startTimeUtc, endTimeUtc, exchangeHours, tickType, isCustomData, dataType)
{
Symbol = symbol;
Expand All @@ -149,6 +161,8 @@ public HistoryRequest(DateTime startTimeUtc,
TickType = tickType;
DataMappingMode = dataMappingMode;
ContractDepthOffset = contractDepthOffset;
DataMappingModeDaysOffset = dataMappingModeDaysOffset;
ContractMonthCycle = contractMonthCycle;
}

/// <summary>
Expand All @@ -161,7 +175,8 @@ public HistoryRequest(DateTime startTimeUtc,
public HistoryRequest(SubscriptionDataConfig config, SecurityExchangeHours hours, DateTime startTimeUtc, DateTime endTimeUtc)
: this(startTimeUtc, endTimeUtc, config.Type, config.Symbol, config.Resolution,
hours, config.DataTimeZone, config.FillDataForward ? config.Resolution : (Resolution?)null,
config.ExtendedMarketHours, config.IsCustomData, config.DataNormalizationMode, config.TickType, config.DataMappingMode, config.ContractDepthOffset)
config.ExtendedMarketHours, config.IsCustomData, config.DataNormalizationMode, config.TickType, config.DataMappingMode,
config.ContractDepthOffset, config.DataMappingModeDaysOffset, config.ContractMonthCycle)
{
}

Expand All @@ -173,7 +188,8 @@ public HistoryRequest(SubscriptionDataConfig config, SecurityExchangeHours hours
/// <param name="newEndTimeUtc">The end time for this request</param>
public HistoryRequest(HistoryRequest request, Symbol newSymbol, DateTime newStartTimeUtc, DateTime newEndTimeUtc)
: this (newStartTimeUtc, newEndTimeUtc, request.DataType, newSymbol, request.Resolution, request.ExchangeHours, request.DataTimeZone, request.FillForwardResolution,
request.IncludeExtendedMarketHours, request.IsCustomData, request.DataNormalizationMode, request.TickType, request.DataMappingMode, request.ContractDepthOffset)
request.IncludeExtendedMarketHours, request.IsCustomData, request.DataNormalizationMode, request.TickType, request.DataMappingMode,
request.ContractDepthOffset, request.DataMappingModeDaysOffset, request.ContractMonthCycle)
{ }
}
}
Loading