Esempio n. 1
0
        protected override bool OnInitialize(Platform platform)
        {
            ChangeOperationalState(OperationalStateEnum.Operational);

            base.OnInitialize(platform);

            GeneralHelper.FireAndForget(delegate()
            {
                Thread.Sleep(2000);

                if (_dataSourceId.HasValue)
                {
                    _delivery = base.ObtainDataDelivery(_dataSourceId.Value);
                    if (_delivery != null)
                    {
                        _delivery.OperationalStateChangedEvent += new OperationalStateChangedDelegate(_delivery_OperationalStatusChangedEvent);
                        if (_delivery.OperationalState == OperationalStateEnum.Operational)
                        {
                            _delivery_OperationalStatusChangedEvent(_delivery, OperationalStateEnum.Unknown);
                        }
                    }
                }
            });


            return(true);
        }
        /// <summary>
        /// 
        /// </summary>
        public DataTickHistoryProvider(ISourceDataDelivery dataDelivery, DataSessionInfo session)
        {
            _dataDelivery = dataDelivery;
            _session = session;

            Initialize();
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        public bool RemoveSymbol(Symbol symbol)
        {
            ISourceDataDelivery delivery = _delivery;

            if (delivery != null)
            {
                RuntimeDataSessionInformation info = delivery.GetSymbolRuntimeSessionInformation(symbol);
                if (info == null)
                {
                    return(false);
                }

                delivery.SubscribeToData(info.Info, false, new DataSubscriptionInfo(true, false, null));
            }

            lock (this)
            {
                _symbolQuotes.Remove(symbol);
                if (_symbolQuotes.Count == 0)
                {// Release the delivery on last.
                    //_delivery.UnInitialize();
                    //_delivery.Dispose();
                    //_delivery = null;
                    _dataSourceId = null;
                }
            }

            return(true);
        }
Esempio n. 4
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);
            }
        }
Esempio n. 5
0
        public bool AddSymbol(ComponentId dataSourceId, Symbol symbol, out string operationResultMessage)
        {
            if (_dataSourceId.HasValue && _dataSourceId.Value != dataSourceId)
            {
                operationResultMessage = "One source per watch component currently supported.";
                SystemMonitor.Error(operationResultMessage);
                return(false);
            }

            _dataSourceId = dataSourceId;
            if (_delivery == null)
            {
                _delivery = base.ObtainDataDelivery(_dataSourceId.Value);
            }

            RuntimeDataSessionInformation info = _delivery.GetSymbolRuntimeSessionInformation(symbol);

            if (info == null)
            {
                operationResultMessage = "Failed to obtain symbol runtime session information.";
                return(false);
            }

            _delivery.SubscribeToData(info.Info, true, new DataSubscriptionInfo(true, false, null));
            _delivery.QuoteUpdateEvent += new QuoteUpdateDelegate(delivery_QuoteUpdateEvent);

            operationResultMessage = string.Empty;
            return(true);
        }
Esempio n. 6
0
        /// <summary>
        /// Get all sources that match the given criteria; multiple criteria can be combined in one sourceType
        /// using bitwise operators (for ex. operationResult = operationResult | SourceTypeEnum.Remote).
        /// </summary>
        /// <param name="sourceType"></param>
        /// <param name="partialMatch">Should we try to match filtering criteria fully or partially.</param>
        /// <returns></returns>
        public Dictionary <Symbol, TimeSpan[]> SearchSymbols(ComponentId sourceId, string symbolMatch)
        {
            if (IsLocalSource(sourceId))
            {
                ISourceDataDelivery delivery = ObtainDataDelivery(sourceId);
                return(delivery.SearchSymbols(symbolMatch));
            }

            List <ArbiterClientId?> sourcePath;

            if (GetSourcePath(sourceId, out sourcePath) == false)
            {
                SystemMonitor.OperationError("Failed to establish source path.");
                return(new Dictionary <Symbol, TimeSpan[]>());
            }

            RequestSymbolsMessage request = new RequestSymbolsMessage()
            {
                SymbolMatch = symbolMatch
            };
            ResponseMessage result = this.SendAndReceiveForwarding <ResponseMessage>(sourcePath, request);

            if (result != null && result.OperationResult)
            {
                return(((RequestSymbolsResponseMessage)result).SymbolsPeriods);
            }

            return(new Dictionary <Symbol, TimeSpan[]>());
        }
