Skip to content

Commit

Permalink
Remove SecurityInitializer from Universe Selection (QuantConnect#5418)
Browse files Browse the repository at this point in the history
* Attempt to remove SecurityInitializer from UniverseSelection stack entirely

* Fix broken Python Universe Selection model

* Remove mistake change (meant for another branch)
  • Loading branch information
C-SELLERS authored May 28, 2021
1 parent 5f6a40f commit 41f9d9d
Show file tree
Hide file tree
Showing 35 changed files with 83 additions and 352 deletions.
3 changes: 1 addition & 2 deletions Algorithm.CSharp/CapmAlphaRankingFrameworkAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ public override IEnumerable<Universe> CreateUniverses(QCAlgorithm algorithm)
algorithm.DateRules.MonthStart(benchmark.Symbol),
algorithm.TimeRules.AfterMarketOpen(benchmark.Symbol),
datetime => SelectPair(algorithm, datetime),
algorithm.UniverseSettings,
algorithm.SecurityInitializer);
algorithm.UniverseSettings);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override void Initialize()

UniverseSettings.FillForward = false;

AddUniverse(new CustomDataCoarseFundamentalUniverse(UniverseSettings, SecurityInitializer, CoarseSelectionFunction));
AddUniverse(new CustomDataCoarseFundamentalUniverse(UniverseSettings, CoarseSelectionFunction));

_symbols = new List<Symbol>();
}
Expand Down Expand Up @@ -93,8 +93,8 @@ public override void OnSecuritiesChanged(SecurityChanges changes)

