/// <summary>
        /// Gets stocks to retrieve quotes for
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Stock> GetStocks(Subscription subscription)
        {
            if (subscription == null) throw new ArgumentNullException("subscription");

            // get the latest data for the subscription
            return _marketDataRepository.GetStaticStocksForSubscription(subscription.ID);
        }
        /// <summary>
        /// Instantiates a <see cref="MarketDataSubscription"/>
        /// </summary>
        /// <param name="logger">The logger</param>
        /// <param name="repositoryFactory">The repository to store and retrieve market data</param>
        /// <param name="marketDataProvider">The provider for refreshing market data</param>
        /// <param name="stockListProvider">The provider for lists of stocks for which to retrieve data</param>
        /// <param name="quotesPublisher"></param>
        /// <param name="subscriptionData">The subscriptionData data for determining</param>
        public MarketDataSubscription(ILog logger,
            IMarketDataRepositoryFactory repositoryFactory,
            IMarketDataProvider marketDataProvider,
            IStockListProvider stockListProvider,
            IPublisher<NewQuotesData> quotesPublisher,
            Subscription subscriptionData)
        {
            // perform null checks
            if (logger == null)
                throw new ArgumentNullException("logger");
            if (repositoryFactory == null)
                throw new ArgumentNullException("repositoryFactory");
            if (marketDataProvider == null)
                throw new ArgumentNullException("marketDataProvider");
            if (subscriptionData == null)
                throw new ArgumentNullException("subscriptionData");

            // set dependencies
            _logger = logger;
            _repositoryFactory = repositoryFactory;
            _marketDataProvider = marketDataProvider;
            _stockListProvider = stockListProvider;
            _quotesPublisher = quotesPublisher;
            _subscriptionData = subscriptionData;

            // set up timer
            _timer = new Timer(obj => GetLatestQuotes());

            // status initialized to idle
            Status = SubscriptionStatus.Idle;
        }
        public void GetLatestData()
        {
            // create fakes
            var logger = A.Fake<ILog>();
            var marketDataProvider = A.Fake<IMarketDataProvider>();
            var stockListProvider = A.Fake<IStockListProvider>();
            var marketDataRepositoryFactory = A.Fake<IMarketDataRepositoryFactory>();
            var marketDataRepository = A.Fake<IMarketDataRepository>();

            A.CallTo(() => marketDataRepositoryFactory.CreateRepository()).Returns(marketDataRepository);

            var stockQuoteSet = A.Fake<IDbSet<StockQuote>>();
            marketDataRepository.StockQuotes = stockQuoteSet;

            // create subscriptionData
            var stocks = new List<Stock>();
            var updateIntervalTime = TimeSpan.FromMilliseconds(int.MaxValue);
            var subscription = new Subscription
                {
                    UpdateInterval = new DateTime(1900, 1, 1, updateIntervalTime.Hours, updateIntervalTime.Minutes, updateIntervalTime.Seconds),
                    TimeOfDayStart = new DateTime(1900, 1, 1, 0, 0, 0),
                    TimeOfDayEnd = new DateTime(1900, 1, 1, 23, 59, 59, 999)
                };

            // create fake return value
            var stockQuotes = new List<StockQuote>
                {
                    new StockQuote { AskPrice = 1.0m, BidPrice = 1.2m, Change = 0.01m, OpenPrice = 0.9m, QuoteDateTime = DateTime.Now, Symbol = "TST1" },
                    new StockQuote { AskPrice = 2.0m, BidPrice = 2.2m, Change = 0.02m, OpenPrice = 1.0m, QuoteDateTime = DateTime.Now, Symbol = "TST2" },
                    new StockQuote { AskPrice = 3.0m, BidPrice = 3.2m, Change = 0.03m, OpenPrice = 1.1m, QuoteDateTime = DateTime.Now, Symbol = "TST3" }
                };

            // hold onto list of quotes added to repository
            var repositoryQuotes = new List<StockQuote>();

            // handle calls to fakes
            A.CallTo(() => stockListProvider.GetStocks(subscription)).Returns(stocks);
            A.CallTo(() => marketDataProvider.GetQuotes(stocks)).Returns(stockQuotes);
            A.CallTo(() => stockQuoteSet.Add(A<StockQuote>.Ignored))
             .Invokes((StockQuote quote) => repositoryQuotes.Add(quote));

            // create subscriptionData
            var marketDataSubscription =
                new MarketDataSubscription(logger, marketDataRepositoryFactory, marketDataProvider, stockListProvider, null, subscription);

            // call get latest data
            marketDataSubscription.InvokeMethod("GetLatestQuotes");

            // ensure call to get data was made
            A.CallTo(() => marketDataProvider.GetQuotes(stocks)).MustHaveHappened();
            
            // ensure call to store data was made
            A.CallTo(() => stockQuoteSet.Add(A<StockQuote>.Ignored)).MustHaveHappened(Repeated.Exactly.Times(3));

            // check that quotes were added to the repository
            repositoryQuotes.Should().HaveCount(3);
            repositoryQuotes.Should().OnlyContain(q => stockQuotes.Contains(q));
        }
