Exemple #1
0
        /// <summary>
        /// Reader converts each line of the data source into BaseData objects. Each data type creates its own factory method, and returns a new instance of the object
        /// each time it is called.
        /// </summary>
        /// <param name="config">Subscription data config setup object</param>
        /// <param name="line">Line of the source document</param>
        /// <param name="date">Date of the requested data</param>
        /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
        /// <returns>Instance of the T:BaseData object generated by this line of the CSV</returns>
        public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
        {
            try
            {
                var csv    = line.Split(',');
                var coarse = new CoarseFundamental
                {
                    Symbol       = new Symbol(SecurityIdentifier.Parse(csv[0]), csv[1]),
                    Time         = date,
                    Market       = config.Market,
                    Value        = csv[2].ToDecimal(),
                    Volume       = csv[3].ToInt64(),
                    DollarVolume = csv[4].ToDecimal()
                };

                if (csv.Length > 5)
                {
                    coarse.HasFundamentalData = Convert.ToBoolean(csv[5]);
                }

                if (csv.Length > 7)
                {
                    coarse.PriceFactor = csv[6].ToDecimal();
                    coarse.SplitFactor = csv[7].ToDecimal();
                }

                return(coarse);
            }
            catch (Exception)
            {
                return(null);
            }
        }
 /// <summary>
 /// Gets the <see cref="CoarseFundamental"/> data for the specified market/date
 /// </summary>
 public static IEnumerable<CoarseFundamental> GetCoarseFundamentals(string market, DateTimeZone timeZone, DateTime date, bool isLiveMode)
 {
     var factory = new CoarseFundamental();
     var config = new SubscriptionDataConfig(typeof(CoarseFundamental), SecurityType.Equity, new Symbol(market + "-coarse"), Resolution.Daily, market, timeZone, true, false, true, false);
     var reader = new BaseDataSubscriptionFactory(config, date, isLiveMode);
     var source = factory.GetSource(config, date, isLiveMode);
     return reader.Read(source).OfType<CoarseFundamental>();
 }