Esempio n. 7
0
        public virtual void Dispose()
        {
            //if (_tradeEntities != null)
            //{
            //    _tradeEntities.UnInitialize();
            //    _tradeEntities = null;
            //}

            if (_statistics != null)
            {
                _statistics.Dispose();
                _statistics = null;
            }

            _dataDelivery = null;
            _manager      = null;

            if (_provider != null)
            {
                _provider.AccountInfoUpdateEvent       -= new AccountInfoUpdateDelegate(_provider_AccountInfoUpdateEvent);
                _provider.OperationalStateChangedEvent -= new OperationalStateChangedDelegate(_provider_OperationalStatusChangedEvent);

                _provider = null;
            }
        }
Esempio n. 8
0
        /// <summary>
        ///
        /// </summary>
        public DataTickHistoryProvider(ISourceDataDelivery dataDelivery, DataSessionInfo session)
        {
            _dataDelivery = dataDelivery;
            _session      = session;

            Initialize();
        }
Esempio n. 9
0
        void _dataDelivery_QuoteUpdateEvent(ISourceDataDelivery dataDelivery, DataSessionInfo session, Quote?quote)
        {
            _lastDataQuote = quote;

            BeginAccountInfoUpdate(_account.Info);

            BeginManagedOrdersUpdate(quote);
        }
Esempio n. 10
0
        public void Dispose()
        {
            UnInitialize();
            ChangeOperationalState(OperationalStateEnum.Disposed);

            _dataBars     = null;
            _dataDelivery = null;
            _indicators   = null;
            _period       = null;
        }
Esempio n. 11
0
 void _dataDelivery_DataHistoryUpdateEvent(ISourceDataDelivery dataDelivery, DataSessionInfo session, DataHistoryUpdate update)
 {
     if (update.BarDataAssigned)
     {
         lock (this)
         {
             _lastDataBar = update.DataBarsUnsafe[update.DataBarsUnsafe.Count - 1];
         }
     }
 }
Esempio n. 12
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);
            }
        }
Esempio n. 13
0
        /// <summary>
        ///
        /// </summary>
        public bool SetInitialParameters(ISourceManager manager,
                                         ISourceOrderExecution provider,
                                         ISourceDataDelivery dataDelivery, Symbol symbol)
        {
            _manager       = manager;
            _orderProvider = provider;
            _dataDelivery  = dataDelivery;

            _info.Symbol = symbol;

            return(true);
        }
        public bool SetInitialParameters(ISourceManager manager, ComponentId sourceSourceId, ExpertSession session)
        {
            _manager = manager;

            _sessionInfo = session.Info;

            _sourceDataDelivery = manager.ObtainDataDelivery(sourceSourceId);
            //_sourceDataDelivery.OperationalStateChangedEvent += new OperationalStateChangedDelegate(_sourceDataDelivery_OperationalStateChangedEvent);

            StatusSynchronizationSource = _sourceDataDelivery;

            return(true);
        }
Esempio n. 15
0
        /// <summary>
        /// Initialize execution account with its owner provider.
        /// </summary>
        /// <param name="session"></param>
        public bool SetInitialParameters(ISourceManager manager, ISourceOrderExecution orderExecutionProvider, ISourceDataDelivery dataDelivery)
        {
            SystemMonitor.CheckError(_provider == null, "Order account already initialized.");

            _manager = manager;
            //why is the provider not ready
            _provider     = orderExecutionProvider;
            _dataDelivery = dataDelivery;

            SystemMonitor.CheckWarning(_manager != null && _provider != null && _dataDelivery != null, "Account not properly initialized.");

            return(true);
        }
        public void UnInitialize()
        {
            if (_provider != null)
            {
                _provider.TradeEntities.PositionsUpdatedEvent -= new PositionsUpdateDelegate(TradeEntities_PositionsUpdatedEvent);
                _provider.TradeEntities.PositionsAddedEvent   -= new PositionsUpdateDelegate(TradeEntities_PositionsAddedEvent);
                _provider.TradeEntities.PositionsRemovedEvent -= new PositionsUpdateDelegate(TradeEntities_PositionsRemovedEvent);
                _provider.PositionsUpdateEvent -= new PositionUpdateDelegate(_provider_PositionUpdateEvent);
            }

            _manager      = null;
            _provider     = null;
            _dataDelivery = null;
        }
