Add GetLatestPrices for symbol list

This commit is contained in:
2025-05-19 15:31:53 -04:00
parent 0da8485d13
commit c1bea02fe7

View File

@@ -253,41 +253,101 @@ namespace MarketData.DataAccess
if (null != sqlConnection) sqlConnection.Close();
}
}
public static DateTime GetLatestDate(String symbol)
public static Dictionary<String, Price> GetLatestPrices(List<String> symbols)
{
MySqlConnection sqlConnection=null;
MySqlDataReader sqlDataReader=null;
MySqlCommand sqlCommand=null;
String strQuery=null;
DateTime maxDate=DateTime.Parse("01-01-0001");
MySqlConnection sqlConnection = null;
MySqlDataReader sqlDataReader = null;
MySqlCommand sqlCommand=null;
String strQuery = null;
Dictionary<String, Price> latestPrices = new Dictionary<String, Price>();
try
{
StringBuilder sb=new StringBuilder();
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
sb.Append("select max(date) from prices");
sb.Append(" where symbol='").Append(symbol).Append("'");
strQuery=sb.ToString();
sqlCommand=new MySqlCommand(strQuery,sqlConnection);
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
sqlDataReader=sqlCommand.ExecuteReader();
if(sqlDataReader.Read())
StringBuilder sb = new StringBuilder();
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
sb.Append($"SELECT B.date, B.symbol,B.open,B.high,B.low,B.close,B.volume,B.adjclose,B.source from ");
sb.Append($"(SELECT date, symbol, open, high, low, close, volume, adjclose, SOURCE, ROW_NUMBER() OVER (");
sb.Append($"PARTITION BY symbol ORDER BY DATE DESC) AS rownum from prices ");
sb.Append($"WHERE symbol IN {SqlUtils.CreateInClause(symbols)})B where B.rownum=1");
strQuery = sb.ToString(); ;
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
sqlDataReader = sqlCommand.ExecuteReader();
while(sqlDataReader.Read())
{
if(!sqlDataReader.IsDBNull(0)) maxDate=sqlDataReader.GetDateTime(0);
Price price = new Price();
if(!sqlDataReader.IsDBNull(0))price.Date = sqlDataReader.GetDateTime(0);
else throw new Exception("Date cannot be null");
if(!sqlDataReader.IsDBNull(1))price.Symbol = sqlDataReader.GetString(1);
else throw new Exception("Symbol cannot be null");
if(!sqlDataReader.IsDBNull(2))price.Open = sqlDataReader.GetDouble(2);
else price.Open=double.NaN;
if(!sqlDataReader.IsDBNull(3))price.High = sqlDataReader.GetDouble(3);
else price.High=double.NaN;
if(!sqlDataReader.IsDBNull(4))price.Low = sqlDataReader.GetDouble(4);
else price.Low=double.NaN;
if(!sqlDataReader.IsDBNull(5))price.Close = sqlDataReader.GetDouble(5);
else price.Close=double.NaN;
if(!sqlDataReader.IsDBNull(6))price.Volume = sqlDataReader.GetInt64(6);
else price.Volume=0;
if(!sqlDataReader.IsDBNull(7))price.AdjClose = sqlDataReader.GetDouble(7);
else price.AdjClose=double.NaN;
if(!sqlDataReader.IsDBNull(8))price.Source = (Price.PriceSource)Utility.CharToNum(sqlDataReader.GetChar(8));
else price.Source=Price.PriceSource.Other;
if(!latestPrices.ContainsKey(price.Symbol))latestPrices.Add(price.Symbol, price);
}
return maxDate;
return latestPrices;
}
catch(Exception exception)
catch (Exception exception)
{
MDTrace.WriteLine(LogLevel.DEBUG,exception);
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery));
return latestPrices;
}
finally
{
if(null!=sqlCommand)sqlCommand.Dispose();
if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
if (null != sqlConnection) sqlConnection.Close();
}
}
public static DateTime GetLatestDate(String symbol)
{
MySqlConnection sqlConnection = null;
MySqlDataReader sqlDataReader = null;
MySqlCommand sqlCommand = null;
String strQuery = null;
DateTime maxDate = DateTime.Parse("01-01-0001");
try
{
StringBuilder sb = new StringBuilder();
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
sb.Append("select max(date) from prices");
sb.Append(" where symbol='").Append(symbol).Append("'");
strQuery = sb.ToString();
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.Read())
{
if (!sqlDataReader.IsDBNull(0)) maxDate = sqlDataReader.GetDateTime(0);
}
return maxDate;
}
catch (Exception exception)
{
MDTrace.WriteLine(LogLevel.DEBUG, exception);
MDTrace.WriteLine(LogLevel.DEBUG, String.Format("Query was {0}", strQuery));
return maxDate;
}
finally
{
if(null!=sqlCommand) sqlCommand.Dispose();
if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
if(null!=sqlConnection) sqlConnection.Close();
if (null != sqlCommand) sqlCommand.Dispose();
if (null != sqlDataReader) { sqlDataReader.Close(); sqlDataReader.Dispose(); }
if (null != sqlConnection) sqlConnection.Close();
}
}
public static Dictionary<String,DateTime> GetLatestDates(List<String> symbols)