コード例 #1
0
ファイル: FileSystemDataFeed.cs プロジェクト: zxbe/Lean
        /// <summary>
        /// Initializes the data feed for the specified job and algorithm
        /// </summary>
        public void Initialize(IAlgorithm algorithm,
                               AlgorithmNodePacket job,
                               IResultHandler resultHandler,
                               IMapFileProvider mapFileProvider,
                               IFactorFileProvider factorFileProvider,
                               IDataProvider dataProvider,
                               IDataFeedSubscriptionManager subscriptionManager,
                               IDataFeedTimeProvider dataFeedTimeProvider)
        {
            _algorithm               = algorithm;
            _resultHandler           = resultHandler;
            _mapFileProvider         = mapFileProvider;
            _factorFileProvider      = factorFileProvider;
            _dataProvider            = dataProvider;
            _subscriptions           = subscriptionManager.DataFeedSubscriptions;
            _universeSelection       = subscriptionManager.UniverseSelection;
            _cancellationTokenSource = new CancellationTokenSource();
            _subscriptionFactory     = new SubscriptionDataReaderSubscriptionEnumeratorFactory(
                _resultHandler,
                _mapFileProvider,
                _factorFileProvider,
                _dataProvider,
                includeAuxiliaryData: true);

            IsActive = true;
            var threadCount = Math.Max(1, Math.Min(4, Environment.ProcessorCount - 3));
        }
コード例 #2
0
        /// <summary>
        /// Initializes the data feed for the specified job and algorithm
        /// </summary>
        public void Initialize(IAlgorithm algorithm,
                               AlgorithmNodePacket job,
                               IResultHandler resultHandler,
                               IMapFileProvider mapFileProvider,
                               IFactorFileProvider factorFileProvider,
                               IDataProvider dataProvider,
                               IDataFeedSubscriptionManager subscriptionManager,
                               IDataFeedTimeProvider dataFeedTimeProvider,
                               IDataChannelProvider dataChannelProvider)
        {
            _algorithm               = algorithm;
            _resultHandler           = resultHandler;
            _mapFileProvider         = mapFileProvider;
            _factorFileProvider      = factorFileProvider;
            _dataProvider            = dataProvider;
            _timeProvider            = dataFeedTimeProvider.FrontierTimeProvider;
            _subscriptions           = subscriptionManager.DataFeedSubscriptions;
            _cancellationTokenSource = new CancellationTokenSource();
            _subscriptionFactory     = new SubscriptionDataReaderSubscriptionEnumeratorFactory(
                _resultHandler,
                _mapFileProvider,
                _factorFileProvider,
                _dataProvider,
                includeAuxiliaryData: true,
                enablePriceScaling: false);

            IsActive = true;
        }
コード例 #3
0
 public void Initialize(
     IAlgorithm algorithm,
     AlgorithmNodePacket job,
     IResultHandler resultHandler,
     IMapFileProvider mapFileProvider,
     IFactorFileProvider factorFileProvider,
     IDataProvider dataProvider,
     IDataFeedSubscriptionManager subscriptionManager,
     IDataFeedTimeProvider dataFeedTimeProvider
     )
 {
 }
コード例 #4
0
 /// <inheritdoc />
 public void Initialize(
     IAlgorithm algorithm,
     AlgorithmNodePacket job,
     IResultHandler resultHandler,
     IMapFileProvider mapFileProvider,
     IFactorFileProvider factorFileProvider,
     IDataProvider dataProvider,
     IDataFeedSubscriptionManager subscriptionManager,
     IDataFeedTimeProvider dataFeedTimeProvider
     )
 {
     throw new NotImplementedException("Unexpected usage of null data feed implementation.");
 }
コード例 #5
0
        /// <summary>
        /// Initializes the data feed for the specified job and algorithm
        /// </summary>
        public void Initialize(IAlgorithm algorithm,
                               AlgorithmNodePacket job,
                               IResultHandler resultHandler,
                               IMapFileProvider mapFileProvider,
                               IFactorFileProvider factorFileProvider,
                               IDataProvider dataProvider,
                               IDataFeedSubscriptionManager subscriptionManager,
                               IDataFeedTimeProvider dataFeedTimeProvider,
                               IDataChannelProvider dataChannelProvider)
        {
            if (!(job is LiveNodePacket))
            {
                throw new ArgumentException("The LiveTradingDataFeed requires a LiveNodePacket.");
            }

            _cancellationTokenSource = new CancellationTokenSource();

            _job                  = (LiveNodePacket)job;
            _timeProvider         = dataFeedTimeProvider.TimeProvider;
            _dataProvider         = dataProvider;
            _mapFileProvider      = mapFileProvider;
            _factorFileProvider   = factorFileProvider;
            _channelProvider      = dataChannelProvider;
            _frontierTimeProvider = dataFeedTimeProvider.FrontierTimeProvider;
            _customExchange       = new BaseDataExchange("CustomDataExchange")
            {
                SleepInterval = 10
            };
            _subscriptions = subscriptionManager.DataFeedSubscriptions;

            _dataQueueHandler = GetDataQueueHandler();
            _dataQueueHandler?.SetJob(_job);

            // run the custom data exchange
            var manualEvent = new ManualResetEventSlim(false);

            Task.Factory.StartNew(() =>
            {
                manualEvent.Set();
                _customExchange.Start(_cancellationTokenSource.Token);
            }, TaskCreationOptions.LongRunning);
            manualEvent.Wait();
            manualEvent.DisposeSafely();

            IsActive = true;
        }