Esempio n. 17
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;
            }
        }
Esempio n. 18
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();
        }
        /// <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();
        }
        /// <summary>
        ///
        /// </summary>
        public bool SetInitialParameters(ISourceManager manager, ISourceOrderExecution provider,
                                         ISourceDataDelivery delivery)
        {
            //SystemMonitor.CheckError(_provider == null, "OrderExecutionProvider already assigned.");

            _manager  = manager;
            _provider = provider;
            _delivery = delivery;

            if (_provider == null)
            {
                SystemMonitor.Warning("Failed to properly initialize entity keeper.");
            }

            return(true);
        }
Esempio n. 21
0
        public virtual void Dispose()
        {
            UnInitialize();

            _manager = null;

            _dataDelivery = null;

            _quote = null;

            _tickProvider = null;

            _dataBarProvider = null;

            ChangeOperationalState(OperationalStateEnum.Disposed);
        }
        public void SetInitialParameters(ISourceDataDelivery dataDelivery)
        {
            base.StatusSynchronizationSource = dataDelivery;
            _dataDelivery = dataDelivery;

            if (_dataDelivery != null)
            {
                _dataDelivery.OperationalStateChangedEvent += new OperationalStateChangedDelegate(_dataDelivery_OperationalStatusChangedEvent);
                if (_dataDelivery.OperationalState == OperationalStateEnum.Operational)
                {
                    _dataDelivery_OperationalStatusChangedEvent(_dataDelivery, OperationalStateEnum.NotOperational);
                }

                _dataDelivery.QuoteUpdateEvent += new QuoteUpdateDelegate(_dataDelivery_QuoteUpdateEvent);
            }
        }
