Пример #1
0
        /// <summary>
        /// Get the output zip file
        /// </summary>
        /// <param name="baseDirectory">Base output directory for the zip file</param>
        /// <param name="time">Date/time for the data we're writing</param>
        /// <returns></returns>
        private string GetZipOutputFileName(string baseDirectory, DateTime time)
        {
            string file;

            // Further determine path based on the remaining data: security type
            switch (_securityType)
            {
            case SecurityType.Equity:
            case SecurityType.Forex:
            case SecurityType.Cfd:
                // Base directory includes the market
                if (_resolution == Resolution.Daily || _resolution == Resolution.Hour)
                {
                    file = Path.Combine(baseDirectory, _resolution.ToString().ToLower(), Compression.CreateZipFileName(_symbol, _securityType, time, _resolution));
                }
                else
                {
                    file = Path.Combine(baseDirectory, _resolution.ToString().ToLower(), _symbol.ToLower(), Compression.CreateZipFileName(_symbol, _securityType, time, _resolution));
                }
                break;

            default:
                throw new Exception("Sorry this security type is not yet supported by the LEAN data writer: " + _securityType);
            }

            return(file);
        }
Пример #2
0
        /// <summary>
        /// Enumerate over the tick zip file and return a list of BaseData.
        /// </summary>
        /// <returns>IEnumerable of ticks</returns>
        public IEnumerable <BaseData> Parse()
        {
            if (!File.Exists(_zipPath))
            {
                Log.Error($"LeanDataReader.Parse(): File does not exist: {_zipPath}");
                yield break;
            }

            var factory = (BaseData)ObjectActivator.GetActivator(_config.Type).Invoke(new object[0]);

            if (_config.Type.ImplementsStreamReader())
            {
                using (var zip = new ZipFile(_zipPath))
                {
                    foreach (var zipEntry in zip.Where(x => _zipentry == null || string.Equals(x.FileName, _zipentry, StringComparison.OrdinalIgnoreCase)))
                    {
                        // we get the contract symbol from the zip entry if not already provided with the zip entry
                        var symbol = _config.Symbol;
                        if (_zipentry == null && (_config.SecurityType == SecurityType.Future || _config.SecurityType.IsOption()))
                        {
                            symbol = LeanData.ReadSymbolFromZipEntry(_config.Symbol, _config.Resolution, zipEntry.FileName);
                        }
                        using (var entryReader = new StreamReader(zipEntry.OpenReader()))
                        {
                            while (!entryReader.EndOfStream)
                            {
                                var dataPoint = factory.Reader(_config, entryReader, _date, false);
                                dataPoint.Symbol = symbol;
                                yield return(dataPoint);
                            }
                        }
                    }
                }
            }
            // for futures and options if no entry was provided we just read all
            else if (_zipentry == null && (_config.SecurityType == SecurityType.Future || _config.SecurityType.IsOption()))
            {
                foreach (var entries in Compression.Unzip(_zipPath))
                {
                    // we get the contract symbol from the zip entry
                    var symbol = LeanData.ReadSymbolFromZipEntry(_config.Symbol, _config.Resolution, entries.Key);
                    foreach (var line in entries.Value)
                    {
                        var dataPoint = factory.Reader(_config, line, _date, false);
                        dataPoint.Symbol = symbol;
                        yield return(dataPoint);
                    }
                }
            }
            else
            {
                ZipFile zipFile;
                using (var unzipped = Compression.Unzip(_zipPath, _zipentry, out zipFile))
                {
                    if (unzipped == null)
                    {
                        yield break;
                    }
                    string line;
                    while ((line = unzipped.ReadLine()) != null)
                    {
                        yield return(factory.Reader(_config, line, _date, false));
                    }
                }
                zipFile.Dispose();
            }
        }