GLPriceCache changes
Some checks failed
Build .NET Project / build (push) Has been cancelled

This commit is contained in:
2026-02-26 16:56:17 -05:00
parent eee0418271
commit 2fc472dbbb
2 changed files with 78 additions and 41 deletions

View File

@@ -147,6 +147,7 @@ namespace MarketData.Services
{ {
LocalPriceCache.GetInstance().Dispose(); LocalPriceCache.GetInstance().Dispose();
GBPriceCache.GetInstance().Dispose(); GBPriceCache.GetInstance().Dispose();
GLPriceCache.GetInstance().Dispose();
} }
MDTrace.WriteLine(LogLevel.DEBUG,$"[RunService] Done, total took {profiler.End()}(ms)"); MDTrace.WriteLine(LogLevel.DEBUG,$"[RunService] Done, total took {profiler.End()}(ms)");

View File

@@ -163,24 +163,33 @@ namespace MarketData.Cache
public void Add(PortfolioTrades portfolioTrades) public void Add(PortfolioTrades portfolioTrades)
{ {
List<string> symbols = portfolioTrades.Symbols; List<string> symbols = portfolioTrades.Symbols;
Dictionary<string, DateTime> minTradeDates = symbols.ToDictionary(sym => sym, sym => portfolioTrades.GetMinTradeDate(sym)); DateTime today = DateTime.Today;
Dictionary<string, DateTime> minTradeDates = symbols.ToDictionary(symbol => symbol, symbol => portfolioTrades.GetMinTradeDate(symbol));
// Symbols that need an intraday refresh:
// - open positions (no close date), or
// *** REMOVED THIS - closed today (close price may still be settling) TODO *****
HashSet<string> mutableSymbols = new HashSet<string>(symbols.Where(symbol => portfolioTrades.HasOpenPositions(symbol)));
Dictionary<string, DateTime> minCacheDates; Dictionary<string, DateTime> minCacheDates;
lock (thisLock) 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, 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 = 8 }, symbol =>
{ {
DateTime minTradeDate = minTradeDates[symbol]; DateTime minTradeDate = minTradeDates[symbol];
DateTime minCacheDate = minCacheDates[symbol]; DateTime minCacheDate = minCacheDates[symbol];
Prices prices = null;
try try
{ {
// Historical fetch <20> only when cache is missing or incomplete
Prices prices = null;
if (minCacheDate == DateTime.MaxValue) if (minCacheDate == DateTime.MaxValue)
{ {
prices = PricingDA.GetPrices(symbol, minTradeDate); prices = PricingDA.GetPrices(symbol, minTradeDate);
@@ -194,6 +203,14 @@ namespace MarketData.Cache
{ {
fetchedPrices[symbol] = prices; fetchedPrices[symbol] = prices;
} }
// Intraday refresh open positions and positions closed today only
if (mutableSymbols.Contains(symbol))
{
Price latestPrice = PricingDA.GetPrice(symbol);
if (latestPrice != null)
latestPrices[symbol] = latestPrice;
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -203,14 +220,33 @@ namespace MarketData.Cache
lock (thisLock) lock (thisLock)
{ {
foreach (KeyValuePair<string, Prices> kvp in fetchedPrices) // Historical prices idempotent, will not overwrite existing entries
foreach (var kvp in fetchedPrices)
{ {
foreach (Price price in kvp.Value) foreach (var price in kvp.Value)
{ {
Add(price); Add(price);
} }
} }
// Latest prices unconditional overwrite to capture any intraday updates
foreach (var kvp in latestPrices)
{
if (!priceCache.TryGetValue(kvp.Key, out var pricesByDate))
{
pricesByDate = new PricesByDate();
priceCache[kvp.Key] = pricesByDate;
} }
if (pricesByDate.ContainsKey(kvp.Value.Date))
pricesByDate.Remove(kvp.Value.Date);
pricesByDate.Add(kvp.Value.Date, kvp.Value);
}
}
MDTrace.WriteLine(LogLevel.DEBUG,
$"[GLPriceCache:Add] Symbols: {symbols.Count}, Mutable: {mutableSymbols.Count}, " +
$"Historical fetches: {fetchedPrices.Count}, Intraday updates: {latestPrices.Count}");
} }
public void Add(Prices prices) public void Add(Prices prices)