|
17 | 17 | using System.Collections.Generic;
|
18 | 18 | using System.Linq;
|
19 | 19 | using Newtonsoft.Json.Linq;
|
| 20 | +using NodaTime; |
20 | 21 | using NUnit.Framework;
|
| 22 | +using Python.Runtime; |
21 | 23 | using QuantConnect.Algorithm.CSharp;
|
22 | 24 | using QuantConnect.Data;
|
23 | 25 | using QuantConnect.Data.Fundamental;
|
24 | 26 | using QuantConnect.Data.Market;
|
25 | 27 | using QuantConnect.Data.UniverseSelection;
|
| 28 | +using QuantConnect.Python; |
26 | 29 | using QuantConnect.Securities;
|
27 | 30 | using QuantConnect.Tests.Common.Data.Fundamental;
|
28 | 31 |
|
@@ -359,6 +362,65 @@ public void AddDataEquity_OHLC_IgnoresQuoteBar()
|
359 | 362 | Assert.AreEqual(110, securityCache.BidSize);
|
360 | 363 | }
|
361 | 364 |
|
| 365 | + [Test] |
| 366 | + public void AddDataEquity_OHLC_PythonData() |
| 367 | + { |
| 368 | + using (Py.GIL()) |
| 369 | + { |
| 370 | + dynamic testModule = PyModule.FromString("testModule", |
| 371 | + @" |
| 372 | +from AlgorithmImports import * |
| 373 | +
|
| 374 | +class CustomDataTest(PythonData): |
| 375 | + def Reader(self, config, line, date, isLiveMode): |
| 376 | + result = CustomDataTest() |
| 377 | + result.Symbol = config.Symbol |
| 378 | + result.Value = 3.7 |
| 379 | + result.Open = 3.1 |
| 380 | + result.High = 4.1 |
| 381 | + result.low = 2.0 |
| 382 | + result.close = 3.7 |
| 383 | + result.Time = datetime.strptime(""2022-05-05"", ""%Y-%m-%d"") |
| 384 | + return result"); |
| 385 | + |
| 386 | + var data = GetDataFromModule(testModule); |
| 387 | + |
| 388 | + var securityCache = new SecurityCache(); |
| 389 | + securityCache.AddData(data); |
| 390 | + |
| 391 | + Assert.AreEqual(4.1, securityCache.High); |
| 392 | + Assert.AreEqual(3.7, securityCache.Close); |
| 393 | + Assert.AreEqual(2.0, securityCache.Low); |
| 394 | + Assert.AreEqual(3.1, securityCache.Open); |
| 395 | + |
| 396 | + testModule = PyModule.FromString("testModule", |
| 397 | + @" |
| 398 | +from AlgorithmImports import * |
| 399 | +
|
| 400 | +class CustomDataTest(PythonData): |
| 401 | + def Reader(self, config, line, date, isLiveMode): |
| 402 | + result = CustomDataTest() |
| 403 | + result.Symbol = config.Symbol |
| 404 | + result.Value = 3.7 |
| 405 | + result.Open = 3.1 |
| 406 | + result.High = 4 |
| 407 | + result.low = 2.0 |
| 408 | + result.Close = ""test"" |
| 409 | + result.Time = datetime.strptime(""2022-05-05"", ""%Y-%m-%d"") |
| 410 | + return result"); |
| 411 | + |
| 412 | + data = GetDataFromModule(testModule); |
| 413 | + |
| 414 | + securityCache.Reset(); |
| 415 | + securityCache.AddData(data); |
| 416 | + |
| 417 | + Assert.AreEqual(4, securityCache.High); |
| 418 | + Assert.AreEqual(0, securityCache.Close); |
| 419 | + Assert.AreEqual(2.0, securityCache.Low); |
| 420 | + Assert.AreEqual(3.1, securityCache.Open); |
| 421 | + } |
| 422 | + } |
| 423 | + |
362 | 424 | [Test]
|
363 | 425 | [TestCaseSource(nameof(GetSecurityCacheInitialStates))]
|
364 | 426 | public void AddDataWithSameEndTime_SetsQuoteBarValues(SecurityCache cache, SecuritySeedData seedType)
|
@@ -456,6 +518,15 @@ public void GetAllData_ReturnsListOfDataOnTargetCache()
|
456 | 518 | Assert.AreEqual(2m, data[1].Ask);
|
457 | 519 | }
|
458 | 520 |
|
| 521 | + private static BaseData GetDataFromModule(dynamic testModule) |
| 522 | + { |
| 523 | + var type = Extensions.CreateType(testModule.GetAttr("CustomDataTest")); |
| 524 | + var customDataTest = new PythonData(testModule.GetAttr("CustomDataTest")()); |
| 525 | + var config = new SubscriptionDataConfig(type, Symbols.SPY, Resolution.Daily, DateTimeZone.Utc, |
| 526 | + DateTimeZone.Utc, false, false, false, isCustom: true); |
| 527 | + return customDataTest.Reader(config, "something", DateTime.UtcNow, false); |
| 528 | + } |
| 529 | + |
459 | 530 | private void AddDataAndAssertChanges(SecurityCache cache, SecuritySeedData seedType, SecuritySeedData dataType, BaseData data, Dictionary<string, string> cacheToBaseDataPropertyMap = null)
|
460 | 531 | {
|
461 | 532 | var before = JObject.FromObject(cache);
|
|
0 commit comments