/* * 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.IO; using QuantConnect.Python; using QuantConnect.Securities; using QuantConnect.Util; namespace QuantConnect.Data.UniverseSelection { /// /// Represents a chain universe. /// Intended as a base for options and futures universe data. /// public abstract class BaseChainUniverseData : BaseDataCollection, IChainUniverseData { /// /// Csv line to get the values from /// /// We keep the properties as they are in the csv file to reduce memory usage (strings vs decimals) protected string CsvLine { get; } /// /// The security identifier of the option symbol /// [PandasIgnore] public SecurityIdentifier ID => Symbol.ID; /// /// Price of the security /// [PandasIgnore] public override decimal Value => Close; /// /// Open price of the security /// public decimal Open { get { // Parse the values every time to avoid keeping them in memory return CsvLine.GetDecimalFromCsv(0); } } /// /// High price of the security /// public decimal High { get { return CsvLine.GetDecimalFromCsv(1); } } /// /// Low price of the security /// public decimal Low { get { return CsvLine.GetDecimalFromCsv(2); } } /// /// Close price of the security /// public decimal Close { get { return CsvLine.GetDecimalFromCsv(3); } } /// /// Volume value of the security /// public decimal Volume { get { return CsvLine.GetDecimalFromCsv(4); } } /// /// Open interest value /// public virtual decimal OpenInterest { get { return CsvLine.GetDecimalFromCsv(5); } } /// /// Time that the data became available to use /// public override DateTime EndTime { get { return Time + QuantConnect.Time.OneDay; } set { Time = value - QuantConnect.Time.OneDay; } } /// /// Creates a new instance of the class /// protected BaseChainUniverseData() { } /// /// Creates a new instance of the class /// protected BaseChainUniverseData(DateTime date, Symbol symbol, string csv) : base(date, date, symbol, null, null) { CsvLine = csv; } /// /// Creates a new instance of the class as a copy of the given instance /// protected BaseChainUniverseData(BaseChainUniverseData other) : base(other) { CsvLine = other.CsvLine; } /// /// Return the URL string source of the file. This will be converted to a stream /// /// Configuration object /// Date of this source file /// true if we're in live mode, false for backtesting mode /// String URL of source file. public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode) { var path = GetUniverseFullFilePath(config.Symbol, date); return new SubscriptionDataSource(path, SubscriptionTransportMedium.LocalFile, FileFormat.FoldingCollection); } /// /// Generates the file path for a universe data file based on the given symbol and date. /// Optionally, creates the directory if it does not exist. /// /// The financial symbol for which the universe file is generated. /// The date associated with the universe file. /// The full file path to the universe data file. public static string GetUniverseFullFilePath(Symbol symbol, DateTime date) { return Path.Combine(LeanData.GenerateUniversesDirectory(Globals.DataFolder, symbol), $"{date:yyyyMMdd}.csv"); } /// /// Gets the default resolution for this data and security type /// /// This is a method and not a property so that python /// custom data types can override it public override Resolution DefaultResolution() { return Resolution.Daily; } /// /// Gets the symbol of the option /// public Symbol ToSymbol() { return Symbol; } } }