|
20 | 20 | using QuantConnect.Data; |
21 | 21 | using QuantConnect.ToolBox.RandomDataGenerator; |
22 | 22 | using QuantConnect.Data.Market; |
| 23 | +using QuantConnect.Data.UniverseSelection; |
23 | 24 | using QuantConnect.Lean.Engine.DataFeeds.Enumerators; |
24 | 25 | using QuantConnect.Configuration; |
25 | 26 | using QuantConnect.Data.Auxiliary; |
@@ -222,6 +223,116 @@ public void RandomDataGeneratorCompletesSuccessfully() |
222 | 223 | } |
223 | 224 | } |
224 | 225 |
|
| 226 | + [TestCase(SecurityType.Option, Market.USA, "AAPL", Resolution.Minute)] |
| 227 | + [TestCase(SecurityType.Future, Market.CME, "ES", Resolution.Minute)] |
| 228 | + [TestCase(SecurityType.Future, Market.CME, "ES", Resolution.Hour)] |
| 229 | + [TestCase(SecurityType.Future, Market.CME, "ES", Resolution.Daily)] |
| 230 | + [TestCase(SecurityType.Future, Market.CME, "ES", Resolution.Tick)] |
| 231 | + public void RandomDataGeneratorWritesDerivativeUniverseFiles(SecurityType securityType, string market, string ticker, Resolution resolution) |
| 232 | + { |
| 233 | + var tempFolder = Path.Combine(Path.GetTempPath(), $"LeanTest_{Guid.NewGuid()}"); |
| 234 | + var originalDataFolder = Config.Get("data-folder"); |
| 235 | + try |
| 236 | + { |
| 237 | + Directory.CreateDirectory(tempFolder); |
| 238 | + Config.Set("data-folder", tempFolder); |
| 239 | + Globals.Reset(); |
| 240 | + |
| 241 | + var settings = new RandomDataGeneratorSettings |
| 242 | + { |
| 243 | + Start = new DateTime(2020, 1, 6), |
| 244 | + End = new DateTime(2020, 1, 10), |
| 245 | + SymbolCount = 1, |
| 246 | + Market = market, |
| 247 | + SecurityType = securityType, |
| 248 | + Resolution = resolution, |
| 249 | + // keep the minute option case fast, the option price model is expensive |
| 250 | + DataDensity = resolution == Resolution.Minute ? DataDensity.Sparse : DataDensity.Dense, |
| 251 | + IncludeCoarse = false, |
| 252 | + QuoteTradeRatio = 1.0, |
| 253 | + RandomSeed = 123456, |
| 254 | + RandomSeedSet = true, |
| 255 | + ChainSymbolCount = 2, |
| 256 | + OptionPriceEngineName = "BaroneAdesiWhaleyApproximationEngine", |
| 257 | + Tickers = new List<string>() { ticker } |
| 258 | + }; |
| 259 | + |
| 260 | + var generator = GetGenerator(settings); |
| 261 | + Assert.DoesNotThrow(() => generator.Run()); |
| 262 | + |
| 263 | + var canonical = securityType == SecurityType.Future |
| 264 | + ? Symbol.Create(ticker, SecurityType.Future, market) |
| 265 | + : Symbol.CreateCanonicalOption(Symbol.Create(ticker, SecurityType.Equity, market)); |
| 266 | + var universeFiles = Directory.GetFiles(LeanData.GenerateUniversesDirectory(tempFolder, canonical), "*.csv"); |
| 267 | + Assert.IsNotEmpty(universeFiles); |
| 268 | + |
| 269 | + var config = new SubscriptionDataConfig(securityType == SecurityType.Future ? typeof(FutureUniverse) : typeof(OptionUniverse), |
| 270 | + canonical, Resolution.Daily, TimeZones.NewYork, TimeZones.NewYork, true, true, false); |
| 271 | + BaseChainUniverseData factory = securityType == SecurityType.Future ? new FutureUniverse() : new OptionUniverse(); |
| 272 | + var expectedHeader = securityType == SecurityType.Future ? FutureUniverse.CsvHeader : OptionUniverse.CsvHeader(securityType); |
| 273 | + var expectedContractsCount = securityType == SecurityType.Future ? 1 : settings.ChainSymbolCount * 2; |
| 274 | + var maxContractsCount = 0; |
| 275 | + var anyOpenInterest = false; |
| 276 | + var anyContractPrice = false; |
| 277 | + foreach (var universeFile in universeFiles) |
| 278 | + { |
| 279 | + var date = DateTime.ParseExact(Path.GetFileNameWithoutExtension(universeFile), DateFormat.EightCharacter, null); |
| 280 | + Assert.IsTrue(date >= settings.Start && date <= settings.End, universeFile); |
| 281 | + |
| 282 | + var lines = File.ReadAllLines(universeFile); |
| 283 | + Assert.AreEqual($"#{expectedHeader}", lines[0], universeFile); |
| 284 | + |
| 285 | + // make sure Lean can read them back |
| 286 | + var rows = new List<BaseChainUniverseData>(); |
| 287 | + using var reader = new StreamReader(universeFile); |
| 288 | + while (!reader.EndOfStream) |
| 289 | + { |
| 290 | + var data = (BaseChainUniverseData)factory.Reader(config, reader, date, false); |
| 291 | + if (data != null) |
| 292 | + { |
| 293 | + rows.Add(data); |
| 294 | + } |
| 295 | + } |
| 296 | + Assert.AreEqual(lines.Length - 1, rows.Count, universeFile); |
| 297 | + |
| 298 | + // options have an underlying data row first, then the contracts |
| 299 | + var contracts = rows.Where(x => x.Symbol.SecurityType == securityType).ToList(); |
| 300 | + Assert.AreEqual(securityType == SecurityType.Future ? 0 : 1, rows.Count - contracts.Count, universeFile); |
| 301 | + Assert.IsTrue(securityType == SecurityType.Future || rows[0].Symbol == canonical.Underlying, universeFile); |
| 302 | + // options warm up on the first underlying data points, so the first days might have no contracts |
| 303 | + Assert.LessOrEqual(contracts.Count, expectedContractsCount, universeFile); |
| 304 | + maxContractsCount = Math.Max(maxContractsCount, contracts.Count); |
| 305 | + |
| 306 | + foreach (var row in rows) |
| 307 | + { |
| 308 | + Assert.IsFalse(row.Symbol.IsCanonical(), universeFile); |
| 309 | + Assert.AreEqual(canonical.ID.Symbol, row.Symbol.ID.Symbol, universeFile); |
| 310 | + if (row.Symbol.SecurityType == securityType) |
| 311 | + { |
| 312 | + Assert.GreaterOrEqual(row.Symbol.ID.Date, date, universeFile); |
| 313 | + // the price model can price a contract at zero and open interest is generated once a day, starting the second day |
| 314 | + anyContractPrice |= row.Close > 0 && row.Volume > 0; |
| 315 | + anyOpenInterest |= row.OpenInterest > 0; |
| 316 | + } |
| 317 | + else |
| 318 | + { |
| 319 | + Assert.Greater(row.Close, 0, universeFile); |
| 320 | + Assert.Greater(row.Volume, 0, universeFile); |
| 321 | + } |
| 322 | + } |
| 323 | + } |
| 324 | + Assert.AreEqual(expectedContractsCount, maxContractsCount); |
| 325 | + Assert.IsTrue(anyContractPrice); |
| 326 | + Assert.IsTrue(anyOpenInterest); |
| 327 | + } |
| 328 | + finally |
| 329 | + { |
| 330 | + Config.Set("data-folder", originalDataFolder); |
| 331 | + Globals.Reset(); |
| 332 | + Directory.Delete(tempFolder, true); |
| 333 | + } |
| 334 | + } |
| 335 | + |
225 | 336 | private static QuantConnect.ToolBox.RandomDataGenerator.RandomDataGenerator GetGenerator(RandomDataGeneratorSettings settings) |
226 | 337 | { |
227 | 338 | var securityManager = new SecurityManager(new TimeKeeper(settings.Start, new[] { TimeZones.Utc })); |
|
0 commit comments