using System.Text; using MySql.Data.MySqlClient; using MarketData.MarketDataModel; using MarketData.Utils; using System; using System.Collections.Generic; namespace MarketData.DataAccess { public class PricingDA { public static readonly int ForwardLookingDays = 90; private PricingDA() { } public static bool AddSecurity(String symbol,String companyName) { MySqlConnection sqlConnection = null; MySqlTransaction sqlTransaction = null; MySqlCommand sqlCommand=null; String strQuery = null; try { if(null==symbol)return false; symbol=symbol.Trim(); if (SecurityExists(symbol)) return false; sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted); StringBuilder sb = new StringBuilder(); sb.Append("insert into securitymaster (symbol,company,security_type)values("); sb.Append(SqlUtils.AddQuotes(symbol)).Append(","); sb.Append(SqlUtils.AddQuotes(SqlUtils.SqlString(companyName))).Append(","); sb.Append("'EQUITY'"); sb.Append(")"); strQuery = sb.ToString(); sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlCommand.ExecuteNonQuery(); sqlTransaction.Commit(); return true; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return false; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlTransaction) sqlTransaction.Dispose(); if (null != sqlConnection) sqlConnection.Close(); } } public static bool SecurityExists(String symbol) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; int recordCount = 0; try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select count(*) from securitymaster 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)) recordCount = sqlDataReader.GetInt32(0); } sqlCommand.Dispose(); return 0 == recordCount ? false : true; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return false; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static List GetSymbolsNotIn(List symbols) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; List selectedSymbols = new List(); try { StringBuilder sb = new StringBuilder(); if (null == symbols || 0 == symbols.Count) return symbols; sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select symbol from SecurityMaster where symbol in ").Append(SqlUtils.CreateInClause(symbols)); strQuery = sb.ToString(); sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); while (sqlDataReader.Read()) { selectedSymbols.Add(sqlDataReader.GetString(0)); } for (int index = 0; index < selectedSymbols.Count; index++) { symbols.RemoveAll(x => x.Equals(selectedSymbols[index])); } sqlCommand.Dispose(); return symbols; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return selectedSymbols; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static int GetPriceCountOn(DateTime date) { MySqlConnection sqlConnection=null; MySqlDataReader sqlDataReader=null; MySqlCommand sqlCommand=null; String strQuery=null; int recordCount=0; try { StringBuilder sb=new StringBuilder(); sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select count(*) from prices where date='").Append(Utility.DateTimeToStringYYYYHMMHDD(date)).Append("' "); strQuery=sb.ToString(); sqlCommand=new MySqlCommand(strQuery,sqlConnection); sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT; sqlDataReader=sqlCommand.ExecuteReader(); if(sqlDataReader.Read()) { if(!sqlDataReader.IsDBNull(0)) recordCount=sqlDataReader.GetInt32(0); } return recordCount; } catch(Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return 0; } finally { if(null!=sqlCommand) sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if(null!=sqlConnection) sqlConnection.Close(); } } public static bool CheckPricingOn(String symbol,DateTime date) { List symbols=new List(); symbols.Add(symbol); return CheckPricingOn(symbols,date); } public static bool CheckPricingOn(List symbols,DateTime date) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand =null; String strQuery = null; int recordCount = 0; try { StringBuilder sb = new StringBuilder(); if (null == symbols || 0 == symbols.Count) return false; sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select count(*) from prices where date='").Append(Utility.DateTimeToStringYYYYHMMHDD(date)).Append("' "); sb.Append("and symbol in ").Append(SqlUtils.CreateInClause(symbols)); strQuery = sb.ToString(); sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); if (sqlDataReader.Read()) { if (!sqlDataReader.IsDBNull(0)) recordCount = sqlDataReader.GetInt32(0); } return 0==recordCount?false:true; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return false; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static DateTime GetEarliestDate() { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; DateTime earliestDate = DateTime.Parse("01-01-0001"); try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select min(date) from prices"); strQuery = sb.ToString(); sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); if (sqlDataReader.Read()) { if (!sqlDataReader.IsDBNull(0)) earliestDate = sqlDataReader.GetDateTime(0); } return earliestDate; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return earliestDate; } 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(); } } public static Dictionary GetLatestDates(List symbols) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand = null; String strQuery = null; Dictionary latestDates = new Dictionary(); try { if(null ==symbols || 0==symbols.Count)return latestDates; StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append(" select symbol, max(date) as date "); sb.Append(" from prices where symbol in").Append(SqlUtils.CreateInClause(symbols)); sb.Append(" group by symbol order by symbol"); strQuery = sb.ToString(); sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); while (sqlDataReader.Read()) { if(sqlDataReader.IsDBNull(0) || sqlDataReader.IsDBNull(1))continue; String symbol = sqlDataReader.GetString(0); DateTime latestDate = sqlDataReader.GetDateTime(1); if(latestDates.ContainsKey(symbol))continue; latestDates.Add(symbol, latestDate); } return latestDates; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return latestDates; } finally { if (null != sqlCommand) sqlCommand.Dispose(); if (null != sqlDataReader) sqlDataReader.Close(); if (null != sqlConnection) sqlConnection.Close(); } } public static DateTime GetLatestDate(List symbols) { 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(" WITH CTE AS "); sb.Append(" ( "); sb.Append(" select symbol, max(date) as date "); sb.Append(" from prices where symbol in").Append(SqlUtils.CreateInClause(symbols)); sb.Append(" group by symbol "); sb.Append(" ) "); sb.Append(" select min(date) from CTE "); 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(); } } public static DateTime GetLatestDate() { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand = null; String strQuery = null; DateTime earliestDate = 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"); strQuery = sb.ToString(); sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); if (sqlDataReader.Read()) { if (!sqlDataReader.IsDBNull(0)) earliestDate = sqlDataReader.GetDateTime(0); } return earliestDate; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return earliestDate; } finally { if (null != sqlCommand) sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static DateTime GetLatestDateOnOrBefore(String symbol,DateTime date) { 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("'").Append(" "); sb.Append(" and date<='").Append(SqlUtils.SqlDate(date)).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(); } } public static DateTime GetEarliestDate(String symbol) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; DateTime earliestDate = DateTime.Parse("01-01-0001"); try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select min(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)) earliestDate = sqlDataReader.GetDateTime(0); } return earliestDate; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return earliestDate; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } // selects dates for symbol ordered earliest to latest public static List GetPricingDatesForSymbol(String symbol) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; List pricingDates=new List(); try { if(null==symbol)return pricingDates; symbol=symbol.ToUpper(); StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select date from prices where symbol='").Append(symbol).Append("' order by date asc"); strQuery = sb.ToString(); sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); while (sqlDataReader.Read()) { pricingDates.Add(sqlDataReader.GetDateTime(0)); } return pricingDates; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return pricingDates; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } // Get pricing dates from lowerBoundary (historical date ) to mosr recent public static List GetPricingDates(DateTime lowerBoundary) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand = null; String strQuery = null; List pricingDates = new List(); try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select distinct(date) from prices where date>='").Append(SqlUtils.ToSqlDateTime(lowerBoundary)).Append("' order by date desc"); strQuery = sb.ToString(); sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); while (sqlDataReader.Read()) { pricingDates.Add(sqlDataReader.GetDateTime(0)); } return pricingDates; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return pricingDates; } finally { if (null != sqlCommand) sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static List GetPricingDatesBetween(DateTime lowerBoundary,DateTime upperBoundary) { MySqlConnection sqlConnection=null; MySqlDataReader sqlDataReader=null; MySqlCommand sqlCommand=null; String strQuery=null; List pricingDates=new List(); try { StringBuilder sb=new StringBuilder(); sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select distinct(date) from prices where date>='").Append(SqlUtils.ToSqlDateTime(lowerBoundary)).Append("' "); sb.Append("and date<='").Append(SqlUtils.ToSqlDateTime(upperBoundary)).Append("'"); sb.Append(" order by date desc"); strQuery=sb.ToString(); sqlCommand=new MySqlCommand(strQuery,sqlConnection); sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT; sqlDataReader=sqlCommand.ExecuteReader(); while(sqlDataReader.Read()) { pricingDates.Add(sqlDataReader.GetDateTime(0)); } return pricingDates; } catch(Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return pricingDates; } finally { if(null!=sqlCommand) sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if(null!=sqlConnection) sqlConnection.Close(); } } public static List GetSymbols() { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; List symbols = new List(); try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select distinct symbol from securitymaster "); sb.Append("order by symbol;"); strQuery = sb.ToString(); ; sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); while (sqlDataReader.Read()) { symbols.Add(sqlDataReader.GetString(0)); } return symbols; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return symbols; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static List GetSymbolsETF() { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; List symbols = new List(); try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select distinct symbol from securitymaster where security_type in ('ETF','ETN')"); sb.Append("order by symbol;"); strQuery = sb.ToString(); ; sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); while (sqlDataReader.Read()) { symbols.Add(sqlDataReader.GetString(0)); } return symbols; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return symbols; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static Dictionary GetNamesForSymbols(List symbols) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; Dictionary dictionary = new Dictionary(); 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 "); sb.Append(SqlUtils.CreateInClause(symbols)); sb.Append(";"); strQuery = sb.ToString(); ; sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); while (sqlDataReader.Read()) { String symbol = sqlDataReader.GetString(0); String companyName = sqlDataReader.GetString(1); if(null==companyName || null==symbol)continue; companyName=companyName.ToUpper(); symbol = symbol.ToUpper(); if(dictionary.ContainsKey(symbol))continue; dictionary.Add(symbol,companyName); } return dictionary; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return null; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static String GetNameForSymbol(String symbol) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; List symbols = new List(); try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select company from securitymaster where symbol='"); sb.Append(symbol).Append("';"); strQuery = sb.ToString(); ; sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); if(!sqlDataReader.Read())return null; String companyName=sqlDataReader.GetString(0); if (null != companyName) companyName=companyName.ToUpper(); return companyName; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return null; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static String GetSectorForSymbol(String symbol) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; List symbols = new List(); try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select sector from securitymaster where symbol='"); sb.Append(symbol).Append("';"); strQuery = sb.ToString(); ; sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); if (!sqlDataReader.Read()) return null; String sector = null; if(!sqlDataReader.IsDBNull(0))sector=sqlDataReader.GetString(0); return sector; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return null; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static String GetIndustryForSymbol(String symbol) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; List symbols = new List(); try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select industry from securitymaster where symbol='"); sb.Append(symbol).Append("';"); strQuery = sb.ToString(); ; sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); if (!sqlDataReader.Read()) return null; String industry = null; if(!sqlDataReader.IsDBNull(0))industry=sqlDataReader.GetString(0); return industry; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return null; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } // ********************************************************************************* // ****************************** Manage Central Index Key ************************* // ********************************************************************************* public static List GetSymbolsForNullCIK() { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; List unmapped = new List(); try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select distinct symbol from securitymaster where cik is null"); strQuery = sb.ToString(); ; sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); while (sqlDataReader.Read()) { String symbol = sqlDataReader.GetString(0); unmapped.Add(symbol); } return unmapped; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return null; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static String GetCIKForSymbol(String symbol) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; String cik; try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select distinct cik from securitymaster where symbol='"); sb.Append(symbol); sb.Append("' and cik is not null;"); strQuery = sb.ToString(); ; sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); if (!sqlDataReader.Read()) return null; cik=sqlDataReader.GetString(0); return cik; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery));; return null; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static bool UpdateCIKForSymbol(String symbol,String cik) { MySqlConnection sqlConnection = null; MySqlTransaction sqlTransaction = null; MySqlCommand sqlCommand =null; String strQuery = null; try { if (null == symbol || null == cik) return false; sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted); StringBuilder sb = new StringBuilder(); sb.Append("update securitymaster "); sb.Append("set "); sb.Append("cik=").Append(SqlUtils.AddQuotes(cik)).Append(" "); sb.Append(" where symbol=").Append(SqlUtils.AddQuotes(symbol)); strQuery = sb.ToString(); sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlCommand.ExecuteNonQuery(); sqlTransaction.Commit(); return true; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return false; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlTransaction) sqlTransaction.Dispose(); if (null != sqlConnection) sqlConnection.Close(); } } public static Price GetPrice(String symbol,DateTime pricingDate) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; Price price = null; try { if(Utility.IsEpoch(pricingDate))return null; StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select date,symbol,open,high,low,close,volume,adjclose,source from prices where symbol='").Append(symbol).Append("'").Append(" "); sb.Append("and date='").Append(Utility.DateTimeToStringYYYYHMMHDD(pricingDate)).Append("'").Append(" "); strQuery = sb.ToString(); ; sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); if (sqlDataReader.Read()) { price = new Price(); price.Date = sqlDataReader.GetDateTime(0); price.Symbol = sqlDataReader.GetString(1); if(!sqlDataReader.IsDBNull(2))price.Open = sqlDataReader.GetDouble(2); if(!sqlDataReader.IsDBNull(3))price.High = sqlDataReader.GetDouble(3); if(!sqlDataReader.IsDBNull(4))price.Low = sqlDataReader.GetDouble(4); if(!sqlDataReader.IsDBNull(5))price.Close = sqlDataReader.GetDouble(5); if(!sqlDataReader.IsDBNull(6))price.Volume = sqlDataReader.GetInt64(6); if(!sqlDataReader.IsDBNull(7))price.AdjClose = sqlDataReader.GetDouble(7); if(!sqlDataReader.IsDBNull(8))price.Source = (Price.PriceSource)Utility.CharToNum(sqlDataReader.GetChar(8)); else price.Source=Price.PriceSource.Other; } return price; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return price; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static Price GetPrice(String symbol) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand =null; String strQuery = null; Price price = null; try { StringBuilder sb = new StringBuilder(); DateTime latestDate = GetLatestDate(symbol); if(Utility.IsEpoch(latestDate))return null; sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select date,symbol,open,high,low,close,volume,adjclose,source from prices where symbol='").Append(symbol).Append("'").Append(" "); sb.Append("and date='").Append(Utility.DateTimeToStringYYYYHMMHDD(latestDate)).Append("'").Append(" "); strQuery = sb.ToString(); ; sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); if (sqlDataReader.Read()) { price = new Price(); price.Date = sqlDataReader.GetDateTime(0); price.Symbol = sqlDataReader.GetString(1); 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; price.Volume = sqlDataReader.GetInt64(6); price.AdjClose = sqlDataReader.GetDouble(7); if(!sqlDataReader.IsDBNull(8))price.Source = (Price.PriceSource)Utility.CharToNum(sqlDataReader.GetChar(8)); else price.Source=Price.PriceSource.Other; } return price; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return price; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static Dictionary GetLatestPrices(List symbols) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; Dictionary latestPrices = new Dictionary(); try { 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()) { 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 latestPrices; } 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 Prices GetPrices(String symbol) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; Prices prices = new Prices(); try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select date,symbol,open,high,low,close,volume,adjclose,source from prices where symbol='").Append(symbol).Append("' order by date desc"); strQuery = sb.ToString(); ; sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); while(sqlDataReader.Read()) { 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; prices.Add(price); } return prices; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return prices; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static Prices GetPrices(String symbol, int days) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; Prices prices = new Prices(); try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select date,symbol,open,high,low,close,volume,adjclose,source from prices where symbol='").Append(symbol).Append("'"); sb.Append(" order by date desc"); sb.Append(" limit ").Append(days); strQuery = sb.ToString(); ; sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); int count = 0; while (sqlDataReader.Read() && count < days) { Price price = new Price(); price.Date = sqlDataReader.GetDateTime(0); price.Symbol = sqlDataReader.GetString(1); 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; price.Volume = sqlDataReader.GetInt64(6); price.AdjClose = sqlDataReader.GetDouble(7); if(!sqlDataReader.IsDBNull(8))price.Source = (Price.PriceSource)Utility.CharToNum(sqlDataReader.GetChar(8)); else price.Source=Price.PriceSource.Other; prices.Add(price); count++; } return prices; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return prices; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } // Prices are returned with the latest (most current) price in the lowest index location public static Prices GetPrices(String symbol,DateTime startDate, int days) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; Prices prices = new Prices(); try { StringBuilder sb = new StringBuilder(); DateGenerator dateGenerator = new DateGenerator(); DateTime maxHistoricalDate = dateGenerator.GenerateHistoricalDate(startDate, days * 2); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select date,symbol,open,high,low,close,volume,adjclose,source from prices where symbol='").Append(symbol).Append("'"); sb.Append(" and date<=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(startDate)).Append("'"); sb.Append(" and date>=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(maxHistoricalDate)).Append("'"); sb.Append(" order by date desc"); sb.Append(" limit ").Append(days); strQuery = sb.ToString(); ; sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); while (sqlDataReader.Read()) { Price price = new Price(); price.Date = sqlDataReader.GetDateTime(0); price.Symbol = sqlDataReader.GetString(1); if(!sqlDataReader.IsDBNull(2))price.Open = sqlDataReader.GetDouble(2); if(!sqlDataReader.IsDBNull(3))price.High = sqlDataReader.GetDouble(3); if(!sqlDataReader.IsDBNull(4))price.Low = sqlDataReader.GetDouble(4); if(!sqlDataReader.IsDBNull(5))price.Close = sqlDataReader.GetDouble(5); if(!sqlDataReader.IsDBNull(6))price.Volume = sqlDataReader.GetInt64(6); if(!sqlDataReader.IsDBNull(7))price.AdjClose = sqlDataReader.GetDouble(7); if(!sqlDataReader.IsDBNull(8))price.Source = (Price.PriceSource)Utility.CharToNum(sqlDataReader.GetChar(8)); else price.Source=Price.PriceSource.Other; prices.Add(price); } return prices; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return prices; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } // Get prices starting at "startDate" and "days" number of days going into future public static Prices GetPricesForward(String symbol,DateTime startDate,int days) { MySqlConnection sqlConnection=null; MySqlDataReader sqlDataReader=null; MySqlCommand sqlCommand=null; String strQuery=null; Prices prices=new Prices(); try { StringBuilder sb=new StringBuilder(); DateGenerator dateGenerator=new DateGenerator(); DateTime maxDate=dateGenerator.GenerateFutureBusinessDate(startDate,days); sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select date,symbol,open,high,low,close,volume,adjclose,source from prices where symbol='").Append(symbol).Append("'"); sb.Append(" and date>=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(startDate)).Append("'"); sb.Append(" and date<=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(maxDate)).Append("'"); sb.Append(" order by date asc"); sb.Append(" limit ").Append(days); strQuery=sb.ToString(); ; sqlCommand=new MySqlCommand(strQuery,sqlConnection); sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT; sqlDataReader=sqlCommand.ExecuteReader(); while(sqlDataReader.Read()) { Price price=new Price(); price.Date=sqlDataReader.GetDateTime(0); price.Symbol=sqlDataReader.GetString(1); if(!sqlDataReader.IsDBNull(2)) price.Open=sqlDataReader.GetDouble(2); if(!sqlDataReader.IsDBNull(3)) price.High=sqlDataReader.GetDouble(3); if(!sqlDataReader.IsDBNull(4)) price.Low=sqlDataReader.GetDouble(4); if(!sqlDataReader.IsDBNull(5)) price.Close=sqlDataReader.GetDouble(5); if(!sqlDataReader.IsDBNull(6)) price.Volume=sqlDataReader.GetInt64(6); if(!sqlDataReader.IsDBNull(7)) price.AdjClose=sqlDataReader.GetDouble(7); if(!sqlDataReader.IsDBNull(8))price.Source = (Price.PriceSource)Utility.CharToNum(sqlDataReader.GetChar(8)); else price.Source=Price.PriceSource.Other; prices.Add(price); } return prices; } catch(Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return prices; } finally { if(null!=sqlCommand) sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if(null!=sqlConnection) sqlConnection.Close(); } } public static Prices GetPricesOnOrBefore(String symbol,DateTime startDate) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; Prices prices = new Prices(); try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select date,symbol,open,high,low,close,volume,adjclose,source from prices where symbol='").Append(symbol).Append("'"); sb.Append(" and date<=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(startDate)).Append("'"); sb.Append(" order by date desc"); strQuery = sb.ToString(); ; sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); while (sqlDataReader.Read()) { Price price = new Price(); price.Date = sqlDataReader.GetDateTime(0); price.Symbol = sqlDataReader.GetString(1); if(!sqlDataReader.IsDBNull(2))price.Open = sqlDataReader.GetDouble(2); if(!sqlDataReader.IsDBNull(3))price.High = sqlDataReader.GetDouble(3); if(!sqlDataReader.IsDBNull(4))price.Low = sqlDataReader.GetDouble(4); if(!sqlDataReader.IsDBNull(5))price.Close = sqlDataReader.GetDouble(5); if(!sqlDataReader.IsDBNull(6))price.Volume = sqlDataReader.GetInt64(6); if(!sqlDataReader.IsDBNull(7))price.AdjClose = sqlDataReader.GetDouble(7); if(!sqlDataReader.IsDBNull(8))price.Source = (Price.PriceSource)Utility.CharToNum(sqlDataReader.GetChar(8)); else price.Source=Price.PriceSource.Other; prices.Add(price); } return prices; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return prices; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } // StartDate: The most recent date // EndDate: The most historical date public static Prices GetPrices(String symbol,DateTime startDate,DateTime endDate) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; Prices prices = new Prices(); try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select date,symbol,open,high,low,close,volume,adjclose,source from prices where symbol='").Append(symbol).Append("'"); sb.Append(" "); sb.Append("and date<='").Append(Utility.DateTimeToStringYYYYHMMHDD(startDate)).Append("'"); sb.Append(" and date>='").Append(Utility.DateTimeToStringYYYYHMMHDD(endDate)).Append("'"); sb.Append(" order by date desc"); strQuery = sb.ToString(); ; // MDTrace.WriteLine(LogLevel.DEBUG,strQuery); sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); while (sqlDataReader.Read()) { Price price = new Price(); price.Date = sqlDataReader.GetDateTime(0); price.Symbol = sqlDataReader.GetString(1); if(sqlDataReader.IsDBNull(2))price.Open=double.NaN; else price.Open = sqlDataReader.GetDouble(2); if(sqlDataReader.IsDBNull(3))price.High=double.NaN; else price.High = sqlDataReader.GetDouble(3); if(sqlDataReader.IsDBNull(4))price.Low=double.NaN; else price.Low = sqlDataReader.GetDouble(4); if(sqlDataReader.IsDBNull(5))price.Close=double.NaN; else price.Close = sqlDataReader.GetDouble(5); price.Volume = sqlDataReader.GetInt64(6); price.AdjClose = sqlDataReader.GetDouble(7); if(!sqlDataReader.IsDBNull(8))price.Source = (Price.PriceSource)Utility.CharToNum(sqlDataReader.GetChar(8)); else price.Source=Price.PriceSource.Other; prices.Add(price); } return prices; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return prices; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static Prices GetPrices(String symbol, DateTime endDate) { MySqlConnection sqlConnection = null; MySqlDataReader sqlDataReader = null; MySqlCommand sqlCommand=null; String strQuery = null; Prices prices = new Prices(); try { StringBuilder sb = new StringBuilder(); sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sb.Append("select date,symbol,open,high,low,close,volume,adjclose,source from prices where symbol='").Append(symbol).Append("'"); sb.Append(" "); sb.Append(" and date>='").Append(Utility.DateTimeToStringYYYYHMMHDD(endDate)).Append("'"); sb.Append(" order by date desc"); strQuery = sb.ToString(); ; sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlDataReader = sqlCommand.ExecuteReader(); while (sqlDataReader.Read()) { Price price = new Price(); price.Date = sqlDataReader.GetDateTime(0); price.Symbol = sqlDataReader.GetString(1); if(!sqlDataReader.IsDBNull(2))price.Open = sqlDataReader.GetDouble(2); if(!sqlDataReader.IsDBNull(3))price.High = sqlDataReader.GetDouble(3); if(!sqlDataReader.IsDBNull(4))price.Low = sqlDataReader.GetDouble(4); if(!sqlDataReader.IsDBNull(5))price.Close = sqlDataReader.GetDouble(5); if(!sqlDataReader.IsDBNull(6))price.Volume = sqlDataReader.GetInt64(6); if(!sqlDataReader.IsDBNull(7))price.AdjClose = sqlDataReader.GetDouble(7); if(!sqlDataReader.IsDBNull(8))price.Source = (Price.PriceSource)Utility.CharToNum(sqlDataReader.GetChar(8)); else price.Source=Price.PriceSource.Other; prices.Add(price); } return prices; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return prices; } finally { if(null!=sqlCommand)sqlCommand.Dispose(); if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} if (null != sqlConnection) sqlConnection.Close(); } } public static bool InsertPrice(Price price) { Prices prices = new Prices(); prices.Add(price); DeletePrice(price.Symbol, price.Date); return InsertPrices(prices); } public static bool InsertPrices(Prices prices) { MySqlConnection sqlConnection=null; MySqlTransaction sqlTransaction=null; String strQuery=null; try { if(null==prices||0==prices.Count)return false; sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); sqlTransaction=sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted); DeletePrices(prices,sqlConnection,sqlTransaction); for(int index=0;index='").Append(Utility.DateTimeToStringYYYYHMMHDD(date)).Append("'"); strQuery = sb.ToString(); MySqlCommand sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlCommand.ExecuteNonQuery(); sqlCommand.Dispose(); return true; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return false; } finally { if (null != sqlConnection) sqlConnection.Close(); } } public static bool DeletePriceOnOrBefore(String symbol, DateTime date) { MySqlConnection sqlConnection = null; String strQuery = null; try { sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); StringBuilder sb = new StringBuilder(); sb.Append("delete from prices where symbol='").Append(symbol).Append("'").Append(" "); sb.Append("and date>='").Append(Utility.DateTimeToStringYYYYHMMHDD(date)).Append("'"); strQuery = sb.ToString(); MySqlCommand sqlCommand = new MySqlCommand(strQuery, sqlConnection); sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; sqlCommand.ExecuteNonQuery(); sqlCommand.Dispose(); return true; } catch (Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,exception); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); return false; } finally { if (null != sqlConnection) sqlConnection.Close(); } } private static bool DeletePrices(Prices prices, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction) { for(int index=0;index