diff --git a/MarketData/MarketDataLib/Cache/LocalPriceCache.cs b/MarketData/MarketDataLib/Cache/LocalPriceCache.cs index c2f12f6..a274616 100755 --- a/MarketData/MarketDataLib/Cache/LocalPriceCache.cs +++ b/MarketData/MarketDataLib/Cache/LocalPriceCache.cs @@ -94,7 +94,6 @@ namespace MarketData.Cache DateTime maxDate=symbolPrices.MaxDate; // get the latest date in the cache if(maxDbDates.ContainsKey(symbol) && !maxDbDates[symbol].Date.Equals(maxDate.Date)) // if the cache date and the database date are not equal then reload the cache { -// MDTrace.WriteLine(LogLevel.DEBUG,$"[LocalPriceCache] Cache date and Database date for {symbol} are not equal, reloading cache. Cache Date:{maxDate.ToShortDateString()} Database Date:{maxDbDates[symbol].Date.ToShortDateString()}"); Prices prices=PricingDA.GetPrices(symbol,symbolPrices.MinDate); // reload the prices for this symbol using the current minDate in the cache as a lower boundary if(null==prices)continue; // if we can't load any prices for symbol then just continue priceCache.Remove(symbol); // remove the pricing entries in the price cache for the symbol @@ -102,7 +101,6 @@ namespace MarketData.Cache } else { -// MDTrace.WriteLine(LogLevel.DEBUG,$"[LocalPriceCache] Fetching latest price from database for {symbol} on {maxDate.ToShortDateString()}"); Price price=PricingDA.GetPrice(symbol,maxDate); // the max date from the cache equals the max date from the database so just reload the latest price from the database if(null==price)continue; // if no latest price then just continue symbolPrices.Remove(maxDate); // remove the current price associated with the max date @@ -114,7 +112,7 @@ namespace MarketData.Cache // This version of Add(PortfolioTrades) will account for adding multiple lots at different times. So instead of just checking for the existance of the symbol in the cache // we look to see if the symbol is in the cache and what dates are available. If the date range specified in the trade are not available then we load those date ranges. -// This is a brute force approach always maintaining the gap between successive TradeDates in th.e portfolio trades and the maximum date for the symbol in the database. +// This is a brute force approach always maintaining the gap between successive TradeDates in the portfolio trades and the maximum date for the symbol in the database. // So while it is inefficient in terms of memory usage it alleviates the need for figuring out contiguous price sections public void Add(PortfolioTrades portfolioTrades) { @@ -185,6 +183,7 @@ namespace MarketData.Cache pricesByDate.Add(price.Date,price); } } + public DateTime GetMinCacheDate(String symbol) { if(!ContainsSymbol(symbol))return Utility.Epoch; @@ -204,6 +203,23 @@ namespace MarketData.Cache } } } + + public Prices GetPrices(String symbol,DateTime date,int dayCount) + { + lock(typeof(LocalPriceCache)) + { + DateGenerator dateGenerator = new DateGenerator(); + List historicalDates = dateGenerator.GenerateHistoricalDates(date, dayCount); + Prices prices = new Prices(); + foreach(DateTime historicalDate in historicalDates) + { + Price price = GetPrice(symbol, historicalDate); + if(null!=price)prices.Add(price); + } + return prices; + } + } + public Price GetPrice(String symbol,DateTime date) { lock(typeof(LocalPriceCache)) @@ -214,6 +230,7 @@ namespace MarketData.Cache return pricesByDate[date]; } } + public bool ContainsPrice(String symbol,DateTime date) { lock(typeof(LocalPriceCache)) @@ -224,6 +241,7 @@ namespace MarketData.Cache return true; } } + public bool ContainsPrice(List symbols,DateTime date) { lock(typeof(LocalPriceCache)) diff --git a/MarketDataServer/Controllers/GainLossController.cs b/MarketDataServer/Controllers/GainLossController.cs index 610667d..d894920 100755 --- a/MarketDataServer/Controllers/GainLossController.cs +++ b/MarketDataServer/Controllers/GainLossController.cs @@ -157,11 +157,10 @@ namespace MarketDataServer.Controllers gainLossSummaryItemDetail.DividendYield = weightAdjustedDividendYield; gainLossSummaryItemDetail.AnnualDividend = exposure * weightAdjustedDividendYield; } - DateGenerator dateGenerator = new DateGenerator(); - DateTime priorDate = dateGenerator.FindPrevBusinessDay(currentDate); - Price p1 = PricingDA.GetPrice(gainLossSummaryItem.Symbol, currentDate); - Price p2 = PricingDA.GetPrice(gainLossSummaryItem.Symbol, priorDate); - + Prices prices = LocalPriceCache.GetInstance().GetPrices(gainLossSummaryItem.Symbol, currentDate, 3); // cache should be refreshed after GainLossSummaryItemCollection +// Prices prices = PricingDA.GetPrices(gainLossSummaryItem.Symbol, currentDate, 2); + Price p1 = prices.Count>0?prices[0]:default; + Price p2 = prices.Count>1?prices[1]:default; PortfolioTrades symbolTrades = new PortfolioTrades(portfolioTrades.Where(x=>x.Symbol.Equals(gainLossSummaryItem.Symbol)).ToList()); ParityElement parityElement = ParityGenerator.GenerateBreakEven(symbolTrades, p1); gainLossSummaryItemDetail.ParityElement = parityElement; @@ -170,11 +169,6 @@ namespace MarketDataServer.Controllers gainLossSummaryItemDetail.AllTimeGainLossPercent = gainLossItem.GainLossPercent; gainLossSummaryItemDetail.PercentDistanceFromAllTimeGainLossPercent = parityElement.ParityOffsetPercent - (gainLossItem.GainLossPercent / 100); } - if (null == p2 && null != p1) - { - priorDate = dateGenerator.FindPrevBusinessDay(priorDate); - p2 = PricingDA.GetPrice(gainLossSummaryItem.Symbol, priorDate); - } if (null != p1 && null != p2) { double change = (p1.Close - p2.Close) / p2.Close;