Merge MKDT_0003

This commit is contained in:
2026-02-26 16:51:29 -05:00
parent 2f9c01e5a0
commit 67aa1bd0cf
2 changed files with 11 additions and 8 deletions

View File

@@ -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))
{

View File

@@ -1857,6 +1857,7 @@ namespace MarketData
{
LocalPriceCache.GetInstance().Dispose();
GBPriceCache.GetInstance().Dispose();
GLPriceCache.GetInstance().Dispose();
}
} // main
// *****************************************************************************************************************************************************************************