Exemple #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CoarseFundamentalUniverse"/> class
        /// </summary>
        /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
        /// <param name="securityInitializer">Initializes securities when they're added to the universe</param>
        /// <param name="selector">Returns the symbols that should be included in the universe</param>
        public CoarseFundamentalUniverse(UniverseSettings universeSettings, ISecurityInitializer securityInitializer, PyObject selector)
            : base(CreateConfiguration(CoarseFundamental.CreateUniverseSymbol(QuantConnect.Market.USA)), securityInitializer)
        {
            _universeSettings = universeSettings;
            Func <IEnumerable <CoarseFundamental>, Symbol[]> func;

            if (selector.TryConvertToDelegate(out func))
            {
                _selector = func;
            }
        }
        /// <summary>
        /// Converts a given fundamental data point into row format
        /// </summary>
        public static string ToRow(CoarseFundamental coarse)
        {
            // sid,symbol,close,volume,dollar volume,has fundamental data,price factor,split factor
            var values = new object[]
            {
                coarse.Symbol.ID,
                coarse.Symbol.Value,
                coarse.Value,
                coarse.Volume,
                coarse.DollarVolume,
                coarse.HasFundamentalData,
                coarse.PriceFactor,
                coarse.SplitFactor
            };

            return(string.Join(",", values.Select(s => Convert.ToString(s, CultureInfo.InvariantCulture))));
        }
        /// <summary>
        /// Reader converts each line of the data source into BaseData objects. Each data type creates its own factory method, and returns a new instance of the object 
        /// each time it is called. 
        /// </summary>
        /// <param name="config">Subscription data config setup object</param>
        /// <param name="line">Line of the source document</param>
        /// <param name="date">Date of the requested data</param>
        /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
        /// <returns>Instance of the T:BaseData object generated by this line of the CSV</returns>
        public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
        {
            try
            {
                var csv = line.Split(',');
                var coarse = new CoarseFundamental
                {
                    Symbol = new Symbol(SecurityIdentifier.Parse(csv[0]), csv[1]),
                    Time = date,
                    Market = config.Market,
                    Value = csv[2].ToDecimal(),
                    Volume = csv[3].ToInt64(),
                    DollarVolume = csv[4].ToDecimal()
                };

                if (csv.Length > 5)
                {
                    coarse.HasFundamentalData = Convert.ToBoolean(csv[5]);
                }

                return coarse;
            }
            catch (Exception)
            {
                return null;
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="CoarseFundamentalUniverse"/> class
 /// </summary>
 /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
 /// <param name="selector">Returns the symbols that should be included in the universe</param>
 public CoarseFundamentalUniverse(UniverseSettings universeSettings, PyObject selector)
     : this(CoarseFundamental.CreateUniverseSymbol(QuantConnect.Market.USA), universeSettings, selector)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="CoarseFundamentalUniverse"/> class
 /// </summary>
 /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
 /// <param name="selector">Returns the symbols that should be included in the universe</param>
 public CoarseFundamentalUniverse(UniverseSettings universeSettings, Func <IEnumerable <CoarseFundamental>, IEnumerable <Symbol> > selector)
     : base(CreateConfiguration(CoarseFundamental.CreateUniverseSymbol(QuantConnect.Market.USA)))
 {
     _universeSettings = universeSettings;
     _selector         = selector;
 }
        /// <summary>
        /// Adds a new subscription for universe selection
        /// </summary>
        /// <param name="universe">The universe to add a subscription for</param>
        /// <param name="startTimeUtc">The start time of the subscription in utc</param>
        /// <param name="endTimeUtc">The end time of the subscription in utc</param>
        public void AddUniverseSubscription(Universe universe, DateTime startTimeUtc, DateTime endTimeUtc)
        {
            // TODO : Consider moving the creating of universe subscriptions to a separate, testable class

            // grab the relevant exchange hours
            var config = universe.Configuration;

            var marketHoursDatabase = MarketHoursDatabase.FromDataFolder();
            var exchangeHours = marketHoursDatabase.GetExchangeHours(config);

            // create a canonical security object
            var security = new Security(exchangeHours, config);

            var localStartTime = startTimeUtc.ConvertFromUtc(security.Exchange.TimeZone);
            var localEndTime = endTimeUtc.ConvertFromUtc(security.Exchange.TimeZone);

            // define our data enumerator
            IEnumerator<BaseData> enumerator;

            var tradeableDates = Time.EachTradeableDay(security, localStartTime, localEndTime);

            var userDefined = universe as UserDefinedUniverse;
            if (userDefined != null)
            {
                // spoof a tick on the requested interval to trigger the universe selection function
                enumerator = userDefined.GetTriggerTimes(startTimeUtc, endTimeUtc, marketHoursDatabase)
                    .Select(x => new Tick { Time = x, Symbol = config.Symbol }).GetEnumerator();

                // route these custom subscriptions through the exchange for buffering
                var enqueueable = new EnqueueableEnumerator<BaseData>(true);

                // add this enumerator to our exchange
                ScheduleEnumerator(enumerator, enqueueable, GetLowerThreshold(config.Resolution), GetUpperThreshold(config.Resolution));

                enumerator = enqueueable;
            }
            else if (config.Type == typeof (CoarseFundamental))
            {
                var cf = new CoarseFundamental();

                var enqueueable = new EnqueueableEnumerator<BaseData>(true);

                // load coarse data day by day
                var coarse = from date in Time.EachTradeableDay(security, _algorithm.StartDate, _algorithm.EndDate)
                             let dateInDataTimeZone = date.ConvertTo(config.ExchangeTimeZone, config.DataTimeZone).Date
                             let factory = new BaseDataSubscriptionFactory(config, dateInDataTimeZone, false)
                             let source = cf.GetSource(config, dateInDataTimeZone, false)
                             let coarseFundamentalForDate = factory.Read(source)
                             select new BaseDataCollection(date, config.Symbol, coarseFundamentalForDate);

                
                ScheduleEnumerator(coarse.GetEnumerator(), enqueueable, 5, 100000, 2);

                enumerator = enqueueable;
            }
            else
            {
                // normal reader for all others
                enumerator = new SubscriptionDataReader(config, localStartTime, localEndTime, _resultHandler, MapFileResolver.Empty, _factorFileProvider, tradeableDates, false);

                // route these custom subscriptions through the exchange for buffering
                var enqueueable = new EnqueueableEnumerator<BaseData>(true);

                // add this enumerator to our exchange
                ScheduleEnumerator(enumerator, enqueueable, GetLowerThreshold(config.Resolution), GetUpperThreshold(config.Resolution));

                enumerator = enqueueable;
            }

            // create the subscription
            var timeZoneOffsetProvider = new TimeZoneOffsetProvider(security.Exchange.TimeZone, startTimeUtc, endTimeUtc);
            var subscription = new Subscription(universe, security, enumerator, timeZoneOffsetProvider, startTimeUtc, endTimeUtc, true);
            _subscriptions.AddOrUpdate(subscription.Security.Symbol, subscription);
        }
Exemple #9
0
 /// <summary>
 /// Converts a given fundamental data point into row format
 /// </summary>
 public static string ToRow(CoarseFundamental coarse)
 {
     // sid,symbol,close,volume,dollar volume,has fundamental data,price factor,split factor
     return($"{coarse.Symbol.ID},{coarse.Symbol.Value},{coarse.Value},{coarse.Volume},{coarse.DollarVolume},{coarse.HasFundamentalData},{coarse.PriceFactor},{coarse.SplitFactor}");
 }