Esempio n. 23
0
        /// <summary>
        /// One time init by passing references essential for operation.
        /// </summary>
        public bool SetInitialParameters(ISourceManager manager, ISourceDataDelivery dataDelivery)
        {
            _manager      = manager;
            _dataDelivery = dataDelivery;

            _account.SetInitialParameters(_manager, this, _dataDelivery);
            _tradeEntities.SetInitialParameters(_manager, this, _dataDelivery);

            _timeControl                 = new TimeControl();
            _timeControl.CanStepBack     = false;
            _timeControl.CanStepForward  = true;
            _timeControl.CanRestart      = false;
            _timeControl.TotalStepsCount = int.MaxValue;

            ChangeOperationalState(OperationalStateEnum.Initializing);

            return(true);
        }
        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);
            }
        }
        public IQuoteProvider ObtainQuoteProvider(ComponentId source, Symbol symbol)
        {
            if (source.IsEmpty || symbol.IsEmpty)
            {
                SystemMonitor.Warning("Source or symbol empty.");
                return(null);
            }

            lock (this)
            {
                if (_quoteProviders.ContainsKey(source) &&
                    _quoteProviders[source].ContainsKey(symbol))
                {
                    return(_quoteProviders[source][symbol]);
                }
            }

            ISourceDataDelivery delivery = ObtainDataDelivery(source);

            if (delivery == null)
            {
                SystemMonitor.OperationError("Failed to establish data delivery for quote provider.");
                return(null);
            }

            RuntimeDataSessionInformation session = delivery.GetSymbolRuntimeSessionInformation(symbol);

            if (session == null)
            {
                SystemMonitor.OperationError("Failed to establish runtime session information for symbol [" + symbol.Name + "]");
                return(null);
            }

            QuoteProvider provider = new QuoteProvider(session.Info);

            // Make sure to *add before setting up* the initial paramters,
            // since a call from them can get back to this method, and cause a stack overflow.
            AddElement(source, symbol, provider);

            provider.SetInitialParameters(delivery);

            return(provider);
        }
        public IDataBarHistoryProvider ObtainDataBarHistoryProvider(ComponentId source, Symbol symbol, TimeSpan period)
        {
            if (source.IsEmpty || symbol.IsEmpty)
            {
                SystemMonitor.Warning("Source or symbol empty.");
                return(null);
            }

            lock (this)
            {
                if (_dataBarProviders.ContainsKey(source) &&
                    _dataBarProviders[source].ContainsKey(symbol) &&
                    _dataBarProviders[source][symbol].ContainsKey(period))
                {
                    return(_dataBarProviders[source][symbol][period]);
                }
            }

            ISourceDataDelivery delivery = GetDataDelivery(source);

            if (delivery == null)
            {
                SystemMonitor.OperationError("Failed to establish data delivery for quote provider.");
                return(null);
            }

            RuntimeDataSessionInformation session = delivery.GetSymbolRuntimeSessionInformation(symbol);

            if (session == null)
            {
                SystemMonitor.OperationError("Failed to establish runtime session information for symbol [" + symbol.Name + "]");
            }

            DataBarHistoryProvider provider = new DataBarHistoryProvider(delivery, session.Info, period, null);

            // By default assign a volume indicator.
            provider.Indicators.AddIndicator(new VolumeCustom());

            AddElement(source, symbol, period, provider);

            return(provider);
        }
        /// <summary>
        ///
        /// </summary>
        public ISourceDataDelivery ObtainDataDelivery(ComponentId sourceId)
        {
            ISourceDataDelivery result = GetDataDelivery(sourceId);

            if (result != null)
            {
                return(result);
            }

            result = CreateDataDelivery(sourceId);

            if (result != null)
            {
                result.Initialize();
                AddElement(sourceId, result);

                return(result);
            }

            return(null);
        }
        /// <summary>
        ///
        /// </summary>
        public void Initialize(ISourceAndExpertSessionManager manager, ISourceDataDelivery dataDelivery, ISourceOrderExecution provider)
        {
            _manager      = manager;
            _provider     = provider;
            _dataDelivery = dataDelivery;

            if (_provider != null)
            {
                _provider.TradeEntities.PositionsUpdatedEvent += new PositionsUpdateDelegate(TradeEntities_PositionsUpdatedEvent);
                _provider.TradeEntities.PositionsAddedEvent   += new PositionsUpdateDelegate(TradeEntities_PositionsAddedEvent);
                _provider.TradeEntities.PositionsRemovedEvent += new PositionsUpdateDelegate(TradeEntities_PositionsRemovedEvent);
                _provider.PositionsUpdateEvent += new PositionUpdateDelegate(_provider_PositionUpdateEvent);
            }

            UpdateUI();

            if (listView.Items.Count > 0 && listView.SelectedItems.Count == 0)
            {
                listView.Items[0].Selected = true;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public void Initialize(ISourceAndExpertSessionManager manager, ISourceDataDelivery dataDelivery, ISourceOrderExecution provider)
        {
            _manager = manager;
            _provider = provider;
            _dataDelivery = dataDelivery;

            if (_provider != null)
            {
                _provider.TradeEntities.PositionsUpdatedEvent += new PositionsUpdateDelegate(TradeEntities_PositionsUpdatedEvent);
                _provider.TradeEntities.PositionsAddedEvent += new PositionsUpdateDelegate(TradeEntities_PositionsAddedEvent);
                _provider.TradeEntities.PositionsRemovedEvent += new PositionsUpdateDelegate(TradeEntities_PositionsRemovedEvent);
                _provider.PositionsUpdateEvent += new PositionUpdateDelegate(_provider_PositionUpdateEvent);
            }

            UpdateUI();

            if (listView.Items.Count > 0 && listView.SelectedItems.Count == 0)
            {
                listView.Items[0].Selected = true;
            }
        }
Esempio n. 30
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);
        }
        public IDataTickHistoryProvider ObtainDataTickHistoryProvider(ComponentId source, Symbol symbol)
        {
            if (source.IsEmpty || symbol.IsEmpty)
            {
                SystemMonitor.Warning("Source or symbol empty.");
                return(null);
            }

            lock (this)
            {
                if (_dataTickProviders.ContainsKey(source) &&
                    _dataTickProviders[source].ContainsKey(symbol))
                {
                    return(_dataTickProviders[source][symbol]);
                }
            }

            ISourceDataDelivery delivery = ObtainDataDelivery(source);

            if (delivery == null)
            {
                SystemMonitor.OperationError("Failed to establish data delivery for quote provider.");
                return(null);
            }

            RuntimeDataSessionInformation session = delivery.GetSymbolRuntimeSessionInformation(symbol);

            if (session == null)
            {
                SystemMonitor.OperationError("Failed to establish runtime session information for symbol [" + symbol.Name + "]");
                return(null);
            }

            DataTickHistoryProvider provider = new DataTickHistoryProvider(delivery, session.Info);

            AddElement(source, symbol, provider);

            return(provider);
        }
        /// <summary>
        /// Create a and setup a dataDelivery provider for this sourceSourceId, and to the given sessionInformation.
        /// </summary>
        /// <param name="sourceSourceId"></param>
        /// <returns></returns>
        public SessionDataProvider CreateSessionDataProvider(ComponentId sourceId, PlatformExpertSession session /*, bool isBackTest*/)
        {
            // Create dataDelivery delivery (mandatory).
            ISourceDataDelivery dataDelivery = ObtainDataDelivery(sourceId);

            if (dataDelivery == null)
            {
                SystemMonitor.OperationError("Failed to create data delivery.");
                return(null);
            }

            SessionDataProvider dataProvider = new SessionDataProvider();

            // Setup the provider with the delivery.
            if (dataProvider.SetInitialParameters(this, sourceId, session) == false)
            {
                SystemMonitor.OperationError("Failed to create session data provider.");
                return(null);
            }

            return(dataProvider);
        }