示例#4
0
        /// <summary>
        /// Creates a market data subscription
        /// </summary>
        /// <param name="subscription"></param>
        /// <returns></returns>
        public IMarketDataSubscription CreateSubscription(Subscription subscription)
        {
            // ensure subscription is not null
            if (subscription == null) throw new ArgumentNullException("subscription");

            return new MarketDataSubscription(_logger,
                _repositoryFactory,
                GetDataProvider(subscription),
                GetStockListProvider(subscription),
                _publisherFactory.CreatePublisher<NewQuotesData>(),
                subscription);
        }
 /// <summary>
 /// Gets the collection of stocks that have been parsed from the email feed
 /// </summary>
 /// <param name="subscription"></param>
 /// <returns></returns>
 public IEnumerable<Stock> GetStocks(Subscription subscription)
 {
     return new ReadOnlyCollection<Stock>(_stocks);
 }
        /// <summary>
        /// Updates the data for the subscription for a subscription data object
        /// </summary>
        public void UpdateSubscriptionData(Subscription subscriptionData)
        {
            lock (_subscriptionDataLock)
            {
                // stop the timer
                StopTimer();
                
                // set new subscription data
                _subscriptionData = subscriptionData;

                // start timer with new data
                StartTimer();
            }
        }
示例#7
0
        /// <summary>
        /// Gets the stock list provider for the subscription
        /// </summary>
        /// <param name="subscription"></param>
        /// <returns></returns>
        private IStockListProvider GetStockListProvider(Subscription subscription)
        {
            // ensure data provider is not null
            if (subscription.StockListProvider == null)
                throw new SubscriptionCreationException(Resources.StockListProviderNullException);

            // create the data provider
            var stockListProvider =
                _stockListProviders.FirstOrDefault(slp => slp.GetType() == subscription.StockListProvider.Type.ParseType());
            if (stockListProvider == null)
                throw new SubscriptionCreationException(Resources.StockListProviderTypeNotRegisteredExceptionMessage,
                    subscription.StockListProvider.Type);

            return stockListProvider;
        }
示例#8
0
        /// <summary>
        /// Gets the market data provider for the subscription
        /// </summary>
        /// <param name="subscription"></param>
        /// <returns></returns>
        private IMarketDataProvider GetDataProvider(Subscription subscription)
        {
            // ensure data provider is not null
            if (subscription.DataProvider == null)
                throw new SubscriptionCreationException(Resources.DataProviderNullException);

            // create the data provider
            var marketDataProvider =
                _marketDataProviders.FirstOrDefault(mdp => mdp.GetType() == subscription.DataProvider.Type.ParseType());
            if (marketDataProvider == null)
                throw new SubscriptionCreationException(Resources.DataProviderTypeNotRegisteredExceptionFormat,
                    subscription.DataProvider.Type);

            return marketDataProvider;
        }