diff --git a/Common/Securities/SecurityCache.cs b/Common/Securities/SecurityCache.cs index 3db072e591c9..e66ab017110c 100644 --- a/Common/Securities/SecurityCache.cs +++ b/Common/Securities/SecurityCache.cs @@ -20,8 +20,6 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using QuantConnect.Python; -using System.Reflection; -using Python.Runtime; using QuantConnect.Data.Fundamental; using QuantConnect.Data.UniverseSelection; using QuantConnect.Util; @@ -230,38 +228,6 @@ protected virtual void ProcessDataPoint(BaseData data, bool cacheByType) return; } - var pythonData = data as PythonData; - if (pythonData != null && (_lastQuoteBarUpdate != data.EndTime || _lastOHLCUpdate != data.EndTime) && isDefaultDataType) - { - // Get matching pythonData and IBar properties - IDictionary storage = pythonData.GetStorageDictionary(); - PropertyInfo[] barProperties = typeof(IBar).GetProperties(); - List fieldsRequired = barProperties.Select(property => property.Name.ToLowerInvariant()).ToList(); - var matches = storage.Where(kvp => fieldsRequired.Contains(kvp.Key) && kvp.Value != null) ; - - IDictionary validOHLC = new Dictionary(); - - // Convert OHLC to decimal & update properties - if (matches.Count() == fieldsRequired.Count) - { - foreach (KeyValuePair match in matches) - { - decimal.TryParse(match.Value.ToString(), out decimal result); - validOHLC.Add(match.Key, result); - } - _lastOHLCUpdate = data.EndTime; - if (validOHLC["open"] != 0) Open = validOHLC["open"]; - if (validOHLC["high"] != 0) High = validOHLC["high"]; - if (validOHLC["low"] != 0) Low = validOHLC["low"]; - if (validOHLC["close"] != 0) - { - Price = validOHLC["close"]; - Close = validOHLC["close"]; - } - return; - } - } - var bar = data as IBar; if (bar != null) { @@ -303,6 +269,36 @@ protected virtual void ProcessDataPoint(BaseData data, bool cacheByType) Price = data.Price; } } + + var pythonData = data as PythonData; + if (pythonData != null && (_lastQuoteBarUpdate != data.EndTime || _lastOHLCUpdate != data.EndTime) && isDefaultDataType) + { + IDictionary storage = pythonData.GetStorageDictionary(); + List validFields = new List { "open", "high", "low", "close", "volume" }; + IDictionary validOHLC = new Dictionary(); + + foreach (string fieldName in validFields) + { + if (!storage.ContainsKey(fieldName)) + validOHLC.Add(fieldName, 0); + else + { + string match = storage[fieldName].ToString(); + decimal.TryParse(match, out decimal result); + validOHLC.Add(fieldName, result); + } + } + _lastOHLCUpdate = data.EndTime; + if (validOHLC["open"] != 0) Open = validOHLC["open"]; + if (validOHLC["high"] != 0) High = validOHLC["high"]; + if (validOHLC["low"] != 0) Low = validOHLC["low"]; + if (validOHLC["volume"] != 0) Volume = validOHLC["volume"]; + if (validOHLC["close"] != 0) + { + Price = validOHLC["close"]; + Close = validOHLC["close"]; + } + } } /// diff --git a/Tests/Common/Securities/SecurityCacheTests.cs b/Tests/Common/Securities/SecurityCacheTests.cs index a61193087794..b66a4aafaf8f 100644 --- a/Tests/Common/Securities/SecurityCacheTests.cs +++ b/Tests/Common/Securities/SecurityCacheTests.cs @@ -380,6 +380,7 @@ def Reader(self, config, line, date, isLiveMode): result.High = 4.1 result.low = 2.0 result.close = 3.7 + result.volume = 33 result.Time = datetime.strptime(""2022-05-05"", ""%Y-%m-%d"") return result"); @@ -392,6 +393,7 @@ def Reader(self, config, line, date, isLiveMode): Assert.AreEqual(3.7, securityCache.Close); Assert.AreEqual(2.0, securityCache.Low); Assert.AreEqual(3.1, securityCache.Open); + Assert.AreEqual(33, securityCache.Volume); testModule = PyModule.FromString("testModule", @" @@ -406,6 +408,7 @@ def Reader(self, config, line, date, isLiveMode): result.High = 4 result.low = 2.0 result.Close = ""test"" + result.volume = 0 result.Time = datetime.strptime(""2022-05-05"", ""%Y-%m-%d"") return result"); @@ -418,6 +421,7 @@ def Reader(self, config, line, date, isLiveMode): Assert.AreEqual(0, securityCache.Close); Assert.AreEqual(2.0, securityCache.Low); Assert.AreEqual(3.1, securityCache.Open); + Assert.AreEqual(0, securityCache.Volume); } }