Esempio n. 33
0
        void delivery_QuoteUpdateEvent(ISourceDataDelivery dataDelivery, DataSessionInfo session, Quote?quote)
        {
            bool isUp = true;
            SymbolInformation information;

            lock (this)
            {
                if (_symbolQuotes.ContainsKey(session.Symbol) == false ||
                    _symbolQuotes[session.Symbol] == null)
                {
                    information = new SymbolInformation(session.Symbol);
                    _symbolQuotes.Add(session.Symbol, information);
                }
                else
                {
                    information = _symbolQuotes[session.Symbol];
                    if (information.Quote.HasValue && quote.HasValue)
                    {
                        if (information.Quote.Value.Ask == quote.Value.Ask &&
                            information.Quote.Value.Bid == quote.Value.Bid)
                        {
                            return;
                        }

                        isUp = information.Quote.Value.Ask < quote.Value.Ask;
                    }
                }

                information.Update(quote, isUp);
            }

            if (QuotesUpdateEvent != null)
            {
                QuotesUpdateEvent(this, information);
            }
        }
        public void Dispose()
        {
            UnInitialize();
            ChangeOperationalState(OperationalStateEnum.Disposed);

            _dataBars = null;
            _dataDelivery = null;
            _indicators = null;
            _period = null;
        }
        /// <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;
        }
        public void SetInitialParameters(ISourceDataDelivery dataDelivery)
        {
            base.StatusSynchronizationSource = dataDelivery;
            _dataDelivery = dataDelivery;

            if (_dataDelivery != null)
            {
                _dataDelivery.OperationalStateChangedEvent += new OperationalStateChangedDelegate(_dataDelivery_OperationalStatusChangedEvent);
                if (_dataDelivery.OperationalState == OperationalStateEnum.Operational)
                {
                    _dataDelivery_OperationalStatusChangedEvent(_dataDelivery, OperationalStateEnum.NotOperational);
                }

                _dataDelivery.QuoteUpdateEvent += new QuoteUpdateDelegate(_dataDelivery_QuoteUpdateEvent);
            }
        }
Esempio n. 37
0
 protected virtual void dataDelivery_QuoteUpdateEvent(ISourceDataDelivery dataDelivery, DataSessionInfo session, Quote? quote)
 {
 }
Esempio n. 38
0
        public virtual void Dispose()
        {
            //if (_tradeEntities != null)
            //{
            //    _tradeEntities.UnInitialize();
            //    _tradeEntities = null;
            //}

            if (_statistics != null)
            {
                _statistics.Dispose();
                _statistics = null;
            }

            _dataDelivery = null;
            _manager = null;

            if (_provider != null)
            {
                _provider.AccountInfoUpdateEvent -= new AccountInfoUpdateDelegate(_provider_AccountInfoUpdateEvent);
                _provider.OperationalStateChangedEvent -= new OperationalStateChangedDelegate(_provider_OperationalStatusChangedEvent);

                _provider = null;
            }
        }
        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>
        /// 
        /// </summary>
        public bool SetInitialParameters(ISourceManager manager, ISourceOrderExecution provider,
            ISourceDataDelivery delivery)
        {
            //SystemMonitor.CheckError(_provider == null, "OrderExecutionProvider already assigned.");

            _manager = manager;
            _provider = provider;
            _delivery = delivery;

            if (_provider == null)
            {
                SystemMonitor.Warning("Failed to properly initialize entity keeper.");
            }

            return true;
        }
 public void Dispose()
 {
     _manager = null;
     _provider = null;
     _delivery = null;
 }
