public void ManualUniverseSelectionModelCanBeInherited()
        {
            var code = @"
from clr import AddReference
AddReference('QuantConnect.Common')

from QuantConnect import Market, SecurityType, Symbol
from Selection.ManualUniverseSelectionModel import ManualUniverseSelectionModel

class MockUniverseSelectionModel(ManualUniverseSelectionModel):
    def __init__(self):
        super().__init__([Symbol.Create('SPY', SecurityType.Equity, Market.USA)])";

            using (Py.GIL())
            {
                dynamic pyModel = PythonEngine
                                  .ModuleFromString(Guid.NewGuid().ToString(), code)
                                  .GetAttr("MockUniverseSelectionModel");

                var model = new UniverseSelectionModelPythonWrapper(pyModel());

                var universes = model.CreateUniverses(new QCAlgorithm()).ToList();
                Assert.AreEqual(1, universes.Count);

                var universe = universes.First();
                var symbols  = universe.SelectSymbols(DateTime.Now, null).ToList();
                Assert.AreEqual(1, symbols.Count);

                var expected = Symbol.Create("SPY", SecurityType.Equity, Market.USA);
                var symbol   = symbols.First();
                Assert.AreEqual(expected, symbol);
            }
        }
        public void FundamentalUniverseSelectionModelCanBeInherited()
        {
            var code = @"
from AlgorithmImports import *
from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel

class MockUniverseSelectionModel(FundamentalUniverseSelectionModel):
    def __init__(self):
        super().__init__(False)
    def SelectCoarse(self, algorithm, coarse):
        return [Symbol.Create('SPY', SecurityType.Equity, Market.USA)]";

            using (Py.GIL())
            {
                dynamic pyModel = PythonEngine
                                  .ModuleFromString(Guid.NewGuid().ToString(), code)
                                  .GetAttr("MockUniverseSelectionModel");

                var model = new UniverseSelectionModelPythonWrapper(pyModel());

                var universes = model.CreateUniverses(new QCAlgorithm()).ToList();
                Assert.AreEqual(1, universes.Count);

                var data = new BaseDataCollection();
                data.Data.Add(new CoarseFundamental());

                var universe = universes.First();
                var symbols  = universe.SelectSymbols(DateTime.Now, data).ToList();
                Assert.AreEqual(1, symbols.Count);

                var expected = Symbol.Create("SPY", SecurityType.Equity, Market.USA);
                var symbol   = symbols.First();
                Assert.AreEqual(expected, symbol);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Adds a new universe selection model
        /// </summary>
        /// <param name="universeSelection">Model defining universes for the algorithm to add</param>
        public void AddUniverseSelection(PyObject universeSelection)
        {
            IUniverseSelectionModel model;

            if (!universeSelection.TryConvert(out model))
            {
                model = new UniverseSelectionModelPythonWrapper(universeSelection);
            }
            AddUniverseSelection(model);
        }
        /// <summary>
        /// Sets the universe selection model
        /// </summary>
        /// <param name="portfolioSelection">Model defining universes for the algorithm</param>
        public void SetUniverseSelection(PyObject portfolioSelection)
        {
            IUniverseSelectionModel model;

            if (portfolioSelection.TryConvert(out model))
            {
                SetUniverseSelection(model);
            }
            else
            {
                UniverseSelection = new UniverseSelectionModelPythonWrapper(portfolioSelection);
            }
        }