Пример #1
0
        private Insight SetGeneratedAndClosedTimes(Insight insight)
        {
            insight.GeneratedTimeUtc = UtcTime;
            insight.ReferenceValue   = _securityValuesProvider.GetValues(insight.Symbol).Get(insight.Type);
            if (string.IsNullOrEmpty(insight.SourceModel))
            {
                // set the source model name if not already set
                insight.SourceModel = Alpha.GetModelName();
            }

            TimeSpan barSize;
            Security security;
            SecurityExchangeHours exchangeHours;

            if (Securities.TryGetValue(insight.Symbol, out security))
            {
                exchangeHours = security.Exchange.Hours;
                barSize       = security.Resolution.ToTimeSpan();
            }
            else
            {
                barSize       = insight.Period.ToHigherResolutionEquivalent(false).ToTimeSpan();
                exchangeHours = MarketHoursDatabase.GetExchangeHours(insight.Symbol.ID.Market, insight.Symbol, insight.Symbol.SecurityType);
            }

            var localStart = UtcTime.ConvertFromUtc(exchangeHours.TimeZone);

            barSize = QuantConnect.Time.Max(barSize, QuantConnect.Time.OneMinute);
            var barCount = (int)(insight.Period.Ticks / barSize.Ticks);

            insight.CloseTimeUtc = QuantConnect.Time.GetEndTimeForTradeBars(exchangeHours, localStart, barSize, barCount, false).ConvertToUtc(exchangeHours.TimeZone);

            return(insight);
        }
Пример #2
0
        private Alpha SetGeneratedAndClosedTimes(Alpha alpha)
        {
            alpha.GeneratedTimeUtc = UtcTime;

            TimeSpan barSize;
            Security security;
            SecurityExchangeHours exchangeHours;

            if (Securities.TryGetValue(alpha.Symbol, out security))
            {
                exchangeHours = security.Exchange.Hours;
                barSize       = security.Resolution.ToTimeSpan();
            }
            else
            {
                barSize       = alpha.Period.ToHigherResolutionEquivalent(false).ToTimeSpan();
                exchangeHours = MarketHoursDatabase.GetExchangeHours(alpha.Symbol.ID.Market, alpha.Symbol, alpha.Symbol.SecurityType);
            }

            var localStart = UtcTime.ConvertFromUtc(exchangeHours.TimeZone);

            barSize = QuantConnect.Time.Max(barSize, QuantConnect.Time.OneMinute);
            var barCount = (int)(alpha.Period.Ticks / barSize.Ticks);

            alpha.CloseTimeUtc = QuantConnect.Time.GetEndTimeForTradeBars(exchangeHours, localStart, barSize, barCount, false).ConvertToUtc(exchangeHours.TimeZone);

            return(alpha);
        }
Пример #3
0
        /// <summary>
        /// Determines if the exchange for the specified symbol is open at the current time.
        /// </summary>
        /// <param name="symbol">The symbol</param>
        /// <returns>True if the exchange is considered open at the current time, false otherwise</returns>
        public bool IsMarketOpen(Symbol symbol)
        {
            var exchangeHours = MarketHoursDatabase
                .FromDataFolder()
                .GetExchangeHours(symbol.ID.Market, symbol, symbol.SecurityType);

            var time = UtcTime.ConvertFromUtc(exchangeHours.TimeZone);

            return exchangeHours.IsOpen(time, false);
        }
Пример #4
0
        /// <summary>
        /// Gets the history requests required for provide warm up data for the algorithm
        /// </summary>
        /// <returns></returns>
        public IEnumerable <HistoryRequest> GetWarmupHistoryRequests()
        {
            if (_warmupBarCount.HasValue)
            {
                return(CreateBarCountHistoryRequests(Securities.Keys, _warmupBarCount.Value, _warmupResolution));
            }
            if (_warmupTimeSpan.HasValue)
            {
                var end = UtcTime.ConvertFromUtc(TimeZone);
                return(CreateDateRangeHistoryRequests(Securities.Keys, end - _warmupTimeSpan.Value, end, _warmupResolution));
            }

            // if not warmup requested return nothing
            return(Enumerable.Empty <HistoryRequest>());
        }
Пример #5
0
        /// <summary>
        /// Gets the start time required for the specified bar count for a security in terms of the algorithm's time zone
        /// Used when the security has not yet been subscribed to
        /// </summary>
        private DateTime GetStartTimeAlgoTzForSecurity(SecurityExchange securityExchange,
                                                       int periods,
                                                       Resolution resolution,
                                                       bool isExtendedMarketHours)
        {
            var timeSpan = resolution.ToTimeSpan();

            // make this a minimum of one second
            timeSpan = timeSpan < QuantConnect.Time.OneSecond ? QuantConnect.Time.OneSecond : timeSpan;

            var localStartTime = QuantConnect.Time.GetStartTimeForTradeBars(
                securityExchange.Hours,
                UtcTime.ConvertFromUtc(securityExchange.TimeZone),
                timeSpan,
                periods,
                isExtendedMarketHours);

            return(localStartTime.ConvertTo(securityExchange.TimeZone, TimeZone));
        }
Пример #6
0
        /// <summary>
        /// Gets the start time required for the specified bar count in terms of the algorithm's time zone
        /// </summary>
        private DateTime GetStartTimeAlgoTz(Symbol symbol, int periods, Resolution?resolution = null)
        {
            var security = Securities[symbol];
            var timeSpan = (resolution ?? security.Resolution).ToTimeSpan();

            // make this a minimum of one second
            timeSpan = timeSpan < QuantConnect.Time.OneSecond ? QuantConnect.Time.OneSecond : timeSpan;
            var localStartTime = QuantConnect.Time.GetStartTimeForTradeBars(security.Exchange.Hours, UtcTime.ConvertFromUtc(security.Exchange.TimeZone), timeSpan, periods, security.IsExtendedMarketHours);

            return(localStartTime.ConvertTo(security.Exchange.TimeZone, TimeZone));
        }
Пример #7
0
        /// <summary>
        /// Gets the start time required for the specified bar count in terms of the algorithm's time zone
        /// </summary>
        private DateTime GetStartTimeAlgoTz(Symbol symbol, int periods, Resolution?resolution = null)
        {
            Security security;

            resolution = GetResolution(symbol, resolution);
            var exchange = GetExchangeHours(symbol);
            var isExtendedMarketHours = Securities.TryGetValue(symbol, out security) ? security.IsExtendedMarketHours : false;

            var timeSpan = resolution.Value.ToTimeSpan();

            // make this a minimum of one second
            timeSpan = timeSpan < QuantConnect.Time.OneSecond ? QuantConnect.Time.OneSecond : timeSpan;
            var localStartTime = QuantConnect.Time.GetStartTimeForTradeBars(exchange, UtcTime.ConvertFromUtc(exchange.TimeZone), timeSpan, periods, isExtendedMarketHours);

            return(localStartTime.ConvertTo(exchange.TimeZone, TimeZone));
        }