Optimizations for CMTrend

This commit is contained in:
2025-04-21 18:03:07 -04:00
parent 53e84e765c
commit 14bd7651ca
9 changed files with 302 additions and 34 deletions

View File

@@ -44,6 +44,7 @@ namespace MarketData.DataAccess
if (null != sqlConnection) sqlConnection.Close();
}
}
public static List<DateTime> GetIncomeStatementDates(String symbol,IncomeStatement.PeriodType periodType)
{
List<DateTime> incomeStatementDates = new List<DateTime>();
@@ -82,6 +83,7 @@ namespace MarketData.DataAccess
if (null != sqlConnection) sqlConnection.Close();
}
}
public static DateTime? GetLatestIncomeStatementDate(String symbol,IncomeStatement.PeriodType periodType)
{
MySqlConnection sqlConnection = null;
@@ -120,6 +122,7 @@ namespace MarketData.DataAccess
if (null != sqlConnection) sqlConnection.Close();
}
}
public static IncomeStatement GetIncomeStatement(String symbol,IncomeStatement.PeriodType periodType)
{
MySqlConnection sqlConnection = null;
@@ -172,6 +175,7 @@ namespace MarketData.DataAccess
if (null != sqlConnection) sqlConnection.Close();
}
}
public static TimeSeriesCollection GetRevenue(String symbol,IncomeStatement.PeriodType period)
{
Profiler profiler = new Profiler();
@@ -215,6 +219,7 @@ namespace MarketData.DataAccess
if (null != sqlConnection) sqlConnection.Close();
}
}
// Profit Margin is calculated as a percentage.
public static TimeSeriesCollection GetProfitMargin(String symbol,IncomeStatement.PeriodType period=IncomeStatement.PeriodType.Annual)
{
@@ -263,10 +268,10 @@ namespace MarketData.DataAccess
if (null != sqlConnection) sqlConnection.Close();
}
}
// Profit Margin is calculated as a percentage.
public static TimeSeriesCollection GetProfitMarginMaxAsOf(String symbol,DateTime maxDate, IncomeStatement.PeriodType period = IncomeStatement.PeriodType.Annual)
public static Dictionary<String,TimeSeriesCollection> GetProfitMarginMaxAsOf(List<String> symbols, DateTime maxDate,int maxSeries,IncomeStatement.PeriodType period = IncomeStatement.PeriodType.Annual)
{
TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
Dictionary<String,TimeSeriesCollection> timeSeriesCollection = new Dictionary<String,TimeSeriesCollection>();
MySqlConnection sqlConnection = null;
MySqlDataReader sqlDataReader = null;
MySqlCommand sqlCommand = null;
@@ -276,9 +281,11 @@ namespace MarketData.DataAccess
{
StringBuilder sb = new StringBuilder();
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
sb.Append("select symbol,asof,total_revenue,gross_profit from incomestatement where symbol='").Append(symbol).Append("'").Append(" ");
sb.Append(" and asof<='").Append(SqlUtils.SqlDate(maxDate)).Append("' ");
sb.Append(" and period=").Append(period.Equals(IncomeStatement.PeriodType.Annual) ? 0 : 1).Append(" order by asof desc;");
sb.Append("SELECT B.symbol, B.asof, B.total_revenue, B.gross_profit FROM ");
sb.Append("(SELECT symbol, asof, total_revenue, gross_profit, period , ROW_NUMBER() OVER(PARTITION BY symbol ORDER BY asof desc) AS rownum FROM incomestatement ");
sb.Append($" WHERE symbol IN {SqlUtils.CreateInClause(symbols)} AND asof<={SqlUtils.ToSqlDate(maxDate.Date,true)}");
sb.Append($" AND period={(period.Equals(IncomeStatement.PeriodType.Annual) ? 0 : 1)} )B ");
sb.Append($" WHERE B.rownum<={maxSeries} ");
strQuery = sb.ToString();
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
@@ -288,15 +295,19 @@ namespace MarketData.DataAccess
TimeSeriesElement timeSeriesElement = new TimeSeriesElement();
timeSeriesElement.Symbol = sqlDataReader.GetString(0);
timeSeriesElement.AsOf = sqlDataReader.GetDateTime(1);
timeSeriesElement.Type = period.Equals(IncomeStatement.PeriodType.Quarterly) ? TimeSeriesElement.ElementType.QuarterlyRevenue : TimeSeriesElement.ElementType.Revenue;
timeSeriesElement.Type = TimeSeriesElement.ElementType.OTHER;
timeSeriesElement.OtherType = "Profit Margin %";
if (sqlDataReader.IsDBNull(2)) continue;
double total_revenue = sqlDataReader.GetDouble(2);
if (sqlDataReader.IsDBNull(3)) continue;
double gross_profit = sqlDataReader.GetDouble(3);
if (0 == gross_profit) continue;
timeSeriesElement.Value = (gross_profit / total_revenue) * 100.00;
timeSeriesCollection.Add(timeSeriesElement);
if(!timeSeriesCollection.ContainsKey(timeSeriesElement.Symbol))
{
timeSeriesCollection.Add(timeSeriesElement.Symbol, new TimeSeriesCollection());
}
timeSeriesCollection[timeSeriesElement.Symbol].Add(timeSeriesElement);
}
return timeSeriesCollection;
}
@@ -311,7 +322,8 @@ namespace MarketData.DataAccess
if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
if (null != sqlConnection) sqlConnection.Close();
}
}
}
public static IncomeStatement GetIncomeStatement(String symbol,DateTime asof,IncomeStatement.PeriodType periodType)
{
MySqlConnection sqlConnection = null;
@@ -362,6 +374,7 @@ namespace MarketData.DataAccess
if (null != sqlConnection) sqlConnection.Close();
}
}
public static IncomeStatement GetIncomeStatementMaxAsOf(String symbol,DateTime asof,IncomeStatement.PeriodType periodType)
{
MySqlConnection sqlConnection = null;
@@ -413,6 +426,7 @@ namespace MarketData.DataAccess
if (null != sqlConnection) sqlConnection.Close();
}
}
public static bool InsertIncomeStatements(List<IncomeStatement> incomeStatements)
{
MySqlConnection sqlConnection = null;
@@ -479,6 +493,7 @@ namespace MarketData.DataAccess
if (null != sqlConnection) sqlConnection.Close();
}
}
private static bool DeleteIncomeStatements(List<IncomeStatement> incomeStatements, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
{
for (int index = 0; index < incomeStatements.Count; index++)
@@ -487,6 +502,7 @@ namespace MarketData.DataAccess
}
return true;
}
private static bool DeleteIncomeStatement(IncomeStatement incomeStatement, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
{
StringBuilder sb = new StringBuilder();