chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 13:02:50 +08:00
commit 0fc60fdcb1
5008 changed files with 910633 additions and 0 deletions
@@ -0,0 +1,198 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Linq;
using Python.Runtime;
using NUnit.Framework;
using QuantConnect.Algorithm;
using QuantConnect.Securities;
using System.Collections.Generic;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Algorithm.Framework.Selection;
using Moq;
namespace QuantConnect.Tests.Algorithm.Framework.Selection
{
[TestFixture]
public class ETFConstituentsUniverseSelectionModelTests
{
[TestCase("from Selection.ETFConstituentsUniverseSelectionModel import *", "Selection.ETFConstituentsUniverseSelectionModel.ETFConstituentsUniverseSelectionModel")]
[TestCase("from QuantConnect.Algorithm.Framework.Selection import *", "QuantConnect.Algorithm.Framework.Selection.ETFConstituentsUniverseSelectionModel")]
public void TestPythonAndCSharpImports(string importStatement, string expected)
{
using (Py.GIL())
{
dynamic module = PyModule.FromString("testModule",
@$"{importStatement}
class ETFConstituentsFrameworkAlgorithm(QCAlgorithm):
def initialize(self):
self.universe_settings.resolution = Resolution.DAILY
symbol = Symbol.create('SPY', SecurityType.EQUITY, Market.USA)
selection_model = ETFConstituentsUniverseSelectionModel(symbol, self.universe_settings, self.etf_constituents_filter)
self.universe_type = str(type(selection_model))
def etf_constituents_filter(self, constituents):
return [c.symbol for c in constituents]");
dynamic algorithm = module.GetAttr("ETFConstituentsFrameworkAlgorithm").Invoke();
algorithm.initialize();
string universeTypeStr = algorithm.universe_type.ToString();
Assert.IsTrue(universeTypeStr.Contains(expected, StringComparison.InvariantCulture));
}
}
[TestCase("'SPY'")]
[TestCase("'SPY', None")]
[TestCase("'SPY', None, None")]
[TestCase("'SPY', self.universe_settings")]
[TestCase("'SPY', self.universe_settings, None")]
[TestCase("'SPY', None, self.etf_constituents_filter")]
[TestCase("'SPY', self.universe_settings, self.etf_constituents_filter")]
[TestCase("Symbol.create('SPY', SecurityType.EQUITY, Market.USA)")]
[TestCase("Symbol.create('SPY', SecurityType.EQUITY, Market.USA), None, None")]
[TestCase("Symbol.create('SPY', SecurityType.EQUITY, Market.USA), self.universe_settings")]
[TestCase("Symbol.create('SPY', SecurityType.EQUITY, Market.USA), self.universe_settings, None")]
[TestCase("Symbol.create('SPY', SecurityType.EQUITY, Market.USA), None, self.etf_constituents_filter")]
[TestCase("Symbol.create('SPY', SecurityType.EQUITY, Market.USA), self.universe_settings, self.etf_constituents_filter")]
[TestCase("Symbol.create('SPY', SecurityType.EQUITY, Market.USA), universe_filter_func=self.etf_constituents_filter")]
public void ETFConstituentsUniverseSelectionModelWithVariousConstructor(string constructorParameters)
{
using (Py.GIL())
{
dynamic module = PyModule.FromString("testModule",
@$"from AlgorithmImports import *
from Selection.ETFConstituentsUniverseSelectionModel import *
class ETFConstituentsFrameworkAlgorithm(QCAlgorithm):
selection_model = None
def initialize(self):
self.universe_settings.resolution = Resolution.DAILY
self.selection_model = ETFConstituentsUniverseSelectionModel({constructorParameters})
def etf_constituents_filter(self, constituents):
return [c.symbol for c in constituents]");
dynamic algorithm = module.GetAttr("ETFConstituentsFrameworkAlgorithm").Invoke();
algorithm.initialize();
Assert.IsNotNull(algorithm.selection_model);
Assert.IsTrue(algorithm.selection_model.etf_symbol.GetPythonType().ToString().Contains($"{nameof(Symbol)}", StringComparison.InvariantCulture));
Assert.IsTrue(algorithm.selection_model.etf_symbol.ToString().Contains(Symbols.SPY, StringComparison.InvariantCulture));
}
}
[TestCase("TSLA")]
public void ETFConstituentsUniverseSelectionModelGetNoCachedSymbol(string ticker)
{
using (Py.GIL())
{
var etfAlgorithm = GetETFConstituentsFrameworkAlgorithm(ticker);
etfAlgorithm.initialize();
Assert.IsNotNull(etfAlgorithm.selection_model);
Assert.IsTrue(etfAlgorithm.selection_model.etf_symbol.GetPythonType().ToString().Contains($"{nameof(Symbol)}", StringComparison.InvariantCulture));
var etfSymbol = (Symbol)etfAlgorithm.selection_model.etf_symbol;
Assert.IsTrue(etfSymbol.Value.Contains(ticker, StringComparison.InvariantCulture));
Assert.IsTrue(etfSymbol.SecurityType == SecurityType.Equity);
}
}
[TestCase("SPY", "CACHED")]
public void ETFConstituentsUniverseSelectionModelGetCachedSymbol(string ticker, string expectedAlias)
{
using (Py.GIL())
{
var etfAlgorithm = GetETFConstituentsFrameworkAlgorithm(ticker);
etfAlgorithm.initialize();
Assert.IsNotNull(etfAlgorithm.selection_model);
Assert.IsTrue(etfAlgorithm.selection_model.etf_symbol.GetPythonType().ToString().Contains($"{nameof(Symbol)}", StringComparison.InvariantCulture));
var etfSymbol = (Symbol)etfAlgorithm.selection_model.etf_symbol;
Assert.IsTrue(etfSymbol.Value.Contains(expectedAlias, StringComparison.InvariantCulture));
Assert.IsTrue(etfSymbol.ID == Symbols.SPY.ID);
Assert.IsTrue(etfSymbol.SecurityType == SecurityType.Equity);
}
}
[Test]
public void ETFConstituentsUniverseSelectionModelTestAllConstructor()
{
int numberOfOperation = 0;
var ticker = "SPY";
var symbol = Symbol.Create(ticker, SecurityType.Equity, Market.USA);
var universeSettings = new UniverseSettings(Resolution.Minute, Security.NullLeverage, true, false, TimeSpan.FromDays(1));
do
{
ETFConstituentsUniverseSelectionModel etfConstituents = numberOfOperation switch
{
0 => new ETFConstituentsUniverseSelectionModel(ticker),
1 => new ETFConstituentsUniverseSelectionModel(ticker, universeSettings),
2 => new ETFConstituentsUniverseSelectionModel(ticker, ETFConstituentsFilter),
3 => new ETFConstituentsUniverseSelectionModel(ticker, universeSettings, ETFConstituentsFilter),
4 => new ETFConstituentsUniverseSelectionModel(ticker, universeSettings, default(PyObject)),
5 => new ETFConstituentsUniverseSelectionModel(symbol),
6 => new ETFConstituentsUniverseSelectionModel(symbol, universeSettings),
7 => new ETFConstituentsUniverseSelectionModel(symbol, ETFConstituentsFilter),
8 => new ETFConstituentsUniverseSelectionModel(symbol, universeSettings, ETFConstituentsFilter),
9 => new ETFConstituentsUniverseSelectionModel(symbol, universeSettings, default(PyObject)),
_ => throw new ArgumentException("Not recognize number of operation")
};
var universe = etfConstituents.CreateUniverses(new QCAlgorithm()).First();
Assert.IsNotNull(etfConstituents);
Assert.IsNotNull(universe);
Assert.IsTrue(universe.Configuration.Symbol.HasUnderlying);
Assert.AreEqual(symbol, universe.Configuration.Symbol.Underlying);
Assert.AreEqual(symbol.SecurityType, universe.Configuration.Symbol.SecurityType);
Assert.IsTrue(universe.Configuration.Symbol.ID.Symbol.StartsWithInvariant("qc-universe-"));
var data = new Mock<BaseDataCollection>();
Assert.DoesNotThrow(() => universe.PerformSelection(DateTime.UtcNow, data.Object));
} while (++numberOfOperation <= 9) ;
}
private IEnumerable<Symbol> ETFConstituentsFilter(IEnumerable<ETFConstituentUniverse> constituents)
{
return constituents.Select(c => c.Symbol);
}
private static dynamic GetETFConstituentsFrameworkAlgorithm(string etfTicker, string cachedAlias = "CACHED")
{
dynamic module = PyModule.FromString("testModule",
@$"from AlgorithmImports import *
from Selection.ETFConstituentsUniverseSelectionModel import *
class ETFConstituentsFrameworkAlgorithm(QCAlgorithm):
selection_model = None
def initialize(self):
SymbolCache.set('SPY', Symbol.create('SPY', SecurityType.EQUITY, Market.USA, '{cachedAlias}'))
self.universe_settings.resolution = Resolution.DAILY
self.selection_model = ETFConstituentsUniverseSelectionModel(""{etfTicker}"")"
);
dynamic algorithm = module.GetAttr("ETFConstituentsFrameworkAlgorithm").Invoke();
return algorithm;
}
}
}
@@ -0,0 +1,45 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Linq;
using NUnit.Framework;
using QuantConnect.Algorithm;
using QuantConnect.Algorithm.Framework.Selection;
namespace QuantConnect.Tests.Algorithm.Framework.Selection
{
[TestFixture]
public class ManualUniverseSelectionModelTests
{
[Test]
public void ExcludesCanonicalSymbols()
{
var symbols = new[]
{
Symbols.SPY,
Symbol.CreateOption(Symbols.SPY, Market.USA, default(OptionStyle), default(OptionRight), 0m, SecurityIdentifier.DefaultDate, "?SPY")
};
var model = new ManualUniverseSelectionModel(symbols);
var universe = model.CreateUniverses(new QCAlgorithm()).Single();
var selectedSymbols = universe.SelectSymbols(default(DateTime), null).ToList();
Assert.AreEqual(1, selectedSymbols.Count);
Assert.AreEqual(Symbols.SPY, selectedSymbols[0]);
Assert.IsFalse(selectedSymbols.Any(s => s.IsCanonical()));
}
}
}
@@ -0,0 +1,159 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using Moq;
using NodaTime;
using NUnit.Framework;
using QuantConnect.Algorithm.Framework.Selection;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Interfaces;
using QuantConnect.Securities;
namespace QuantConnect.Tests.Algorithm.Framework.Selection
{
[TestFixture]
public class OpenInterestFutureUniverseSelectionModelTests
{
private static readonly Symbol Jan = Symbol.CreateFuture(Futures.Metals.Gold, Market.COMEX, new DateTime(2020, 01, 01));
private static readonly Symbol Feb = Symbol.CreateFuture(Futures.Metals.Gold, Market.COMEX, new DateTime(2020, 02, 01));
private static readonly Symbol March = Symbol.CreateFuture(Futures.Metals.Gold, Market.COMEX, new DateTime(2020, 03, 01));
private static readonly Symbol April = Symbol.CreateFuture(Futures.Metals.Gold, Market.COMEX, new DateTime(2020, 04, 01));
private static readonly DateTime TestDate = new DateTime(2020, 05, 11, 0, 0, 0, DateTimeKind.Utc);
private static readonly DateTime ExpectedPreviousDate = new DateTime(2020, 05, 09, 20, 0, 0, DateTimeKind.Utc);
private static readonly IReadOnlyDictionary<Symbol, decimal> OpenInterestData = new Dictionary<Symbol, decimal>
{
[Jan] = 3,
[Feb] = 6,
[March] = 3, // Same as Jan.
[April] = 1
};
private static readonly MarketHoursDatabase.Entry MarketHours = MarketHoursDatabase.FromDataFolder().GetEntry(Jan.ID.Market, Jan, Jan.SecurityType);
private Mock<IHistoryProvider> _mockHistoryProvider;
private OpenInterestFutureUniverseSelectionModel _underTest;
[Test]
public void No_Open_Interest_Returns_Empty()
{
SetupSubject(OpenInterestData.Count, OpenInterestData.Count);
_mockHistoryProvider.Setup(x => x.GetHistory(It.IsAny<IEnumerable<HistoryRequest>>(), It.IsAny<DateTimeZone>()))
.Returns<IEnumerable<HistoryRequest>, DateTimeZone>((r, tz) => new Slice[0])
.Verifiable();
var data = OpenInterestData.Keys.ToDictionary(x => x, x => MarketHours);
var results = _underTest.FilterByOpenInterest(data).ToList();
_mockHistoryProvider.Verify();
Assert.IsEmpty(results);
}
[Test]
public void Can_Sort_By_Open_Interest()
{
SetupSubject(OpenInterestData.Count, OpenInterestData.Count);
_mockHistoryProvider.Setup(x => x.GetHistory(It.IsAny<IEnumerable<HistoryRequest>>(), It.IsAny<DateTimeZone>()))
.Returns<IEnumerable<HistoryRequest>, DateTimeZone>(
(r, tz) =>
{
var requests = r.ToList();
Assert.AreEqual(4, requests.Count);
var slices = new List<Slice>(requests.Count);
foreach (var request in requests)
{
Assert.NotNull(request.Symbol);
Assert.AreEqual(typeof(Tick), request.DataType);
Assert.AreEqual(DataNormalizationMode.Raw, request.DataNormalizationMode);
Assert.AreEqual(ExpectedPreviousDate, request.StartTimeUtc);
Assert.AreEqual(TestDate, request.EndTimeUtc);
Assert.AreEqual(Resolution.Tick, request.Resolution);
Assert.AreEqual(TickType.OpenInterest, request.TickType);
Assert.AreEqual(tz, MarketHours.ExchangeHours.TimeZone);
slices.Add(CreateReplySlice(request.Symbol, OpenInterestData[request.Symbol]));
}
return slices;
}
)
.Verifiable();
var data = OpenInterestData.Keys.ToDictionary(x => x, x => MarketHours);
var results = _underTest.FilterByOpenInterest(data).ToList();
// Results should be sorted by open interest (descending), and then by the date.
_mockHistoryProvider.Verify();
Assert.AreEqual(4, results.Count);
Assert.AreEqual(Feb, results[0]);
Assert.AreEqual(Jan, results[1]);
Assert.AreEqual(March, results[2]);
Assert.AreEqual(April, results[3]);
}
[Test]
public void Can_Limit_Number_Of_Contracts()
{
SetupSubject(6, 4);
var expected = Enumerable.Range(1, 4).Select(d => Symbol.CreateFuture(Futures.Metals.Gold, Market.COMEX, new DateTime(2020, 01, d))).ToList();
// Create 7 requests. Reverse the list so the order isn't correct, but remains consistent for tests.
var data = expected.Concat(Enumerable.Range(5, 3).Select(d => Symbol.CreateFuture(Futures.Metals.Gold, Market.COMEX, new DateTime(2020, 01, d))))
.Reverse()
.ToDictionary(x => x, _ => MarketHours);
// 7 input requests, but the look-up should be limited to only 6.
_mockHistoryProvider.Setup(x => x.GetHistory(It.IsAny<IEnumerable<HistoryRequest>>(), It.IsAny<DateTimeZone>()))
.Returns<IEnumerable<HistoryRequest>, DateTimeZone>((rq, tz) => rq.Select(r => CreateReplySlice(r.Symbol, 1)).ToArray());
// Run the test.
var results = _underTest.FilterByOpenInterest(data).ToList();
// Verify the chain limit was applied.
_mockHistoryProvider.Verify(x => x.GetHistory(It.Is<IEnumerable<HistoryRequest>>(r => r.Count() == 6), MarketHours.ExchangeHours.TimeZone), Times.Once);
// Verify the results.
CollectionAssert.AreEqual(expected, results);
}
[Test]
public void Limits_Do_Not_Need_To_Be_Provided()
{
SetupSubject(null, null);
var startDate = new DateTime(2020, 01, 01);
var items = Enumerable.Range(0, 100).ToDictionary(d => Symbol.CreateFuture(Futures.Metals.Gold, Market.COMEX, startDate.AddDays(d)), _ => MarketHours);
_mockHistoryProvider.Setup(x => x.GetHistory(It.IsAny<IEnumerable<HistoryRequest>>(), It.IsAny<DateTimeZone>()))
.Returns<IEnumerable<HistoryRequest>, DateTimeZone>((rq, tz) => rq.Select(r => CreateReplySlice(r.Symbol, 1)).ToArray());
var results = _underTest.FilterByOpenInterest(items).ToList();
_mockHistoryProvider.Verify(x => x.GetHistory(It.Is<IEnumerable<HistoryRequest>>(r => r.Count() == 100), MarketHours.ExchangeHours.TimeZone), Times.Once);
Assert.AreEqual(items.Keys, results);
}
private static Slice CreateReplySlice(Symbol symbol, decimal openInterest)
{
var ticks = new Ticks {{symbol, new List<Tick> {new OpenInterest(TestDate, symbol, openInterest)}}};
return new Slice(TestDate, null, null, null, ticks, null, null, null, null, null, null, null, TestDate, true);
}
private void SetupSubject(int? testChainContractLookupLimit, int? testResultsLimit)
{
_mockHistoryProvider = new Mock<IHistoryProvider>();
var mockAlgorithm = new Mock<IAlgorithm>();
mockAlgorithm.SetupGet(x => x.HistoryProvider).Returns(_mockHistoryProvider.Object);
mockAlgorithm.SetupGet(x => x.UtcTime).Returns(TestDate);
_underTest = new OpenInterestFutureUniverseSelectionModel(mockAlgorithm.Object, _ => OpenInterestData.Keys, testChainContractLookupLimit, testResultsLimit);
}
}
}
@@ -0,0 +1,265 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using NUnit.Framework;
using Python.Runtime;
using QuantConnect.Algorithm;
using QuantConnect.Algorithm.Framework.Selection;
using QuantConnect.Data.Fundamental;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Interfaces;
using QuantConnect.Tests.Common.Data.Fundamental;
using System;
using System.Collections.Generic;
using System.Linq;
using static QuantConnect.Data.UniverseSelection.CoarseFundamentalDataProvider;
namespace QuantConnect.Tests.Algorithm.Framework.Selection
{
[TestFixture]
public class QC500UniverseSelectionModelTests
{
private readonly Dictionary<char, string> _industryTemplateCodeDict =
new Dictionary<char, string>
{
{'0', "B"}, {'1', "I"}, {'2', "M"}, {'3', "N"}, {'4', "T"}, {'5', "U"}
};
private readonly List<Symbol> _symbols = Enumerable.Range(0, 6000)
.Select(x => Symbol.Create($"{x:0000}", SecurityType.Equity, Market.USA))
.ToList();
[TestCase(Language.CSharp)]
[TestCase(Language.Python)]
public void FiltersUniverseCorrectlyWithValidData(Language language)
{
QCAlgorithm algorithm;
Dictionary<DateTime, int> coarseCountByDateTime;
Dictionary<DateTime, int> fineCountByDateTime;
RunSimulation(language,
(symbol, time) => new CoarseFundamentalSource
{
Symbol = symbol,
EndTime = time,
Value = 100,
VolumeSetter = 1000,
DollarVolumeSetter = 100000 * double.Parse(symbol.Value.Substring(3)),
HasFundamentalDataSetter = true
},
(symbol, time) => new FineFundamental(time, symbol)
{
Value = 100
},
new TestFundamentalDataProvider(_industryTemplateCodeDict),
out algorithm,
out coarseCountByDateTime,
out fineCountByDateTime);
var months = (algorithm.EndDate - algorithm.StartDate).Days / 30;
// Universe Changed 4 times
Assert.AreEqual(months, coarseCountByDateTime.Count);
Assert.AreEqual(months, fineCountByDateTime.Count);
// Universe Changed on the 1st. Coarse returned 1000 and Fine 500
Assert.IsTrue(coarseCountByDateTime.All(kvp => kvp.Key.Day == 1 && kvp.Value == 1000));
Assert.IsTrue(fineCountByDateTime.All(kvp => kvp.Key.Day == 1 && kvp.Value == 500));
}
[TestCase(Language.CSharp)]
[TestCase(Language.Python)]
public void DoesNotFilterUniverseWithCoarseDataHasFundamentalFalse(Language language)
{
QCAlgorithm algorithm;
Dictionary<DateTime, int> coarseCountByDateTime;
Dictionary<DateTime, int> fineCountByDateTime;
RunSimulation(language,
(symbol, time) => new CoarseFundamentalSource
{
Symbol = symbol,
EndTime = time,
},
(symbol, time) => new FineFundamental(time, symbol),
new TestFundamentalDataProvider(_industryTemplateCodeDict),
out algorithm,
out coarseCountByDateTime,
out fineCountByDateTime); ;
// No Universe Changes
Assert.AreEqual(0, coarseCountByDateTime.Count);
Assert.AreEqual(0, fineCountByDateTime.Count);
}
[TestCase(Language.CSharp)]
[TestCase(Language.Python)]
public void DoesNotFilterUniverseWithInvalidFineData(Language language)
{
QCAlgorithm algorithm;
Dictionary<DateTime, int> coarseCountByDateTime;
Dictionary<DateTime, int> fineCountByDateTime;
RunSimulation(language,
(symbol, time) => new CoarseFundamentalSource
{
Symbol = symbol,
EndTime = time,
Value = 100,
VolumeSetter = 1000,
DollarVolumeSetter = 100000 * double.Parse(symbol.Value.Substring(3)),
HasFundamentalDataSetter = true
},
(symbol, time) => new FineFundamental(time, symbol)
{
Value = 100
},
new NullFundamentalDataProvider(),
out algorithm,
out coarseCountByDateTime,
out fineCountByDateTime);
// Coarse Fundamental called every day.
Assert.Greater(coarseCountByDateTime.Count, 4);
Assert.IsTrue(coarseCountByDateTime.All(kvp => kvp.Value == 1000));
// No Universe Changes
Assert.AreEqual(0, fineCountByDateTime.Count);
}
private void RunSimulation(Language language,
Func<Symbol, DateTime, CoarseFundamental> getCoarseFundamental,
Func<Symbol, DateTime, FineFundamental> getFineFundamental,
IFundamentalDataProvider fundamentalDataProvider,
out QCAlgorithm algorithm,
out Dictionary<DateTime, int> coarseCountByDateTime,
out Dictionary<DateTime, int> fineCountByDateTime)
{
algorithm = new QCAlgorithm();
algorithm.SetStartDate(2019, 10, 1);
algorithm.SetEndDate(2020, 1, 30);
algorithm.SetDateTime(algorithm.StartDate.AddHours(6));
FundamentalService.Initialize(TestGlobals.DataProvider, fundamentalDataProvider, false);
coarseCountByDateTime = new Dictionary<DateTime, int>();
fineCountByDateTime = new Dictionary<DateTime, int>();
Func<QCAlgorithm, IEnumerable<CoarseFundamental>, IEnumerable<Symbol>> SelectCoarse;
Func<QCAlgorithm, IEnumerable<FineFundamental>, IEnumerable<Symbol>> SelectFine;
GetUniverseSelectionModel(language, out SelectCoarse, out SelectFine);
while (algorithm.EndDate > algorithm.UtcTime)
{
var time = algorithm.UtcTime;
var coarse = _symbols.Select(x => getCoarseFundamental(x, time));
var selectSymbolsResult = SelectCoarse(algorithm, coarse);
if (!ReferenceEquals(selectSymbolsResult, Universe.Unchanged))
{
coarseCountByDateTime[time] = selectSymbolsResult.Count();
var fine = selectSymbolsResult.Select(x => getFineFundamental(x, time));
selectSymbolsResult = SelectFine(algorithm, fine);
if (!ReferenceEquals(selectSymbolsResult, Universe.Unchanged))
{
fineCountByDateTime[time] = selectSymbolsResult.Count();
}
}
algorithm.SetDateTime(time.AddDays(1));
}
}
private void GetUniverseSelectionModel(
Language language,
out Func<QCAlgorithm, IEnumerable<CoarseFundamental>, IEnumerable<Symbol>> SelectCoarse,
out Func<QCAlgorithm, IEnumerable<FineFundamental>, IEnumerable<Symbol>> SelectFine)
{
if (language == Language.CSharp)
{
var model = new QC500UniverseSelectionModel();
SelectCoarse = model.SelectCoarse;
SelectFine = model.SelectFine;
return;
}
using (Py.GIL())
{
var name = "QC500UniverseSelectionModel";
dynamic model = Py.Import(name).GetAttr(name).Invoke();
SelectCoarse = ConvertToUniverseSelectionSymbolDelegate<IEnumerable<CoarseFundamental>>(model.select_coarse);
SelectFine = ConvertToUniverseSelectionSymbolDelegate<IEnumerable<FineFundamental>>(model.select_fine);
}
}
public static Func<QCAlgorithm, T, IEnumerable<Symbol>> ConvertToUniverseSelectionSymbolDelegate<T>(PyObject pySelector)
{
Func<QCAlgorithm, T, object> selector;
pySelector.TrySafeAs(out selector);
return (algorithm, data) =>
{
var result = selector(algorithm, data);
return ReferenceEquals(result, Universe.Unchanged)
? Universe.Unchanged
: ((object[])result).Select(x => (Symbol)x);
};
}
private class TestFundamentalDataProvider : IFundamentalDataProvider
{
private readonly Dictionary<char, string> _industryTemplateCodeDict;
public TestFundamentalDataProvider(Dictionary<char, string> industryTemplateCodeDict)
{
_industryTemplateCodeDict = industryTemplateCodeDict;
}
public T Get<T>(DateTime time, SecurityIdentifier securityIdentifier, FundamentalProperty name)
{
if (securityIdentifier == SecurityIdentifier.Empty)
{
return default;
}
return Get(time, securityIdentifier, name);
}
private dynamic Get(DateTime time, SecurityIdentifier securityIdentifier, FundamentalProperty enumName)
{
var name = Enum.GetName(enumName);
switch (name)
{
case "CompanyProfile_MarketCap":
return 500000001;
case "SecurityReference_IPODate":
return time.AddDays(-200);
case "EarningReports_BasicAverageShares_ThreeMonths":
return 5000000.01d;
case "CompanyReference_CountryId":
return "USA";
case "CompanyReference_PrimaryExchangeID":
return "NYS";
case "CompanyReference_IndustryTemplateCode":
return _industryTemplateCodeDict[securityIdentifier.Symbol[0]];
}
return null;
}
public void Initialize(IDataProvider dataProvider, bool liveMode)
{
}
}
}
}