コード例 #6
0
        /// <summary>
        /// Initializes the data feed for the specified job and algorithm
        /// </summary>
        public void Initialize(IAlgorithm algorithm,
                               AlgorithmNodePacket job,
                               IResultHandler resultHandler,
                               IMapFileProvider mapFileProvider,
                               IFactorFileProvider factorFileProvider,
                               IDataProvider dataProvider,
                               IDataFeedSubscriptionManager subscriptionManager,
                               IDataFeedTimeProvider dataFeedTimeProvider)
        {
            if (!(job is LiveNodePacket))
            {
                throw new ArgumentException("The LiveTradingDataFeed requires a LiveNodePacket.");
            }

            _cancellationTokenSource = new CancellationTokenSource();

            _algorithm = algorithm;
            _job       = (LiveNodePacket)job;

            _timeProvider     = dataFeedTimeProvider.TimeProvider;
            _dataQueueHandler = GetDataQueueHandler();
            _dataProvider     = dataProvider;
            _channelProvider  = GetDataChannelProvider();

            _frontierTimeProvider = dataFeedTimeProvider.FrontierTimeProvider;
            _customExchange       = new BaseDataExchange("CustomDataExchange")
            {
                SleepInterval = 10
            };
            // sleep is controlled on this exchange via the GetNextTicksEnumerator
            _exchange = new BaseDataExchange("DataQueueExchange")
            {
                SleepInterval = 0
            };
            _exchange.AddEnumerator(DataQueueHandlerSymbol, GetNextTicksEnumerator());
            _subscriptions = subscriptionManager.DataFeedSubscriptions;

            _universeSelection = subscriptionManager.UniverseSelection;

            // run the exchanges
            Task.Run(() => _exchange.Start(_cancellationTokenSource.Token));
            Task.Run(() => _customExchange.Start(_cancellationTokenSource.Token));

            IsActive = true;
        }
コード例 #7
0
        /// <summary>
        /// Initializes the data feed for the specified job and algorithm
        /// </summary>
        public override void Initialize(IAlgorithm algorithm,
                                        AlgorithmNodePacket job,
                                        IResultHandler resultHandler,
                                        IMapFileProvider mapFileProvider,
                                        IFactorFileProvider factorFileProvider,
                                        IDataProvider dataProvider,
                                        IDataFeedSubscriptionManager subscriptionManager,
                                        IDataFeedTimeProvider dataFeedTimeProvider,
                                        IDataChannelProvider dataChannelProvider)
        {
            if (!(job is LiveNodePacket))
            {
                throw new ArgumentException("The LiveTradingDataFeed requires a LiveNodePacket.");
            }

            _algorithm            = algorithm;
            _job                  = (LiveNodePacket)job;
            _timeProvider         = dataFeedTimeProvider.TimeProvider;
            _dataProvider         = dataProvider;
            _mapFileProvider      = mapFileProvider;
            _factorFileProvider   = factorFileProvider;
            _channelProvider      = dataChannelProvider;
            _frontierTimeProvider = dataFeedTimeProvider.FrontierTimeProvider;
            _customExchange       = GetBaseDataExchange();
            _subscriptions        = subscriptionManager.DataFeedSubscriptions;

            _dataQueueHandler = GetDataQueueHandler();
            _dataQueueHandler?.SetJob(_job);

            // run the custom data exchange
            _customExchange.Start();

            IsActive = true;

            base.Initialize(algorithm, job, resultHandler, mapFileProvider, factorFileProvider, dataProvider, subscriptionManager, dataFeedTimeProvider, dataChannelProvider);
        }
