Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        public DataTickHistoryProvider(ISourceDataDelivery dataDelivery, DataSessionInfo session)
        {
            _dataDelivery = dataDelivery;
            _session      = session;

            Initialize();
        }
        void _dataDelivery_OperationalStatusChangedEvent(IOperational operational, OperationalStateEnum previousOperationState)
        {
            if (operational.OperationalState != OperationalStateEnum.Operational)
            {
                return;
            }

            // Re-map the session orderInfo.
            RuntimeDataSessionInformation information = _dataDelivery.GetSymbolRuntimeSessionInformation(_symbol);

            if (information == null)
            {
                SystemMonitor.OperationError("Failed to map session information for quote provider.");
                _sessionInfo = DataSessionInfo.Empty;
            }
            else
            {
                _sessionInfo = information.Info;

                if (_dataDelivery.SubscribeToData(_sessionInfo, true, new DataSubscriptionInfo(true, false, null)))
                {
                    RequestQuoteUpdate(false);
                }
            }
        }
Exemplo n.º 3
0
        void _dataDelivery_QuoteUpdateEvent(ISourceDataDelivery dataDelivery, DataSessionInfo session, Quote?quote)
        {
            if (session.Symbol == this.Symbol)
            {
                if (quote.HasValue)
                {
                    lock (this)
                    {
                        if (this.Volume < 0)
                        {
                            _price = quote.Value.Ask;
                        }
                        else
                        {
                            _price = quote.Value.Bid;
                        }
                    }
                }
                else
                {
                    _price = null;
                }

                RecalculateParameters(true);
            }
        }
Exemplo n.º 4
0
        void _dataDelivery_QuoteUpdateEvent(ISourceDataDelivery dataDelivery, DataSessionInfo session, Quote?quote)
        {
            _lastDataQuote = quote;

            BeginAccountInfoUpdate(_account.Info);

            BeginManagedOrdersUpdate(quote);
        }
Exemplo n.º 5
0
 void _dataDelivery_DataHistoryUpdateEvent(ISourceDataDelivery dataDelivery, DataSessionInfo session, DataHistoryUpdate update)
 {
     if (update.BarDataAssigned)
     {
         lock (this)
         {
             _lastDataBar = update.DataBarsUnsafe[update.DataBarsUnsafe.Count - 1];
         }
     }
 }
Exemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        public void FilterUpdate(ISourceDataDelivery dataDelivery, DataSessionInfo session, DataHistoryUpdate update)
        {
            if (_enabled == false)
            {
                return;
            }

            decimal?lastHigh = null;
            decimal?lastLow  = null;

            decimal divergence = MaxDivergenceCoefficientStock;

            if (session.Symbol.IsForexPair)
            {
                divergence = MaxDivergenceCoefficientForex;
            }

            int consecutiveSpikes = 0;

            for (int i = 0; i < update.DataBarsUnsafe.Count; i++)
            {
                if (lastHigh.HasValue && lastLow.HasValue)
                {
                    DataBar bar = update.DataBarsUnsafe[i];

                    if ((bar.High > lastHigh.Value * (1 + divergence) || bar.Low < lastLow.Value / (1 + divergence)) &&
                        consecutiveSpikes <= _maximumConsecutiveSpikes)
                    {// Bar spike detected.
                        consecutiveSpikes++;

                        SystemMonitor.Report("Spike detected in data [" + session.Symbol.Name + "]. Bar values limited to previous [" + lastHigh.Value + ", " + bar.High + "; " + lastLow.Value + ", " + bar.Low + "].");

                        update.DataBarsUnsafe[i] = new DataBar(bar.DateTime,
                                                               GeneralHelper.LimitRange(bar.Open, lastLow.Value, lastHigh.Value),
                                                               GeneralHelper.LimitRange(bar.High, lastLow.Value, lastHigh.Value),
                                                               GeneralHelper.LimitRange(bar.Low, lastLow.Value, lastHigh.Value),
                                                               GeneralHelper.LimitRange(bar.Close, lastLow.Value, lastHigh.Value),
                                                               bar.Volume);
                    }
                    else
                    {
                        consecutiveSpikes = 0;
                    }
                }

                lastHigh = update.DataBarsUnsafe[i].High;
                lastLow  = update.DataBarsUnsafe[i].Low;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Constructor.
        /// </summary>
        public DataBarHistoryProvider(ISourceDataDelivery dataDelivery, DataSessionInfo session, TimeSpan period, int?defaultHistoryBarsCount)
        {
            _sessionInfo = session;

            if (defaultHistoryBarsCount.HasValue)
            {
                _defaultHistoryBarsCount = defaultHistoryBarsCount.Value;
            }

            _dataDelivery = dataDelivery;
            _indicators   = new IndicatorManager(this);
            _period       = period;

            Construct();
        }
        void _dataDelivery_QuoteUpdateEvent(ISourceDataDelivery dataDelivery, DataSessionInfo session, Quote?quote)
        {
            if (_sessionInfo.Equals(session) == false)
            {
                return;
            }

            lock (this)
            {
                _lastQuoteTime = DateTime.Now;
                _currentQuote  = quote;
            }

            if (QuoteUpdateEvent != null)
            {
                QuoteUpdateEvent(this);
            }
        }
Exemplo n.º 9
0
        void _dataDelivery_OperationalStatusChangedEvent(IOperational operational, OperationalStateEnum previousOperationState)
        {
            if (operational.OperationalState != OperationalStateEnum.Operational)
            {
                return;
            }

            if (_sessionInfo.IsEmtpy)
            {
                SystemMonitor.OperationError("Data bar provider has no valid session assiged.");
                return;
            }

            // Re-map the session orderInfo.
            RuntimeDataSessionInformation information = _dataDelivery.GetSymbolRuntimeSessionInformation(_sessionInfo.Symbol);

            if (information == null)
            {
                SystemMonitor.OperationError("Failed to map session information for data provider.");
                _sessionInfo = DataSessionInfo.Empty;
                return;
            }
            else
            {
                _sessionInfo = information.Info;
            }

            if (_dataDelivery.SubscribeToData(_sessionInfo, true, new DataSubscriptionInfo(false, false, new TimeSpan[] { Period.Value })) == false)
            {
                SystemMonitor.OperationError("Failed to subscribe to bar data updates.");
                return;
            }

            RuntimeDataSessionInformation session = _dataDelivery.GetSymbolRuntimeSessionInformation(_sessionInfo.Symbol);

            if (session != null && session.AvailableDataBarPeriods.Contains(_period.Value))
            {
                _dataDelivery.RequestDataHistoryUpdate(_sessionInfo, new DataHistoryRequest(_period.Value, _defaultHistoryBarsCount), false);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        ///
        /// </summary>
        public virtual bool SetInitialParameters(ISourceManager manager, ComponentId sourceId, ExpertSession session)
        {
            _sessionInfo = session.Info;

            _manager = manager;

            _sourceId = sourceId;

            _dataDelivery = manager.ObtainDataDelivery(sourceId);

            _quote = manager.ObtainQuoteProvider(sourceId, _sessionInfo.Symbol);

            _tickProvider = manager.ObtainDataTickHistoryProvider(sourceId, _sessionInfo.Symbol);

            _dataBarProvider = null;

            bool result = _dataDelivery != null && _quote != null && _tickProvider != null;

            SystemMonitor.CheckError(result, "Failed to initialize data provider.");

            return(result);
        }
 /// <summary>
 /// Constructor.
 /// </summary>
 public RuntimeDataSessionInformation(DataSessionInfo info)
 {
     _info = info;
 }
Exemplo n.º 12
0
 protected virtual void dataDelivery_QuoteUpdateEvent(ISourceDataDelivery dataDelivery, DataSessionInfo session, Quote?quote)
 {
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 public RuntimeDataSessionInformation(DataSessionInfo info, IEnumerable <TimeSpan> spans)
 {
     _info = info;
     _availableDataBarPeriods.AddRange(spans);
 }
Exemplo n.º 14
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public ExpertSession(DataSessionInfo sessionInfo)
 {
     _sessionInfo = sessionInfo;
 }
Exemplo n.º 15
0
        void _dataDelivery_DataHistoryUpdateDelegate(ISourceDataDelivery dataDelivery, DataSessionInfo session, DataHistoryUpdate update)
        {
            if (session.Equals(_session) == false || update.DataTicksUnsafe.Count == 0)
            {
                return;
            }

            DataTickUpdateType?updateType = null;

            if (_dataTicks.Count == 0)
            {
                updateType = DataTickUpdateType.HistoryUpdate;
            }

            lock (this)
            {
                _lastDataUpdate = DateTime.Now;

                for (int i = 0; i < update.DataTicksUnsafe.Count; i++)
                {
                    if (_dataTicks.Count == 0 || update.DataTicksUnsafe[i].DateTime > _dataTicks[_dataTicks.Count - 1].DateTime)
                    {
                        if (updateType.HasValue == false)
                        {
                            updateType = DataTickUpdateType.HistoryUpdate;
                        }

                        _dataTicks.Add(update.DataTicksUnsafe[i]);
                        _cachedDataTickIndexSearches.Add(update.DataTicksUnsafe[i].DateTime, i);
                    }
                }

                // Also check the last 5 units for any requotes that might have been sent,
                // this happens when price changes and we get updates for the last unit.
                for (int i = 0; i < 5 && update.DataTicksUnsafe.Count - 1 - i > 0 && _dataTicks.Count - 1 - i > 0; i++)
                {
                    if (update.DataTicksUnsafe[update.DataTicksUnsafe.Count - 1 - i].DateTime == _dataTicks[_dataTicks.Count - 1 - i].DateTime &&
                        update.DataTicksUnsafe[update.DataTicksUnsafe.Count - 1 - i].Equals(_dataTicks[_dataTicks.Count - 1 - i]) == false)
                    {
                        // Since this update is only when the date times are the same, the helper cache dictionary needs not be updated.
                        _dataTicks[_dataTicks.Count - 1 - i] = update.DataTicksUnsafe[update.DataTicksUnsafe.Count - 1 - i];

                        if (updateType.HasValue == false)
                        {
                            updateType = DataTickUpdateType.HistoryUpdate;
                        }
                    }
                }
            }

            if (updateType.HasValue && DataTickHistoryUpdateEvent != null)
            {
                DataTickHistoryUpdateEvent(this, updateType.Value);
            }
        }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="orderInfo"></param>
 /// <param name="spans"></param>
 public RuntimeDataSessionInformation(DataSessionInfo info, TimeSpan period)
 {
     _info = info;
     _availableDataBarPeriods.Add(period);
 }
Exemplo n.º 17
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public QuoteProvider(DataSessionInfo sessionInfo)
 {// Make sure this dataDelivery keeper is in the same state as its dataDelivery delivery.
     _sessionInfo = sessionInfo;
     _symbol      = _sessionInfo.Symbol;
 }
Exemplo n.º 18
0
        /// <summary>
        /// Data delivery has received a dataDelivery update.
        /// </summary>
        void _dataDelivery_DataHistoryUpdateDelegate(ISourceDataDelivery dataDelivery, DataSessionInfo session, DataHistoryUpdate update)
        {
            if (_sessionInfo.Equals(session) == false)
            {
                return;
            }

            if (update.Period != _period.Value)
            {// This update is aimed at another provider.
                return;
            }

            DataBarUpdateType?updateType = null;

            if (_dataBars.Count == 0)
            {
                updateType = DataBarUpdateType.Initial;
            }

            _barFilter.FilterUpdate(dataDelivery, session, update);

            int updatedBarsCount = 0;

            lock (this)
            {
                _lastDataUpdate = DateTime.Now;

                // Add new bars - search for new later bars.
                for (int i = 0; i < update.DataBarsUnsafe.Count; i++)
                {
                    if (_dataBars.Count == 0 || update.DataBarsUnsafe[i].DateTime > _dataBars[_dataBars.Count - 1].DateTime)
                    {
                        if (updateType.HasValue == false)
                        {
                            updateType = DataBarUpdateType.HistoryUpdate;
                        }

                        updatedBarsCount++;

                        _dataBars.Add(update.DataBarsUnsafe[i]);
                        _cachedDataBarIndexSearches.Add(update.DataBarsUnsafe[i].DateTime, _dataBars.Count - 1);
                    }
                }

                bool preTimeUpdate = false;
                // Add new bars - search for previous bars - a separate cycle needed since we need to move backwards on this.
                for (int i = update.DataBarsUnsafe.Count - 1; i >= 0; i--)
                {
                    if (_dataBars.Count > 0 && update.DataBarsUnsafe[i].DateTime < _dataBars[0].DateTime)
                    {// This is a bar from previous history, we do not know about - insert first place.
                        _dataBars.Insert(0, update.DataBarsUnsafe[i]);
                        preTimeUpdate = true;
                    }
                }

                // Also check the last 5 units for any requotes that might have been sent,
                // this happens when price changes and we get updates for the last unit.
                for (int i = 0; i < 5 && update.DataBarsUnsafe.Count - 1 - i > 0 && _dataBars.Count - 1 - i > 0; i++)
                {
                    if (update.DataBarsUnsafe[update.DataBarsUnsafe.Count - 1 - i].DateTime == _dataBars[_dataBars.Count - 1 - i].DateTime
                        /*&& update.DataBarsUnsafe[update.DataBarsUnsafe.Count - 1 - i].Equals(_dataBars[_dataBars.Count - 1 - i]) == false*/)
                    {
                        updatedBarsCount++;

                        // Since this update is only when the date times are the same, the helper cache dictionary needs not be updated.
                        _dataBars[_dataBars.Count - 1 - i] = update.DataBarsUnsafe[update.DataBarsUnsafe.Count - 1 - i];

                        if (updateType.HasValue == false)
                        {
                            updateType = DataBarUpdateType.HistoryUpdate;
                        }
                    }
                }

                if (preTimeUpdate)
                {// Make a full update if we have inserted something in the beggining.
                    updateType       = DataBarUpdateType.HistoryUpdate;
                    updatedBarsCount = _dataBars.Count;
                }
            }

            if (updateType.HasValue && DataBarHistoryUpdateEvent != null)
            {
                DataBarHistoryUpdateEvent(this, updateType.Value, updatedBarsCount);
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// Follow quotes update.
        /// </summary>
        void _dataDelivery_QuoteUpdateEvent(ISourceDataDelivery dataDelivery, DataSessionInfo session, Quote?quote)
        {
            if (_sessionInfo.Equals(session) == false || _period.HasValue == false ||
                quote.HasValue == false)
            {
                return;
            }

            DataBar?lastBar = null;

            if (_dataBars.Count > 0)
            {
                lastBar = _dataBars[_dataBars.Count - 1];
            }
            else
            {// If there are no existing bars, we do not operate, to evade mixing up start / end period etc.
                return;
            }

            TimeSpan?period = _period;

            DataBarUpdateType?updateType = null;

            // The time of a bar is its open time, so everything after a "period" closeVolume of time after it is part of that bar.
            if (lastBar.Value.DateTime + period < quote.Value.Time)
            {// We need to append a new bar.
                DateTime newBarDateTime = lastBar.Value.DateTime;
                int      i = 0;
                while (newBarDateTime.Add(period.Value) < quote.Value.Time)
                {// Establish end time of new bar (max 1000 steps to evade potential lockups).
                    newBarDateTime = newBarDateTime.Add(period.Value);
                    i++;

                    if (i > 1000)
                    {// We have tried and failed to establish proper new period so just abort.
                        SystemMonitor.OperationError("We have tried and failed to establish proper new period so quote aborted.");
                        return;
                    }
                }

                DataBar?newBar = BarFromQuote(lastBar, newBarDateTime, quote.Value);
                if (newBar.HasValue == false)
                {// Failed to establish bar from quote.
                    return;
                }

                updateType = DataBarUpdateType.NewPeriod;

                lock (this)
                {// Add the new bar.
                    _lastDataUpdate = DateTime.Now;
                    _dataBars.Add(newBar.Value);
                }
            }
            else
            {
                updateType = DataBarUpdateType.CurrentBarUpdate;

                lock (this)
                {// Just update the current last bar.
                    _lastDataUpdate = DateTime.Now;
                    _dataBars[_dataBars.Count - 1] = UpdateCurrentBar(lastBar.Value, quote.Value);
                }
            }

            if (updateType.HasValue && DataBarHistoryUpdateEvent != null)
            {
                DataBarHistoryUpdateEvent(this, updateType.Value, 1);
            }
        }
Exemplo n.º 20
0
 protected override void dataDelivery_QuoteUpdateEvent(ISourceDataDelivery dataDelivery, DataSessionInfo session, Quote?quote)
 {
     base.dataDelivery_QuoteUpdateEvent(dataDelivery, session, quote);
     DoUpdate();
 }
        /// <summary>
        /// Will calculate the result of this execution up until now, against the values of the provider.
        /// </summary>
        /// <param name="provider"></param>
        /// <returns></returns>
        public decimal?GetPendingResult(IQuoteProvider provider, Order.ResultModeEnum mode, DataSessionInfo info)
        {
            if (this.IsEmpty || _executedPrice.HasValue == false || this._executedPrice == 0 || provider == null)
            {
                return(null);
            }

            return(Order.GetResult(mode, _executedPrice, null, _volumeExecuted, Symbol, OrderStateEnum.Executed, OrderType, null, Symbol.Empty,
                                   info.LotSize, info.DecimalPlaces, provider.Ask, provider.Bid));
        }