Optimizations

This commit is contained in:
2025-03-31 15:23:18 -04:00
parent 5567aa129c
commit 90c4acb55a
3 changed files with 162 additions and 46 deletions

View File

@@ -19,66 +19,89 @@ namespace MarketData.MarketDataModel.GainLoss
}
public GainLossSummaryItemCollection(PortfolioTrades portfolioTrades,ITotalGainLossGenerator gainLossGenerator,IActiveGainLossGenerator activeGainLossGenerator,DateTime? maxDateRef=null)
{
List<String> symbols=portfolioTrades.Symbols;
Profiler profiler = new Profiler();
if(null==gainLossGenerator || null==activeGainLossGenerator)return;
foreach(String symbol in symbols)
try
{
PortfolioTrades portfolioTradesSymbol=portfolioTrades.FilterSymbol(symbol);
GainLossCollection gainLossCollection=activeGainLossGenerator.GenerateGainLoss(portfolioTradesSymbol,maxDateRef);
List<String> symbols=portfolioTrades.Symbols;
if(null==gainLossGenerator || null==activeGainLossGenerator)return;
TotalGainLossCollection totalGainLossCollection=gainLossGenerator.GenerateTotalGainLoss(portfolioTradesSymbol,maxDateRef);
GainLossCompoundModelCollection gainLossCompoundModelCollection=new GainLossCompoundModelCollection(gainLossCollection,totalGainLossCollection);
Dictionary<String,String> companyNames = PricingDA.GetNamesForSymbols(symbols);
Dictionary<String,bool> stopLimits = PortfolioDA.HasStopLimit(symbols);
if(1>gainLossCompoundModelCollection.Count) continue;
GainLossSummaryItem gainLossSummaryItem=new GainLossSummaryItem();
gainLossSummaryItem.Date=gainLossCompoundModelCollection[gainLossCompoundModelCollection.Count-1].Date;
gainLossSummaryItem.Symbol=symbol;
gainLossSummaryItem.CompanyName=PricingDA.GetNameForSymbol(symbol);
gainLossSummaryItem.CurrentGainLoss=gainLossCompoundModelCollection[gainLossCompoundModelCollection.Count-1].ActiveGainLoss;
double previousGainLoss=1==gainLossCompoundModelCollection.Count?0.00:gainLossCompoundModelCollection[gainLossCompoundModelCollection.Count-2].ActiveGainLoss;
gainLossSummaryItem.PreviousGainLoss=previousGainLoss;
gainLossSummaryItem.Change=gainLossSummaryItem.CurrentGainLoss-gainLossSummaryItem.PreviousGainLoss;
if(1==gainLossCollection.Count) gainLossSummaryItem.ChangePercent=0.00;
else
foreach(String symbol in symbols)
{
double currentMarketValue=gainLossCollection[gainLossCollection.Count-1].Exposure+gainLossCollection[gainLossCollection.Count-1].GainLoss;
double previousMarketValue=gainLossCollection[gainLossCollection.Count-2].Exposure+gainLossCollection[gainLossCollection.Count-2].GainLoss;
if(0.00==previousMarketValue) gainLossSummaryItem.ChangePercent=0.00;
else gainLossSummaryItem.ChangePercent=((currentMarketValue-previousMarketValue)/previousMarketValue)*100;
if(gainLossSummaryItem.CurrentGainLoss<0&&gainLossSummaryItem.PreviousGainLoss<0)
{ // if current gainloss is negative and previous gainloss is negative then show change percent as a further dip into negative (i.e.) make sure sign is negative
if(Math.Abs(gainLossSummaryItem.CurrentGainLoss)>Math.Abs(gainLossSummaryItem.PreviousGainLoss)) gainLossSummaryItem.ChangePercent=Math.Abs(gainLossSummaryItem.ChangePercent)*-1.00;
}
else if(gainLossSummaryItem.CurrentGainLoss<0&&gainLossSummaryItem.PreviousGainLoss>0)
PortfolioTrades portfolioTradesSymbol=portfolioTrades.FilterSymbol(symbol);
GainLossCollection gainLossCollection=activeGainLossGenerator.GenerateGainLoss(portfolioTradesSymbol,maxDateRef);
TotalGainLossCollection totalGainLossCollection=gainLossGenerator.GenerateTotalGainLoss(portfolioTradesSymbol,maxDateRef);
GainLossCompoundModelCollection gainLossCompoundModelCollection=new GainLossCompoundModelCollection(gainLossCollection,totalGainLossCollection);
if(1>gainLossCompoundModelCollection.Count) continue;
GainLossSummaryItem gainLossSummaryItem=new GainLossSummaryItem();
gainLossSummaryItem.Date=gainLossCompoundModelCollection[gainLossCompoundModelCollection.Count-1].Date;
gainLossSummaryItem.Symbol=symbol;
if(companyNames.ContainsKey(symbol))
{
gainLossSummaryItem.ChangePercent=Math.Abs(gainLossSummaryItem.ChangePercent)*-1.00;
gainLossSummaryItem.CompanyName=companyNames[symbol];
}
else if(gainLossSummaryItem.CurrentGainLoss>0&&gainLossSummaryItem.PreviousGainLoss>0)
// gainLossSummaryItem.CompanyName=PricingDA.GetNameForSymbol(symbol);
gainLossSummaryItem.CurrentGainLoss=gainLossCompoundModelCollection[gainLossCompoundModelCollection.Count-1].ActiveGainLoss;
double previousGainLoss=1==gainLossCompoundModelCollection.Count?0.00:gainLossCompoundModelCollection[gainLossCompoundModelCollection.Count-2].ActiveGainLoss;
gainLossSummaryItem.PreviousGainLoss=previousGainLoss;
gainLossSummaryItem.Change=gainLossSummaryItem.CurrentGainLoss-gainLossSummaryItem.PreviousGainLoss;
if(1==gainLossCollection.Count) gainLossSummaryItem.ChangePercent=0.00;
else
{
if(gainLossSummaryItem.CurrentGainLoss<gainLossSummaryItem.PreviousGainLoss) gainLossSummaryItem.ChangePercent=Math.Abs(gainLossSummaryItem.ChangePercent)*-1.00;
double currentMarketValue=gainLossCollection[gainLossCollection.Count-1].Exposure+gainLossCollection[gainLossCollection.Count-1].GainLoss;
double previousMarketValue=gainLossCollection[gainLossCollection.Count-2].Exposure+gainLossCollection[gainLossCollection.Count-2].GainLoss;
if(0.00==previousMarketValue) gainLossSummaryItem.ChangePercent=0.00;
else gainLossSummaryItem.ChangePercent=((currentMarketValue-previousMarketValue)/previousMarketValue)*100;
if(gainLossSummaryItem.CurrentGainLoss<0&&gainLossSummaryItem.PreviousGainLoss<0)
{ // if current gainloss is negative and previous gainloss is negative then show change percent as a further dip into negative (i.e.) make sure sign is negative
if(Math.Abs(gainLossSummaryItem.CurrentGainLoss)>Math.Abs(gainLossSummaryItem.PreviousGainLoss)) gainLossSummaryItem.ChangePercent=Math.Abs(gainLossSummaryItem.ChangePercent)*-1.00;
}
else if(gainLossSummaryItem.CurrentGainLoss<0&&gainLossSummaryItem.PreviousGainLoss>0)
{
gainLossSummaryItem.ChangePercent=Math.Abs(gainLossSummaryItem.ChangePercent)*-1.00;
}
else if(gainLossSummaryItem.CurrentGainLoss>0&&gainLossSummaryItem.PreviousGainLoss>0)
{
if(gainLossSummaryItem.CurrentGainLoss<gainLossSummaryItem.PreviousGainLoss) gainLossSummaryItem.ChangePercent=Math.Abs(gainLossSummaryItem.ChangePercent)*-1.00;
}
}
// here we need to check maxDateRef for null and then call appropriate HasOpenPositions() / HasOpenPositionsOn(date) method
if(null!=maxDateRef)
{
if(!portfolioTrades.HasOpenPositionsOn(symbol,maxDateRef.Value)) continue;
}
else
{
if(!portfolioTrades.HasOpenPositions(symbol)) continue;
}
// gainLossSummaryItem.HasStopLimit=PortfolioDA.HasStopLimit(symbol);
gainLossSummaryItem.HasStopLimit=stopLimits.ContainsKey(symbol);
Add(gainLossSummaryItem);
}
// here we need to check maxDateRef for null and then call appropriate HasOpenPositions() / HasOpenPositionsOn(date) method
if(null!=maxDateRef)
{
if(!portfolioTrades.HasOpenPositionsOn(symbol,maxDateRef.Value)) continue;
}
else
{
if(!portfolioTrades.HasOpenPositions(symbol)) continue;
}
gainLossSummaryItem.HasStopLimit=PortfolioDA.HasStopLimit(symbol);
Add(gainLossSummaryItem);
GainLossSummaryItemCollection gainLossSummaryCollection=new GainLossSummaryItemCollection((from GainLossSummaryItem gainLossSummaryItem in this orderby gainLossSummaryItem.Date descending,gainLossSummaryItem.Change descending,gainLossSummaryItem.Symbol descending select gainLossSummaryItem).ToList());
Clear();
AddRange(gainLossSummaryCollection);
}
catch(Exception exception)
{
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[GainLossSummaryItemCollection] Exception:{0}",exception.ToString()));
}
finally
{
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[GainLossSummaryItemCollection] Done, took {0}(ms)",profiler.End()));
}
GainLossSummaryItemCollection gainLossSummaryCollection=new GainLossSummaryItemCollection((from GainLossSummaryItem gainLossSummaryItem in this orderby gainLossSummaryItem.Date descending,gainLossSummaryItem.Change descending,gainLossSummaryItem.Symbol descending select gainLossSummaryItem).ToList());
Clear();
AddRange(gainLossSummaryCollection);
}
public GainLossSummaryItemCollection(PortfolioTrades portfolioTrades,DateTime? maxDateRef=null)
{
List<String> symbols=portfolioTrades.Symbols;
Dictionary<String,bool> stopLimits = PortfolioDA.HasStopLimit(symbols);
foreach(String symbol in symbols)
{
PortfolioTrades portfolioTradesSymbol=portfolioTrades.FilterSymbol(symbol);
@@ -125,7 +148,8 @@ namespace MarketData.MarketDataModel.GainLoss
{
if(!portfolioTrades.HasOpenPositions(symbol)) continue;
}
gainLossSummaryItem.HasStopLimit=PortfolioDA.HasStopLimit(symbol);
gainLossSummaryItem.HasStopLimit = stopLimits.ContainsKey(symbol);
// gainLossSummaryItem.HasStopLimit=PortfolioDA.HasStopLimit(symbol);
Add(gainLossSummaryItem);
}
GainLossSummaryItemCollection gainLossSummaryCollection=new GainLossSummaryItemCollection((from GainLossSummaryItem gainLossSummaryItem in this orderby gainLossSummaryItem.Date descending,gainLossSummaryItem.Change descending,gainLossSummaryItem.Symbol descending select gainLossSummaryItem).ToList());