private class CustomDataCoarseFundamentalUniverse : CoarseFundamentalUniverse
{
public CustomDataCoarseFundamentalUniverse(UniverseSettings universeSettings, ISecurityInitializer securityInitializer, Func<IEnumerable<CoarseFundamental>, IEnumerable<Symbol>> selector)
: base(universeSettings, securityInitializer, selector)
public CustomDataCoarseFundamentalUniverse(UniverseSettings universeSettings, Func<IEnumerable<CoarseFundamental>, IEnumerable<Symbol>> selector)
: base(universeSettings, selector)
{ }

public override IEnumerable<SubscriptionRequest> GetSubscriptionRequests(Security security, DateTime currentTimeUtc, DateTime maximumEndTimeUtc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ public class CoarseFundamentalUniverseSelectionModel : FundamentalUniverseSelect
/// </summary>
/// <param name="coarseSelector">Selects symbols from the provided coarse data set</param>
/// <param name="universeSettings">Universe settings define attributes of created subscriptions, such as their resolution and the minimum time in universe before they can be removed</param>
/// <param name="securityInitializer">Performs extra initialization (such as setting models) after we create a new security object</param>
public CoarseFundamentalUniverseSelectionModel(
Func<IEnumerable<CoarseFundamental>, IEnumerable<Symbol>> coarseSelector,
UniverseSettings universeSettings = null,
ISecurityInitializer securityInitializer = null
UniverseSettings universeSettings = null
)
: base(false, universeSettings, securityInitializer)
: base(false, universeSettings)
{
_coarseSelector = coarseSelector;
}
Expand All @@ -48,13 +46,11 @@ public CoarseFundamentalUniverseSelectionModel(
/// </summary>
/// <param name="coarseSelector">Selects symbols from the provided coarse data set</param>
/// <param name="universeSettings">Universe settings define attributes of created subscriptions, such as their resolution and the minimum time in universe before they can be removed</param>
/// <param name="securityInitializer">Performs extra initialization (such as setting models) after we create a new security object</param>
public CoarseFundamentalUniverseSelectionModel(
PyObject coarseSelector,
UniverseSettings universeSettings = null,
ISecurityInitializer securityInitializer = null
UniverseSettings universeSettings = null
)
: base(false, universeSettings, securityInitializer)
: base(false, universeSettings)
{
Func<IEnumerable<CoarseFundamental>, object> func;
if (coarseSelector.TryConvertToDelegate(out func))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ public class EmaCrossUniverseSelectionModel : FundamentalUniverseSelectionModel
/// <param name="fastPeriod">Fast EMA period</param>
/// <param name="slowPeriod">Slow EMA period</param>
/// <param name="universeCount">Maximum number of members of this universe selection</param>
/// <param name="universeSettings">The settings used when adding symbols to the algorithm, specify null to use algorthm.UniverseSettings</param>
/// <param name="securityInitializer">Optional security initializer invoked when creating new securities, specify null to use algorithm.SecurityInitializer</param>
/// <param name="universeSettings">The settings used when adding symbols to the algorithm, specify null to use algorithm.UniverseSettings</param>
public EmaCrossUniverseSelectionModel(
int fastPeriod = 100,
int slowPeriod = 300,
int universeCount = 500,
UniverseSettings universeSettings = null,
ISecurityInitializer securityInitializer = null)
: base(false, universeSettings, securityInitializer)
UniverseSettings universeSettings = null)
: base(false, universeSettings)
{
_fastPeriod = fastPeriod;
_slowPeriod = slowPeriod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ def __init__(self,
fastPeriod = 100,
slowPeriod = 300,
universeCount = 500,
universeSettings = None,
securityInitializer = None):
universeSettings = None):
'''Initializes a new instance of the EmaCrossUniverseSelectionModel class
Args:
fastPeriod: Fast EMA period
slowPeriod: Slow EMA period
universeCount: Maximum number of members of this universe selection
universeSettings: The settings used when adding symbols to the algorithm, specify null to use algorthm.UniverseSettings
securityInitializer: Optional security initializer invoked when creating new securities, specify null to use algorithm.SecurityInitializer'''
super().__init__(False, universeSettings, securityInitializer)
universeSettings: The settings used when adding symbols to the algorithm, specify null to use algorthm.UniverseSettings'''
super().__init__(False, universeSettings)
self.fastPeriod = fastPeriod
self.slowPeriod = slowPeriod
self.universeCount = universeCount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,11 @@ public class FineFundamentalUniverseSelectionModel : FundamentalUniverseSelectio
/// <param name="coarseSelector">Selects symbols from the provided coarse data set</param>
/// <param name="fineSelector">Selects symbols from the provided fine data set (this set has already been filtered according to the coarse selection)</param>
/// <param name="universeSettings">Universe settings define attributes of created subscriptions, such as their resolution and the minimum time in universe before they can be removed</param>
/// <param name="securityInitializer">Performs extra initialization (such as setting models) after we create a new security object</param>
public FineFundamentalUniverseSelectionModel(
Func<IEnumerable<CoarseFundamental>, IEnumerable<Symbol>> coarseSelector,
Func<IEnumerable<FineFundamental>, IEnumerable<Symbol>> fineSelector,
UniverseSettings universeSettings = null,
ISecurityInitializer securityInitializer = null
)
: base(true, universeSettings, securityInitializer)
UniverseSettings universeSettings = null)
: base(true, universeSettings)
{
_coarseSelector = coarseSelector;
_fineSelector = fineSelector;
Expand All @@ -55,14 +52,12 @@ public FineFundamentalUniverseSelectionModel(
/// <param name="coarseSelector">Selects symbols from the provided coarse data set</param>
/// <param name="fineSelector">Selects symbols from the provided fine data set (this set has already been filtered according to the coarse selection)</param>
/// <param name="universeSettings">Universe settings define attributes of created subscriptions, such as their resolution and the minimum time in universe before they can be removed</param>
/// <param name="securityInitializer">Performs extra initialization (such as setting models) after we create a new security object</param>
public FineFundamentalUniverseSelectionModel(
PyObject coarseSelector,
PyObject fineSelector,
UniverseSettings universeSettings = null,
ISecurityInitializer securityInitializer = null
UniverseSettings universeSettings = null
)
: base(true, universeSettings, securityInitializer)
: base(true, universeSettings)
{
Func<IEnumerable<FineFundamental>, object> fineFunc;
Func<IEnumerable<CoarseFundamental>, object> coarseFunc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,25 @@ public abstract class FundamentalUniverseSelectionModel : UniverseSelectionModel
{
private readonly bool _filterFineData;
private readonly UniverseSettings _universeSettings;
private readonly ISecurityInitializer _securityInitializer;

/// <summary>
/// Initializes a new instance of the <see cref="FundamentalUniverseSelectionModel"/> class
/// </summary>
/// <param name="filterFineData">True to also filter using fine fundamental data, false to only filter on coarse data</param>
protected FundamentalUniverseSelectionModel(bool filterFineData)
: this(filterFineData, null, null)
: this(filterFineData, null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="FundamentalUniverseSelectionModel"/> class
/// </summary>
/// <param name="filterFineData">True to also filter using fine fundamental data, false to only filter on coarse data</param>
/// <param name="universeSettings">The settings used when adding symbols to the algorithm, specify null to use algorthm.UniverseSettings</param>
/// <param name="securityInitializer">Optional security initializer invoked when creating new securities, specify null to use algorithm.SecurityInitializer</param>
protected FundamentalUniverseSelectionModel(bool filterFineData, UniverseSettings universeSettings, ISecurityInitializer securityInitializer)
/// <param name="universeSettings">The settings used when adding symbols to the algorithm, specify null to use algorithm.UniverseSettings</param>
protected FundamentalUniverseSelectionModel(bool filterFineData, UniverseSettings universeSettings)
{
_filterFineData = filterFineData;
_universeSettings = universeSettings;
_securityInitializer = securityInitializer;
}

/// <summary>
Expand All @@ -77,8 +74,7 @@ public override IEnumerable<Universe> CreateUniverses(QCAlgorithm algorithm)
public virtual Universe CreateCoarseFundamentalUniverse(QCAlgorithm algorithm)
{
var universeSettings = _universeSettings ?? algorithm.UniverseSettings;
var securityInitializer = _securityInitializer ?? algorithm.SecurityInitializer;
return new CoarseFundamentalUniverse(universeSettings, securityInitializer, coarse =>
return new CoarseFundamentalUniverse(universeSettings, coarse =>
{
// if we're using fine fundamental selection than exclude symbols without fine data
if (_filterFineData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ class FundamentalUniverseSelectionModel:

def __init__(self,
filterFineData,
universeSettings = None,
securityInitializer = None):
universeSettings = None):
'''Initializes a new instance of the FundamentalUniverseSelectionModel class
Args:
filterFineData: True to also filter using fine fundamental data, false to only filter on coarse data
universeSettings: The settings used when adding symbols to the algorithm, specify null to use algorthm.UniverseSettings
securityInitializer: Optional security initializer invoked when creating new securities, specify null to use algorithm.SecurityInitializer'''
universeSettings: The settings used when adding symbols to the algorithm, specify null to use algorthm.UniverseSettings'''
self.filterFineData = filterFineData
self.universeSettings = universeSettings
self.securityInitializer = securityInitializer


def CreateUniverses(self, algorithm):
Expand All @@ -55,8 +52,7 @@ def CreateCoarseFundamentalUniverse(self, algorithm):
Returns:
The coarse fundamental universe'''
universeSettings = algorithm.UniverseSettings if self.universeSettings is None else self.universeSettings
securityInitializer = algorithm.SecurityInitializer if self.securityInitializer is None else self.securityInitializer
return CoarseFundamentalUniverse(universeSettings, securityInitializer, lambda coarse: self.FilteredSelectCoarse(algorithm, coarse))
return CoarseFundamentalUniverse(universeSettings, lambda coarse: self.FilteredSelectCoarse(algorithm, coarse))


def FilteredSelectCoarse(self, algorithm, coarse):
Expand Down
37 changes: 0 additions & 37 deletions Algorithm.Framework/Selection/FutureUniverseSelectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,6 @@ public FutureUniverseSelectionModel(TimeSpan refreshInterval, Func<DateTime, IEn
{
}

/// <summary>
/// Creates a new instance of <see cref="FutureUniverseSelectionModel"/>
/// </summary>
/// <param name="refreshInterval">Time interval between universe refreshes</param>
/// <param name="futureChainSymbolSelector">Selects symbols from the provided future chain</param>
/// <param name="universeSettings">Universe settings define attributes of created subscriptions, such as their resolution and the minimum time in universe before they can be removed</param>
/// <param name="securityInitializer">Performs extra initialization (such as setting models) after we create a new security object</param>
[Obsolete("This constructor is obsolete because SecurityInitializer is obsolete and will not be used.")]
public FutureUniverseSelectionModel(
TimeSpan refreshInterval,
Func<DateTime, IEnumerable<Symbol>> futureChainSymbolSelector,
UniverseSettings universeSettings,
ISecurityInitializer securityInitializer
)
: this(refreshInterval, futureChainSymbolSelector, universeSettings)
{

}

/// <summary>
/// Creates a new instance of <see cref="FutureUniverseSelectionModel"/>
/// </summary>
Expand Down Expand Up @@ -113,24 +94,6 @@ public override IEnumerable<Universe> CreateUniverses(QCAlgorithm algorithm)
}
}

/// <summary>
/// Creates the canonical <see cref="Future"/> chain security for a given symbol
/// </summary>
/// <param name="algorithm">The algorithm instance to create universes for</param>
/// <param name="symbol">Symbol of the future</param>
/// <param name="settings">Universe settings define attributes of created subscriptions, such as their resolution and the minimum time in universe before they can be removed</param>
/// <param name="initializer">Performs extra initialization (such as setting models) after we create a new security object</param>
/// <returns><see cref="Future"/> for the given symbol</returns>
[Obsolete("This method is obsolete because SecurityInitializer is obsolete and will not be used.")]
protected virtual Future CreateFutureChainSecurity(QCAlgorithm algorithm, Symbol symbol, UniverseSettings settings, ISecurityInitializer initializer)
{
return CreateFutureChainSecurity(
algorithm.SubscriptionManager.SubscriptionDataConfigService,
symbol,
settings,
algorithm.Securities);
}

/// <summary>
/// Creates the canonical <see cref="Future"/> chain security for a given symbol
/// </summary>
Expand Down
15 changes: 5 additions & 10 deletions Algorithm.Framework/Selection/FutureUniverseSelectionModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,17 @@ class FutureUniverseSelectionModel(UniverseSelectionModel):
def __init__(self,
refreshInterval,
futureChainSymbolSelector,
universeSettings = None,
securityInitializer = None):
universeSettings = None):
'''Creates a new instance of FutureUniverseSelectionModel
Args:
refreshInterval: Time interval between universe refreshes</param>
futureChainSymbolSelector: Selects symbols from the provided future chain
universeSettings: Universe settings define attributes of created subscriptions, such as their resolution and the minimum time in universe before they can be removed
securityInitializer: [Obsolete, will not be used] Performs extra initialization (such as setting models) after we create a new security object'''
universeSettings: Universe settings define attributes of created subscriptions, such as their resolution and the minimum time in universe before they can be removed'''
self.nextRefreshTimeUtc = datetime.min

self.refreshInterval = refreshInterval
self.futureChainSymbolSelector = futureChainSymbolSelector
self.universeSettings = universeSettings
self.securityInitializer = securityInitializer

def GetNextRefreshTimeUtc(self):
'''Gets the next time the framework should invoke the `CreateUniverses` method to refresh the set of universes.'''
Expand Down Expand Up @@ -83,11 +80,10 @@ def CreateFutureChain(self, algorithm, symbol):

# resolve defaults if not specified
settings = self.universeSettings if self.universeSettings is not None else algorithm.UniverseSettings
initializer = self.securityInitializer if self.securityInitializer is not None else algorithm.SecurityInitializer
# create canonical security object, but don't duplicate if it already exists
securities = [s for s in algorithm.Securities if s.Key == symbol]
if len(securities) == 0:
futureChain = self.CreateFutureChainSecurity(algorithm, symbol, settings, initializer)
futureChain = self.CreateFutureChainSecurity(algorithm, symbol, settings)
else:
futureChain = securities[0]

Expand All @@ -97,15 +93,14 @@ def CreateFutureChain(self, algorithm, symbol):
# force future chain security to not be directly tradable AFTER it's configured to ensure it's not overwritten
futureChain.IsTradable = False

return FuturesChainUniverse(futureChain, settings, algorithm.SubscriptionManager, initializer)
return FuturesChainUniverse(futureChain, settings)

def CreateFutureChainSecurity(self, algorithm, symbol, settings, initializer):
def CreateFutureChainSecurity(self, algorithm, symbol, settings):
'''Creates the canonical Future chain security for a given symbol
Args:
algorithm: The algorithm instance to create universes for
symbol: Symbol of the future
settings: Universe settings define attributes of created subscriptions, such as their resolution and the minimum time in universe before they can be removed
initializer: [Obsolete, will not be used] Performs extra initialization (such as setting models) after we create a new security object
Returns
Future for the given symbol'''
config = algorithm.SubscriptionManager.SubscriptionDataConfigService.Add(typeof(ZipEntryName),
Expand Down
Loading

0 comments on commit 41f9d9d

Please sign in to comment.