Add LatestPricingDate to LocalPriceCache and optimize company name retrieval

This commit is contained in:
2025-04-07 21:20:43 -04:00
parent 7daa60f8c0
commit 4c4a9b5cf9
7 changed files with 43 additions and 19 deletions

11
.vscode/tasks.json vendored
View File

@@ -7,7 +7,7 @@
"type": "process",
"args": [
"build",
"${workspaceFolder}/MarketData/MarketData.sln",
"${workspaceFolder}/MarketData.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
@@ -19,7 +19,7 @@
"type": "process",
"args": [
"publish",
"${workspaceFolder}/MarketData/MarketData.sln",
"${workspaceFolder}/MarketData.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
@@ -33,9 +33,14 @@
"watch",
"run",
"--project",
"${workspaceFolder}/MarketData/MarketData.sln"
"${workspaceFolder}/MarketData.sln"
],
"problemMatcher": "$msCompile"
},
{
"label": "echo",
"type": "shell",
"command": "echo ${workspaceFolder}"
}
]
}

View File

@@ -3,14 +3,15 @@ using MarketData.Utils;
using MarketData.DataAccess;
// This cache is mainly used by gainloss generator. This cache is intended to be front loaded and then used.
// This cache will not attempt to load an item that is not found. It does have a Refresh() that will reload only the most recent pricing data from the database in order to
// maintain the most updated pricing.
// This cache will not attempt to load an item that is not found. It does have a Refresh() that will reload only the most recent
// pricing data from the database in order to maintain the most updated pricing.
namespace MarketData.Cache
{
public class LocalPriceCache
{
private Dictionary<String,PricesByDate> priceCache=new Dictionary<String,PricesByDate>();
private static LocalPriceCache instance=null;
private DateTime latestDate = Utility.Epoch;
private Thread cacheMonitorThread=null;
private volatile bool threadRun=true;
private int cacheCycle=300000;
@@ -26,7 +27,8 @@ namespace MarketData.Cache
{
lock(thisLock)
{
priceCache=new Dictionary<String,PricesByDate>();
priceCache=new Dictionary<String,PricesByDate>();
RefreshLatestDate();
}
}
@@ -51,18 +53,38 @@ namespace MarketData.Cache
{
lock(typeof(LocalPriceCache))
{
if(null==instance)instance=new LocalPriceCache();
if(null==instance)
{
instance=new LocalPriceCache();
instance.RefreshLatestDate();
}
return instance;
}
}
public void RefreshLatestDate()
{
lock(typeof(LocalPriceCache))
{
latestDate=PricingDA.GetLatestDate();
}
}
public DateTime GetLatestDate()
{
lock(typeof(LocalPriceCache))
{
return latestDate;
}
}
public void Refresh()
{
lock(typeof(LocalPriceCache))
{
List<String> symbols=new List<String>(priceCache.Keys);
Dictionary<String, DateTime> maxDbDates = PricingDA.GetLatestDates(symbols);
RefreshLatestDate();
foreach(String symbol in symbols)
{
PricesByDate symbolPrices=priceCache[symbol];

View File

@@ -682,6 +682,7 @@ namespace MarketData.DataAccess
try
{
if(null == symbols || 0==symbols.Count)return dictionary;
StringBuilder sb = new StringBuilder();
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
sb.Append("select symbol, company from securitymaster where symbol in ");

View File

@@ -16,10 +16,6 @@ namespace MarketData.Generator.GainLoss
public ActiveGainLossGenerator()
{
}
//public void RefreshPriceCache()
//{
// LocalPriceCache.GetInstance().Refresh();
//}
// *****************************************************************************************************************************************************************
// ************************************************ G E N E R A T E A C T I V E G A I N L O S S / G A I N L O S S P E R C E N T *****************************
// *****************************************************************************************************************************************************************
@@ -28,7 +24,8 @@ namespace MarketData.Generator.GainLoss
if (null == portfolioTrades || 0 == portfolioTrades.Count) return null;
LocalPriceCache.GetInstance().Add(portfolioTrades);
DateTime minTradeDate = portfolioTrades.GetMinTradeDate();
DateTime maxDate = PricingDA.GetLatestDate();
// DateTime maxDate = PricingDA.GetLatestDate();
DateTime maxDate=LocalPriceCache.GetInstance().GetLatestDate();
if(null!=maxDateRef)maxDate=maxDateRef.Value;
Dictionary<DateTime,GainLossItem> gainLoss = new Dictionary<DateTime, GainLossItem>();
DateGenerator dateGenerator = new DateGenerator();

View File

@@ -24,7 +24,8 @@ namespace MarketData.Generator.GainLoss
if (null == portfolioTrades || 0 == portfolioTrades.Count) return null;
LocalPriceCache.GetInstance().Add(portfolioTrades);
DateTime minTradeDate = portfolioTrades.GetMinTradeDate();
DateTime maxDate = PricingDA.GetLatestDate();
// DateTime maxDate = PricingDA.GetLatestDate();
DateTime maxDate=LocalPriceCache.GetInstance().GetLatestDate();
if(null!=maxDateRef)maxDate=maxDateRef.Value;
Dictionary<DateTime,TotalGainLossItem> gainLossCollection = new Dictionary<DateTime, TotalGainLossItem>();
DateGenerator dateGenerator = new DateGenerator();

View File

@@ -99,8 +99,8 @@ namespace MarketData.MarketDataModel.GainLoss
public GainLossSummaryItemCollection(PortfolioTrades portfolioTrades,DateTime? maxDateRef=null)
{
List<String> symbols=portfolioTrades.Symbols;
Dictionary<String,bool> stopLimits = PortfolioDA.HasStopLimit(symbols);
Dictionary<String,String> namesDictionary = PricingDA.GetNamesForSymbols(symbols);
foreach(String symbol in symbols)
{
@@ -114,7 +114,8 @@ namespace MarketData.MarketDataModel.GainLoss
GainLossSummaryItem gainLossSummaryItem=new GainLossSummaryItem();
gainLossSummaryItem.Date=gainLossCompoundModelCollection[gainLossCompoundModelCollection.Count-1].Date;
gainLossSummaryItem.Symbol=symbol;
gainLossSummaryItem.CompanyName=PricingDA.GetNameForSymbol(symbol);
// gainLossSummaryItem.CompanyName=PricingDA.GetNameForSymbol(symbol);
gainLossSummaryItem.CompanyName=namesDictionary.ContainsKey(symbol)?namesDictionary[symbol]:"";
gainLossSummaryItem.CurrentGainLoss=gainLossCompoundModelCollection[gainLossCompoundModelCollection.Count-1].ActiveGainLoss;
double previousGainLoss=1==gainLossCompoundModelCollection.Count?0.00:gainLossCompoundModelCollection[gainLossCompoundModelCollection.Count-2].ActiveGainLoss;
gainLossSummaryItem.PreviousGainLoss=previousGainLoss;

View File

@@ -21,11 +21,8 @@ namespace MarketData.MarketDataModel
if(Utility.IsEpoch(key))return;
base.Add(key,price);
if(key>maxDate) maxDate=key;
// if(!Utility.IsEpoch(minDate) && key<minDate)minDate=key;
if(Utility.IsEpoch(minDate))minDate=key;
else if(key<minDate)minDate=key;
//else if(Utility.IsEpoch(minDate)) minDate=key;
//else if(key<minDate && !Utility.IsEpoch(key)) minDate=key;
}
public DateTime MaxDate
{