private async Task AddHistoryToSecurities(Dictionary <Symbol, Security?> securities, HistoryFlags historyFlags, CancellationToken ct) { List <Security> secs = securities.Values.NotNull().ToList(); var dividendJobs = secs.Where(_ => historyFlags.HasFlag(HistoryFlags.DividendHistory)) .Select(sec => (sec, History.GetTicksAsync <DividendTick>(sec.Symbol, ct))).ToList(); var splitJobs = secs.Where(_ => historyFlags.HasFlag(HistoryFlags.SplitHistory)) .Select(sec => (sec, History.GetTicksAsync <SplitTick>(sec.Symbol, ct))).ToList(); var priceJobs = secs.Where(_ => historyFlags.HasFlag(HistoryFlags.PriceHistory)) .Select(sec => (sec, History.GetTicksAsync <PriceTick>(sec.Symbol, ct))).ToList(); List <Task> tasks = new(); tasks.AddRange(dividendJobs.Select(t => t.Item2)); tasks.AddRange(splitJobs.Select(t => t.Item2)); tasks.AddRange(priceJobs.Select(t => t.Item2)); await TaskExt.WhenAll(tasks).ConfigureAwait(false); foreach (var(security, task) in dividendJobs) { security.DividendHistory = task.Result; } foreach (var(security, task) in splitJobs) { security.SplitHistory = task.Result; } foreach (var(security, task) in priceJobs) { security.PriceHistory = task.Result; security.PriceHistoryBase = GetPriceHistoryBase(task.Result, security); } }
public async Task <Dictionary <Symbol, Security?> > GetAsync(IEnumerable <Symbol> symbols, HistoryFlags historyFlags, Symbol historyBase, CancellationToken ct = default) { HashSet <Symbol> syms = symbols.ToHashSet(); if (syms.Any(s => s.IsEmpty)) { throw new ArgumentException("Empty symbol."); } if (historyBase.IsCurrencyRate) { throw new ArgumentException($"Invalid base symbol: {historyBase}."); } if (!historyBase.IsEmpty && syms.Any(s => s.IsCurrencyRate)) { throw new ArgumentException($"Invalid symbol: {syms.First(s => s.IsCurrencyRate)}."); } if (historyBase.IsEmpty && syms.Any(s => s.IsCurrency)) { throw new ArgumentException($"Invalid symbol: {syms.First(s => s.IsCurrency)}."); } if (!historyBase.IsEmpty && !historyFlags.HasFlag(HistoryFlags.PriceHistory)) { throw new ArgumentException("PriceHistory must be enabled when historyBase is specified."); } try { Dictionary <Symbol, Security?> securities = await GetSecuritiesyAsync(syms, historyFlags, historyBase, ct).ConfigureAwait(false); return(syms.ToDictionary(symbol => symbol, symbol => securities[symbol])); } catch (Exception e) { Logger.LogCritical(e, "YahooQuotes: GetAsync() error."); throw; } }