Esempio n. 42
0
        /// <summary>
        /// 
        /// </summary>
        public bool SetInitialParameters(ISourceManager manager,
            ISourceOrderExecution provider, 
            ISourceDataDelivery dataDelivery, Symbol symbol)
        {
            _manager = manager;
            _provider = provider;
            _dataDelivery = dataDelivery;

            _info.Symbol = symbol;

            return true;
        }
Esempio n. 43
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);
            }
        }
 void _dataDelivery_DataHistoryUpdateEvent(ISourceDataDelivery dataDelivery, DataSessionInfo session, DataHistoryUpdate update)
 {
     if (update.BarDataAssigned)
     {
         lock (this)
         {
             _lastDataBar = update.DataBarsUnsafe[update.DataBarsUnsafe.Count - 1];
         }
     }
 }
        public bool SetInitialParameters(ISourceManager manager, ComponentId sourceSourceId, ExpertSession session)
        {
            _manager = manager;

            _sessionInfo = session.Info;

            _sourceDataDelivery = manager.ObtainDataDelivery(sourceSourceId);
            //_sourceDataDelivery.OperationalStateChangedEvent += new OperationalStateChangedDelegate(_sourceDataDelivery_OperationalStateChangedEvent);

            StatusSynchronizationSource = _sourceDataDelivery;

            return true;
        }
        public void UnInitialize()
        {
            if (_provider != null)
            {
                _provider.TradeEntities.PositionsUpdatedEvent -= new PositionsUpdateDelegate(TradeEntities_PositionsUpdatedEvent);
                _provider.TradeEntities.PositionsAddedEvent -= new PositionsUpdateDelegate(TradeEntities_PositionsAddedEvent);
                _provider.TradeEntities.PositionsRemovedEvent -= new PositionsUpdateDelegate(TradeEntities_PositionsRemovedEvent);
                _provider.PositionsUpdateEvent -= new PositionUpdateDelegate(_provider_PositionUpdateEvent);
            }

            _manager = null;
            _provider = null;
            _dataDelivery = null;
        }
 protected override void dataDelivery_QuoteUpdateEvent(ISourceDataDelivery dataDelivery, DataSessionInfo session, Quote? quote)
 {
     base.dataDelivery_QuoteUpdateEvent(dataDelivery, session, quote);
     DoUpdate();
 }
        protected override bool OnInitialize(Platform platform)
        {
            ChangeOperationalState(OperationalStateEnum.Operational);

            base.OnInitialize(platform);

            GeneralHelper.FireAndForget(delegate()
            {
                Thread.Sleep(2000);

                if (_dataSourceId.HasValue)
                {
                    _delivery = base.ObtainDataDelivery(_dataSourceId.Value);
                    if (_delivery != null)
                    {
                        _delivery.OperationalStateChangedEvent += new OperationalStateChangedDelegate(_delivery_OperationalStatusChangedEvent);
                        if (_delivery.OperationalState == OperationalStateEnum.Operational)
                        {
                            _delivery_OperationalStatusChangedEvent(_delivery, OperationalStateEnum.Unknown);
                        }
                    }
                }
            });

            return true;
        }
        /// <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;
            }
        }
        void delivery_QuoteUpdateEvent(ISourceDataDelivery dataDelivery, DataSessionInfo session, Quote? quote)
        {
            bool isUp = true;
            SymbolInformation information;
            lock (this)
            {
                if (_symbolQuotes.ContainsKey(session.Symbol) == false
                    || _symbolQuotes[session.Symbol] == null)
                {
                    information = new SymbolInformation(session.Symbol);
                    _symbolQuotes.Add(session.Symbol, information);
                }
                else
                {
                    information = _symbolQuotes[session.Symbol];
                    if (information.Quote.HasValue && quote.HasValue)
                    {
                        if (information.Quote.Value.Ask == quote.Value.Ask
                            && information.Quote.Value.Bid == quote.Value.Bid)
                        {
                            return;
                        }

                        isUp = information.Quote.Value.Ask < quote.Value.Ask;
                    }
                }

                information.Update(quote, isUp);
            }

            if (QuotesUpdateEvent != null)
            {
                QuotesUpdateEvent(this, information);
            }
        }