コード例 #8
0
ファイル: LiveTradingDataFeed.cs プロジェクト: jalenka/Lean
        /// <summary>
        /// Initializes the data feed for the specified job and algorithm
        /// </summary>
        public void Initialize(IAlgorithm algorithm,
                               AlgorithmNodePacket job,
                               IResultHandler resultHandler,
                               IMapFileProvider mapFileProvider,
                               IFactorFileProvider factorFileProvider,
                               IDataProvider dataProvider,
                               IDataFeedSubscriptionManager subscriptionManager,
                               IDataFeedTimeProvider dataFeedTimeProvider)
        {
            if (!(job is LiveNodePacket))
            {
                throw new ArgumentException("The LiveTradingDataFeed requires a LiveNodePacket.");
            }

            _cancellationTokenSource = new CancellationTokenSource();

            _algorithm = algorithm;
            _job       = (LiveNodePacket)job;

            _timeProvider     = dataFeedTimeProvider.TimeProvider;
            _dataQueueHandler = GetDataQueueHandler();
            _dataProvider     = dataProvider;

            _frontierTimeProvider = dataFeedTimeProvider.FrontierTimeProvider;
            _customExchange       = new BaseDataExchange("CustomDataExchange")
            {
                SleepInterval = 10
            };
            // sleep is controlled on this exchange via the GetNextTicksEnumerator
            _exchange = new BaseDataExchange("DataQueueExchange")
            {
                SleepInterval = 0
            };
            _exchange.AddEnumerator(DataQueueHandlerSymbol, GetNextTicksEnumerator());
            _subscriptions = subscriptionManager.DataFeedSubscriptions;

            _universeSelection = subscriptionManager.UniverseSelection;

            // run the exchanges
            Task.Run(() => _exchange.Start(_cancellationTokenSource.Token));
            Task.Run(() => _customExchange.Start(_cancellationTokenSource.Token));

            IsActive = true;
            // wire ourselves up to receive notifications when universes are added/removed
            var start = _timeProvider.GetUtcNow();

            algorithm.UniverseManager.CollectionChanged += (sender, args) =>
            {
                switch (args.Action)
                {
                case NotifyCollectionChangedAction.Add:
                    foreach (var universe in args.NewItems.OfType <Universe>())
                    {
                        var config = universe.Configuration;
                        var marketHoursDatabase = MarketHoursDatabase.FromDataFolder();
                        var exchangeHours       = marketHoursDatabase.GetExchangeHours(config);

                        Security security;
                        if (!_algorithm.Securities.TryGetValue(config.Symbol, out security))
                        {
                            // create a canonical security object
                            security = new Security(
                                exchangeHours,
                                config,
                                _algorithm.Portfolio.CashBook[CashBook.AccountCurrency],
                                SymbolProperties.GetDefault(CashBook.AccountCurrency),
                                _algorithm.Portfolio.CashBook
                                );
                        }

                        AddSubscription(new SubscriptionRequest(true, universe, security, config, start, Time.EndOfTime));
                    }
                    break;

                case NotifyCollectionChangedAction.Remove:
                    foreach (var universe in args.OldItems.OfType <Universe>())
                    {
                        RemoveSubscription(universe.Configuration);
                    }
                    break;

                default:
                    throw new NotImplementedException("The specified action is not implemented: " + args.Action);
                }
            };
        }
コード例 #9
0
        /// <summary>
        /// Initializes the data feed for the specified job and algorithm
        /// </summary>
        public void Initialize(IAlgorithm algorithm,
                               AlgorithmNodePacket job,
                               IResultHandler resultHandler,
                               IMapFileProvider mapFileProvider,
                               IFactorFileProvider factorFileProvider,
                               IDataProvider dataProvider,
                               IDataFeedSubscriptionManager subscriptionManager,
                               IDataFeedTimeProvider dataFeedTimeProvider)
        {
            _algorithm               = algorithm;
            _resultHandler           = resultHandler;
            _mapFileProvider         = mapFileProvider;
            _factorFileProvider      = factorFileProvider;
            _dataProvider            = dataProvider;
            _subscriptions           = subscriptionManager.DataFeedSubscriptions;
            _universeSelection       = subscriptionManager.UniverseSelection;
            _cancellationTokenSource = new CancellationTokenSource();
            _subscriptionFactory     = new SubscriptionDataReaderSubscriptionEnumeratorFactory(_resultHandler, _mapFileProvider, _factorFileProvider, _dataProvider, false, true);

            IsActive = true;
            var threadCount = Math.Max(1, Math.Min(4, Environment.ProcessorCount - 3));

            _controller = new ParallelRunnerController(threadCount);
            _controller.Start(_cancellationTokenSource.Token);

            // wire ourselves up to receive notifications when universes are added/removed
            algorithm.UniverseManager.CollectionChanged += (sender, args) =>
            {
                switch (args.Action)
                {
                case NotifyCollectionChangedAction.Add:
                    foreach (var universe in args.NewItems.OfType <Universe>())
                    {
                        var config = universe.Configuration;
                        var start  = _algorithm.UtcTime;

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

                        Security security;
                        if (!_algorithm.Securities.TryGetValue(config.Symbol, out security))
                        {
                            // create a canonical security object if it doesn't exist
                            security = new Security(
                                exchangeHours,
                                config,
                                _algorithm.Portfolio.CashBook[CashBook.AccountCurrency],
                                SymbolProperties.GetDefault(CashBook.AccountCurrency),
                                _algorithm.Portfolio.CashBook
                                );
                        }

                        var end = _algorithm.EndDate.ConvertToUtc(_algorithm.TimeZone);
                        AddSubscription(new SubscriptionRequest(true, universe, security, config, start, end));
                    }
                    break;

                case NotifyCollectionChangedAction.Remove:
                    foreach (var universe in args.OldItems.OfType <Universe>())
                    {
                        RemoveSubscription(universe.Configuration);
                    }
                    break;

                default:
                    throw new NotImplementedException("The specified action is not implemented: " + args.Action);
                }
            };
        }