using System.Text;
using MySql.Data.MySqlClient;
using MarketData.MarketDataModel;
using MarketData.Utils;

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<String> GetSymbolsNotIn(List<String> symbols)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;
      List<String> selectedSymbols = new List<String>();

      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<String> symbols=new List<String>();
      symbols.Add(symbol);
      return CheckPricingOn(symbols,date);
    }

    public static bool CheckPricingOn(List<String> 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<String,DateTime> GetLatestDates(List<String> symbols)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
      MySqlCommand sqlCommand = null;
      String strQuery = null;
      Dictionary<String,DateTime> latestDates = new Dictionary<String,DateTime>();

      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<String> 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<DateTime> GetPricingDatesForSymbol(String symbol)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;
      List<DateTime> pricingDates=new List<DateTime>();

      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<DateTime> GetPricingDates(DateTime lowerBoundary)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
      MySqlCommand sqlCommand = null;
      String strQuery = null;
      List<DateTime> pricingDates = new List<DateTime>();

      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<DateTime> GetPricingDatesBetween(DateTime lowerBoundary,DateTime upperBoundary)
    {
      MySqlConnection sqlConnection=null;
      MySqlDataReader sqlDataReader=null;
      MySqlCommand sqlCommand=null;
      String strQuery=null;
      List<DateTime> pricingDates=new List<DateTime>();

      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<String> GetSymbols()
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;
      List<String> symbols = new List<String>();

      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<String> GetSymbolsETF()
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;
      List<String> symbols = new List<String>();

      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<String,String> GetNamesForSymbols(List<String> symbols)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;
      Dictionary<String,String> dictionary = new Dictionary<String,String>();

      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<String> symbols = new List<String>();

      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<String> symbols = new List<String>();

      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<String> symbols = new List<String>();

      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<String> GetSymbolsForNullCIK()
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;
      List<String> unmapped = new List<String>();

      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 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)
    {
      Profiler profiler = new Profiler();
      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();
        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[GetPricesForward] Done, took {0}(ms)",profiler.End()));
      }
    }
    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<prices.Count;index++)
        {
          Price price=prices[index];
          if(double.IsNaN(price.Open)&&double.IsNaN(price.Close)&&double.IsNaN(price.High)&&double.IsNaN(price.Low)&&double.IsNaN(price.AdjClose))
          {
            MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Cannot insert null data price for {0}/{1}",price.Symbol,price.Date.ToShortDateString()));
            continue;
          }
          StringBuilder sb=new StringBuilder();
          sb.Append("insert into prices (date,symbol,open,high,low,close,volume,adjclose,source)");
          sb.Append("values(");
          sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(price.Date)).Append("'").Append(",");
          sb.Append("'").Append(price.Symbol).Append("'").Append(",");
          if(double.NaN.Equals(price.Open))sb.Append("null").Append(",");
          else sb.Append(price.Open).Append(",");
          if(double.NaN.Equals(price.High))sb.Append("null").Append(",");
          else sb.Append(price.High).Append(",");
          if(double.NaN.Equals(price.Low))sb.Append("null").Append(",");
          else sb.Append(price.Low).Append(",");
          sb.Append(price.Close).Append(",");
          sb.Append(price.Volume).Append(",");
          sb.Append(price.AdjClose).Append(",");
          sb.Append((int)price.Source);
          sb.Append(")");
          strQuery=sb.ToString();
          MySqlCommand sqlCommand=new MySqlCommand(strQuery,sqlConnection,sqlTransaction);
          sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
          sqlCommand.ExecuteNonQuery();
  				sqlCommand.Dispose();
        }
        sqlTransaction.Commit();
        return true;
      }
      catch(Exception exception)
      {
				if(null!=sqlTransaction)sqlTransaction.Rollback();
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
				if(null!=strQuery)MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery));
        return false;
      }
      finally
      {
        if (null != sqlTransaction) sqlTransaction.Dispose();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static bool DeletePrice(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();
      }
    }
    public static bool DeletePriceOnOrAfter(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();
      }
    }
    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<prices.Count;index++)
      {
        DeletePrice(prices[index],sqlConnection,sqlTransaction);
      }
      return true;
    }
    private static bool DeletePrice(Price price, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
    {
      StringBuilder sb = new StringBuilder();
      String strQuery = null;

      try
      {
        sb.Append("delete from prices where ");
        sb.Append("symbol='").Append(price.Symbol).Append("'");
        sb.Append(" and ");
        sb.Append("date='").Append(Utility.DateTimeToStringYYYYHMMHDD(price.Date)).Append("'");
        strQuery = sb.ToString();
        MySqlCommand sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlCommand.ExecuteNonQuery();
				sqlCommand.Dispose();
        return true;
      }
      catch (Exception exception)
      {
        sqlTransaction.Rollback();
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        if (null != strQuery) MDTrace.WriteLine(LogLevel.DEBUG, "Query was " + strQuery);
        return false;
      }
      finally
      {
      }
    }
  }
}