Esempio n. 51
0
        /// <summary>
        /// Initialize execution account with its owner provider.
        /// </summary>
        /// <param name="session"></param>
        public bool SetInitialParameters(ISourceManager manager, ISourceOrderExecution orderExecutionProvider, ISourceDataDelivery dataDelivery)
        {
            SystemMonitor.CheckError(_provider == null, "Order account already initialized.");

            _manager = manager;
            //why is the provider not ready
            _provider = orderExecutionProvider;
            _dataDelivery = dataDelivery;

            SystemMonitor.CheckWarning(_manager != null && _provider != null && _dataDelivery != null, "Account not properly initialized.");

            return true;
        }
        public bool AddSymbol(ComponentId dataSourceId, Symbol symbol, out string operationResultMessage)
        {
            if (_dataSourceId.HasValue && _dataSourceId.Value != dataSourceId)
            {
                operationResultMessage = "One source per watch component currently supported.";
                SystemMonitor.Error(operationResultMessage);
                return false;
            }

            _dataSourceId = dataSourceId;
            if (_delivery == null)
            {
                _delivery = base.ObtainDataDelivery(_dataSourceId.Value);
            }

            RuntimeDataSessionInformation info = _delivery.GetSymbolRuntimeSessionInformation(symbol);
            if (info == null)
            {
                operationResultMessage = "Failed to obtain symbol runtime session information.";
                return false;
            }

            _delivery.SubscribeToData(info.Info, true, new DataSubscriptionInfo(true, false, null));
            _delivery.QuoteUpdateEvent += new QuoteUpdateDelegate(delivery_QuoteUpdateEvent);

            operationResultMessage = string.Empty;
            return true;
        }
        /// <summary>
        /// Register ISourceDataDelivery.
        /// </summary>
        protected bool AddElement(ComponentId id, ISourceDataDelivery delivery)
        {
            if (id.IsEmpty || delivery == null)
            {
                SystemMonitor.Warning("Invalid Id or data delivery instance.");
                return false;
            }

            lock (this)
            {
                if (_dataDeliveries.ContainsKey(id))
                {
                    SystemMonitor.Warning("Failed to add data delivery, since already added with this Id.");
                    return false;
                }

                foreach (ComponentId deliveryId in _dataDeliveries.Keys)
                {
                    SystemMonitor.CheckThrow(id.Name.ToLower() == id.Name.ToLower(), "Data Delivery with this name Id already present [" + id.Name + "].");
                }

                _dataDeliveries.Add(id, delivery);
            }

            return true;
        }
        /// <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);
            }
        }
        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);
            }
        }
        /// <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);
            }
        }
        void _dataDelivery_QuoteUpdateEvent(ISourceDataDelivery dataDelivery, DataSessionInfo session, Quote? quote)
        {
            _lastDataQuote = quote;

            BeginAccountInfoUpdate(_account.Info);

            BeginManagedOrdersUpdate(quote);
        }
        /// <summary>
        /// One time init by passing references essential for operation.
        /// </summary>
        public bool SetInitialParameters(ISourceManager manager, ISourceDataDelivery dataDelivery)
        {
            _manager = manager;
            _dataDelivery = dataDelivery;

            _account.SetInitialParameters(_manager, this, _dataDelivery);
            _tradeEntities.SetInitialParameters(_manager, this, _dataDelivery);

            _timeControl = new TimeControl();
            _timeControl.CanStepBack = false;
            _timeControl.CanStepForward = true;
            _timeControl.CanRestart = false;
            _timeControl.TotalStepsCount = int.MaxValue;

            ChangeOperationalState(OperationalStateEnum.Initializing);

            return true;
        }
 public void Dispose()
 {
     _manager = null;
     _dataDelivery = null;
 }
        public virtual void Dispose()
        {
            UnInitialize();

            _manager = null;

            _dataDelivery = null;

            _quote = null;

            _tickProvider = null;

            _dataBarProvider = null;

            ChangeOperationalState(OperationalStateEnum.Disposed);
        }