Пример #1
0
        /// <summary>
        /// Gets the fundamental data for the share with the supplied symbol.
        /// </summary>
        /// <param name="symbol">The share symbol.</param>
        /// <returns>The share's fundamental data.</returns>
        public ShareFundamentals GetShareFundamentals(string symbol)
        {
            if (string.IsNullOrWhiteSpace(symbol))
            {
                throw new ArgumentException($"Argument '{nameof(symbol)}' is required.");
            }

            // Get the share information.
            ShareInfo info = _shareInfoProvider.GetShareInfo(symbol);

            if (info == null)
            {
                return(null);
            }

            var fundamentals = new ShareFundamentals(info.Symbol, info.Name, info.Industry);

            Task[] tasks = new[]
            {
                GetKeyStatistics(symbol, fundamentals),
                GetPriceHistory(symbol, fundamentals)
            };

            Task.WaitAll(tasks);

            // PE Ratio not provided by Yahoo. We have to calculate it manually.
            if (fundamentals.PreviousClose.HasValue && fundamentals.EarningsShare.HasValue)
            {
                fundamentals.PERatio = Math.Round(fundamentals.PreviousClose.Value / fundamentals.EarningsShare.Value, 2);
            }

            return(fundamentals);
        }
        /// <summary>
        /// Gets detailed information about a specific watchlist.
        /// </summary>
        /// <param name="userId">The unique identifier of the user to retrieve the watchlist for.</param>
        /// <param name="watchlistId">The unique identifier of the watchlist to retrieve.</param>
        /// <returns>The watchlist details.</returns>
        public WatchlistDetails GetWatchlistDetails(Guid userId, Guid watchlistId)
        {
            Validate.NotEmpty(userId, nameof(userId));
            Validate.NotEmpty(watchlistId, nameof(watchlistId));

            Watchlist watchlist = GetWatchlist(userId, watchlistId);

            IReadOnlyDictionary <string, ShareInfo> shareDetails = _shareInfoProvider
                                                                   .GetShareInfo(watchlist.Symbols);

            IReadOnlyDictionary <string, Quote> quotes = _shareQuoteProvider
                                                         .GetQuotes(watchlist.Symbols);

            List <WatchlistShare> shares = watchlist.Symbols
                                           .Where(symbol => shareDetails.ContainsKey(symbol) && quotes.ContainsKey(symbol))
                                           .Select(symbol => new
            {
                Symbol = symbol,
                Detail = shareDetails[symbol],
                Quote  = quotes[symbol],
            })
                                           .Select(i => new WatchlistShare(i.Symbol, i.Detail.Name, i.Quote.Last, i.Quote.Change, i.Quote.ChangePercent))
                                           .ToList();

            return(new WatchlistDetails(watchlist.Id, watchlist.Name, shares));
        }
        /// <summary>
        /// Gets detailed information about a specific trading accounts.
        /// </summary>
        /// <param name="account">The trading account to return the details for.</param>
        /// <returns>The trading account details.</returns>
        public AccountDetails GetAccountDetails(Account account)
        {
            Validate.NotNull(account, nameof(account));

            IReadOnlyDictionary <string, ShareInfo> shareDetails = _shareInfoProvider
                                                                   .GetShareInfo(account.Positions.Select(position => position.Symbol));

            IReadOnlyDictionary <string, Quote> quotes = _shareQuoteProvider
                                                         .GetQuotes(account.Positions.Select(position => position.Symbol));

            List <PositionInfo> positions = account.Positions
                                            .Where(p => shareDetails.ContainsKey(p.Symbol) && quotes.ContainsKey(p.Symbol))
                                            .Select(p => new
            {
                P = p,
                D = shareDetails[p.Symbol],
                Q = quotes[p.Symbol],
            })
                                            .Select(i => new PositionInfo(i.D.Symbol, i.D.Name, i.P.Quantity, i.P.AveragePrice, i.Q.Last, i.Q.Change, i.Q.ChangePercent))
                                            .ToList();

            return(new AccountDetails(account.Id, account.Name, account.Balance, positions));
        }