|
|
|
|
@@ -66,8 +66,12 @@ namespace MarketData.Cache
|
|
|
|
|
List<string> symbols = portfolioTrades.Symbols;
|
|
|
|
|
DateTime today = DateTime.Today;
|
|
|
|
|
|
|
|
|
|
Dictionary<string, DateTime> minTradeDates = symbols.ToDictionary(
|
|
|
|
|
sym => sym, sym => portfolioTrades.GetMinTradeDate(sym));
|
|
|
|
|
// Default: 3x logical cores, capped at 32 to avoid saturating the connection pool.
|
|
|
|
|
// I/O-bound DB calls benefit from more threads than CPU cores, but diminishing
|
|
|
|
|
// returns set in beyond ~32 concurrent connections for most DB workloads.
|
|
|
|
|
int defaultParallelism = Math.Min(Environment.ProcessorCount * 3, 32);
|
|
|
|
|
|
|
|
|
|
Dictionary<string, DateTime> minTradeDates = symbols.ToDictionary(symbol => symbol, symbol => portfolioTrades.GetMinTradeDate(symbol));
|
|
|
|
|
|
|
|
|
|
// Symbols that need an intraday refresh:
|
|
|
|
|
// - open positions (no close date), or
|
|
|
|
|
@@ -78,15 +82,13 @@ namespace MarketData.Cache
|
|
|
|
|
Dictionary<string, DateTime> minCacheDates;
|
|
|
|
|
lock (thisLock)
|
|
|
|
|
{
|
|
|
|
|
minCacheDates = symbols.ToDictionary(
|
|
|
|
|
sym => sym,
|
|
|
|
|
sym => priceCache.ContainsKey(sym) ? priceCache[sym].MinDate : DateTime.MaxValue);
|
|
|
|
|
minCacheDates = symbols.ToDictionary(symbol => symbol,symbol => priceCache.ContainsKey(symbol) ? priceCache[symbol].MinDate : DateTime.MaxValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConcurrentDictionary<string, Prices> fetchedPrices = new ConcurrentDictionary<string, Prices>();
|
|
|
|
|
ConcurrentDictionary<string, Price> latestPrices = new ConcurrentDictionary<string, Price>();
|
|
|
|
|
|
|
|
|
|
Parallel.ForEach(symbols, new ParallelOptions { MaxDegreeOfParallelism = 8 }, symbol =>
|
|
|
|
|
Parallel.ForEach(symbols, new ParallelOptions { MaxDegreeOfParallelism = defaultParallelism }, symbol =>
|
|
|
|
|
{
|
|
|
|
|
DateTime minTradeDate = minTradeDates[symbol];
|
|
|
|
|
DateTime minCacheDate = minCacheDates[symbol];
|
|
|
|
|
@@ -126,7 +128,7 @@ namespace MarketData.Cache
|
|
|
|
|
lock (thisLock)
|
|
|
|
|
{
|
|
|
|
|
// Historical prices — idempotent, will not overwrite existing entries
|
|
|
|
|
foreach (var kvp in fetchedPrices)
|
|
|
|
|
foreach (KeyValuePair<string, Prices> kvp in fetchedPrices)
|
|
|
|
|
{
|
|
|
|
|
foreach (var price in kvp.Value)
|
|
|
|
|
{
|
|
|
|
|
@@ -135,7 +137,7 @@ namespace MarketData.Cache
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Latest prices — unconditional overwrite to capture any intraday updates
|
|
|
|
|
foreach (var kvp in latestPrices)
|
|
|
|
|
foreach (KeyValuePair<string, Price> kvp in latestPrices)
|
|
|
|
|
{
|
|
|
|
|
if (!priceCache.TryGetValue(kvp.Key, out var pricesByDate))
|
|
|
|
|
{
|
|
|
|
|
|