Beispiel #1
0
        /// <summary>
        /// Determines whether or not fill forward is required, and if true, will produce the new fill forward data
        /// </summary>
        /// <param name="previous">The last piece of data emitted by this enumerator</param>
        /// <param name="next">The next piece of data on the source enumerator</param>
        /// <param name="fillForward">When this function returns true, this will have a non-null value, null when the function returns false</param>
        /// <returns>True when a new fill forward piece of data was produced and should be emitted by this enumerator</returns>
        private bool RequiresFillForwardData(BaseData previous, BaseData next, out BaseData fillForward)
        {
            if (next.EndTime < previous.Time)
            {
                throw new ArgumentException("FillForwardEnumerator received data out of order.");
            }

            // check to see if the gap between previous and next warrants fill forward behavior
            if (next.Time - previous.Time <= _fillForwardResolution)
            {
                fillForward = null;
                return(false);
            }

            // is the bar after previous in market hours?
            var barAfterPreviousEndTime = previous.EndTime + _fillForwardResolution;

            if (_exchange.IsOpenDuringBar(previous.EndTime, barAfterPreviousEndTime, _isExtendedMarketHours))
            {
                // this is the normal case where we had a hole in the middle of the day
                fillForward      = previous.Clone(true);
                fillForward.Time = (previous.Time + _fillForwardResolution).RoundDown(_fillForwardResolution);
                return(true);
            }

            // find the next fill forward time after the next market open
            var nextFillForwardTime = _exchange.Hours.GetNextMarketOpen(previous.EndTime, _isExtendedMarketHours) + _fillForwardResolution;

            if (_dataResolution == Time.OneDay)
            {
                // special case for daily, we need to emit a midnight bar even though markets are closed
                var dailyBarEnd = GetNextOpenDateAfter(previous.Time.Date) + Time.OneDay;
                if (dailyBarEnd < nextFillForwardTime)
                {
                    // only emit the midnight bar if it's the next bar to be emitted
                    nextFillForwardTime = dailyBarEnd;
                }
            }

            if (nextFillForwardTime < next.EndTime)
            {
                // if next is still in the future then we need to emit a fill forward for market open
                fillForward      = previous.Clone(true);
                fillForward.Time = (nextFillForwardTime - _dataResolution).RoundDown(_fillForwardResolution);
                return(true);
            }

            // the next is before the next fill forward time, so do nothing
            fillForward = null;
            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// Advances the enumerator to the next element of the collection.
        /// </summary>
        /// <returns>
        /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
        /// </returns>
        /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
        public bool MoveNext()
        {
            while (_enumerator.MoveNext())
            {
                var current = _enumerator.Current;
                if (current != null)
                {
                    try
                    {
                        // execute user data filters
                        if (current.DataType != MarketDataType.Auxiliary && !_dataFilter.Filter(_security, current))
                        {
                            continue;
                        }
                    }
                    catch (Exception err)
                    {
                        OnDataFilterError(err);
                        continue;
                    }

                    // verify that the bar is within the exchange's market hours
                    if (current.DataType != MarketDataType.Auxiliary && !_exchange.IsOpenDuringBar(current.Time, current.EndTime, _extendedMarketHours))
                    {
                        if (_liveMode && !current.IsFillForward)
                        {
                            // TODO: replace for setting security.RealTimePrice not to modify security cache data directly
                            _security.SetMarketPrice(current);
                        }
                        continue;
                    }

                    // make sure we haven't passed the end
                    if (current.Time > _endTime)
                    {
                        return(false);
                    }
                }

                Current = current;
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Advances the enumerator to the next element of the collection.
        /// </summary>
        /// <returns>
        /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
        /// </returns>
        /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
        public bool MoveNext()
        {
            while (_enumerator.MoveNext())
            {
                var current = _enumerator.Current;
                if (current != null)
                {
                    try
                    {
                        // execute user data filters
                        if (current.DataType != MarketDataType.Auxiliary && !_dataFilter.Filter(_security, current))
                        {
                            continue;
                        }
                    }
                    catch (Exception err)
                    {
                        OnDataFilterError(err);
                        continue;
                    }

                    // verify that the bar is within the exchange's market hours
                    if (current.DataType != MarketDataType.Auxiliary && !_exchange.IsOpenDuringBar(current.Time, current.EndTime, _security.IsExtendedMarketHours))
                    {
                        continue;
                    }

                    // make sure we haven't passed the end
                    if (current.Time > _endTime)
                    {
                        return(false);
                    }
                }

                Current = current;
                return(true);
            }

            return(false);
        }
Beispiel #4
0
        /// <summary>
        /// Advances the enumerator to the next element of the collection.
        /// </summary>
        /// <returns>
        /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
        /// </returns>
        /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
        public bool MoveNext()
        {
            if (_delistedTime.HasValue)
            {
                // don't fill forward after data after the delisted date
                if (_previous == null || _previous.EndTime >= _delistedTime.Value)
                {
                    return(false);
                }
            }

            if (!_emittedAuxilliaryData)
            {
                // only set the _previous if the last item we emitted was NOT auxilliary data,
                // since _previous is used for fill forward behavior
                _previous = Current;
            }

            BaseData fillForward;

            if (!_isFillingForward)
            {
                // if we're filling forward we don't need to move next since we haven't emitted _enumerator.Current yet
                if (!_enumerator.MoveNext())
                {
                    if (_delistedTime.HasValue)
                    {
                        // don't fill forward delisted data
                        return(false);
                    }

                    // check to see if we ran out of data before the end of the subscription
                    if (_previous == null || _previous.EndTime >= _subscriptionEndTime)
                    {
                        // we passed the end of subscription, we're finished
                        return(false);
                    }

                    // we can fill forward the rest of this subscription if required
                    var endOfSubscription = Current.Clone(true);
                    endOfSubscription.Time = _subscriptionEndTime.RoundDown(_dataResolution);
                    if (RequiresFillForwardData(_previous, endOfSubscription, out fillForward))
                    {
                        // don't mark as filling forward so we come back into this block, subscription is done
                        //_isFillingForward = true;
                        Current = fillForward;
                        _emittedAuxilliaryData = false;
                        return(true);
                    }

                    // don't emit the last bar if the market isn't considered open!
                    if (!_exchange.IsOpenDuringBar(endOfSubscription.Time, endOfSubscription.EndTime, _isExtendedMarketHours))
                    {
                        return(false);
                    }

                    Current = endOfSubscription;
                    _emittedAuxilliaryData = false;
                    return(true);
                }
            }

            if (_previous == null)
            {
                // first data point we dutifully emit without modification
                Current = _enumerator.Current;
                return(true);
            }

            if (_enumerator.Current.DataType == MarketDataType.Auxiliary)
            {
                _emittedAuxilliaryData = true;
                Current = _enumerator.Current;
                var delisting = Current as Delisting;
                if (delisting != null && delisting.Type == DelistingType.Delisted)
                {
                    _delistedTime = delisting.EndTime;
                }
                return(true);
            }

            _emittedAuxilliaryData = false;
            if (RequiresFillForwardData(_previous, _enumerator.Current, out fillForward))
            {
                // we require fill forward data because the _enumerator.Current is too far in future
                _isFillingForward = true;
                Current           = fillForward;
                return(true);
            }

            _isFillingForward = false;
            Current           = _enumerator.Current;
            return(true);
        }
        /// <summary>
        /// Determines whether or not fill forward is required, and if true, will produce the new fill forward data
        /// </summary>
        /// <param name="previous">The last piece of data emitted by this enumerator</param>
        /// <param name="next">The next piece of data on the source enumerator</param>
        /// <param name="fillForward">When this function returns true, this will have a non-null value, null when the function returns false</param>
        /// <returns>True when a new fill forward piece of data was produced and should be emitted by this enumerator</returns>
        private bool RequiresFillForwardData(BaseData previous, BaseData next, out BaseData fillForward)
        {
            if (next.EndTime < previous.Time)
            {
                throw new ArgumentException("FillForwardEnumerator received data out of order.");
            }

            // check to see if the gap between previous and next warrants fill forward behavior
            if (next.Time - previous.Time <= _fillForwardResolution)
            {
                fillForward = null;
                return(false);
            }

            // is the bar after previous in market hours?
            var barAfterPreviousEndTime = previous.EndTime + _fillForwardResolution;

            if (_exchange.IsOpenDuringBar(previous.EndTime, barAfterPreviousEndTime, _isExtendedMarketHours))
            {
                // this is the normal case where we had a hole in the middle of the day
                fillForward      = previous.Clone(true);
                fillForward.Time = previous.Time + _fillForwardResolution;
                return(true);
            }

            // This next if/else block's entire job is to locate the market open time following previous.EndTime
            // we'll then add the ff resolution to that time to produce the nextFillForwardTime, which is the time
            // we need to emit our next piece of data at

            DateTime nextFillForwardTime;
            var      frontierDate = previous.EndTime.Date;

            if (previous.EndTime > GetMarketOpen(frontierDate) - _fillForwardResolution)
            {
                // after market hours on the same day, advance to the next emit time at market open
                nextFillForwardTime = GetMarketOpen(GetNextOpenDateAfter(frontierDate)) + _fillForwardResolution;
            }
            else
            {
                // find the next market open time, we use -1 here because the GetNextOpenDataAfter will start with +1, so if frontierDate
                // is already open, the frontierDate will be returned, but if frontierDate is say, saturday at midnight (daily case),
                // then we'll need to zoom forward to monday at market open
                nextFillForwardTime = GetMarketOpen(GetNextOpenDateAfter(frontierDate.Date.AddDays(-1))) + _fillForwardResolution;
            }

            if (_dataResolution == Time.OneDay)
            {
                // special case for daily, we need to emit a midnight bar even though markets are closed
                var dailyBarEnd = GetNextOpenDateAfter(previous.Time.Date) + Time.OneDay;
                if (dailyBarEnd < nextFillForwardTime)
                {
                    // only emit the midnight bar if it's the next bar to be emitted
                    nextFillForwardTime = dailyBarEnd;
                }
            }

            if (nextFillForwardTime < next.EndTime)
            {
                // if next is still in the future then we need to emit a fill forward for market open
                fillForward      = previous.Clone(true);
                fillForward.Time = (nextFillForwardTime - _dataResolution).RoundDown(_fillForwardResolution);
                return(true);
            }

            // the next is before the next fill forward time, so do nothing
            fillForward = null;
            return(false);
        }