Initial Commit
This commit is contained in:
168
MarketData/MarketDataLib/DataAccess/AnalystPriceTargetDA.cs
Executable file
168
MarketData/MarketDataLib/DataAccess/AnalystPriceTargetDA.cs
Executable file
@@ -0,0 +1,168 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class AnalystPriceTargetDA
|
||||
{
|
||||
private AnalystPriceTargetDA()
|
||||
{
|
||||
}
|
||||
public static bool CheckAnalystPriceTargetModifiedOn(String symbol,DateTime modified)
|
||||
{
|
||||
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 count(*) from analystpricetarget").Append(" ");
|
||||
sb.Append("where symbol='").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and date='").Append(Utility.DateTimeToStringYYYYHMMHDD(modified)).Append("'");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
sqlDataReader.Read();
|
||||
int recordCount = sqlDataReader.GetInt32(0);
|
||||
return 0 == recordCount ? false : true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static AnalystPriceTarget GetAnalystPriceTarget(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select date,symbol,mean_target,median_target,high_target,low_target from analystpricetarget where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and date=").Append(" (select max(date) from analystpricetarget where symbol='").Append(symbol).Append("')");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
AnalystPriceTarget analystPriceTarget = new AnalystPriceTarget();
|
||||
analystPriceTarget.Date = sqlDataReader.GetDateTime(0);
|
||||
analystPriceTarget.Symbol = sqlDataReader.GetString(1);
|
||||
if (!sqlDataReader.IsDBNull(2))analystPriceTarget.MeanTargetPrice = sqlDataReader.GetDouble(2);
|
||||
if (!sqlDataReader.IsDBNull(2))analystPriceTarget.MedianTargetPrice = sqlDataReader.GetDouble(3);
|
||||
if (!sqlDataReader.IsDBNull(2))analystPriceTarget.HighTargetPrice = sqlDataReader.GetDouble(4);
|
||||
if (!sqlDataReader.IsDBNull(2))analystPriceTarget.LowTargetPrice = sqlDataReader.GetDouble(5);
|
||||
return analystPriceTarget;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool InsertAnalystPriceTarget(AnalystPriceTarget analystPriceTarget)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
DeleteAnalystPriceTarget(analystPriceTarget, sqlConnection, sqlTransaction);
|
||||
sqlTransaction.Commit();
|
||||
sqlTransaction = sqlConnection.BeginTransaction();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into analystpricetarget (date,symbol,mean_target,median_target,high_target,low_target) ");
|
||||
sb.Append("values(");
|
||||
sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(analystPriceTarget.Date)).Append("'").Append(",");
|
||||
sb.Append("'").Append(analystPriceTarget.Symbol).Append("'").Append(",");
|
||||
if (!Double.IsNaN(analystPriceTarget.MeanTargetPrice)) sb.Append(analystPriceTarget.MeanTargetPrice).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(analystPriceTarget.MedianTargetPrice)) sb.Append(analystPriceTarget.MedianTargetPrice).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(analystPriceTarget.HighTargetPrice)) sb.Append(analystPriceTarget.HighTargetPrice).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(analystPriceTarget.LowTargetPrice)) sb.Append(analystPriceTarget.LowTargetPrice);
|
||||
else sb.Append("null");
|
||||
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,"Query was '" + strQuery + "'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static bool DeleteAnalystPriceTarget(AnalystPriceTarget analystPriceTarget, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sb.Append("delete from analystpricetarget where ");
|
||||
sb.Append("symbol='").Append(analystPriceTarget.Symbol).Append("'");
|
||||
sb.Append(" and ");
|
||||
sb.Append("date='").Append(Utility.DateTimeToStringYYYYHMMHDD(analystPriceTarget.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
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
653
MarketData/MarketDataLib/DataAccess/AnalystRatingsDA.cs
Executable file
653
MarketData/MarketDataLib/DataAccess/AnalystRatingsDA.cs
Executable file
@@ -0,0 +1,653 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class AnalystRatingsDA
|
||||
{
|
||||
private AnalystRatingsDA()
|
||||
{
|
||||
}
|
||||
public static AnalystRatings GetAnalystRatings()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
AnalystRatings analystRatings = new AnalystRatings();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select A.* from ");
|
||||
sb.Append("(select date,symbol,upper(company),brokerage_firm,type,ratings_change,price_target from analystratings ");
|
||||
sb.Append(" union ");
|
||||
sb.Append(" select zr.date,zr.symbol,upper(sm.company) as company,'Zacks' as brokerage_firm,zr.type,zr.zacks_rank as ratings_change,null as price_target from zacksrank zr ");
|
||||
sb.Append(" left join securitymaster sm on sm.symbol=zr.symbol ");
|
||||
sb.Append(" where zr.symbol=@symbol and zr.type is not null ");
|
||||
sb.Append(" )A ");
|
||||
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())
|
||||
{
|
||||
AnalystRating analystRating = new AnalystRating();
|
||||
analystRating.Date = sqlDataReader.GetDateTime(0);
|
||||
analystRating.Symbol = sqlDataReader.GetString(1);
|
||||
analystRating.CompanyName = sqlDataReader.GetString(2);
|
||||
analystRating.BrokerageFirm = sqlDataReader.GetString(3);
|
||||
analystRating.Type = sqlDataReader.GetString(4);
|
||||
analystRating.RatingsChange = sqlDataReader.GetString(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) analystRating.PriceTarget = sqlDataReader.GetDouble(6);
|
||||
analystRatings.Add(analystRating);
|
||||
}
|
||||
analystRatings.CalculateUpdatePercentiles();
|
||||
return analystRatings;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static AnalystRatings GetAnalystRatings(String symbol, DateTime minDate,DateTime maxDate)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
AnalystRatings analystRatings = new AnalystRatings();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("set @symbol='").Append(symbol).Append("';");
|
||||
sb.Append("set @minDate='").Append(Utility.DateTimeToStringYYYYHMMHDD(minDate)).Append("';");
|
||||
sb.Append("set @maxDate='").Append(Utility.DateTimeToStringYYYYHMMHDD(maxDate)).Append("';");
|
||||
sb.Append(" select A.* from ");
|
||||
sb.Append(" (select date,symbol,upper(company),brokerage_firm,type,ratings_change,price_target from analystratings where symbol=@symbol ");
|
||||
sb.Append(" union ");
|
||||
sb.Append(" select zr.date,zr.symbol,upper(sm.company) as company,'Zacks' as brokerage_firm,zr.type,zr.zacks_rank as ratings_change,null as price_target from zacksrank zr ");
|
||||
sb.Append(" left join securitymaster sm on sm.symbol=zr.symbol ");
|
||||
sb.Append(" where zr.symbol=@symbol and zr.type is not null ");
|
||||
sb.Append(" )A ");
|
||||
sb.Append(" where A.date>=@minDate and A.date<=@maxDate ");
|
||||
sb.Append(" order by date ");
|
||||
|
||||
|
||||
//sb.Append(" select A.* from ");
|
||||
//sb.Append(" (select date,symbol,upper(company),brokerage_firm,type,ratings_change,price_target from analystratings where symbol=").Append(SqlUtils.ToSqlString(symbol));
|
||||
//sb.Append(" union ");
|
||||
//sb.Append(" select zr.date,zr.symbol,upper(sm.company) as company,'Zacks' as brokerage_firm,zr.type,zr.zacks_rank as ratings_change,null as price_target from zacksrank zr ");
|
||||
//sb.Append(" left join securitymaster sm on sm.symbol=zr.symbol ");
|
||||
//sb.Append(" where zr.symbol=").Append(SqlUtils.ToSqlString(symbol)).Append(" and zr.type is not null ");
|
||||
//sb.Append(" )A ");
|
||||
//sb.Append(" where A.date>=").Append(SqlUtils.SqlDate(minDate,true)).Append(" and A.date<=").Append(SqlUtils.SqlDate(maxDate,true));
|
||||
//sb.Append(" order by date ");
|
||||
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
AnalystRating analystRating = new AnalystRating();
|
||||
analystRating.Date = sqlDataReader.GetDateTime(0);
|
||||
analystRating.Symbol = sqlDataReader.GetString(1);
|
||||
analystRating.CompanyName = sqlDataReader.GetString(2);
|
||||
analystRating.BrokerageFirm = sqlDataReader.GetString(3);
|
||||
analystRating.Type = sqlDataReader.GetString(4);
|
||||
analystRating.RatingsChange = sqlDataReader.GetString(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) analystRating.PriceTarget = sqlDataReader.GetDouble(6);
|
||||
analystRatings.Add(analystRating);
|
||||
}
|
||||
analystRatings.CalculateUpdatePercentiles();
|
||||
return analystRatings;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DateTime GetMaxDateNoZacks()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select max(date) from analystratings where brokerage_firm<>'Zacks'");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return Utility.Epoch;
|
||||
return sqlDataReader.GetDateTime(0);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return Utility.Epoch;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static AnalystRatings GetAnalystRatingsMaxDateNoZacks(String symbol,DateTime maxDate)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
AnalystRatings analystRatings = new AnalystRatings();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select date,symbol,upper(company),brokerage_firm,type,ratings_change,price_target from analystratings ");
|
||||
sb.Append(" where symbol='").Append(symbol).Append("'").Append(" and ");
|
||||
sb.Append(" date<=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(maxDate)).Append("'").Append(" ");
|
||||
sb.Append("order by date");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
AnalystRating analystRating = new AnalystRating();
|
||||
analystRating.Date = sqlDataReader.GetDateTime(0);
|
||||
analystRating.Symbol = sqlDataReader.GetString(1);
|
||||
analystRating.CompanyName = sqlDataReader.GetString(2);
|
||||
analystRating.BrokerageFirm = sqlDataReader.GetString(3);
|
||||
analystRating.Type = sqlDataReader.GetString(4);
|
||||
analystRating.RatingsChange = sqlDataReader.GetString(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) analystRating.PriceTarget = sqlDataReader.GetDouble(6);
|
||||
analystRatings.Add(analystRating);
|
||||
}
|
||||
analystRatings.CalculateUpdatePercentiles();
|
||||
return analystRatings;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static AnalystRatings GetAnalystRatingsMaxDate(String symbol,DateTime maxDate)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
AnalystRatings analystRatings = new AnalystRatings();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
// sb.Append("set @symbol='").Append(symbol).Append("' COLLATE utf8mb4_unicode_ci ;");
|
||||
//sb.Append("set @symbol='").Append(symbol).Append("';");
|
||||
//sb.Append("set @maxDate='").Append(Utility.DateTimeToStringYYYYHMMHDD(maxDate)).Append("';");
|
||||
//sb.Append(" select A.* from ");
|
||||
//sb.Append(" (select date,symbol,upper(company),brokerage_firm,type,ratings_change,price_target from analystratings where symbol=@symbol ");
|
||||
//sb.Append(" union ");
|
||||
//sb.Append(" select zr.date,zr.symbol,upper(sm.company) as company,'Zacks' as brokerage_firm,zr.type,zr.zacks_rank as ratings_change,null as price_target from zacksrank zr ");
|
||||
//sb.Append(" left join securitymaster sm on sm.symbol=zr.symbol ");
|
||||
//sb.Append(" where zr.symbol=@symbol and zr.type is not null ");
|
||||
//sb.Append(" )A ");
|
||||
//sb.Append(" where A.date<=@maxDate ");
|
||||
//sb.Append(" order by date");
|
||||
|
||||
|
||||
|
||||
//sb.Append(" select A.* from ");
|
||||
//sb.Append(" (select date,symbol,upper(company),brokerage_firm,type,ratings_change,price_target from analystratings where symbol=").Append(SqlUtils.ToSqlString(symbol));
|
||||
//sb.Append(" union ");
|
||||
//sb.Append(" select zr.date,zr.symbol,upper(sm.company) as company,'Zacks' as brokerage_firm,zr.type,zr.zacks_rank as ratings_change,null as price_target from zacksrank zr ");
|
||||
//sb.Append(" left join securitymaster sm on sm.symbol=zr.symbol ");
|
||||
//sb.Append(" where zr.symbol=").Append(SqlUtils.ToSqlString(symbol)).Append(" and zr.type is not null ");
|
||||
//sb.Append(" )A ");
|
||||
//sb.Append(" where A.date<=").Append(SqlUtils.SqlDate(maxDate,true));
|
||||
//sb.Append(" order by date");
|
||||
|
||||
sb.Append("set @symbol='").Append(symbol).Append("';");
|
||||
sb.Append("set @maxDate='").Append(Utility.DateTimeToStringYYYYHMMHDD(maxDate)).Append("';");
|
||||
sb.Append(" select A.* from ");
|
||||
sb.Append(" (select date,symbol,upper(company),brokerage_firm,type,ratings_change,price_target from analystratings where symbol=@symbol ");
|
||||
sb.Append(" union ");
|
||||
sb.Append(" select zr.date,zr.symbol,upper(sm.company) as company,'Zacks' as brokerage_firm,zr.type,zr.zacks_rank as ratings_change,null as price_target from zacksrank zr ");
|
||||
sb.Append(" left join securitymaster sm on sm.symbol=zr.symbol ");
|
||||
sb.Append(" where zr.symbol=@symbol and zr.type is not null ");
|
||||
sb.Append(" )A ");
|
||||
sb.Append(" where A.date<=@maxDate ");
|
||||
sb.Append(" order by date");
|
||||
|
||||
|
||||
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
AnalystRating analystRating = new AnalystRating();
|
||||
analystRating.Date = sqlDataReader.GetDateTime(0);
|
||||
analystRating.Symbol = sqlDataReader.GetString(1);
|
||||
analystRating.CompanyName = sqlDataReader.GetString(2);
|
||||
analystRating.BrokerageFirm = sqlDataReader.GetString(3);
|
||||
analystRating.Type = sqlDataReader.GetString(4);
|
||||
analystRating.RatingsChange = sqlDataReader.GetString(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) analystRating.PriceTarget = sqlDataReader.GetDouble(6);
|
||||
analystRatings.Add(analystRating);
|
||||
}
|
||||
analystRatings.CalculateUpdatePercentiles();
|
||||
return analystRatings;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static AnalystRatings GetAnalystRatings(String symbol, DateTime date)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
AnalystRatings analystRatings = new AnalystRatings();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
// sb.Append("set @symbol='").Append(symbol).Append("' COLLATE utf8mb4_unicode_ci ;");
|
||||
sb.Append("set @symbol='").Append(symbol).Append("';");
|
||||
sb.Append("set @date='").Append(Utility.DateTimeToStringYYYYHMMHDD(date)).Append("';");
|
||||
sb.Append(" select A.* from ");
|
||||
sb.Append(" (select date,symbol,upper(company),brokerage_firm,type,ratings_change,price_target from analystratings where symbol=@symbol ");
|
||||
sb.Append(" union ");
|
||||
sb.Append(" select zr.date,zr.symbol,upper(sm.company) as company,'Zacks' as brokerage_firm,zr.type,zr.zacks_rank as ratings_change,null as price_target from zacksrank zr ");
|
||||
sb.Append(" left join securitymaster sm on sm.symbol=zr.symbol ");
|
||||
sb.Append(" where zr.symbol=@symbol and zr.type is not null ");
|
||||
sb.Append(" )A ");
|
||||
sb.Append(" where A.date=@date ");
|
||||
sb.Append(" order by A.symbol");
|
||||
|
||||
//sb.Append(" select A.* from ");
|
||||
//sb.Append(" (select date,symbol,upper(company),brokerage_firm,type,ratings_change,price_target from analystratings where symbol=").Append(SqlUtils.ToSqlString(symbol));
|
||||
//sb.Append(" union ");
|
||||
//sb.Append(" select zr.date,zr.symbol,upper(sm.company) as company,'Zacks' as brokerage_firm,zr.type,zr.zacks_rank as ratings_change,null as price_target from zacksrank zr ");
|
||||
//sb.Append(" left join securitymaster sm on sm.symbol=zr.symbol ");
|
||||
//sb.Append(" where zr.symbol=@symbol and zr.type is not null ");
|
||||
//sb.Append(" )A ");
|
||||
//sb.Append(" where A.date=").Append(SqlUtils.SqlDate(date,true));
|
||||
//sb.Append(" order by A.symbol");
|
||||
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
AnalystRating analystRating = new AnalystRating();
|
||||
analystRating.Date = sqlDataReader.GetDateTime(0);
|
||||
analystRating.Symbol = sqlDataReader.GetString(1);
|
||||
analystRating.CompanyName = sqlDataReader.GetString(2);
|
||||
analystRating.BrokerageFirm = sqlDataReader.GetString(3);
|
||||
analystRating.Type = sqlDataReader.GetString(4);
|
||||
analystRating.RatingsChange = sqlDataReader.GetString(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) analystRating.PriceTarget = sqlDataReader.GetDouble(6);
|
||||
analystRatings.Add(analystRating);
|
||||
}
|
||||
analystRatings.CalculateUpdatePercentiles();
|
||||
return analystRatings;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static AnalystRatings GetAnalystRatings(DateTime date)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
AnalystRatings analystRatings = new AnalystRatings();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
//sb.Append("set @date='").Append(Utility.DateTimeToStringYYYYHMMHDD(date)).Append("';");
|
||||
//sb.Append(" select A.* from ");
|
||||
//sb.Append(" (select date,symbol,upper(company),brokerage_firm,type,ratings_change,price_target from analystratings ");
|
||||
//sb.Append(" union ");
|
||||
//sb.Append(" select zr.date,zr.symbol,upper(sm.company) as company,'Zacks' as brokerage_firm,zr.type,zr.zacks_rank as ratings_change,null as price_target from zacksrank zr ");
|
||||
//sb.Append(" left join securitymaster sm on sm.symbol=zr.symbol ");
|
||||
//sb.Append(" where zr.type is not null ");
|
||||
//sb.Append(" )A ");
|
||||
//sb.Append(" where A.date=@date ");
|
||||
//sb.Append(" order by A.symbol; ");
|
||||
|
||||
//sb.Append("set @date='").Append(Utility.DateTimeToStringYYYYHMMHDD(date)).Append("';");
|
||||
sb.Append(" select A.* from ");
|
||||
sb.Append(" (select date,symbol,upper(company),brokerage_firm,type,ratings_change,price_target from analystratings ");
|
||||
sb.Append(" union ");
|
||||
sb.Append(" select zr.date,zr.symbol,upper(sm.company) as company,'Zacks' as brokerage_firm,zr.type,zr.zacks_rank as ratings_change,null as price_target from zacksrank zr ");
|
||||
sb.Append(" left join securitymaster sm on sm.symbol=zr.symbol ");
|
||||
sb.Append(" where zr.type is not null ");
|
||||
sb.Append(" )A ");
|
||||
sb.Append(" where A.date=").Append(SqlUtils.SqlDate(date,true));
|
||||
sb.Append(" order by A.symbol; ");
|
||||
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
AnalystRating analystRating = new AnalystRating();
|
||||
analystRating.Date = sqlDataReader.GetDateTime(0);
|
||||
analystRating.Symbol = sqlDataReader.GetString(1);
|
||||
analystRating.CompanyName = sqlDataReader.GetString(2);
|
||||
analystRating.BrokerageFirm = sqlDataReader.GetString(3);
|
||||
analystRating.Type = sqlDataReader.GetString(4);
|
||||
analystRating.RatingsChange = sqlDataReader.GetString(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) analystRating.PriceTarget = sqlDataReader.GetDouble(6);
|
||||
analystRatings.Add(analystRating);
|
||||
}
|
||||
analystRatings.CalculateUpdatePercentiles();
|
||||
return analystRatings;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static AnalystRatings GetAnalystRatings(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
AnalystRatings analystRatings = new AnalystRatings();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
|
||||
//sb.Append(" set @symbol='").Append(symbol).Append("';");
|
||||
//sb.Append(" select A.* from ");
|
||||
//sb.Append(" (select date,symbol,upper(company),brokerage_firm,type,ratings_change,price_target from analystratings where symbol=@symbol ");
|
||||
//sb.Append(" union ");
|
||||
//sb.Append(" select zr.date,zr.symbol,upper(sm.company) as company,'Zacks' as brokerage_firm,zr.type,zr.zacks_rank as ratings_change,null as price_target from zacksrank zr ");
|
||||
//sb.Append(" left join securitymaster sm on sm.symbol=zr.symbol ");
|
||||
//sb.Append(" where zr.symbol=@symbol and zr.type is not null ");
|
||||
//sb.Append(" )A ");
|
||||
//sb.Append(" order by date desc ");
|
||||
|
||||
//sb.Append(" select A.* from ");
|
||||
//sb.Append(" (select date,symbol,upper(company),brokerage_firm,type,ratings_change,price_target from analystratings where symbol=").Append(SqlUtils.ToSqlString(symbol));
|
||||
//sb.Append(" union ");
|
||||
//sb.Append(" select zr.date,zr.symbol,upper(sm.company) as company,'Zacks' as brokerage_firm,zr.type,zr.zacks_rank as ratings_change,null as price_target from zacksrank zr ");
|
||||
//sb.Append(" left join securitymaster sm on sm.symbol=zr.symbol ");
|
||||
//sb.Append(" where zr.symbol=").Append(SqlUtils.ToSqlString(symbol)).Append(" and zr.type is not null ");
|
||||
//sb.Append(" )A ");
|
||||
//sb.Append(" order by date desc ");
|
||||
|
||||
sb.Append(" set @symbol='").Append(symbol).Append("';");
|
||||
sb.Append(" select A.* from ");
|
||||
sb.Append(" (select date,symbol,upper(company),brokerage_firm,type,ratings_change,price_target from analystratings where symbol=@symbol ");
|
||||
sb.Append(" union ");
|
||||
sb.Append(" select zr.date,zr.symbol,upper(sm.company) as company,'Zacks' as brokerage_firm,zr.type,zr.zacks_rank as ratings_change,null as price_target from zacksrank zr ");
|
||||
sb.Append(" left join securitymaster sm on sm.symbol=zr.symbol ");
|
||||
sb.Append(" where zr.symbol=@symbol and zr.type is not null ");
|
||||
sb.Append(" )A ");
|
||||
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())
|
||||
{
|
||||
AnalystRating analystRating = new AnalystRating();
|
||||
analystRating.Date = sqlDataReader.GetDateTime(0);
|
||||
analystRating.Symbol = sqlDataReader.GetString(1);
|
||||
analystRating.CompanyName = sqlDataReader.GetString(2);
|
||||
analystRating.BrokerageFirm = sqlDataReader.GetString(3);
|
||||
analystRating.Type = sqlDataReader.GetString(4);
|
||||
analystRating.RatingsChange = sqlDataReader.GetString(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) analystRating.PriceTarget = sqlDataReader.GetDouble(6);
|
||||
analystRatings.Add(analystRating);
|
||||
}
|
||||
analystRatings.CalculateUpdatePercentiles();
|
||||
return analystRatings;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
// ****************************************************************************************************************************************************
|
||||
public static List<String> GetAnalystRatingsDates()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
List<String> analystRatingsDates = new List<String>();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select distinct date from analystratings order by date desc");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
DateTime ratingDate = sqlDataReader.GetDateTime(0);
|
||||
analystRatingsDates.Add(Utility.DateTimeToStringMMHDDHYYYY(ratingDate));
|
||||
}
|
||||
return analystRatingsDates;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool InsertAnalystRatings(AnalystRatings analystRatings)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == analystRatings || 0 == analystRatings.Count) return true;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
DeleteAnalystRatings(analystRatings, sqlConnection, sqlTransaction);
|
||||
for (int index = 0; index < analystRatings.Count; index++)
|
||||
{
|
||||
AnalystRating analystRating = analystRatings[index];
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into analystratings (date,symbol,company,brokerage_firm,type,ratings_change,price_target) ");
|
||||
sb.Append("values(");
|
||||
sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(analystRating.Date)).Append("'").Append(",");
|
||||
sb.Append("'").Append(analystRating.Symbol).Append("'").Append(",");
|
||||
sb.Append("'").Append(SqlUtils.SqlString(analystRating.CompanyName)).Append("'").Append(",");
|
||||
sb.Append("'").Append(SqlUtils.SqlString(analystRating.BrokerageFirm)).Append("'").Append(",");
|
||||
sb.Append("'").Append(analystRating.Type).Append("'").Append(",");
|
||||
sb.Append("'").Append(analystRating.RatingsChange).Append("'").Append(",");
|
||||
if (!Double.IsNaN(analystRating.PriceTarget)) sb.Append(analystRating.PriceTarget);
|
||||
else sb.Append("null");
|
||||
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,"Query was '" + strQuery + "'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool ContainsAnalystRating(AnalystRating analystRating)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select count(*) from analystratings where ");
|
||||
sb.Append("symbol='").Append(analystRating.Symbol).Append("'").Append(" and ");
|
||||
sb.Append("date='").Append(Utility.DateTimeToStringYYYYHMMHDD(analystRating.Date)).Append("'").Append(" and ");
|
||||
sb.Append("brokerage_firm='").Append(SqlUtils.SqlString(analystRating.BrokerageFirm)).Append("'");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return false;
|
||||
int count = sqlDataReader.GetInt32(0);
|
||||
return count > 0 ? true : false;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, "Query was '" + strQuery + "'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlDataReader) {sqlDataReader.Close(); sqlDataReader.Dispose();}
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static bool DeleteAnalystRatings(AnalystRatings analystRatings, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (AnalystRating analystRating in analystRatings)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String strQuery = null;
|
||||
sb.Append("delete from analystratings where ");
|
||||
sb.Append("symbol='").Append(analystRating.Symbol).Append("'");
|
||||
sb.Append(" and ");
|
||||
sb.Append("date='").Append(Utility.DateTimeToStringYYYYHMMHDD(analystRating.Date)).Append("'").Append(" ");
|
||||
sb.Append("and brokerage_firm='").Append(SqlUtils.SqlString(analystRating.BrokerageFirm)).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);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
618
MarketData/MarketDataLib/DataAccess/BalanceSheetDA.cs
Executable file
618
MarketData/MarketDataLib/DataAccess/BalanceSheetDA.cs
Executable file
@@ -0,0 +1,618 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class BalanceSheetDA
|
||||
{
|
||||
private BalanceSheetDA()
|
||||
{
|
||||
}
|
||||
// ********************************************* B A L A N C E S H E E T **************************************
|
||||
// get the greatest date on record that is on or before asof date
|
||||
public static DateTime? GetMaxDateFromBalanceSheet(String symbol, DateTime asof,BalanceSheet.PeriodType periodType)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
DateTime? maxDate=null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select asof from balancesheet where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and asof<=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(asof)).Append("' ");
|
||||
sb.Append("and period=").Append(periodType.Equals(BalanceSheet.PeriodType.Annual) ? 0 : 1).Append(" ");
|
||||
sb.Append(" order by asof desc ");
|
||||
sb.Append(" limit 1").Append(";");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
if(!sqlDataReader.IsDBNull(0))maxDate=sqlDataReader.GetDateTime(0);
|
||||
return maxDate;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static double GetTotalStockHolderEquityOnOrBefore(String symbol, DateTime asof,BalanceSheet.PeriodType periodType)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
double totalStockholderEquity=double.NaN;
|
||||
|
||||
try
|
||||
{
|
||||
DateTime? maxDate=GetMaxDateFromBalanceSheet(symbol,asof,periodType);
|
||||
if(null==maxDate)return double.NaN;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select total_stockholder_equity from balancesheet where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and period=").Append(periodType.Equals(BalanceSheet.PeriodType.Annual) ? 0 : 1).Append(" ");
|
||||
sb.Append("and asof=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(maxDate.Value)).Append("'");
|
||||
sb.Append(" limit 1").Append(";");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return double.NaN;
|
||||
if(!sqlDataReader.IsDBNull(0))totalStockholderEquity=sqlDataReader.GetDouble(0);
|
||||
return totalStockholderEquity;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return double.NaN;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static BalanceSheet GetBalanceSheetOnOrBefore(String symbol, DateTime asof,BalanceSheet.PeriodType periodType)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
DateTime? maxDate=GetMaxDateFromBalanceSheet(symbol,asof,periodType);
|
||||
if(null==maxDate)return null;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,long_term_debt,other_liabilities,deferred_long_term_liabilities,minority_interest,negative_goodwill,total_stockholder_equity,inventory,property_plant_and_equipment,intangible_assets,accumulated_amortization,goodwill,period,total_assets,total_current_assets,total_liabilities,total_current_liabilities,cash_and_cash_equivalents,modified from balancesheet where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and period=").Append(periodType.Equals(BalanceSheet.PeriodType.Annual) ? 0 : 1).Append(" ");
|
||||
sb.Append("and asof=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(maxDate.Value)).Append("'");
|
||||
sb.Append(" limit 1").Append(";");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
BalanceSheet balanceSheet=new BalanceSheet();
|
||||
balanceSheet.Symbol=sqlDataReader.GetString(0);
|
||||
balanceSheet.AsOf=sqlDataReader.GetDateTime(1);
|
||||
if (!sqlDataReader.IsDBNull(2))balanceSheet.LongTermDebt= sqlDataReader.GetDouble(2);
|
||||
if (!sqlDataReader.IsDBNull(3))balanceSheet.OtherLiabilities=sqlDataReader.GetDouble(3);
|
||||
if (!sqlDataReader.IsDBNull(4))balanceSheet.DeferredLongTermLiabilities=sqlDataReader.GetDouble(4);
|
||||
if (!sqlDataReader.IsDBNull(5))balanceSheet.MinorityInterest=sqlDataReader.GetDouble(5);
|
||||
if (!sqlDataReader.IsDBNull(6))balanceSheet.NegativeGoodwill=sqlDataReader.GetDouble(6);
|
||||
if (!sqlDataReader.IsDBNull(7)) balanceSheet.TotalStockHolderEquity = sqlDataReader.GetDouble(7);
|
||||
if (!sqlDataReader.IsDBNull(8)) balanceSheet.Inventory = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) balanceSheet.PropertyPlantAndEquipment = sqlDataReader.GetDouble(9);
|
||||
if (!sqlDataReader.IsDBNull(10)) balanceSheet.IntangibleAssets = sqlDataReader.GetDouble(10);
|
||||
if (!sqlDataReader.IsDBNull(11)) balanceSheet.AccumulatedAmortization = sqlDataReader.GetDouble(11);
|
||||
if (!sqlDataReader.IsDBNull(12)) balanceSheet.Goodwill = sqlDataReader.GetDouble(12);
|
||||
if (!sqlDataReader.IsDBNull(13)) balanceSheet.Period = sqlDataReader.GetInt32(13) == 0 ? BalanceSheet.PeriodType.Annual : BalanceSheet.PeriodType.Quarterly;
|
||||
if (!sqlDataReader.IsDBNull(14)) balanceSheet.TotalAssets = sqlDataReader.GetDouble(14);
|
||||
if (!sqlDataReader.IsDBNull(15)) balanceSheet.TotalCurrentAssets = sqlDataReader.GetDouble(15);
|
||||
if (!sqlDataReader.IsDBNull(16)) balanceSheet.TotalLiabilities = sqlDataReader.GetDouble(16);
|
||||
if (!sqlDataReader.IsDBNull(17)) balanceSheet.TotalCurrentLiabilities = sqlDataReader.GetDouble(17);
|
||||
if (!sqlDataReader.IsDBNull(18)) balanceSheet.CashAndCashEquivalents = sqlDataReader.GetDouble(18);
|
||||
if (!sqlDataReader.IsDBNull(19)) balanceSheet.Modified = sqlDataReader.GetDateTime(19);
|
||||
return balanceSheet;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool CheckBalanceSheetModifiedOn(String symbol, DateTime modified,BalanceSheet.PeriodType periodType)
|
||||
{
|
||||
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 count(*) from balancesheet");
|
||||
sb.Append(" where symbol='").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append(" and period=").Append(periodType.Equals(BalanceSheet.PeriodType.Annual) ? 0 : 1).Append("").Append(" ");
|
||||
sb.Append("and modified='").Append(Utility.DateTimeToStringYYYYHMMHDD(modified)).Append("'");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
sqlDataReader.Read();
|
||||
int recordCount = sqlDataReader.GetInt32(0);
|
||||
return 0 == recordCount ? false : true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static List<DateTime> GetBalanceSheetDates(String symbol,BalanceSheet.PeriodType periodType)
|
||||
{
|
||||
List<DateTime> balanceSheetDates = new List<DateTime>();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select asof from balancesheet where symbol='").Append(symbol).Append("' ");
|
||||
sb.Append("and period='").Append(periodType.Equals(BalanceSheet.PeriodType.Annual) ? 0 : 1).Append("'");
|
||||
sb.Append(" order by 1 asc;");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
DateTime asof=sqlDataReader.GetDateTime(0);
|
||||
balanceSheetDates.Add(asof);
|
||||
}
|
||||
return balanceSheetDates;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return balanceSheetDates;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DateTime? GetLatestBalanceSheetDate(String symbol,BalanceSheet.PeriodType periodType)
|
||||
{
|
||||
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(asof) from balancesheet");
|
||||
sb.Append(" where symbol='").Append(symbol).Append("' ");
|
||||
sb.Append(" and period=").Append(periodType.Equals(BalanceSheet.PeriodType.Annual) ? 0 : 1);
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (sqlDataReader.Read())
|
||||
{
|
||||
if (sqlDataReader.IsDBNull(0)) maxDate = null;
|
||||
else maxDate = sqlDataReader.GetDateTime(0);
|
||||
}
|
||||
return maxDate;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return maxDate;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static BalanceSheet GetBalanceSheet(String symbol,BalanceSheet.PeriodType periodType)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime? latestDate = GetLatestBalanceSheetDate(symbol,periodType);
|
||||
if (null == latestDate) return null;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,long_term_debt,other_liabilities,deferred_long_term_liabilities,minority_interest,negative_goodwill,total_stockholder_equity,inventory,property_plant_and_equipment,intangible_assets,accumulated_amortization,goodwill,period,total_assets,total_current_assets,total_liabilities,total_current_liabilities,cash_and_cash_equivalents,modified from balancesheet where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and period=").Append(periodType.Equals(BalanceSheet.PeriodType.Annual) ? 0 : 1).Append(" ");
|
||||
sb.Append("and asof=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(latestDate.Value)).Append("'").Append(";");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
BalanceSheet balanceSheet=new BalanceSheet();
|
||||
balanceSheet.Symbol=sqlDataReader.GetString(0);
|
||||
balanceSheet.AsOf=sqlDataReader.GetDateTime(1);
|
||||
if (!sqlDataReader.IsDBNull(2))balanceSheet.LongTermDebt= sqlDataReader.GetDouble(2);
|
||||
if (!sqlDataReader.IsDBNull(3))balanceSheet.OtherLiabilities=sqlDataReader.GetDouble(3);
|
||||
if (!sqlDataReader.IsDBNull(4))balanceSheet.DeferredLongTermLiabilities=sqlDataReader.GetDouble(4);
|
||||
if (!sqlDataReader.IsDBNull(5))balanceSheet.MinorityInterest=sqlDataReader.GetDouble(5);
|
||||
if (!sqlDataReader.IsDBNull(6))balanceSheet.NegativeGoodwill=sqlDataReader.GetDouble(6);
|
||||
if (!sqlDataReader.IsDBNull(7))balanceSheet.TotalStockHolderEquity = sqlDataReader.GetDouble(7);
|
||||
if (!sqlDataReader.IsDBNull(8)) balanceSheet.Inventory = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) balanceSheet.PropertyPlantAndEquipment = sqlDataReader.GetDouble(9);
|
||||
if (!sqlDataReader.IsDBNull(10)) balanceSheet.IntangibleAssets = sqlDataReader.GetDouble(10);
|
||||
if (!sqlDataReader.IsDBNull(11)) balanceSheet.AccumulatedAmortization = sqlDataReader.GetDouble(11);
|
||||
if (!sqlDataReader.IsDBNull(12)) balanceSheet.Goodwill = sqlDataReader.GetDouble(12);
|
||||
if (!sqlDataReader.IsDBNull(13)) balanceSheet.Period = sqlDataReader.GetInt32(13)==0?BalanceSheet.PeriodType.Annual:BalanceSheet.PeriodType.Quarterly;
|
||||
if (!sqlDataReader.IsDBNull(14)) balanceSheet.TotalAssets = sqlDataReader.GetDouble(14);
|
||||
if (!sqlDataReader.IsDBNull(15)) balanceSheet.TotalCurrentAssets = sqlDataReader.GetDouble(15);
|
||||
if (!sqlDataReader.IsDBNull(16)) balanceSheet.TotalLiabilities = sqlDataReader.GetDouble(16);
|
||||
if (!sqlDataReader.IsDBNull(17)) balanceSheet.TotalCurrentLiabilities = sqlDataReader.GetDouble(17);
|
||||
if (!sqlDataReader.IsDBNull(18)) balanceSheet.CashAndCashEquivalents = sqlDataReader.GetDouble(18);
|
||||
if (!sqlDataReader.IsDBNull(19)) balanceSheet.Modified = sqlDataReader.GetDateTime(19);
|
||||
return balanceSheet;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static BalanceSheet GetBalanceSheet(String symbol, DateTime asof,BalanceSheet.PeriodType periodType)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,long_term_debt,other_liabilities,deferred_long_term_liabilities,minority_interest,negative_goodwill,total_stockholder_equity,inventory,property_plant_and_equipment,intangible_assets,accumulated_amortization,goodwill,period,total_assets,total_current_assets,total_liabilities,total_current_liabilities,cash_and_cash_equivalents,modified from balancesheet where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and period=").Append(periodType.Equals(BalanceSheet.PeriodType.Annual) ? 0 : 1).Append(" ");
|
||||
sb.Append("and asof=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(asof)).Append("'").Append(";");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
BalanceSheet balanceSheet=new BalanceSheet();
|
||||
balanceSheet.Symbol=sqlDataReader.GetString(0);
|
||||
balanceSheet.AsOf=sqlDataReader.GetDateTime(1);
|
||||
if (!sqlDataReader.IsDBNull(2))balanceSheet.LongTermDebt= sqlDataReader.GetDouble(2);
|
||||
if (!sqlDataReader.IsDBNull(3))balanceSheet.OtherLiabilities=sqlDataReader.GetDouble(3);
|
||||
if (!sqlDataReader.IsDBNull(4))balanceSheet.DeferredLongTermLiabilities=sqlDataReader.GetDouble(4);
|
||||
if (!sqlDataReader.IsDBNull(5))balanceSheet.MinorityInterest=sqlDataReader.GetDouble(5);
|
||||
if (!sqlDataReader.IsDBNull(6))balanceSheet.NegativeGoodwill=sqlDataReader.GetDouble(6);
|
||||
if (!sqlDataReader.IsDBNull(7)) balanceSheet.TotalStockHolderEquity = sqlDataReader.GetDouble(7);
|
||||
if (!sqlDataReader.IsDBNull(8)) balanceSheet.Inventory = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) balanceSheet.PropertyPlantAndEquipment = sqlDataReader.GetDouble(9);
|
||||
if (!sqlDataReader.IsDBNull(10)) balanceSheet.IntangibleAssets = sqlDataReader.GetDouble(10);
|
||||
if (!sqlDataReader.IsDBNull(11)) balanceSheet.AccumulatedAmortization = sqlDataReader.GetDouble(11);
|
||||
if (!sqlDataReader.IsDBNull(12)) balanceSheet.Goodwill = sqlDataReader.GetDouble(12);
|
||||
if (!sqlDataReader.IsDBNull(13)) balanceSheet.Period = sqlDataReader.GetInt32(13) == 0 ? BalanceSheet.PeriodType.Annual : BalanceSheet.PeriodType.Quarterly;
|
||||
if (!sqlDataReader.IsDBNull(14)) balanceSheet.TotalAssets = sqlDataReader.GetDouble(14);
|
||||
if (!sqlDataReader.IsDBNull(15)) balanceSheet.TotalCurrentAssets = sqlDataReader.GetDouble(15);
|
||||
if (!sqlDataReader.IsDBNull(16)) balanceSheet.TotalLiabilities = sqlDataReader.GetDouble(16);
|
||||
if (!sqlDataReader.IsDBNull(17)) balanceSheet.TotalCurrentLiabilities = sqlDataReader.GetDouble(17);
|
||||
if (!sqlDataReader.IsDBNull(18)) balanceSheet.CashAndCashEquivalents = sqlDataReader.GetDouble(18);
|
||||
if (!sqlDataReader.IsDBNull(19)) balanceSheet.Modified = sqlDataReader.GetDateTime(19);
|
||||
return balanceSheet;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static TimeSeriesCollection GetInventory(String symbol,BalanceSheet.PeriodType period)
|
||||
{
|
||||
TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,inventory from balancesheet where symbol='").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append(" and period=").Append(period.Equals(BalanceSheet.PeriodType.Annual) ? 0 : 1).Append(" order by asof desc;");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
TimeSeriesElement timeSeriesElement = new TimeSeriesElement();
|
||||
timeSeriesElement.Symbol = sqlDataReader.GetString(0);
|
||||
timeSeriesElement.AsOf = sqlDataReader.GetDateTime(1);
|
||||
timeSeriesElement.Type = period.Equals(BalanceSheet.PeriodType.Quarterly) ? TimeSeriesElement.ElementType.QuarterlyInventory : TimeSeriesElement.ElementType.Inventory;
|
||||
if (!sqlDataReader.IsDBNull(2)) timeSeriesElement.Value = sqlDataReader.GetDouble(2);
|
||||
else continue;
|
||||
timeSeriesCollection.Add(timeSeriesElement);
|
||||
}
|
||||
return timeSeriesCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static TimeSeriesCollection GetGoodwill(String symbol, BalanceSheet.PeriodType period)
|
||||
{
|
||||
TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,goodwill from balancesheet where symbol='").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append(" and period=").Append(period.Equals(BalanceSheet.PeriodType.Annual) ? 0 : 1).Append(" order by asof desc;");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
TimeSeriesElement timeSeriesElement = new TimeSeriesElement();
|
||||
timeSeriesElement.Symbol = sqlDataReader.GetString(0);
|
||||
timeSeriesElement.AsOf = sqlDataReader.GetDateTime(1);
|
||||
timeSeriesElement.Type = period.Equals(BalanceSheet.PeriodType.Quarterly) ? TimeSeriesElement.ElementType.QuarterlyInventory : TimeSeriesElement.ElementType.Inventory;
|
||||
if (!sqlDataReader.IsDBNull(2)) timeSeriesElement.Value = sqlDataReader.GetDouble(2);
|
||||
else continue;
|
||||
timeSeriesCollection.Add(timeSeriesElement);
|
||||
}
|
||||
return timeSeriesCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static TimeSeriesCollection GetTotalStockholderEquity(String symbol, BalanceSheet.PeriodType period)
|
||||
{
|
||||
TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,total_stockholder_equity from balancesheet where symbol='").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append(" and period=").Append(period.Equals(BalanceSheet.PeriodType.Annual) ? 0 : 1).Append(" order by asof desc;");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
TimeSeriesElement timeSeriesElement = new TimeSeriesElement();
|
||||
timeSeriesElement.Symbol = sqlDataReader.GetString(0);
|
||||
timeSeriesElement.AsOf = sqlDataReader.GetDateTime(1);
|
||||
timeSeriesElement.Type = period.Equals(BalanceSheet.PeriodType.Quarterly) ? TimeSeriesElement.ElementType.QuarterlyInventory : TimeSeriesElement.ElementType.Inventory;
|
||||
if (!sqlDataReader.IsDBNull(2)) timeSeriesElement.Value = sqlDataReader.GetDouble(2);
|
||||
else continue;
|
||||
timeSeriesCollection.Add(timeSeriesElement);
|
||||
}
|
||||
return timeSeriesCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool InsertBalanceSheets(List<BalanceSheet> balanceSheets)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if(null==balanceSheets)return false;
|
||||
DateTime modified = DateTime.Now;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
DeleteBalanceSheets(balanceSheets, sqlConnection, sqlTransaction);
|
||||
for (int index = 0; index < balanceSheets.Count; index++)
|
||||
{
|
||||
BalanceSheet balanceSheet = balanceSheets[index];
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into balancesheet (symbol,asof,long_term_debt,other_liabilities,deferred_long_term_liabilities,minority_interest,negative_goodwill,total_stockholder_equity,inventory,property_plant_and_equipment,intangible_assets,accumulated_amortization,goodwill,period,total_assets,total_current_assets,total_liabilities,total_current_liabilities,cash_and_cash_equivalents,modified) ");
|
||||
sb.Append("values(");
|
||||
sb.Append("'").Append(balanceSheet.Symbol).Append("'").Append(",");
|
||||
sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(balanceSheet.AsOf)).Append("'").Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.LongTermDebt)) sb.Append(balanceSheet.LongTermDebt).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.OtherLiabilities)) sb.Append(balanceSheet.OtherLiabilities).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.DeferredLongTermLiabilities)) sb.Append(balanceSheet.DeferredLongTermLiabilities).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.MinorityInterest)) sb.Append(balanceSheet.MinorityInterest).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.NegativeGoodwill)) sb.Append(balanceSheet.NegativeGoodwill).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.TotalStockHolderEquity)) sb.Append(balanceSheet.TotalStockHolderEquity).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.Inventory)) sb.Append(balanceSheet.Inventory).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.PropertyPlantAndEquipment)) sb.Append(balanceSheet.PropertyPlantAndEquipment).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.IntangibleAssets)) sb.Append(balanceSheet.IntangibleAssets).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.AccumulatedAmortization)) sb.Append(balanceSheet.AccumulatedAmortization).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.Goodwill)) sb.Append(balanceSheet.Goodwill).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
sb.Append(BalanceSheet.PeriodType.Annual.Equals(balanceSheet.Period)?0:1).Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.TotalAssets)) sb.Append(balanceSheet.TotalAssets).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.TotalCurrentAssets)) sb.Append(balanceSheet.TotalCurrentAssets).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.TotalLiabilities)) sb.Append(balanceSheet.TotalLiabilities).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.TotalCurrentLiabilities)) sb.Append(balanceSheet.TotalCurrentLiabilities).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(balanceSheet.CashAndCashEquivalents)) sb.Append(balanceSheet.CashAndCashEquivalents).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(modified)).Append("'");
|
||||
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)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was '"+strQuery+"'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static bool DeleteBalanceSheets(List<BalanceSheet> balanceSheets, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
for (int index = 0; index < balanceSheets.Count; index++)
|
||||
{
|
||||
DeleteBalanceSheet(balanceSheets[index], sqlConnection, sqlTransaction);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private static bool DeleteBalanceSheet(BalanceSheet balanceSheet, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sb.Append("delete from balancesheet where ");
|
||||
sb.Append("symbol='").Append(balanceSheet.Symbol).Append("'");
|
||||
sb.Append(" and ");
|
||||
sb.Append("asof='").Append(Utility.DateTimeToStringYYYYHMMHDD(balanceSheet.AsOf)).Append("'");
|
||||
sb.Append(" and period=").Append(balanceSheet.Period.Equals(BalanceSheet.PeriodType.Annual)?0:1);
|
||||
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
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
124
MarketData/MarketDataLib/DataAccess/CashDA.cs
Executable file
124
MarketData/MarketDataLib/DataAccess/CashDA.cs
Executable file
@@ -0,0 +1,124 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class CashDA
|
||||
{
|
||||
private CashDA()
|
||||
{
|
||||
}
|
||||
public static bool AddTransaction(CashTransaction cashTransaction)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == cashTransaction) return false;
|
||||
double accountBalance=GetBalance(cashTransaction.Account);
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
cashTransaction.Balance=accountBalance+(cashTransaction.Credit-cashTransaction.Debit);
|
||||
sb.Append("insert into cash(account,date,description,credit,debit,balance) values(");
|
||||
sb.Append(SqlUtils.AddQuotes(cashTransaction.Account)).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(cashTransaction.TransactionDate))).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(cashTransaction.Description)).Append(",");
|
||||
sb.Append(cashTransaction.Credit).Append(",");
|
||||
sb.Append(cashTransaction.Debit).Append(",");
|
||||
sb.Append(accountBalance+cashTransaction.Credit-cashTransaction.Debit).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);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
}
|
||||
}
|
||||
// get balance for specific account
|
||||
public static double GetBalance(String account)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select balance from cash where account='").Append(account).Append("' and transaction_id=(select max(transaction_id) from cash where account='").Append(account).Append("')");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return double.NaN;
|
||||
double accountBalance = sqlDataReader.GetDouble(0);
|
||||
return accountBalance;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return double.NaN;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
// get latest balance for all accounts
|
||||
public static double GetBalance()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
// sb.Append("select sum(a.balance) from (select account,balance,date from cash b group by account desc) a");
|
||||
sb.Append("select sum(balance) from cash");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return double.NaN;
|
||||
double accountBalance = sqlDataReader.GetDouble(0);
|
||||
return accountBalance;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return double.NaN;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
377
MarketData/MarketDataLib/DataAccess/CashflowStatementDA.cs
Executable file
377
MarketData/MarketDataLib/DataAccess/CashflowStatementDA.cs
Executable file
@@ -0,0 +1,377 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class CashflowStatementDA
|
||||
{
|
||||
private CashflowStatementDA()
|
||||
{
|
||||
}
|
||||
// ********************************************* B A L A N C E S H E E T **************************************
|
||||
public static CashflowStatement GetCashflowStatementOnOrBefore(String symbol, DateTime asof,CashflowStatement.PeriodType periodType)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,depreciation_and_amortization,deferred_income_taxes,stock_based_compensation,accounts_receivable,inventory,accounts_payable,accrued_liabilities,operating_cashflow,free_cashflow,period,modified from cashflowstatement where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and period=").Append(periodType.Equals(CashflowStatement.PeriodType.Annual) ? 0 : 1).Append(" ");
|
||||
sb.Append("and asof<=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(asof)).Append("'");
|
||||
sb.Append(" order by asof desc");;
|
||||
sb.Append(" limit 1").Append(";");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
CashflowStatement cashflowStatement=new CashflowStatement();
|
||||
cashflowStatement.Symbol=sqlDataReader.GetString(0);
|
||||
cashflowStatement.AsOf=sqlDataReader.GetDateTime(1);
|
||||
if (!sqlDataReader.IsDBNull(2))cashflowStatement.DepreciationAndAmortization=sqlDataReader.GetDouble(2);
|
||||
if (!sqlDataReader.IsDBNull(3))cashflowStatement.DeferredIncomeTaxes=sqlDataReader.GetDouble(3);
|
||||
if (!sqlDataReader.IsDBNull(4))cashflowStatement.StockBasedCompensation=sqlDataReader.GetDouble(4);
|
||||
if (!sqlDataReader.IsDBNull(5))cashflowStatement.AccountsReceivable=sqlDataReader.GetDouble(5);
|
||||
if (!sqlDataReader.IsDBNull(6))cashflowStatement.Inventory=sqlDataReader.GetDouble(6);
|
||||
if (!sqlDataReader.IsDBNull(7))cashflowStatement.AccountsPayable=sqlDataReader.GetDouble(7);
|
||||
if (!sqlDataReader.IsDBNull(8))cashflowStatement.AccruedLiabilities=sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9))cashflowStatement.OperatingCashflow=sqlDataReader.GetDouble(9);
|
||||
if (!sqlDataReader.IsDBNull(10))cashflowStatement.FreeCashflow=sqlDataReader.GetDouble(10);
|
||||
if (!sqlDataReader.IsDBNull(11)) cashflowStatement.Period = sqlDataReader.GetInt32(11) == 0 ? CashflowStatement.PeriodType.Annual : CashflowStatement.PeriodType.Quarterly;
|
||||
if (!sqlDataReader.IsDBNull(12)) cashflowStatement.Modified = sqlDataReader.GetDateTime(12);
|
||||
return cashflowStatement;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool CheckCashflowStatementModifiedOn(String symbol, DateTime modified,CashflowStatement.PeriodType periodType)
|
||||
{
|
||||
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 count(*) from cashflowstatement");
|
||||
sb.Append(" where symbol='").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append(" and period=").Append(periodType.Equals(CashflowStatement.PeriodType.Annual) ? 0 : 1).Append("").Append(" ");
|
||||
sb.Append("and modified='").Append(Utility.DateTimeToStringYYYYHMMHDD(modified)).Append("'");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
sqlDataReader.Read();
|
||||
int recordCount = sqlDataReader.GetInt32(0);
|
||||
return 0 == recordCount ? false : true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static List<DateTime> GetCashflowStatementDates(String symbol,CashflowStatement.PeriodType periodType)
|
||||
{
|
||||
List<DateTime> dates = new List<DateTime>();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select asof from cashflowstatement where symbol='").Append(symbol).Append("' ");
|
||||
sb.Append("and period='").Append(periodType.Equals(CashflowStatement.PeriodType.Annual) ? 0 : 1).Append("'");
|
||||
sb.Append(" order by 1 asc;");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
DateTime asof=sqlDataReader.GetDateTime(0);
|
||||
dates.Add(asof);
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return dates;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DateTime? GetLatestCashflowStatementDate(String symbol,CashflowStatement.PeriodType periodType)
|
||||
{
|
||||
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(asof) from cashflowstatement");
|
||||
sb.Append(" where symbol='").Append(symbol).Append("' ");
|
||||
sb.Append(" and period=").Append(periodType.Equals(CashflowStatement.PeriodType.Annual) ? 0 : 1);
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (sqlDataReader.Read())
|
||||
{
|
||||
if (sqlDataReader.IsDBNull(0)) maxDate = null;
|
||||
else maxDate = sqlDataReader.GetDateTime(0);
|
||||
}
|
||||
return maxDate;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return maxDate;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static CashflowStatement GetCashflowStatement(String symbol,CashflowStatement.PeriodType periodType)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime? latestDate = GetLatestCashflowStatementDate(symbol,periodType);
|
||||
if (null == latestDate) return null;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,depreciation_and_amortization,deferred_income_taxes,stock_based_compensation,accounts_receivable,inventory,accounts_payable,accrued_liabilities,operating_cashflow,free_cashflow,period,modified from cashflowstatement where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and period=").Append(periodType.Equals(CashflowStatement.PeriodType.Annual) ? 0 : 1).Append(" ");
|
||||
sb.Append("and asof=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(latestDate.Value)).Append("'").Append(";");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
CashflowStatement cashflowStatement=new CashflowStatement();
|
||||
cashflowStatement.Symbol=sqlDataReader.GetString(0);
|
||||
cashflowStatement.AsOf=sqlDataReader.GetDateTime(1);
|
||||
if (!sqlDataReader.IsDBNull(2))cashflowStatement.DepreciationAndAmortization=sqlDataReader.GetDouble(2);
|
||||
if (!sqlDataReader.IsDBNull(3))cashflowStatement.DeferredIncomeTaxes=sqlDataReader.GetDouble(3);
|
||||
if (!sqlDataReader.IsDBNull(4))cashflowStatement.StockBasedCompensation=sqlDataReader.GetDouble(4);
|
||||
if (!sqlDataReader.IsDBNull(5))cashflowStatement.AccountsReceivable=sqlDataReader.GetDouble(5);
|
||||
if (!sqlDataReader.IsDBNull(6))cashflowStatement.Inventory=sqlDataReader.GetDouble(6);
|
||||
if (!sqlDataReader.IsDBNull(7))cashflowStatement.AccountsPayable=sqlDataReader.GetDouble(7);
|
||||
if (!sqlDataReader.IsDBNull(8))cashflowStatement.AccruedLiabilities=sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9))cashflowStatement.OperatingCashflow=sqlDataReader.GetDouble(9);
|
||||
if (!sqlDataReader.IsDBNull(10))cashflowStatement.FreeCashflow=sqlDataReader.GetDouble(10);
|
||||
if (!sqlDataReader.IsDBNull(11)) cashflowStatement.Period = sqlDataReader.GetInt32(11) == 0 ? CashflowStatement.PeriodType.Annual : CashflowStatement.PeriodType.Quarterly;
|
||||
if (!sqlDataReader.IsDBNull(12)) cashflowStatement.Modified = sqlDataReader.GetDateTime(12);
|
||||
return cashflowStatement;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static CashflowStatement GetCashflowStatement(String symbol, DateTime asof,CashflowStatement.PeriodType periodType)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,depreciation_and_amortization,deferred_income_taxes,stock_based_compensation,accounts_receivable,inventory,accounts_payable,accrued_liabilities,operating_cashflow,free_cashflow,period,modified from cashflowstatement where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and period=").Append(periodType.Equals(CashflowStatement.PeriodType.Annual) ? 0 : 1).Append(" ");
|
||||
sb.Append("and asof=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(asof)).Append("'").Append(";");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
CashflowStatement cashflowStatement=new CashflowStatement();
|
||||
cashflowStatement.Symbol=sqlDataReader.GetString(0);
|
||||
cashflowStatement.AsOf=sqlDataReader.GetDateTime(1);
|
||||
if (!sqlDataReader.IsDBNull(2))cashflowStatement.DepreciationAndAmortization=sqlDataReader.GetDouble(2);
|
||||
if (!sqlDataReader.IsDBNull(3))cashflowStatement.DeferredIncomeTaxes=sqlDataReader.GetDouble(3);
|
||||
if (!sqlDataReader.IsDBNull(4))cashflowStatement.StockBasedCompensation=sqlDataReader.GetDouble(4);
|
||||
if (!sqlDataReader.IsDBNull(5))cashflowStatement.AccountsReceivable=sqlDataReader.GetDouble(5);
|
||||
if (!sqlDataReader.IsDBNull(6))cashflowStatement.Inventory=sqlDataReader.GetDouble(6);
|
||||
if (!sqlDataReader.IsDBNull(7))cashflowStatement.AccountsPayable=sqlDataReader.GetDouble(7);
|
||||
if (!sqlDataReader.IsDBNull(8))cashflowStatement.AccruedLiabilities=sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9))cashflowStatement.OperatingCashflow=sqlDataReader.GetDouble(9);
|
||||
if (!sqlDataReader.IsDBNull(10))cashflowStatement.FreeCashflow=sqlDataReader.GetDouble(10);
|
||||
if (!sqlDataReader.IsDBNull(11)) cashflowStatement.Period = sqlDataReader.GetInt32(11) == 0 ? CashflowStatement.PeriodType.Annual : CashflowStatement.PeriodType.Quarterly;
|
||||
if (!sqlDataReader.IsDBNull(12)) cashflowStatement.Modified = sqlDataReader.GetDateTime(12);
|
||||
return cashflowStatement;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool InsertCashflowStatement(List<CashflowStatement> cashflowStatements)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
DateTime modified=DateTime.Now;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
DeleteCashflowStatements(cashflowStatements, sqlConnection, sqlTransaction);
|
||||
for (int index = 0; index < cashflowStatements.Count; index++)
|
||||
{
|
||||
CashflowStatement cashflowStatement = cashflowStatements[index];
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into cashflowstatement (symbol,asof,depreciation_and_amortization,deferred_income_taxes,stock_based_compensation,accounts_receivable,inventory,accounts_payable,accrued_liabilities,operating_cashflow,free_cashflow,period,modified) ");
|
||||
sb.Append("values(");
|
||||
sb.Append("'").Append(cashflowStatement.Symbol).Append("'").Append(",");
|
||||
sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(cashflowStatement.AsOf)).Append("'").Append(",");
|
||||
if (!Double.IsNaN(cashflowStatement.DepreciationAndAmortization)) sb.Append(cashflowStatement.DepreciationAndAmortization).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(cashflowStatement.DeferredIncomeTaxes)) sb.Append(cashflowStatement.DeferredIncomeTaxes).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(cashflowStatement.StockBasedCompensation)) sb.Append(cashflowStatement.StockBasedCompensation).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(cashflowStatement.AccountsReceivable)) sb.Append(cashflowStatement.AccountsReceivable).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(cashflowStatement.Inventory)) sb.Append(cashflowStatement.Inventory).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(cashflowStatement.AccountsPayable)) sb.Append(cashflowStatement.AccountsPayable).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(cashflowStatement.AccruedLiabilities)) sb.Append(cashflowStatement.AccruedLiabilities).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(cashflowStatement.OperatingCashflow)) sb.Append(cashflowStatement.OperatingCashflow).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(cashflowStatement.FreeCashflow)) sb.Append(cashflowStatement.FreeCashflow).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
sb.Append(CashflowStatement.PeriodType.Annual.Equals(cashflowStatement.Period)?0:1).Append(",");
|
||||
sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(modified)).Append("'");
|
||||
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)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was '"+strQuery+"'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static bool DeleteCashflowStatements(List<CashflowStatement> cashflowStatements, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
for (int index = 0; index < cashflowStatements.Count; index++)
|
||||
{
|
||||
DeleteCashflowStatement(cashflowStatements[index], sqlConnection, sqlTransaction);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private static bool DeleteCashflowStatement(CashflowStatement cashflowStatement, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sb.Append("delete from cashflowstatement where ");
|
||||
sb.Append("symbol='").Append(cashflowStatement.Symbol).Append("'");
|
||||
sb.Append(" and ");
|
||||
sb.Append("asof='").Append(Utility.DateTimeToStringYYYYHMMHDD(cashflowStatement.AsOf)).Append("'");
|
||||
sb.Append(" and period=").Append(cashflowStatement.Period.Equals(CashflowStatement.PeriodType.Annual)?0:1);
|
||||
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
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
247
MarketData/MarketDataLib/DataAccess/CompanyProfileDA.cs
Executable file
247
MarketData/MarketDataLib/DataAccess/CompanyProfileDA.cs
Executable file
@@ -0,0 +1,247 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class CompanyProfileDA
|
||||
{
|
||||
private CompanyProfileDA()
|
||||
{
|
||||
}
|
||||
// ***********************************************************************************************************
|
||||
// ************************************** C O M P A N Y P R O F I L E *************************************
|
||||
// ***********************************************************************************************************
|
||||
public static bool UpdateCompanyProfile(CompanyProfile companyProfile)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == companyProfile || null == companyProfile.Symbol) return false;
|
||||
if(!PricingDA.SecurityExists(companyProfile.Symbol))return false;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
|
||||
if(null!=companyProfile.Industry || null!=companyProfile.Sector)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("update securitymaster ");
|
||||
sb.Append("set ");
|
||||
if((null!=companyProfile.Sector && !"".Equals(companyProfile.Sector)) && (null != companyProfile.Industry && !"".Equals(companyProfile.Industry)))
|
||||
{
|
||||
sb.Append("Sector=").Append(SqlUtils.AddQuotes(companyProfile.Sector)).Append(", ");
|
||||
sb.Append("Industry=").Append(SqlUtils.AddQuotes(companyProfile.Industry)).Append(" ");
|
||||
}
|
||||
else if(null!=companyProfile.Industry && !"".Equals(companyProfile.Industry))
|
||||
{
|
||||
sb.Append("Industry=").Append(SqlUtils.AddQuotes(companyProfile.Industry)).Append(" ");
|
||||
}
|
||||
else if(null!=companyProfile.Sector&&!"".Equals(companyProfile.Industry))
|
||||
{
|
||||
sb.Append("Sector=").Append(SqlUtils.AddQuotes(companyProfile.Sector)).Append(" ");
|
||||
}
|
||||
sb.Append(" where symbol=").Append(SqlUtils.AddQuotes(companyProfile.Symbol));
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
}
|
||||
if(!CompanyProfileDetailsExists(companyProfile,sqlConnection,sqlTransaction))InsertCompanyProfileDescription(companyProfile,sqlConnection,sqlTransaction);
|
||||
else UpdateCompanyProfileDescription(companyProfile,sqlConnection,sqlTransaction);
|
||||
sqlTransaction.Commit();
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static CompanyProfiles GetCompanyProfiles()
|
||||
{
|
||||
CompanyProfiles companyProfiles=new CompanyProfiles();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select sm.symbol,sm.sector,sm.industry,sm.security_type,sm.company,cp.description,cp.pricing_source,cp.can_roll_previous,cp.freeze_pricing from securitymaster sm left outer join companyprofile cp on sm.symbol=cp.symbol");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
CompanyProfile companyProfile = new CompanyProfile();
|
||||
companyProfile.Symbol=sqlDataReader.GetString(0);
|
||||
if (!sqlDataReader.IsDBNull(1)) companyProfile.Sector = sqlDataReader.GetString(1);
|
||||
if (!sqlDataReader.IsDBNull(2)) companyProfile.Industry = sqlDataReader.GetString(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) companyProfile.SecurityType = sqlDataReader.GetString(3);
|
||||
if (!sqlDataReader.IsDBNull(4)) companyProfile.CompanyName = sqlDataReader.GetString(4);
|
||||
if (!sqlDataReader.IsDBNull(5)) companyProfile.Description = sqlDataReader.GetString(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) companyProfile.PricingSource = sqlDataReader.GetString(6).ToUpper();
|
||||
if (!sqlDataReader.IsDBNull(7)) companyProfile.CanRollPrevious = sqlDataReader.GetBoolean(7);
|
||||
if (!sqlDataReader.IsDBNull(8)) companyProfile.FreezePricing = sqlDataReader.GetBoolean(8);
|
||||
companyProfiles.Add(companyProfile);
|
||||
}
|
||||
return companyProfiles;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static CompanyProfile GetCompanyProfile(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select sm.sector,sm.industry,sm.security_type,sm.company,cp.description,cp.pricing_source,cp.can_roll_previous,cp.freeze_pricing from securitymaster sm left outer join companyprofile cp on sm.symbol=cp.symbol where sm.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;
|
||||
CompanyProfile companyProfile = new CompanyProfile();
|
||||
companyProfile.Symbol = symbol;
|
||||
if (!sqlDataReader.IsDBNull(0)) companyProfile.Sector = sqlDataReader.GetString(0);
|
||||
if (!sqlDataReader.IsDBNull(1)) companyProfile.Industry = sqlDataReader.GetString(1);
|
||||
if (!sqlDataReader.IsDBNull(2)) companyProfile.SecurityType = sqlDataReader.GetString(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) companyProfile.CompanyName = sqlDataReader.GetString(3);
|
||||
if (!sqlDataReader.IsDBNull(4)) companyProfile.Description = sqlDataReader.GetString(4);
|
||||
if (!sqlDataReader.IsDBNull(5)) companyProfile.PricingSource = sqlDataReader.GetString(5).ToUpper();
|
||||
if (!sqlDataReader.IsDBNull(6)) companyProfile.CanRollPrevious = sqlDataReader.GetBoolean(6);
|
||||
if (!sqlDataReader.IsDBNull(7)) companyProfile.FreezePricing = sqlDataReader.GetBoolean(7);
|
||||
return companyProfile;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
// This handles the actual company profile table where we store additional company related informtion
|
||||
private static bool CompanyProfileDetailsExists(CompanyProfile companyProfile,MySqlConnection sqlConnection,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == companyProfile || null == companyProfile.Symbol) return false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select count(*) from CompanyProfile where symbol=").Append(SqlUtils.AddQuotes(companyProfile.Symbol)).Append(" and description is not null and description <>''");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
if(!sqlDataReader.Read())return false;
|
||||
return 0==sqlDataReader.GetInt32(0)?false:true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader){sqlDataReader.Close();sqlDataReader.Dispose();}
|
||||
}
|
||||
}
|
||||
private static bool InsertCompanyProfileDescription(CompanyProfile companyProfile,MySqlConnection sqlConnection,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == companyProfile || null == companyProfile.Symbol || null==companyProfile.Description || "".Equals(companyProfile.Description.Trim())) return false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into CompanyProfile(symbol,description)values(");
|
||||
sb.Append(SqlUtils.AddQuotes(companyProfile.Symbol)).Append(",");
|
||||
sb.Append(SqlUtils.ToSqlString(companyProfile.Description.Trim()));
|
||||
sb.Append(")");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
}
|
||||
}
|
||||
private static bool UpdateCompanyProfileDescription(CompanyProfile companyProfile,MySqlConnection sqlConnection,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == companyProfile || null == companyProfile.Symbol || null==companyProfile.Description || "".Equals(companyProfile.Description)) return false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("update CompanyProfile set description=").Append(SqlUtils.ToSqlString(companyProfile.Description));
|
||||
sb.Append(" where symbol=").Append(SqlUtils.AddQuotes(companyProfile.Symbol));
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
357
MarketData/MarketDataLib/DataAccess/CompositeDA.cs
Executable file
357
MarketData/MarketDataLib/DataAccess/CompositeDA.cs
Executable file
@@ -0,0 +1,357 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class CompositeDA
|
||||
{
|
||||
private CompositeDA()
|
||||
{
|
||||
}
|
||||
public static FeedStatistics GetFeedStatistics(FeedStatistic.FeedStatisticType feedStatisticType)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
FeedStatistics feedStatistics = new FeedStatistics();
|
||||
String strQuery;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
switch (feedStatisticType)
|
||||
{
|
||||
case FeedStatistic.FeedStatisticType.ZACKS_RANK:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Zacks Rank' description,count(*) records,date from zacksrank group by 3 order by date desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.VALUATIONS:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Valuations' description, count(*), valuation_date from valuations group by 3 order by valuation_date desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.YIELD_CURVE:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Yield Curve' description,count(*), date from yieldcurve group by 3 order by date desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.SPLITS:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Splits' description,count(*), modified from splits group by 3 order by modified desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.SECURITY_MASTER:
|
||||
// sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Security Master' description,count(*), date(create_date) from securitymaster group by 3 order by create_date desc");
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Security Master' description,count(*), date(create_date) from securitymaster group by 3 order by 3 desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.SEC_FILINGS:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'SEC Filings' description,count(*), filing_date from secfilings group by 3 order by filing_date desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.PRICES:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Prices' description,count(*), date from prices group by 3 order by date desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.OPTIONS:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Options' description,count(*), modified from options where modified is not null group by 3 order by modified desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.INSIDER_TRANSACTIONS:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Insider Transactions' description,count(*), filing_date from insidertransaction group by 3 order by filing_date desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.FUNDAMENTALS:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Fundamentals' description,count(*), asof from fundamentals group by 3 order by asof desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.INCOME_STATEMENT:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Income Statement' description,count(*), modified from incomestatement group by 3 order by modified desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.CASHFLOW_STATEMENT:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Cashflow Statement' description,count(*), modified from cashflowstatement group by 3 order by modified desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.BALANCE_SHEET:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Balance Sheet' description,count(*), modified from balancesheet group by 3 order by modified desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.HISTORICAL:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Historical' description,count(*), asof from historical group by 3 order by asof desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.HEADLINES:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Headlines' description,count(*), asof from headlines group by 3 having count(*) > 10 order by asof desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.ETF_HOLDINGS:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'ETF Holdings' description,count(*), modified from etfholdings group by 3 order by modified desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.ECONOMIC_INDICATORS:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Economic Indicators' description,count(*), Date(modified) from economicindicators group by 3 order by Date(modified) desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.EARNINGS_ANNOUNCEMENTS:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Earnings Announcements' description,count(*), period_ending from earningsannouncements group by 3 order by period_ending desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.DIVIDEND_HISTORY:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Dividend History' description,count(*), CAST(modified AS DATE) modified from dividendhistory group by 3 order by CAST(modified AS DATE) desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.CURRENCY_CONVERSION:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Currency Conversion' description,count(*), asof from currencyconversion group by 3 order by asof desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.ANALYST_RATINGS:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Analyst Ratings' description,count(*), date from analystratings group by 3 order by date desc");
|
||||
break;
|
||||
case FeedStatistic.FeedStatisticType.ANALYST_PRICE_TARGET:
|
||||
sb.Append("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;select 'Analyst Price Target' description,count(*), date from analystpricetarget group by 3 order by date desc");
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
FeedStatistic feedStatistic = new FeedStatistic();
|
||||
feedStatistic.Feed = sqlDataReader.GetString(0);
|
||||
feedStatistic.Records = sqlDataReader.GetInt32(1);
|
||||
feedStatistic.Date = sqlDataReader.GetDateTime(2);
|
||||
feedStatistics.Add(feedStatistic);
|
||||
}
|
||||
return feedStatistics;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception.ToString());
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) { sqlCommand.Dispose(); }
|
||||
if (null != sqlDataReader) { sqlDataReader.Close(); sqlDataReader.Dispose(); }
|
||||
if (null != sqlConnection) { sqlConnection.Close(); sqlConnection.Dispose(); }
|
||||
}
|
||||
}
|
||||
public static TimeSeriesCollection GetDebtToEquity(String symbol)
|
||||
{
|
||||
TimeSeriesCollection timeSeriesCollection=new TimeSeriesCollection();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
double prevStockHoldersEquity=double.NaN;
|
||||
double prevTotalDebt=double.NaN;
|
||||
int recordCount=0;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("set @symbol='").Append(symbol).Append("';");
|
||||
sb.Append("select A.symbol,A.year,A.total_debt,B.total_stockholder_equity,A.total_debt/B.total_stockholder_equity debt_to_equity from ");
|
||||
sb.Append("( ");
|
||||
sb.Append("select MIN(f.symbol) symbol,MIN(YEAR(f.asof))year,AVG(f.total_debt)total_debt from fundamentals f where symbol=").Append(SqlUtils.ToSqlString(symbol));
|
||||
sb.Append(" group by YEAR(f.asof) order by 2 asc ");
|
||||
sb.Append(")A ");
|
||||
sb.Append("left join ");
|
||||
sb.Append("( ");
|
||||
sb.Append("select MIN(bs.symbol)symbol,MIN(YEAR(bs.asof)) year,AVG(bs.total_stockholder_equity) total_stockholder_equity from balancesheet bs where symbol=").Append(SqlUtils.ToSqlString(symbol)).Append(" and period=0 ");
|
||||
sb.Append("group by YEAR(bs.asof) order by 2 asc ");
|
||||
sb.Append(")B ");
|
||||
sb.Append("on A.symbol=B.symbol and A.year=B.year order by 2 asc");
|
||||
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
double totalDebt=double.NaN;
|
||||
double totalStockholderEquity=double.NaN;
|
||||
double debtToEquity=double.NaN;
|
||||
symbol=sqlDataReader.GetString(0);
|
||||
int year=sqlDataReader.GetInt32(1);
|
||||
if(!sqlDataReader.IsDBNull(2))totalDebt=sqlDataReader.GetDouble(2);
|
||||
if(!sqlDataReader.IsDBNull(3))totalStockholderEquity=sqlDataReader.GetDouble(3);
|
||||
if(!sqlDataReader.IsDBNull(4))debtToEquity=sqlDataReader.GetDouble(4);
|
||||
|
||||
if(0==recordCount&&(double.IsNaN(totalDebt)||double.IsNaN(totalStockholderEquity)))continue;
|
||||
if(double.IsNaN(totalStockholderEquity))totalStockholderEquity=prevStockHoldersEquity;
|
||||
if(double.IsNaN(totalDebt))totalDebt=prevTotalDebt;
|
||||
|
||||
TimeSeriesElement timeSeriesElement=new TimeSeriesElement();
|
||||
timeSeriesElement.Symbol=symbol;
|
||||
timeSeriesElement.AsOf=new DateTime(year,1,1);
|
||||
timeSeriesElement.Type=TimeSeriesElement.ElementType.OTHER;
|
||||
timeSeriesElement.OtherType="DebtToEquity";
|
||||
timeSeriesElement.Value=totalDebt/totalStockholderEquity;
|
||||
timeSeriesCollection.Add(timeSeriesElement);
|
||||
recordCount++;
|
||||
prevStockHoldersEquity=totalStockholderEquity;
|
||||
prevTotalDebt=totalDebt;
|
||||
}
|
||||
return timeSeriesCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DividendLoadCollection GetDividendLoad(String symbol)
|
||||
{
|
||||
DividendLoadCollection dividendLoadCollection=new DividendLoadCollection();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("SET @symbol='").Append(symbol).Append("';");
|
||||
sb.Append("select A.symbol,A.year,A.eps,B.cash_amount,B.records,(B.cash_amount/A.eps)*100.00 as dividend_load_pct from ");
|
||||
sb.Append(" (select MIN(h.symbol) symbol,MIN(YEAR(h.asof)) year,min(h.value) eps from historical h ");
|
||||
sb.Append(" where h.symbol=@symbol and h.type='eps' ");
|
||||
sb.Append(" group by YEAR(h.asof))A ");
|
||||
sb.Append(" inner join ( ");
|
||||
sb.Append(" select MIN(symbol) symbol,sum(dh.cash_amount) cash_amount,count(*) records,YEAR(dh.payment_date) year from dividendhistory dh ");
|
||||
sb.Append(" where dh.symbol=@symbol group by 4 ");
|
||||
sb.Append(" )B ");
|
||||
sb.Append(" on A.symbol=B.symbol and A.year=B.year ");
|
||||
sb.Append(" order by 2 desc");
|
||||
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
DividendLoadElement dividendLoadElement = new DividendLoadElement();
|
||||
if(!sqlDataReader.IsDBNull(0))dividendLoadElement.Symbol=sqlDataReader.GetString(0);
|
||||
if(!sqlDataReader.IsDBNull(1))dividendLoadElement.Year=sqlDataReader.GetInt32(1);
|
||||
if(!sqlDataReader.IsDBNull(2))dividendLoadElement.EPS=sqlDataReader.GetDouble(2);
|
||||
if(!sqlDataReader.IsDBNull(3))dividendLoadElement.CashAmount=sqlDataReader.GetDouble(3);
|
||||
if(!sqlDataReader.IsDBNull(4))dividendLoadElement.Payments=sqlDataReader.GetInt32(4);
|
||||
if(!sqlDataReader.IsDBNull(5))dividendLoadElement.DividendLoadPcnt=sqlDataReader.GetDouble(5);
|
||||
dividendLoadCollection.Add(dividendLoadElement);
|
||||
}
|
||||
return dividendLoadCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static CompanyProfiles GetCompanyProfiles(String watchListName)
|
||||
{
|
||||
CompanyProfiles companyProfiles=new CompanyProfiles();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select wl.symbol,sm.sector,sm.industry,sm.security_type,sm.company,cp.description ");
|
||||
sb.Append("from portfolio_data.watchlist wl ");
|
||||
sb.Append("join portfolio_data.watchlists wls on wls.watch_list_id=wl.watch_list_id ");
|
||||
sb.Append("join market_data.securitymaster sm on sm.symbol=wl.symbol ");
|
||||
sb.Append("left outer join market_data.companyprofile cp on cp.symbol=wl.symbol ");
|
||||
sb.Append("where wls.watch_list_name='").Append(watchListName).Append("'");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
CompanyProfile companyProfile = new CompanyProfile();
|
||||
companyProfile.Symbol=sqlDataReader.GetString(0);
|
||||
if (!sqlDataReader.IsDBNull(1)) companyProfile.Sector = sqlDataReader.GetString(1);
|
||||
if (!sqlDataReader.IsDBNull(2)) companyProfile.Industry = sqlDataReader.GetString(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) companyProfile.SecurityType = sqlDataReader.GetString(3);
|
||||
if (!sqlDataReader.IsDBNull(4)) companyProfile.CompanyName = sqlDataReader.GetString(4).ToUpper();
|
||||
if (!sqlDataReader.IsDBNull(5)) companyProfile.Description = sqlDataReader.GetString(5);
|
||||
companyProfiles.Add(companyProfile);
|
||||
}
|
||||
return companyProfiles;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static EarningsAnnouncementModel GetEarningsAnnouncement(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
EarningsAnnouncementModel earningsAnnouncementModel=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select f.symbol,f.asof as last_updated,p.date as pricing_date,p.close,p.open,f.pe,f.peg,(f.pe/f.peg)/100.00 as anticipated_earnings_growth,");
|
||||
sb.Append("case when f.peg<1 then 'UNDERVALUED' when f.peg>1 then 'OVERVALUED' else 'EQUILIBRIUM' end as peg_valuation,f.next_earnings_date,");
|
||||
sb.Append("datediff(f.next_earnings_date,curdate())as daysfromtoday,s.company,a.high_target as upside,(a.high_target-p.close)/p.close as upside_pcnt,");
|
||||
sb.Append("a.low_target as downside,(a.low_target-p.close)/p.close as downside_pcnt,a.date as price_target_asof ");
|
||||
sb.Append(" from fundamentals f ");
|
||||
sb.Append(" left outer join securitymaster s on s.symbol=f.symbol ");
|
||||
sb.Append(" left outer join analystpricetarget a on a.symbol=s.symbol");
|
||||
sb.Append(" left outer join prices p on a.symbol=p.symbol ");
|
||||
sb.Append(" where f.symbol='").Append(symbol).Append("' and f.asof=(select max(asof) from fundamentals where symbol='").Append(symbol).Append("')");
|
||||
sb.Append(" and a.symbol='").Append(symbol).Append("' and a.date=(select max(date) from analystpricetarget where symbol='").Append(symbol).Append("')");
|
||||
sb.Append(" and p.symbol='").Append(symbol).Append("' and p.date=(select max(date) from prices where symbol='").Append(symbol).Append("')");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
earningsAnnouncementModel=new EarningsAnnouncementModel();
|
||||
if(!sqlDataReader.IsDBNull(0))earningsAnnouncementModel.Symbol = sqlDataReader.GetString(0).ToUpper();
|
||||
if(!sqlDataReader.IsDBNull(1))earningsAnnouncementModel.FundamentalDate = sqlDataReader.GetDateTime(1);
|
||||
if(!sqlDataReader.IsDBNull(2))earningsAnnouncementModel.PricingDate = sqlDataReader.GetDateTime(2);
|
||||
if(!sqlDataReader.IsDBNull(3))earningsAnnouncementModel.LastPrice = sqlDataReader.GetDouble(3);
|
||||
if(!sqlDataReader.IsDBNull(5))earningsAnnouncementModel.PE = sqlDataReader.GetDouble(5);
|
||||
if(!sqlDataReader.IsDBNull(6))earningsAnnouncementModel.PEG = sqlDataReader.GetDouble(6);
|
||||
if(!sqlDataReader.IsDBNull(7))earningsAnnouncementModel.AnticipatedEarningsGrowth = sqlDataReader.GetDouble(7);
|
||||
if(!sqlDataReader.IsDBNull(8))earningsAnnouncementModel.PEGValuation = sqlDataReader.GetString(8);
|
||||
if(!sqlDataReader.IsDBNull(9))earningsAnnouncementModel.NextEarningsDate = sqlDataReader.GetDateTime(9);
|
||||
if(!sqlDataReader.IsDBNull(10))earningsAnnouncementModel.DaysFromToday = sqlDataReader.GetInt32(10);
|
||||
if(!sqlDataReader.IsDBNull(11))earningsAnnouncementModel.CompanyName = sqlDataReader.GetString(11).ToUpper();
|
||||
if(!sqlDataReader.IsDBNull(12))earningsAnnouncementModel.Upside = sqlDataReader.GetDouble(12);
|
||||
if(!sqlDataReader.IsDBNull(13))earningsAnnouncementModel.UpsidePcnt = sqlDataReader.GetDouble(13);
|
||||
if(!sqlDataReader.IsDBNull(14))earningsAnnouncementModel.Downside = sqlDataReader.GetDouble(14);
|
||||
if(!sqlDataReader.IsDBNull(15))earningsAnnouncementModel.DownsidePcnt = sqlDataReader.GetDouble(15);
|
||||
if(!sqlDataReader.IsDBNull(16))earningsAnnouncementModel.PriceTargetDate = sqlDataReader.GetDateTime(16);
|
||||
if(Utility.IsEpoch(earningsAnnouncementModel.NextEarningsDate))return null;
|
||||
return earningsAnnouncementModel;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
276
MarketData/MarketDataLib/DataAccess/ConsumerPriceIndexDA.cs
Executable file
276
MarketData/MarketDataLib/DataAccess/ConsumerPriceIndexDA.cs
Executable file
@@ -0,0 +1,276 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class ConsumerPriceIndexDA
|
||||
{
|
||||
private ConsumerPriceIndexDA()
|
||||
{
|
||||
}
|
||||
|
||||
public static List<String> GetDistinctIndices()
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
List<String> indices = new List<String>();
|
||||
try
|
||||
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlCommand=sqlConnection.CreateCommand();
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.Append("select distinct(index_code) from PriceIndex ");
|
||||
sb.Append("ORDER BY 1 desc");
|
||||
sqlCommand.CommandText=sb.ToString();
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
indices.Add(sqlDataReader.GetString(0).ToUpper());
|
||||
}
|
||||
sqlDataReader.Close();
|
||||
sqlDataReader.Dispose();
|
||||
sqlCommand.Dispose();
|
||||
sqlConnection.Close();
|
||||
sqlConnection.Dispose();
|
||||
return indices;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString()));
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlDataReader)
|
||||
{
|
||||
sqlDataReader.Close();
|
||||
sqlDataReader.Dispose();
|
||||
}
|
||||
if(null!=sqlCommand)
|
||||
{
|
||||
sqlCommand.Dispose();
|
||||
}
|
||||
if(null!=sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static PriceIndices GetConsumerPriceIndex(String indexCode)
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
PriceIndices priceIndices=new PriceIndices();
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlCommand=sqlConnection.CreateCommand();
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.Append("select index_code, index_name, index_value, as_of, source from PriceIndex ");
|
||||
sb.Append("where index_code='").Append(indexCode).Append("'");
|
||||
sb.Append("ORDER BY as_of desc");
|
||||
sqlCommand.CommandText=sb.ToString();
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
PriceIndex priceIndex=new PriceIndex();
|
||||
if(!sqlDataReader.IsDBNull(0))priceIndex.Code=sqlDataReader.GetString(0);
|
||||
if(!sqlDataReader.IsDBNull(1))priceIndex.Name=sqlDataReader.GetString(1);
|
||||
if(!sqlDataReader.IsDBNull(2))priceIndex.Value=sqlDataReader.GetDouble(2);
|
||||
if(!sqlDataReader.IsDBNull(3))priceIndex.AsOf=sqlDataReader.GetDateTime(3);
|
||||
if(!sqlDataReader.IsDBNull(4))priceIndex.Source=sqlDataReader.GetString(4);
|
||||
priceIndices.Add(priceIndex);
|
||||
}
|
||||
sqlDataReader.Close();
|
||||
sqlDataReader.Dispose();
|
||||
sqlCommand.Dispose();
|
||||
sqlConnection.Close();
|
||||
sqlConnection.Dispose();
|
||||
return priceIndices;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString()));
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlDataReader)
|
||||
{
|
||||
sqlDataReader.Close();
|
||||
sqlDataReader.Dispose();
|
||||
}
|
||||
if(null!=sqlCommand)
|
||||
{
|
||||
sqlCommand.Dispose();
|
||||
}
|
||||
if(null!=sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
public static PriceIndices GetConsumerPriceIndices()
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
PriceIndices priceIndices=new PriceIndices();
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlCommand=sqlConnection.CreateCommand();
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.Append("select index_code, index_name, index_value, as_of, source from PriceIndex ORDER BY as_of desc");
|
||||
sqlCommand.CommandText=sb.ToString();
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
PriceIndex priceIndex=new PriceIndex();
|
||||
if(!sqlDataReader.IsDBNull(0))priceIndex.Code=sqlDataReader.GetString(0);
|
||||
if(!sqlDataReader.IsDBNull(1))priceIndex.Name=sqlDataReader.GetString(1);
|
||||
if(!sqlDataReader.IsDBNull(2))priceIndex.Value=sqlDataReader.GetDouble(2);
|
||||
if(!sqlDataReader.IsDBNull(3))priceIndex.AsOf=sqlDataReader.GetDateTime(3);
|
||||
if(!sqlDataReader.IsDBNull(4))priceIndex.Source=sqlDataReader.GetString(4);
|
||||
priceIndices.Add(priceIndex);
|
||||
}
|
||||
sqlDataReader.Close();
|
||||
sqlDataReader.Dispose();
|
||||
sqlCommand.Dispose();
|
||||
sqlConnection.Close();
|
||||
sqlConnection.Dispose();
|
||||
return priceIndices;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString()));
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlDataReader)
|
||||
{
|
||||
sqlDataReader.Close();
|
||||
sqlDataReader.Dispose();
|
||||
}
|
||||
if(null!=sqlCommand)
|
||||
{
|
||||
sqlCommand.Dispose();
|
||||
}
|
||||
if(null!=sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
public static bool InsertUpdatePriceIndices(PriceIndices priceIndices)
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlTransaction sqlTransaction=null;
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);
|
||||
sqlCommand=sqlConnection.CreateCommand();
|
||||
sqlCommand.Transaction=sqlTransaction;
|
||||
foreach(PriceIndex priceIndex in priceIndices)
|
||||
{
|
||||
DeletePriceIndex(priceIndex.Code,priceIndex.AsOf,sqlCommand,sqlTransaction);
|
||||
}
|
||||
InsertPriceIndices(priceIndices,sqlCommand,sqlTransaction);
|
||||
sqlTransaction.Commit();
|
||||
sqlTransaction.Dispose();
|
||||
sqlCommand.Dispose();
|
||||
sqlConnection.Close();
|
||||
sqlConnection.Dispose();
|
||||
sqlConnection=null;
|
||||
sqlCommand=null;
|
||||
sqlTransaction=null;
|
||||
return true;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString()));
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection.Dispose();
|
||||
}
|
||||
if(null!=sqlCommand)
|
||||
{
|
||||
sqlCommand.Dispose();
|
||||
}
|
||||
if(null!=sqlTransaction)
|
||||
{
|
||||
sqlTransaction.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
private static bool DeletePriceIndex(String indexCode,DateTime asOf,MySqlCommand sqlCommand,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
String strQuery=null;
|
||||
try
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.Append("delete from PriceIndex ").Append(" where ");
|
||||
sb.Append("index_code=").Append("'").Append(indexCode).Append("'").Append(" and ");
|
||||
sb.Append("as_of= ").Append(SqlUtils.SqlDate(asOf,true));
|
||||
strQuery=sb.ToString();
|
||||
sqlCommand.CommandText=strQuery;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
return true;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}, query was {1}",exception.ToString(),strQuery));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
private static bool InsertPriceIndices(PriceIndices priceIndices,MySqlCommand sqlCommand,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
String strQuery=null;
|
||||
DateTime modified=DateTime.Now;
|
||||
try
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
foreach(PriceIndex priceIndex in priceIndices)
|
||||
{
|
||||
sb.Append("insert into PriceIndex(index_code,index_name,index_value,as_of,source,modified)values(");
|
||||
sb.Append(SqlUtils.ToSqlString(priceIndex.Code)).Append(",");
|
||||
sb.Append(SqlUtils.ToSqlString(priceIndex.Name)).Append(",");
|
||||
sb.Append(priceIndex.Value).Append(",");
|
||||
sb.Append(SqlUtils.SqlDate(priceIndex.AsOf,true)).Append(",");
|
||||
sb.Append(SqlUtils.ToSqlString(priceIndex.Source)).Append(",");
|
||||
sb.Append(SqlUtils.SqlDate(modified,true));
|
||||
sb.Append(");");
|
||||
}
|
||||
strQuery=sb.ToString();
|
||||
sqlCommand.CommandText=sb.ToString();
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
return true;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString()));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
174
MarketData/MarketDataLib/DataAccess/CurrencyConversionDA.cs
Executable file
174
MarketData/MarketDataLib/DataAccess/CurrencyConversionDA.cs
Executable file
@@ -0,0 +1,174 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class CurrencyConversionDA
|
||||
{
|
||||
private CurrencyConversionDA()
|
||||
{
|
||||
}
|
||||
// get the maximum date on record on or before asof. If maxDate turns out to be less than asof then return null
|
||||
public static DateTime? GetMaxDateForCurrency(String sourceCurrency,String destinationCurrency,DateTime asof)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
DateTime? maxDate=null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select asof from CurrencyConversion ");
|
||||
sb.Append(" where ");
|
||||
sb.Append("source_currency=").Append("'").Append(sourceCurrency).Append("'").Append(" ");
|
||||
sb.Append("and ").Append(" destination_currency=").Append("'").Append(destinationCurrency).Append("'").Append(" ");
|
||||
sb.Append("and asof<=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(asof)).Append("'");
|
||||
sb.Append(" order by asof desc ");
|
||||
sb.Append(" limit 1");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return maxDate;
|
||||
if(!sqlDataReader.IsDBNull(0))maxDate=sqlDataReader.GetDateTime(0);
|
||||
if(maxDate<asof)return null;
|
||||
return maxDate;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
// Assumes like source_currency and like asof
|
||||
public static bool InsertCurrencyConversionRates(CurrencyConversionCollection currencyConversionCollection)
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlTransaction sqlTransaction=null;
|
||||
try
|
||||
{
|
||||
if(null==currencyConversionCollection||0==currencyConversionCollection.Count)return false;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
CurrencyConversionElement currencyConversionElementPrimary=currencyConversionCollection[0];
|
||||
RemoveCurrecyConversionForSourceAndDate(currencyConversionElementPrimary.SourceCurrency,currencyConversionElementPrimary.AsOf,sqlConnection,sqlTransaction);
|
||||
foreach(CurrencyConversionElement currencyConversionElement in currencyConversionCollection)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into CurrencyConversion(asof,source_currency,destination_currency,destination_currency_name,units_per_source,source_per_unit) ");
|
||||
sb.Append("values(");
|
||||
sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(currencyConversionElement.AsOf)).Append("'").Append(",");
|
||||
sb.Append("'").Append(currencyConversionElement.SourceCurrency).Append("'").Append(",");
|
||||
sb.Append("'").Append(currencyConversionElement.DestinationCurrency).Append("'").Append(",");
|
||||
sb.Append("'").Append(currencyConversionElement.DestinationCurrencyName).Append("'").Append(",");
|
||||
sb.Append(currencyConversionElement.UnitsPerSource).Append(",");
|
||||
sb.Append(currencyConversionElement.SourcePerUnit);
|
||||
sb.Append(")");
|
||||
String 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)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool RemoveCurrecyConversionForSourceAndDate(String sourceCurrency,DateTime asof,MySqlConnection sqlConnection,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sb.Append("delete from currencyconversion where ");
|
||||
sb.Append("source_currency='").Append(sourceCurrency).Append("'");
|
||||
sb.Append(" and ");
|
||||
sb.Append("asof='").Append(Utility.DateTimeToStringYYYYHMMHDD(asof)).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
|
||||
{
|
||||
}
|
||||
}
|
||||
public static CurrencyConversionElement GetCurrencyConversionMaxDate(String sourceCurrency,String destinationCurrency, DateTime asof)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
DateTime? maxDate=GetMaxDateForCurrency(sourceCurrency,destinationCurrency,asof); // get the maximum date on record on or before max date
|
||||
if(null==maxDate)return null;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select asof,source_currency,destination_currency,destination_currency_name,units_per_source,source_per_unit from CurrencyConversion where source_currency=");
|
||||
sb.Append("'").Append(sourceCurrency).Append("'").Append(" ");
|
||||
sb.Append(" and destination_currency=").Append("'").Append(destinationCurrency).Append("'").Append(" ");
|
||||
sb.Append("and asof=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(maxDate.Value)).Append("'");
|
||||
sb.Append(" limit 1");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
CurrencyConversionElement currencyConversionElement = new CurrencyConversionElement();
|
||||
currencyConversionElement.AsOf = sqlDataReader.GetDateTime(0);
|
||||
currencyConversionElement.SourceCurrency = sqlDataReader.GetString(1);
|
||||
currencyConversionElement.DestinationCurrency = sqlDataReader.GetString(2);
|
||||
if (!sqlDataReader.IsDBNull(2)) currencyConversionElement.DestinationCurrencyName = sqlDataReader.GetString(3);
|
||||
currencyConversionElement.UnitsPerSource = sqlDataReader.GetDouble(4);
|
||||
currencyConversionElement.SourcePerUnit = sqlDataReader.GetDouble(5);
|
||||
return currencyConversionElement;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
45
MarketData/MarketDataLib/DataAccess/DataSource.cs
Executable file
45
MarketData/MarketDataLib/DataAccess/DataSource.cs
Executable file
@@ -0,0 +1,45 @@
|
||||
using MarketData.Configuration;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class MainDataSource
|
||||
{
|
||||
private Dictionary<String,DataSourceEx> dataSourceDictionary = new Dictionary<String,DataSourceEx>();
|
||||
private static MainDataSource mainDataSource;
|
||||
|
||||
private MainDataSource()
|
||||
{
|
||||
}
|
||||
|
||||
public static MainDataSource Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (typeof(MainDataSource))
|
||||
{
|
||||
if (null == mainDataSource)
|
||||
{
|
||||
mainDataSource = new MainDataSource();
|
||||
|
||||
IConfiguration configuration = GlobalConfig.Instance.Configuration;
|
||||
DataSourceEx marketDataSource = new DataSourceEx(configuration, "market_data");
|
||||
mainDataSource.dataSourceDictionary.Add("market_data",marketDataSource);
|
||||
|
||||
DataSourceEx portfolioDataSource = new DataSourceEx(configuration, "portfolio_data");
|
||||
mainDataSource.dataSourceDictionary.Add("portfolio_data",portfolioDataSource);
|
||||
|
||||
DataSourceEx userDataSource = new DataSourceEx(configuration, "user_data");
|
||||
mainDataSource.dataSourceDictionary.Add("user_data",userDataSource);
|
||||
}
|
||||
return mainDataSource;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DataSourceEx LocateDataSource(String sourceKey)
|
||||
{
|
||||
return mainDataSource.dataSourceDictionary[sourceKey];
|
||||
}
|
||||
}
|
||||
}
|
||||
91
MarketData/MarketDataLib/DataAccess/DataSourceEx.cs
Executable file
91
MarketData/MarketDataLib/DataAccess/DataSourceEx.cs
Executable file
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Configuration;
|
||||
using System.Collections.Generic;
|
||||
using MarketData.Utils;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
[Serializable]
|
||||
public sealed class DataSourceEx
|
||||
{
|
||||
private string key;
|
||||
private string database;
|
||||
private string datasource;
|
||||
private string username;
|
||||
private string password;
|
||||
private string port;
|
||||
|
||||
public DataSourceEx(IConfiguration configuration,String configurationKey)
|
||||
{
|
||||
Dictionary<String, String> dictionary = CreateConfigurationSettings(configuration, configurationKey);
|
||||
Database = dictionary["Database"];
|
||||
Datasource = dictionary["Datasource"];
|
||||
Username = dictionary["Username"];
|
||||
Password = dictionary["Password"];
|
||||
}
|
||||
|
||||
private Dictionary<String, String> CreateConfigurationSettings(IConfiguration configuration, String configurationKey)
|
||||
{
|
||||
String marketData = configuration[configurationKey];
|
||||
|
||||
String[] elements = marketData.Split(';');
|
||||
Dictionary<String, String> dictionary = new Dictionary<string, string>();
|
||||
foreach (String element in elements)
|
||||
{
|
||||
String[] valuePairs = element.Split('=');
|
||||
dictionary.Add(valuePairs[0], valuePairs[1]);
|
||||
}
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
public string Key
|
||||
{
|
||||
get { return key; }
|
||||
set { key = value; }
|
||||
}
|
||||
|
||||
public string Database
|
||||
{
|
||||
get { return database; }
|
||||
set { database = value; }
|
||||
}
|
||||
|
||||
public string Datasource
|
||||
{
|
||||
get { return datasource; }
|
||||
set { datasource = value; }
|
||||
}
|
||||
|
||||
public string Port
|
||||
{
|
||||
get { return port; }
|
||||
set { port = value; }
|
||||
}
|
||||
|
||||
public string Password
|
||||
{
|
||||
get { return password; }
|
||||
set { password = value; }
|
||||
}
|
||||
|
||||
public string Username
|
||||
{
|
||||
get { return username; }
|
||||
set { username = value; }
|
||||
}
|
||||
|
||||
public override String ToString()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("Key='").Append(key).Append("', ");
|
||||
sb.Append("Database='").Append(database).Append("', ");
|
||||
sb.Append("DataSource='").Append(datasource).Append("', ");
|
||||
sb.Append("Port='").Append(port).Append("', ");
|
||||
sb.Append("UserName='").Append(username).Append("', ");
|
||||
sb.Append("Password='").Append(Utility.AsteriskForString(password)).Append("'");
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
342
MarketData/MarketDataLib/DataAccess/DictionaryDA.cs
Executable file
342
MarketData/MarketDataLib/DataAccess/DictionaryDA.cs
Executable file
@@ -0,0 +1,342 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class DictionaryDA
|
||||
{
|
||||
private DictionaryDA()
|
||||
{
|
||||
}
|
||||
public static bool TruncateDictionary()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("truncate table dictionary");
|
||||
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,"Query was '" + strQuery + "'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool InsertDictionaryElement(DictionaryElement dictionaryElement)
|
||||
{
|
||||
DictionaryCollection dictionaryCollection=new DictionaryCollection();
|
||||
dictionaryCollection.Add(dictionaryElement);
|
||||
InsertDictionaryCollection(dictionaryCollection);
|
||||
return true;
|
||||
}
|
||||
public static bool InsertDictionaryCollection(DictionaryCollection dictionaryCollection)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime modified=DateTime.Now;
|
||||
DateTime created=modified;
|
||||
foreach(DictionaryElement dictionaryElement in dictionaryCollection)
|
||||
{
|
||||
sb=new StringBuilder();
|
||||
if(ContainsDictionaryElement(dictionaryElement))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
sb.Append("insert into dictionary (word,part_of_speech) ");
|
||||
sb.Append("values(");
|
||||
sb.Append(SqlUtils.ToSqlString(dictionaryElement.Word)).Append(",");
|
||||
sb.Append("'").Append(dictionaryElement.PartOfSpeech).Append("'");
|
||||
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,"Query was '" + strQuery + "'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool ContainsDictionaryElement(DictionaryElement dictionaryElement)
|
||||
{
|
||||
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 dictionary where word=").Append(SqlUtils.ToSqlString(dictionaryElement.Word));
|
||||
sb.Append(" and part_of_speech='").Append(dictionaryElement.PartOfSpeech).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);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool UpdateDictionaryElement(DictionaryElement dictionaryElement)
|
||||
{
|
||||
DictionaryCollection dictionaryCollection=new DictionaryCollection();
|
||||
dictionaryCollection.Add(dictionaryElement);
|
||||
return UpdateDictionaryElements(dictionaryCollection);
|
||||
}
|
||||
public static bool UpdateDictionaryElements(DictionaryCollection dictionaryCollection)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
foreach(DictionaryElement dictionaryElement in dictionaryCollection)
|
||||
{
|
||||
if(!ContainsDictionaryElement(dictionaryElement))continue;
|
||||
if(null==dictionaryElement.PartOfSpeech||0==dictionaryElement.PartOfSpeech.Length)continue;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime modified=DateTime.Now;
|
||||
sb.Append("update dictionary set ");
|
||||
sb.Append("part_of_speech=").Append("'").Append(dictionaryElement.PartOfSpeech).Append("'");
|
||||
sb.Append(" where word=").Append(SqlUtils.ToSqlString(dictionaryElement.Word));
|
||||
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,"Query was '" + strQuery + "'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DictionaryCollection GetWord(String word)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
DictionaryCollection dictionaryCollection=new DictionaryCollection();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime modified=DateTime.Now;
|
||||
sb.Append("select word,part_of_speech from dictionary ");
|
||||
sb.Append(" where word=").Append(SqlUtils.ToSqlString(word));
|
||||
sb.Append(" order by word desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
DictionaryElement dictionaryElement=new DictionaryElement();
|
||||
dictionaryElement.Word=sqlDataReader.GetString(0);
|
||||
if(!sqlDataReader.IsDBNull(1))dictionaryElement.PartOfSpeech=sqlDataReader.GetString(1);
|
||||
dictionaryCollection.Add(dictionaryElement);
|
||||
}
|
||||
return dictionaryCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was '" + strQuery + "'");
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DictionaryCollection GetPartsOfSpeech(String partOfSpeech)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
DictionaryCollection dictionaryCollection=new DictionaryCollection();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime modified=DateTime.Now;
|
||||
sb.Append("select word,part_of_speech from dictionary ");
|
||||
sb.Append(" where part_of_speech='").Append(partOfSpeech).Append("'");
|
||||
sb.Append(" order by word desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
DictionaryElement dictionaryElement=new DictionaryElement();
|
||||
dictionaryElement.Word=sqlDataReader.GetString(0);
|
||||
if(!sqlDataReader.IsDBNull(1))dictionaryElement.PartOfSpeech=sqlDataReader.GetString(1);
|
||||
dictionaryCollection.Add(dictionaryElement);
|
||||
}
|
||||
return dictionaryCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was '" + strQuery + "'");
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DictionaryCollection GetDictionaryCollection()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
DictionaryCollection dictionaryCollection=new DictionaryCollection();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime modified=DateTime.Now;
|
||||
sb.Append("select word,part_of_speech from dictionary ");
|
||||
sb.Append(" order by word desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
DictionaryElement dictionaryElement=new DictionaryElement();
|
||||
dictionaryElement.Word=sqlDataReader.GetString(0);
|
||||
dictionaryElement.PartOfSpeech=sqlDataReader.GetString(1);
|
||||
dictionaryCollection.Add(dictionaryElement);
|
||||
}
|
||||
return dictionaryCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was '" + strQuery + "'");
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static bool DeleteDictionaryCollection(DictionaryCollection dictionaryCollection, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
foreach(DictionaryElement dictionaryElement in dictionaryCollection)DeleteDictionaryElement(dictionaryElement,sqlConnection,sqlTransaction);
|
||||
return true;
|
||||
}
|
||||
private static bool DeleteDictionaryElement(DictionaryElement dictionaryElement, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sb.Append("delete from dictionary where ");
|
||||
sb.Append("word=").Append(SqlUtils.ToSqlString(dictionaryElement.Word));
|
||||
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
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
286
MarketData/MarketDataLib/DataAccess/DividendHistoryDA.cs
Executable file
286
MarketData/MarketDataLib/DataAccess/DividendHistoryDA.cs
Executable file
@@ -0,0 +1,286 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class DividendHistoryDA
|
||||
{
|
||||
private DividendHistoryDA()
|
||||
{
|
||||
}
|
||||
public static List<DivExDateItem> GetLatestDivExDates()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
List<DivExDateItem> divExDates = new List<DivExDateItem>();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select max(div_ex_date),symbol from dividendhistory group by symbol order by symbol asc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
DivExDateItem divExDate = new DivExDateItem();
|
||||
divExDate.DivExDate = sqlDataReader.GetDateTime(0);
|
||||
divExDate.Symbol = sqlDataReader.GetString(1);
|
||||
divExDates.Add(divExDate);
|
||||
}
|
||||
return divExDates;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DividendHistory GetDividendHistory(List<DivExDateItem> divExDates)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
DividendHistory dividendHistory = new DividendHistory();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
|
||||
foreach (DivExDateItem divExDate in divExDates)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select symbol,dividend_type,cash_amount,div_ex_date,declaration_date,record_date,payment_date,modified from dividendhistory where symbol=");
|
||||
sb.Append(SqlUtils.AddQuotes(divExDate.Symbol));
|
||||
sb.Append(" and ");
|
||||
sb.Append("div_ex_date=").Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(divExDate.DivExDate)));
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (sqlDataReader.Read())
|
||||
{
|
||||
DividendHistoryItem dividendHistoryItem = new DividendHistoryItem();
|
||||
dividendHistoryItem.Symbol = sqlDataReader.GetString(0);
|
||||
if (!sqlDataReader.IsDBNull(1)) dividendHistoryItem.DividendType = sqlDataReader.GetString(1);
|
||||
if (!sqlDataReader.IsDBNull(2)) dividendHistoryItem.CashAmount = sqlDataReader.GetDouble(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) dividendHistoryItem.DivExDate = sqlDataReader.GetDateTime(3);
|
||||
if (!sqlDataReader.IsDBNull(4)) dividendHistoryItem.DeclarationDate = sqlDataReader.GetDateTime(4);
|
||||
if (!sqlDataReader.IsDBNull(5)) dividendHistoryItem.RecordDate = sqlDataReader.GetDateTime(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) dividendHistoryItem.PaymentDate = sqlDataReader.GetDateTime(6);
|
||||
if (!sqlDataReader.IsDBNull(7)) dividendHistoryItem.Modified = sqlDataReader.GetDateTime(7);
|
||||
dividendHistory.Add(dividendHistoryItem);
|
||||
}
|
||||
sqlCommand.Dispose();
|
||||
sqlDataReader.Close();
|
||||
}
|
||||
return dividendHistory;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader){sqlDataReader.Close();sqlDataReader.Dispose();}
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DividendHistory GetDividendHistory(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
DividendHistory dividendHistory = new DividendHistory();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,dividend_type,cash_amount,div_ex_date,declaration_date,record_date,payment_date,modified from dividendhistory where symbol=");
|
||||
sb.Append(SqlUtils.AddQuotes(symbol));
|
||||
sb.Append(" order by div_ex_date desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
DividendHistoryItem dividendHistoryItem = new DividendHistoryItem();
|
||||
dividendHistoryItem.Symbol = sqlDataReader.GetString(0);
|
||||
if (!sqlDataReader.IsDBNull(1)) dividendHistoryItem.DividendType = sqlDataReader.GetString(1);
|
||||
if (!sqlDataReader.IsDBNull(2)) dividendHistoryItem.CashAmount = sqlDataReader.GetDouble(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) dividendHistoryItem.DivExDate = sqlDataReader.GetDateTime(3);
|
||||
if (!sqlDataReader.IsDBNull(4)) dividendHistoryItem.DeclarationDate = sqlDataReader.GetDateTime(4);
|
||||
if (!sqlDataReader.IsDBNull(5)) dividendHistoryItem.RecordDate = sqlDataReader.GetDateTime(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) dividendHistoryItem.PaymentDate = sqlDataReader.GetDateTime(6);
|
||||
if (!sqlDataReader.IsDBNull(7)) dividendHistoryItem.Modified = sqlDataReader.GetDateTime(7);
|
||||
dividendHistory.Add(dividendHistoryItem);
|
||||
}
|
||||
return dividendHistory;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader){sqlDataReader.Close();sqlDataReader.Dispose();}
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DividendHistory GetDividendHistory(String symbol,int[] years)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
DividendHistory dividendHistory = new DividendHistory();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,dividend_type,cash_amount,div_ex_date,declaration_date,record_date,payment_date,modified from dividendhistory where symbol=");
|
||||
sb.Append(SqlUtils.AddQuotes(symbol));
|
||||
sb.Append(" and year(div_ex_date) in ").Append(SqlUtils.CreateInClauseInt(years.ToList()));
|
||||
sb.Append(" order by div_ex_date desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
DividendHistoryItem dividendHistoryItem = new DividendHistoryItem();
|
||||
dividendHistoryItem.Symbol = sqlDataReader.GetString(0);
|
||||
if (!sqlDataReader.IsDBNull(1)) dividendHistoryItem.DividendType = sqlDataReader.GetString(1);
|
||||
if (!sqlDataReader.IsDBNull(2)) dividendHistoryItem.CashAmount = sqlDataReader.GetDouble(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) dividendHistoryItem.DivExDate = sqlDataReader.GetDateTime(3);
|
||||
if (!sqlDataReader.IsDBNull(4)) dividendHistoryItem.DeclarationDate = sqlDataReader.GetDateTime(4);
|
||||
if (!sqlDataReader.IsDBNull(5)) dividendHistoryItem.RecordDate = sqlDataReader.GetDateTime(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) dividendHistoryItem.PaymentDate = sqlDataReader.GetDateTime(6);
|
||||
if (!sqlDataReader.IsDBNull(7)) dividendHistoryItem.Modified = sqlDataReader.GetDateTime(7);
|
||||
dividendHistory.Add(dividendHistoryItem);
|
||||
}
|
||||
return dividendHistory;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader){sqlDataReader.Close();sqlDataReader.Dispose();}
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool InsertOrUpdate(DividendHistory dividendHistory)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == dividendHistory || 0 == dividendHistory.Count) return false;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
DeleteDividendHistory(dividendHistory, sqlConnection,sqlTransaction);
|
||||
DateTime modified = DateTime.Now;
|
||||
for (int index = 0; index < dividendHistory.Count; index++)
|
||||
{
|
||||
DividendHistoryItem dividendHistoryItem = dividendHistory[index];
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into dividendhistory(symbol,dividend_type,cash_amount,div_ex_date,declaration_date,record_date,payment_date,modified)values(");
|
||||
sb.Append(SqlUtils.AddQuotes(dividendHistoryItem.Symbol)).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(dividendHistoryItem.DividendType)).Append(",");
|
||||
if (null == dividendHistoryItem.CashAmount) sb.Append("null").Append(",");
|
||||
else sb.Append(dividendHistoryItem.CashAmount.Value).Append(",");
|
||||
|
||||
sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(dividendHistoryItem.DivExDate))).Append(",");
|
||||
|
||||
if (null == dividendHistoryItem.DeclarationDate) sb.Append("null").Append(",");
|
||||
else sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(dividendHistoryItem.DeclarationDate.Value))).Append(",");
|
||||
|
||||
if (null == dividendHistoryItem.RecordDate) sb.Append("null").Append(",");
|
||||
else sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(dividendHistoryItem.RecordDate.Value))).Append(",");
|
||||
|
||||
if (null == dividendHistoryItem.PaymentDate) sb.Append("null").Append(",");
|
||||
else sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(dividendHistoryItem.PaymentDate.Value))).Append(",");
|
||||
|
||||
// sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(modified)));
|
||||
sb.Append(SqlUtils.AddQuotes(SqlUtils.ToSqlDateTime(modified)));
|
||||
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)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was.."+strQuery);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static void DeleteDividendHistory(DividendHistory dividendHistory,MySqlConnection sqlConnection,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
String strQuery = null;
|
||||
try
|
||||
{
|
||||
for (int index = 0; index < dividendHistory.Count; index++)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DividendHistoryItem dividendHistoryItem = dividendHistory[index];
|
||||
sb.Append("delete from dividendhistory where ");
|
||||
sb.Append("symbol=").Append(SqlUtils.AddQuotes(dividendHistoryItem.Symbol));
|
||||
sb.Append(" and div_ex_date=").Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(dividendHistoryItem.DivExDate)));
|
||||
strQuery = sb.ToString();
|
||||
MySqlCommand sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
sqlCommand.Dispose();
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
}
|
||||
finally
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
368
MarketData/MarketDataLib/DataAccess/DividendPaymentDA.cs
Executable file
368
MarketData/MarketDataLib/DataAccess/DividendPaymentDA.cs
Executable file
@@ -0,0 +1,368 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class DividendPaymentDA
|
||||
{
|
||||
private DividendPaymentDA()
|
||||
{
|
||||
}
|
||||
public static DividendPayments GetDividendPayments()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
DividendPayments dividendPayments=new DividendPayments();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select symbol,payment_date,account,amount from dividends order by payment_date desc, symbol asc;");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
DividendPayment dividendPayment=new DividendPayment();
|
||||
dividendPayment.Symbol = sqlDataReader.GetString(0);
|
||||
dividendPayment.PaymentDate=sqlDataReader.GetDateTime(1);
|
||||
dividendPayment.Account=sqlDataReader.GetString(2);
|
||||
dividendPayment.Amount=sqlDataReader.GetDouble(3);
|
||||
dividendPayments.Add(dividendPayment);
|
||||
}
|
||||
sqlCommand.Dispose();
|
||||
sqlDataReader.Close();
|
||||
return dividendPayments;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader)sqlDataReader.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DividendPayments GetDividendPaymentsForSymbol(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
DividendPayments dividendPayments=new DividendPayments();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select symbol,payment_date,account,amount from dividends ");
|
||||
sb.Append("where symbol=").Append("'").Append(symbol).Append("'");
|
||||
sb.Append(" order by payment_date desc, symbol asc;");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
DividendPayment dividendPayment=new DividendPayment();
|
||||
dividendPayment.Symbol = sqlDataReader.GetString(0);
|
||||
dividendPayment.PaymentDate=sqlDataReader.GetDateTime(1);
|
||||
dividendPayment.Account=sqlDataReader.GetString(2);
|
||||
dividendPayment.Amount=sqlDataReader.GetDouble(3);
|
||||
dividendPayments.Add(dividendPayment);
|
||||
}
|
||||
sqlCommand.Dispose();
|
||||
sqlDataReader.Close();
|
||||
return dividendPayments;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader)sqlDataReader.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DividendPayments GetDividendPaymentsForAccount(String account)
|
||||
{
|
||||
List<String> accounts = new List<String>();
|
||||
accounts.Add(account);
|
||||
return GetDividendPaymentsForAccounts(accounts);
|
||||
}
|
||||
public static DividendPayments GetDividendPaymentsForAccounts(List<String> accounts)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
DividendPayments dividendPayments = new DividendPayments();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select symbol,payment_date,account,amount from dividends ");
|
||||
sb.Append("where account in ").Append(SqlUtils.CreateInClause(accounts));
|
||||
sb.Append(" order by payment_date desc, symbol asc;");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
DividendPayment dividendPayment = new DividendPayment();
|
||||
dividendPayment.Symbol = sqlDataReader.GetString(0);
|
||||
dividendPayment.PaymentDate = sqlDataReader.GetDateTime(1);
|
||||
dividendPayment.Account = sqlDataReader.GetString(2);
|
||||
dividendPayment.Amount = sqlDataReader.GetDouble(3);
|
||||
dividendPayments.Add(dividendPayment);
|
||||
}
|
||||
sqlCommand.Dispose();
|
||||
sqlDataReader.Close();
|
||||
return dividendPayments;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DividendPayments GetDividendPaymentsForAccountsAndSymbols(List<String> accounts,List<String> symbols)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
DividendPayments dividendPayments = new DividendPayments();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select symbol,payment_date,account,amount from dividends ");
|
||||
if ((null == accounts || 0 == accounts.Count) && (null == symbols || 0 == symbols.Count)) return GetDividendPayments();
|
||||
if (!accounts.Contains(Constants.CONST_ALL)&&!accounts.Contains(""))
|
||||
{
|
||||
sb.Append("where account in ").Append(SqlUtils.CreateInClause(accounts)).Append(" ");
|
||||
if (!symbols.Contains(Constants.CONST_ALL)&&0!=symbols.Count&&!symbols.Contains("")) sb.Append("and symbol in ").Append(SqlUtils.CreateInClause(symbols)).Append(" ");
|
||||
}
|
||||
else if (!symbols.Contains(Constants.CONST_ALL)&&0!=symbols.Count&&!symbols.Contains("")) sb.Append("where symbol in ").Append(SqlUtils.CreateInClause(symbols)).Append(" ");
|
||||
sb.Append(" order by payment_date desc, account, symbol asc;");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
DividendPayment dividendPayment = new DividendPayment();
|
||||
dividendPayment.Symbol = sqlDataReader.GetString(0);
|
||||
dividendPayment.PaymentDate = sqlDataReader.GetDateTime(1);
|
||||
dividendPayment.Account = sqlDataReader.GetString(2);
|
||||
dividendPayment.Amount = sqlDataReader.GetDouble(3);
|
||||
dividendPayments.Add(dividendPayment);
|
||||
}
|
||||
sqlCommand.Dispose();
|
||||
sqlDataReader.Close();
|
||||
return dividendPayments;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DividendPayments GetDividendPaymentsMaxDate(String symbol,DateTime maxDate)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
DividendPayments dividendPayments=new DividendPayments();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select symbol,payment_date,account,amount from dividends ");
|
||||
sb.Append("where symbol=").Append("'").Append(symbol).Append("'");
|
||||
sb.Append(" and payment_date<=").Append("'").Append(SqlUtils.ToSqlDateTime(maxDate)).Append("'");
|
||||
sb.Append(" order by payment_date desc, symbol asc;");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
DividendPayment dividendPayment=new DividendPayment();
|
||||
dividendPayment.Symbol = sqlDataReader.GetString(0);
|
||||
dividendPayment.PaymentDate=sqlDataReader.GetDateTime(1);
|
||||
dividendPayment.Account=sqlDataReader.GetString(2);
|
||||
dividendPayment.Amount=sqlDataReader.GetDouble(3);
|
||||
dividendPayments.Add(dividendPayment);
|
||||
}
|
||||
sqlCommand.Dispose();
|
||||
sqlDataReader.Close();
|
||||
return dividendPayments;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader)sqlDataReader.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
// ***********
|
||||
public static List<String> GetDividendPaymentAccounts()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
List<String> accounts = new List<String>();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select distinct(account) from dividends ");
|
||||
sb.Append(" order by account asc;");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
accounts.Add(sqlDataReader.GetString(0));
|
||||
}
|
||||
sqlCommand.Dispose();
|
||||
sqlDataReader.Close();
|
||||
return accounts;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static List<String> GetDividendPaymentSymbols()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
List<String> symbols = new List<String>();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select distinct(symbol) from dividends ");
|
||||
sb.Append(" order by symbol asc;");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
symbols.Add(sqlDataReader.GetString(0));
|
||||
}
|
||||
sqlCommand.Dispose();
|
||||
sqlDataReader.Close();
|
||||
return symbols;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static List<String> GetDividendPaymentSymbols(String account)
|
||||
{
|
||||
List<String> accounts = new List<String>();
|
||||
accounts.Add(account);
|
||||
return GetDividendPaymentSymbols(accounts);
|
||||
}
|
||||
public static List<String> GetDividendPaymentSymbols(List<String> accounts)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
List<String> symbols = new List<String>();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select distinct(symbol) from dividends where account in ").Append(SqlUtils.CreateInClause(accounts));
|
||||
sb.Append(" order by symbol asc;");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
symbols.Add(sqlDataReader.GetString(0));
|
||||
}
|
||||
sqlCommand.Dispose();
|
||||
sqlDataReader.Close();
|
||||
return symbols;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
292
MarketData/MarketDataLib/DataAccess/ETFHoldingsDA.cs
Executable file
292
MarketData/MarketDataLib/DataAccess/ETFHoldingsDA.cs
Executable file
@@ -0,0 +1,292 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
// **********************************************************
|
||||
public class ETFHoldingsDA
|
||||
{
|
||||
private ETFHoldingsDA()
|
||||
{
|
||||
}
|
||||
public static List<String> GetETFSymbols()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
List<String> etfSymbols=new List<String>();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select distinct(etf_symbol) from etfholdings ");
|
||||
sb.Append(" order by 1 asc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
etfSymbols.Add(sqlDataReader.GetString(0));
|
||||
}
|
||||
return etfSymbols;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static bool HaveETFHoldings(String etfSymbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
ETFHoldings etfHoldings = new ETFHoldings(); // new ETFHoldings(etfSymbol);
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == etfSymbol) return false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select count(*) from etfholdings ");
|
||||
sb.Append("where etf_symbol='").Append(etfSymbol).Append("'");
|
||||
strQuery = sb.ToString();
|
||||
MySqlCommand sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return false;
|
||||
int count = sqlDataReader.GetInt32(0);
|
||||
sqlCommand.Dispose();
|
||||
return count==0?false:true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static bool DeleteETFHoldings(String etfSymbol,MySqlConnection sqlConnection,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
String strQuery = null;
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("delete from etfholdings ");
|
||||
sb.Append("where etf_symbol='").Append(etfSymbol).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)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
}
|
||||
}
|
||||
public static bool InsertOrUpdate(ETFHoldings etfHoldings)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == etfHoldings || 0 == etfHoldings.Count) return false;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
List<String> etfSymbols = etfHoldings.GetETFSymbols();
|
||||
DateTime modified = DateTime.Now;
|
||||
foreach (String etfSymbol in etfSymbols)
|
||||
{
|
||||
if (HaveETFHoldings(etfSymbol)) DeleteETFHoldings(etfSymbol, sqlConnection, sqlTransaction);
|
||||
}
|
||||
for (int index = 0; index < etfHoldings.Count; index++)
|
||||
{
|
||||
ETFHolding etfHolding = etfHoldings[index];
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into ETFHoldings(etf_symbol,holding_symbol,holding_symbol_sc,pcnt_of_assets,company,modified)values(");
|
||||
sb.Append(SqlUtils.ToSqlString(etfHolding.ETFSymbol)).Append(",");
|
||||
sb.Append(SqlUtils.ToSqlString(etfHolding.HoldingSymbol)).Append(",");
|
||||
sb.Append(SqlUtils.ToSqlString(etfHolding.HoldingSymbolShareClass)).Append(",");
|
||||
sb.Append(etfHolding.PercentOfAssets).Append(",");
|
||||
sb.Append(SqlUtils.ToSqlString(etfHolding.HoldingCompanyName)).Append(",");
|
||||
sb.Append(SqlUtils.ToSqlString(Utility.DateTimeToStringYYYYHMMHDD(modified)));
|
||||
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)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
}
|
||||
}
|
||||
public static ETFHoldings GetETFHoldings(String etfSymbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
ETFHoldings etfHoldings = new ETFHoldings();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select etf_symbol,holding_symbol,holding_symbol_sc,pcnt_of_assets,company,modified from etfholdings ");
|
||||
sb.Append("where etf_symbol='").Append(etfSymbol).Append("'");
|
||||
sb.Append(" order by pcnt_of_assets desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
ETFHolding etfHolding = new ETFHolding();
|
||||
etfHolding.ETFSymbol = sqlDataReader.GetString(0);
|
||||
etfHolding.HoldingSymbol = sqlDataReader.GetString(1);
|
||||
etfHolding.HoldingSymbolShareClass = sqlDataReader.GetString(2);
|
||||
etfHolding.PercentOfAssets = sqlDataReader.GetDouble(3);
|
||||
etfHolding.HoldingCompanyName = sqlDataReader.GetString(4);
|
||||
etfHolding.Modified = sqlDataReader.GetDateTime(5);
|
||||
etfHoldings.Add(etfHolding);
|
||||
}
|
||||
return etfHoldings;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static ETFHoldings GetETFHoldingsWithExposureToSymbolLike(String holdingSymbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
ETFHoldings etfHoldings = new ETFHoldings();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select etf_symbol,holding_symbol,holding_symbol_sc,pcnt_of_assets,company,modified from etfholdings ");
|
||||
sb.Append("where holding_symbol like '%").Append(holdingSymbol).Append("%'");
|
||||
sb.Append(" order by pcnt_of_assets desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
ETFHolding etfHolding = new ETFHolding();
|
||||
etfHolding.ETFSymbol = sqlDataReader.GetString(0);
|
||||
etfHolding.HoldingSymbol = sqlDataReader.GetString(1);
|
||||
etfHolding.HoldingSymbolShareClass = sqlDataReader.GetString(2);
|
||||
etfHolding.PercentOfAssets = sqlDataReader.GetDouble(3);
|
||||
etfHolding.HoldingCompanyName = sqlDataReader.GetString(4);
|
||||
etfHolding.Modified = sqlDataReader.GetDateTime(5);
|
||||
etfHoldings.Add(etfHolding);
|
||||
}
|
||||
return etfHoldings;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static ETFHoldings GetETFHoldingsWithExposureToCompanyLike(String searchValue)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
ETFHoldings etfHoldings = new ETFHoldings();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select etf_symbol,holding_symbol,holding_symbol_sc,pcnt_of_assets,company,modified from etfholdings ");
|
||||
sb.Append("where company like '%").Append(searchValue).Append("%'");
|
||||
sb.Append(" order by pcnt_of_assets desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
ETFHolding etfHolding = new ETFHolding();
|
||||
etfHolding.ETFSymbol = sqlDataReader.GetString(0);
|
||||
etfHolding.HoldingSymbol = sqlDataReader.GetString(1);
|
||||
etfHolding.HoldingSymbolShareClass = sqlDataReader.GetString(2);
|
||||
etfHolding.PercentOfAssets = sqlDataReader.GetDouble(3);
|
||||
etfHolding.HoldingCompanyName = sqlDataReader.GetString(4);
|
||||
etfHolding.Modified = sqlDataReader.GetDateTime(5);
|
||||
etfHoldings.Add(etfHolding);
|
||||
}
|
||||
return etfHoldings;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
249
MarketData/MarketDataLib/DataAccess/EarningsAnnouncementsDA.cs
Executable file
249
MarketData/MarketDataLib/DataAccess/EarningsAnnouncementsDA.cs
Executable file
@@ -0,0 +1,249 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
// Persistence for Zacks Earnings Announcements
|
||||
public class EarningsAnnouncementsDA
|
||||
{
|
||||
private EarningsAnnouncementsDA()
|
||||
{
|
||||
}
|
||||
public static DateTime? GetNextEarningsDate(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
List<DateTime> earningsDates=new List<DateTime>();
|
||||
DateTime currentDate=DateTime.Now.Date;
|
||||
DateGenerator dateGenerator=new DateGenerator();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select date from earningsannouncements where symbol=").Append(SqlUtils.AddQuotes(symbol)).Append(" order by date desc limit 2");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
earningsDates.Add(sqlDataReader.GetDateTime(0));
|
||||
}
|
||||
if(2!=earningsDates.Count)return null;
|
||||
DateTime latestAnnouncementDate=earningsDates[0];
|
||||
DateTime previousAnnouncementDate=earningsDates[1];
|
||||
if(latestAnnouncementDate>currentDate)return latestAnnouncementDate;
|
||||
TimeSpan daysBetween=latestAnnouncementDate-previousAnnouncementDate;
|
||||
return dateGenerator.GetNextBusinessDay(latestAnnouncementDate+daysBetween);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static EarningsAnnouncements GetEarningsAnnouncements(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
EarningsAnnouncements earningsAnnouncements=new EarningsAnnouncements();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,date,period_ending,estimate,reported,surprise,surprise_pct,time from earningsannouncements where symbol=").Append(SqlUtils.AddQuotes(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())
|
||||
{
|
||||
EarningsAnnouncement earningsAnnouncement=new EarningsAnnouncement();
|
||||
earningsAnnouncement.Symbol=symbol;
|
||||
earningsAnnouncement.Date=sqlDataReader.GetDateTime(1);
|
||||
if(!sqlDataReader.IsDBNull(2))earningsAnnouncement.PeriodEnding=sqlDataReader.GetDateTime(2);
|
||||
if(!sqlDataReader.IsDBNull(3))earningsAnnouncement.Estimate=sqlDataReader.GetDouble(3);
|
||||
if(!sqlDataReader.IsDBNull(4))earningsAnnouncement.Reported=sqlDataReader.GetDouble(4);
|
||||
if(!sqlDataReader.IsDBNull(5))earningsAnnouncement.Surprise=sqlDataReader.GetDouble(5);
|
||||
if(!sqlDataReader.IsDBNull(6))earningsAnnouncement.SurprisePct=sqlDataReader.GetDouble(6);
|
||||
if(!sqlDataReader.IsDBNull(7))earningsAnnouncement.Time=sqlDataReader.GetString(7);
|
||||
earningsAnnouncements.Add(earningsAnnouncement);
|
||||
}
|
||||
return earningsAnnouncements;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return earningsAnnouncements;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static EarningsAnnouncement GetLatestEarningsAnnouncement(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
EarningsAnnouncement earningsAnnouncement=null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,date,period_ending,estimate,reported,surprise,surprise_pct,time from earningsannouncements where symbol=").Append(SqlUtils.AddQuotes(symbol));
|
||||
sb.Append("and date=(select max(date) from earningsannouncements where symbol=").Append(SqlUtils.AddQuotes(symbol)).Append(")");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if(!sqlDataReader.Read())return null;
|
||||
earningsAnnouncement=new EarningsAnnouncement();
|
||||
earningsAnnouncement.Symbol=symbol;
|
||||
earningsAnnouncement.Date=sqlDataReader.GetDateTime(1);
|
||||
if(!sqlDataReader.IsDBNull(2))earningsAnnouncement.PeriodEnding=sqlDataReader.GetDateTime(2);
|
||||
if(!sqlDataReader.IsDBNull(3))earningsAnnouncement.Estimate=sqlDataReader.GetDouble(3);
|
||||
if(!sqlDataReader.IsDBNull(4))earningsAnnouncement.Reported=sqlDataReader.GetDouble(4);
|
||||
if(!sqlDataReader.IsDBNull(5))earningsAnnouncement.Surprise=sqlDataReader.GetDouble(5);
|
||||
if(!sqlDataReader.IsDBNull(6))earningsAnnouncement.SurprisePct=sqlDataReader.GetDouble(6);
|
||||
if(!sqlDataReader.IsDBNull(7))earningsAnnouncement.Time=sqlDataReader.GetString(7);
|
||||
return earningsAnnouncement;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool InsertEarningsAnnouncements(EarningsAnnouncements earningsAnnouncements)
|
||||
{
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlTransaction sqlTransaction=null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == earningsAnnouncements || 0 == earningsAnnouncements.Count) return false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
foreach(EarningsAnnouncement earningsAnnouncement in earningsAnnouncements)
|
||||
{
|
||||
if(!EarningsAnnouncementExists(earningsAnnouncement,sqlConnection,sqlTransaction))InsertEarningsAnnouncement(earningsAnnouncement,sqlConnection,sqlTransaction);
|
||||
}
|
||||
sqlTransaction.Commit();
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static bool InsertEarningsAnnouncement(EarningsAnnouncement earningsAnnouncement,MySqlConnection sqlConnection,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == earningsAnnouncement || null == earningsAnnouncement.Symbol || Utility.IsEpoch(earningsAnnouncement.Date) || Utility.IsEpoch(earningsAnnouncement.PeriodEnding)) return false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into EarningsAnnouncements(symbol,date,period_ending,estimate,reported,surprise,surprise_pct,time,modified) values(");
|
||||
sb.Append(SqlUtils.AddQuotes(earningsAnnouncement.Symbol)).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(SqlUtils.SqlDate(earningsAnnouncement.Date))).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(SqlUtils.SqlDate(earningsAnnouncement.PeriodEnding))).Append(",");
|
||||
if(double.IsNaN(earningsAnnouncement.Estimate))sb.Append("null").Append(",");
|
||||
else sb.Append(earningsAnnouncement.Estimate).Append(",");
|
||||
if(double.IsNaN(earningsAnnouncement.Reported))sb.Append("null").Append(",");
|
||||
else sb.Append(earningsAnnouncement.Reported).Append(",");
|
||||
if(double.IsNaN(earningsAnnouncement.Surprise))sb.Append("null").Append(",");
|
||||
else sb.Append(earningsAnnouncement.Surprise).Append(",");
|
||||
if(double.IsNaN(earningsAnnouncement.SurprisePct))sb.Append("null").Append(",");
|
||||
else sb.Append(earningsAnnouncement.SurprisePct).Append(",");
|
||||
if(null==earningsAnnouncement.Time)sb.Append("null").Append(",");
|
||||
else sb.Append(SqlUtils.AddQuotes(earningsAnnouncement.Time)).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(SqlUtils.ToSqlDateTime(DateTime.Now)));
|
||||
sb.Append(")");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
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();
|
||||
}
|
||||
}
|
||||
private static bool EarningsAnnouncementExists(EarningsAnnouncement earningsAnnouncement,MySqlConnection sqlConnection,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == earningsAnnouncement || null == earningsAnnouncement.Symbol) return false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select count(*) from earningsannouncements where symbol=").Append(SqlUtils.AddQuotes(earningsAnnouncement.Symbol)).Append(" and ");
|
||||
sb.Append(" date=").Append(SqlUtils.AddQuotes(SqlUtils.SqlDate(earningsAnnouncement.Date)));
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
if(!sqlDataReader.Read())return false;
|
||||
return 0==sqlDataReader.GetInt32(0)?false:true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader){sqlDataReader.Close();sqlDataReader.Dispose();}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
176
MarketData/MarketDataLib/DataAccess/EconomicIndicatorDA.cs
Executable file
176
MarketData/MarketDataLib/DataAccess/EconomicIndicatorDA.cs
Executable file
@@ -0,0 +1,176 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class EconomicIndicatorDA
|
||||
{
|
||||
private EconomicIndicatorDA()
|
||||
{
|
||||
}
|
||||
public static EconomicIndicators GetEconomicIndicators()
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
EconomicIndicators economicIndicators=new EconomicIndicators();
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlCommand=sqlConnection.CreateCommand();
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.Append("select country_code,country_name,indicator_code,indicator_name,indicator_value,year,source from EconomicIndicators");
|
||||
sqlCommand.CommandText=sb.ToString();
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
EconomicIndicator economicIndicator=new EconomicIndicator();
|
||||
if(!sqlDataReader.IsDBNull(0))economicIndicator.CountryCode=sqlDataReader.GetString(0);
|
||||
if(!sqlDataReader.IsDBNull(1))economicIndicator.CountryName=sqlDataReader.GetString(1);
|
||||
if(!sqlDataReader.IsDBNull(2))economicIndicator.IndicatorCode=sqlDataReader.GetString(2);
|
||||
if(!sqlDataReader.IsDBNull(3))economicIndicator.IndicatorName=sqlDataReader.GetString(3);
|
||||
if(!sqlDataReader.IsDBNull(4))economicIndicator.IndicatorValue=sqlDataReader.GetDouble(4);
|
||||
if(!sqlDataReader.IsDBNull(5))economicIndicator.Year=sqlDataReader.GetInt32(5);
|
||||
if(!sqlDataReader.IsDBNull(6))economicIndicator.Source=sqlDataReader.GetString(6);
|
||||
economicIndicators.Add(economicIndicator);
|
||||
}
|
||||
sqlDataReader.Close();
|
||||
sqlDataReader.Dispose();
|
||||
sqlCommand.Dispose();
|
||||
sqlConnection.Close();
|
||||
sqlConnection.Dispose();
|
||||
return economicIndicators;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString()));
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlDataReader)
|
||||
{
|
||||
sqlDataReader.Close();
|
||||
sqlDataReader.Dispose();
|
||||
}
|
||||
if(null!=sqlCommand)
|
||||
{
|
||||
sqlCommand.Dispose();
|
||||
}
|
||||
if(null!=sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
public static bool InsertUpdateEconomicIndicators(EconomicIndicators economicIndicators)
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlTransaction sqlTransaction=null;
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);
|
||||
sqlCommand=sqlConnection.CreateCommand();
|
||||
sqlCommand.Transaction=sqlTransaction;
|
||||
List<String> distinctCountryCodes=(from EconomicIndicator economicIndicator in economicIndicators select economicIndicator.CountryCode).Distinct().ToList();
|
||||
for(int index=0;index<distinctCountryCodes.Count;index++)
|
||||
{
|
||||
String countryCode=distinctCountryCodes[index];
|
||||
List<int> years=(from EconomicIndicator economicIndicator in economicIndicators select economicIndicator.Year).Distinct().ToList();
|
||||
DeleteEconomicIndicator(countryCode,years,sqlCommand,sqlTransaction);
|
||||
}
|
||||
InsertEconomicIndicators(economicIndicators,sqlCommand,sqlTransaction);
|
||||
sqlTransaction.Commit();
|
||||
sqlTransaction.Dispose();
|
||||
sqlCommand.Dispose();
|
||||
sqlConnection.Close();
|
||||
sqlConnection.Dispose();
|
||||
sqlConnection=null;
|
||||
sqlCommand=null;
|
||||
sqlTransaction=null;
|
||||
return true;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString()));
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection.Dispose();
|
||||
}
|
||||
if(null!=sqlCommand)
|
||||
{
|
||||
sqlCommand.Dispose();
|
||||
}
|
||||
if(null!=sqlTransaction)
|
||||
{
|
||||
sqlTransaction.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
public static bool DeleteEconomicIndicator(String countryCode,List<int> years,MySqlCommand sqlCommand,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
String strQuery=null;
|
||||
try
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.Append("delete from EconomicIndicators ").Append(" where ");
|
||||
sb.Append("country_code=").Append("'").Append(countryCode).Append("'").Append(" and ");
|
||||
sb.Append("year in ").Append(SqlUtils.CreateInClause(years));
|
||||
strQuery=sb.ToString();
|
||||
sqlCommand.CommandText=strQuery;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
return true;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}, query was {1}",exception.ToString(),strQuery));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public static bool InsertEconomicIndicators(EconomicIndicators economicIndicators,MySqlCommand sqlCommand,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
String strQuery=null;
|
||||
try
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
foreach(EconomicIndicator economicIndicator in economicIndicators)
|
||||
{
|
||||
sb.Append("insert into EconomicIndicators(country_code,country_name,indicator_code,indicator_name,indicator_value,year,source)values(");
|
||||
sb.Append(SqlUtils.ToSqlString(economicIndicator.CountryCode)).Append(",");
|
||||
sb.Append(SqlUtils.ToSqlString(economicIndicator.CountryName)).Append(",");
|
||||
sb.Append(SqlUtils.ToSqlString(economicIndicator.IndicatorCode)).Append(",");
|
||||
sb.Append(SqlUtils.ToSqlString(economicIndicator.IndicatorName)).Append(",");
|
||||
sb.Append(economicIndicator.IndicatorValue).Append(",");
|
||||
sb.Append(economicIndicator.Year).Append(",");
|
||||
sb.Append(SqlUtils.ToSqlString(economicIndicator.Source));
|
||||
sb.Append(");");
|
||||
}
|
||||
strQuery=sb.ToString();
|
||||
sqlCommand.CommandText=sb.ToString();
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
return true;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString()));
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
766
MarketData/MarketDataLib/DataAccess/FundamentalDA.cs
Executable file
766
MarketData/MarketDataLib/DataAccess/FundamentalDA.cs
Executable file
@@ -0,0 +1,766 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class FundamentalDA
|
||||
{
|
||||
private FundamentalDA()
|
||||
{
|
||||
}
|
||||
public static bool CheckFundamentalModifiedOn(String symbol,DateTime modified)
|
||||
{
|
||||
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 count(*) from fundamentals").Append(" ");
|
||||
sb.Append("where symbol='").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and asof='").Append(Utility.DateTimeToStringYYYYHMMHDD(modified)).Append("'");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
sqlDataReader.Read();
|
||||
int recordCount = sqlDataReader.GetInt32(0);
|
||||
return 0 == recordCount ? false : true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
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(asof) from fundamentals").Append(" ");
|
||||
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);
|
||||
return maxDate;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader)sqlDataReader.Close();
|
||||
if(null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static TimeSeriesCollection GetTotalCashMils(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
TimeSeriesCollection timeSeriesCollection=new TimeSeriesCollection();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,total_cash from fundamentals where symbol='").Append(symbol).Append("' order by asof desc");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
TimeSeriesElement timeSeriesElement=new TimeSeriesElement();
|
||||
timeSeriesElement.Symbol=sqlDataReader.GetString(0);
|
||||
timeSeriesElement.AsOf=sqlDataReader.GetDateTime(1);
|
||||
if(!sqlDataReader.IsDBNull(2))timeSeriesElement.Value=sqlDataReader.GetDouble(2)/1000000.00;
|
||||
else timeSeriesElement.Value=double.NaN;
|
||||
timeSeriesElement.Type=TimeSeriesElement.ElementType.OTHER;
|
||||
timeSeriesElement.OtherType="Total_Cash";
|
||||
if(double.IsNaN(timeSeriesElement.Value))continue;
|
||||
timeSeriesCollection.Add(timeSeriesElement);
|
||||
}
|
||||
return timeSeriesCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static TimeSeriesCollection GetPERatio(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
TimeSeriesCollection timeSeriesCollection=new TimeSeriesCollection();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,pe from fundamentals where symbol='").Append(symbol).Append("' order by asof desc");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
TimeSeriesElement timeSeriesElement=new TimeSeriesElement();
|
||||
timeSeriesElement.Symbol=sqlDataReader.GetString(0);
|
||||
timeSeriesElement.AsOf=sqlDataReader.GetDateTime(1);
|
||||
if(!sqlDataReader.IsDBNull(2))timeSeriesElement.Value=sqlDataReader.GetDouble(2);
|
||||
else timeSeriesElement.Value=double.NaN;
|
||||
timeSeriesElement.Type=TimeSeriesElement.ElementType.OTHER;
|
||||
timeSeriesElement.OtherType="PE";
|
||||
if(double.IsNaN(timeSeriesElement.Value))continue;
|
||||
timeSeriesCollection.Add(timeSeriesElement);
|
||||
}
|
||||
return timeSeriesCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static TimeSeriesCollection GetEPS(String symbol,DateTime? maxDate=null)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
if(null==maxDate)sb.Append("select symbol,asof,eps from fundamentals where symbol='").Append(symbol).Append("' order by asof desc");
|
||||
else sb.Append("select symbol,asof,eps from fundamentals where symbol='").Append(symbol).Append("'").Append(" and asof<=").Append(SqlUtils.AddQuotes(SqlUtils.ToSqlDateTime(maxDate.Value))).Append(" order by asof desc");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
TimeSeriesElement timeSeriesElement = new TimeSeriesElement();
|
||||
timeSeriesElement.Symbol = sqlDataReader.GetString(0);
|
||||
timeSeriesElement.AsOf = sqlDataReader.GetDateTime(1);
|
||||
if (!sqlDataReader.IsDBNull(2)) timeSeriesElement.Value = sqlDataReader.GetDouble(2);
|
||||
else timeSeriesElement.Value = double.NaN;
|
||||
timeSeriesElement.Type = TimeSeriesElement.ElementType.OTHER;
|
||||
timeSeriesElement.OtherType = "EPS";
|
||||
if (double.IsNaN(timeSeriesElement.Value)) continue;
|
||||
timeSeriesCollection.Add(timeSeriesElement);
|
||||
}
|
||||
return timeSeriesCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Fundamental GetFundamental(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,next_earnings_date,beta,low52,high52,volume,market_cap,pe,eps,peg,return_on_assets,return_on_equity,total_cash,total_debt,shares_outstanding,revenue,revenue_per_share,qtrly_revenue_growth,gross_profit,ebitda,net_income_available_to_common,book_value_per_share,operating_cashflow,leveraged_free_cashflow,book_value_per_share*shares_outstanding as equity,trailing_pe,ebit,enterprise_value,source from fundamentals where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and asof=(select max(asof) from fundamentals where symbol='").Append(symbol).Append("')");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
Fundamental fundamental = new Fundamental();
|
||||
fundamental.Symbol=sqlDataReader.GetString(0);
|
||||
fundamental.AsOf = sqlDataReader.GetDateTime(1);
|
||||
if (!sqlDataReader.IsDBNull(2)) fundamental.NextEarningsDate = sqlDataReader.GetDateTime(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) fundamental.Beta = sqlDataReader.GetDouble(3);
|
||||
if (!sqlDataReader.IsDBNull(4)) fundamental.Low52 = sqlDataReader.GetDouble(4);
|
||||
if (!sqlDataReader.IsDBNull(5)) fundamental.High52 = sqlDataReader.GetDouble(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) fundamental.Volume = sqlDataReader.GetInt64(6);
|
||||
if (!sqlDataReader.IsDBNull(7)) fundamental.MarketCap = sqlDataReader.GetDouble(7);
|
||||
if (!sqlDataReader.IsDBNull(8)) fundamental.PE = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) fundamental.EPS = sqlDataReader.GetDouble(9);
|
||||
if (!sqlDataReader.IsDBNull(10)) fundamental.PEG = sqlDataReader.GetDouble(10);
|
||||
if (!sqlDataReader.IsDBNull(11)) fundamental.ReturnOnAssets = sqlDataReader.GetDouble(11);
|
||||
if (!sqlDataReader.IsDBNull(12)) fundamental.ReturnOnEquity = sqlDataReader.GetDouble(12);
|
||||
if (!sqlDataReader.IsDBNull(13)) fundamental.TotalCash = sqlDataReader.GetDouble(13);
|
||||
if (!sqlDataReader.IsDBNull(14)) fundamental.TotalDebt = sqlDataReader.GetDouble(14);
|
||||
if (!sqlDataReader.IsDBNull(15)) fundamental.SharesOutstanding = sqlDataReader.GetDouble(15);
|
||||
if (!sqlDataReader.IsDBNull(16)) fundamental.Revenue = sqlDataReader.GetDouble(16);
|
||||
if (!sqlDataReader.IsDBNull(17)) fundamental.RevenuePerShare = sqlDataReader.GetDouble(17);
|
||||
if (!sqlDataReader.IsDBNull(18)) fundamental.QtrlyRevenueGrowth = sqlDataReader.GetDouble(18);
|
||||
if (!sqlDataReader.IsDBNull(19)) fundamental.GrossProfit = sqlDataReader.GetDouble(19);
|
||||
if (!sqlDataReader.IsDBNull(20)) fundamental.EBITDA = sqlDataReader.GetDouble(20);
|
||||
if (!sqlDataReader.IsDBNull(21)) fundamental.NetIncomeAvailableToCommon = sqlDataReader.GetDouble(21);
|
||||
if (!sqlDataReader.IsDBNull(22)) fundamental.BookValuePerShare = sqlDataReader.GetDouble(22);
|
||||
if (!sqlDataReader.IsDBNull(23)) fundamental.OperatingCashflow = sqlDataReader.GetDouble(23);
|
||||
if (!sqlDataReader.IsDBNull(24)) fundamental.LeveragedFreeCashflow = sqlDataReader.GetDouble(24);
|
||||
if (!sqlDataReader.IsDBNull(25)) fundamental.Equity = sqlDataReader.GetDouble(25);
|
||||
if (!sqlDataReader.IsDBNull(26)) fundamental.TrailingPE = sqlDataReader.GetDouble(26);
|
||||
if (!sqlDataReader.IsDBNull(27)) fundamental.EBIT = sqlDataReader.GetDouble(27);
|
||||
if (!sqlDataReader.IsDBNull(28)) fundamental.EnterpriseValue = sqlDataReader.GetDouble(28);
|
||||
if (!sqlDataReader.IsDBNull(29)) fundamental.Source = sqlDataReader.GetString(29);
|
||||
BalanceSheet balanceSheet=BalanceSheetDA.GetBalanceSheetOnOrBefore(symbol,fundamental.AsOf,BalanceSheet.PeriodType.Annual);
|
||||
if(null!=balanceSheet&&!double.IsNaN(balanceSheet.TotalStockHolderEquity)&&!double.IsNaN(fundamental.TotalDebt)&&0!=fundamental.TotalDebt)fundamental.DebtToEquity=fundamental.TotalDebt/balanceSheet.TotalStockHolderEquity;
|
||||
return fundamental;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Fundamental GetFundamental(String symbol, DateTime asof)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,next_earnings_date,beta,low52,high52,volume,market_cap,pe,eps,peg,return_on_assets,return_on_equity,total_cash,total_debt,shares_outstanding,revenue,revenue_per_share,qtrly_revenue_growth,gross_profit,ebitda,net_income_available_to_common,book_value_per_share,operating_cashflow,leveraged_free_cashflow,book_value_per_share*shares_outstanding as equity,trailing_pe,ebit,enterprise_value,source from fundamentals where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and asof=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(asof)).Append("'").Append(";");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
BalanceSheet balanceSheet=BalanceSheetDA.GetBalanceSheetOnOrBefore(symbol,asof,BalanceSheet.PeriodType.Annual);
|
||||
Fundamental fundamental = new Fundamental();
|
||||
fundamental.Symbol = sqlDataReader.GetString(0);
|
||||
fundamental.AsOf = sqlDataReader.GetDateTime(1);
|
||||
if (!sqlDataReader.IsDBNull(2)) fundamental.NextEarningsDate = sqlDataReader.GetDateTime(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) fundamental.Beta = sqlDataReader.GetDouble(3);
|
||||
if (!sqlDataReader.IsDBNull(4)) fundamental.Low52 = sqlDataReader.GetDouble(4);
|
||||
if (!sqlDataReader.IsDBNull(5)) fundamental.High52 = sqlDataReader.GetDouble(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) fundamental.Volume = sqlDataReader.GetInt64(6);
|
||||
if (!sqlDataReader.IsDBNull(7)) fundamental.MarketCap = sqlDataReader.GetDouble(7);
|
||||
if (!sqlDataReader.IsDBNull(8)) fundamental.PE = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) fundamental.EPS = sqlDataReader.GetDouble(9);
|
||||
if (!sqlDataReader.IsDBNull(10)) fundamental.PEG = sqlDataReader.GetDouble(10);
|
||||
if (!sqlDataReader.IsDBNull(11)) fundamental.ReturnOnAssets = sqlDataReader.GetDouble(11);
|
||||
if (!sqlDataReader.IsDBNull(12)) fundamental.ReturnOnEquity = sqlDataReader.GetDouble(12);
|
||||
if (!sqlDataReader.IsDBNull(13)) fundamental.TotalCash = sqlDataReader.GetDouble(13);
|
||||
if (!sqlDataReader.IsDBNull(14)) fundamental.TotalDebt = sqlDataReader.GetDouble(14);
|
||||
if (!sqlDataReader.IsDBNull(15)) fundamental.SharesOutstanding = sqlDataReader.GetDouble(15);
|
||||
if (!sqlDataReader.IsDBNull(16)) fundamental.Revenue = sqlDataReader.GetDouble(16);
|
||||
if (!sqlDataReader.IsDBNull(17)) fundamental.RevenuePerShare = sqlDataReader.GetDouble(17);
|
||||
if (!sqlDataReader.IsDBNull(18)) fundamental.QtrlyRevenueGrowth = sqlDataReader.GetDouble(18);
|
||||
if (!sqlDataReader.IsDBNull(19)) fundamental.GrossProfit = sqlDataReader.GetDouble(19);
|
||||
if (!sqlDataReader.IsDBNull(20)) fundamental.EBITDA = sqlDataReader.GetDouble(20);
|
||||
if (!sqlDataReader.IsDBNull(21)) fundamental.NetIncomeAvailableToCommon = sqlDataReader.GetDouble(21);
|
||||
if (!sqlDataReader.IsDBNull(22)) fundamental.BookValuePerShare = sqlDataReader.GetDouble(22);
|
||||
if (!sqlDataReader.IsDBNull(23)) fundamental.OperatingCashflow = sqlDataReader.GetDouble(23);
|
||||
if (!sqlDataReader.IsDBNull(24)) fundamental.LeveragedFreeCashflow = sqlDataReader.GetDouble(24);
|
||||
if (!sqlDataReader.IsDBNull(25)) fundamental.Equity = sqlDataReader.GetDouble(25);
|
||||
if (!sqlDataReader.IsDBNull(26)) fundamental.TrailingPE = sqlDataReader.GetDouble(26);
|
||||
if (!sqlDataReader.IsDBNull(27)) fundamental.EBIT = sqlDataReader.GetDouble(27);
|
||||
if (!sqlDataReader.IsDBNull(28)) fundamental.EnterpriseValue = sqlDataReader.GetDouble(28);
|
||||
if (!sqlDataReader.IsDBNull(29)) fundamental.Source = sqlDataReader.GetString(29);
|
||||
if (null != balanceSheet && !double.IsNaN(balanceSheet.TotalStockHolderEquity) && !double.IsNaN(fundamental.TotalDebt) && 0 != fundamental.TotalDebt)
|
||||
{
|
||||
fundamental.DebtToEquity=fundamental.TotalDebt/balanceSheet.TotalStockHolderEquity;
|
||||
}
|
||||
return fundamental;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
// get the maximum date on record on or before asof
|
||||
public static DateTime? GetMaxDateFromFundamental(String symbol,DateTime asof)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
DateTime? maxDate=null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select asof from fundamentals where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and asof<=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(asof)).Append("'");
|
||||
sb.Append(" order by asof desc ");
|
||||
sb.Append(" limit 1");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return maxDate;
|
||||
if(!sqlDataReader.IsDBNull(0))maxDate=sqlDataReader.GetDateTime(0);
|
||||
return maxDate;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Fundamental GetFundamentalMaxDate(String symbol, DateTime asof)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
DateTime? maxDate=GetMaxDateFromFundamental(symbol,asof); // get the maximum date on record on or before max date
|
||||
if(null==maxDate)return null;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,next_earnings_date,beta,low52,high52,volume,market_cap,pe,eps,peg,return_on_assets,return_on_equity,total_cash,total_debt,shares_outstanding,revenue,revenue_per_share,qtrly_revenue_growth,gross_profit,ebitda,net_income_available_to_common,book_value_per_share,operating_cashflow,leveraged_free_cashflow,book_value_per_share*shares_outstanding as equity,trailing_pe,ebit,enterprise_value,source from fundamentals where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and asof=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(maxDate.Value)).Append("'");
|
||||
sb.Append(" limit 1");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
double totalStockHolderEquity=BalanceSheetDA.GetTotalStockHolderEquityOnOrBefore(symbol,asof,BalanceSheet.PeriodType.Annual);
|
||||
Fundamental fundamental = new Fundamental();
|
||||
fundamental.Symbol = sqlDataReader.GetString(0);
|
||||
fundamental.AsOf = sqlDataReader.GetDateTime(1);
|
||||
if (!sqlDataReader.IsDBNull(2)) fundamental.NextEarningsDate = sqlDataReader.GetDateTime(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) fundamental.Beta = sqlDataReader.GetDouble(3);
|
||||
if (!sqlDataReader.IsDBNull(4)) fundamental.Low52 = sqlDataReader.GetDouble(4);
|
||||
if (!sqlDataReader.IsDBNull(5)) fundamental.High52 = sqlDataReader.GetDouble(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) fundamental.Volume = sqlDataReader.GetInt64(6);
|
||||
if (!sqlDataReader.IsDBNull(7)) fundamental.MarketCap = sqlDataReader.GetDouble(7);
|
||||
if (!sqlDataReader.IsDBNull(8)) fundamental.PE = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) fundamental.EPS = sqlDataReader.GetDouble(9);
|
||||
if (!sqlDataReader.IsDBNull(10)) fundamental.PEG = sqlDataReader.GetDouble(10);
|
||||
if (!sqlDataReader.IsDBNull(11)) fundamental.ReturnOnAssets = sqlDataReader.GetDouble(11);
|
||||
if (!sqlDataReader.IsDBNull(12)) fundamental.ReturnOnEquity = sqlDataReader.GetDouble(12);
|
||||
if (!sqlDataReader.IsDBNull(13)) fundamental.TotalCash = sqlDataReader.GetDouble(13);
|
||||
if (!sqlDataReader.IsDBNull(14)) fundamental.TotalDebt = sqlDataReader.GetDouble(14);
|
||||
if (!sqlDataReader.IsDBNull(15)) fundamental.SharesOutstanding = sqlDataReader.GetDouble(15);
|
||||
if (!sqlDataReader.IsDBNull(16)) fundamental.Revenue = sqlDataReader.GetDouble(16);
|
||||
if (!sqlDataReader.IsDBNull(17)) fundamental.RevenuePerShare = sqlDataReader.GetDouble(17);
|
||||
if (!sqlDataReader.IsDBNull(18)) fundamental.QtrlyRevenueGrowth = sqlDataReader.GetDouble(18);
|
||||
if (!sqlDataReader.IsDBNull(19)) fundamental.GrossProfit = sqlDataReader.GetDouble(19);
|
||||
if (!sqlDataReader.IsDBNull(20)) fundamental.EBITDA = sqlDataReader.GetDouble(20);
|
||||
if (!sqlDataReader.IsDBNull(21)) fundamental.NetIncomeAvailableToCommon = sqlDataReader.GetDouble(21);
|
||||
if (!sqlDataReader.IsDBNull(22)) fundamental.BookValuePerShare = sqlDataReader.GetDouble(22);
|
||||
if (!sqlDataReader.IsDBNull(23)) fundamental.OperatingCashflow = sqlDataReader.GetDouble(23);
|
||||
if (!sqlDataReader.IsDBNull(24)) fundamental.LeveragedFreeCashflow = sqlDataReader.GetDouble(24);
|
||||
if (!sqlDataReader.IsDBNull(25)) fundamental.Equity = sqlDataReader.GetDouble(25);
|
||||
if (!sqlDataReader.IsDBNull(26)) fundamental.TrailingPE = sqlDataReader.GetDouble(26);
|
||||
if (!sqlDataReader.IsDBNull(27)) fundamental.EBIT = sqlDataReader.GetDouble(27);
|
||||
if (!sqlDataReader.IsDBNull(28)) fundamental.EnterpriseValue = sqlDataReader.GetDouble(28);
|
||||
if (!sqlDataReader.IsDBNull(29)) fundamental.Source = sqlDataReader.GetString(29);
|
||||
if (!double.IsNaN(totalStockHolderEquity) && !double.IsNaN(fundamental.TotalDebt))
|
||||
{
|
||||
if(0.00==totalStockHolderEquity)fundamental.TotalDebt=0.00;
|
||||
else fundamental.DebtToEquity=fundamental.TotalDebt/totalStockHolderEquity;
|
||||
}
|
||||
return fundamental;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public static Fundamentals GetFundamentalMaxDateTop(String symbol, DateTime asof, int top)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
Fundamentals fundamentals = new Fundamentals();
|
||||
|
||||
try
|
||||
{
|
||||
DateTime? maxDate=GetMaxDateFromFundamental(symbol,asof); // get the maximum date on record on or before max date
|
||||
if(null==maxDate)return null;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,next_earnings_date,beta,low52,high52,volume,market_cap,pe,eps,peg,return_on_assets,return_on_equity,total_cash,total_debt,shares_outstanding,revenue,revenue_per_share,qtrly_revenue_growth,gross_profit,ebitda,net_income_available_to_common,book_value_per_share,operating_cashflow,leveraged_free_cashflow,book_value_per_share*shares_outstanding as equity,trailing_pe,ebit,enterprise_value,source from fundamentals where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and asof<=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(maxDate.Value)).Append("'");
|
||||
sb.Append(" order by asof desc ");
|
||||
sb.Append(" limit ").Append(top);
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
|
||||
double totalStockHolderEquity=BalanceSheetDA.GetTotalStockHolderEquityOnOrBefore(symbol,asof,BalanceSheet.PeriodType.Annual);
|
||||
Fundamental fundamental = new Fundamental();
|
||||
fundamental.Symbol = sqlDataReader.GetString(0);
|
||||
fundamental.AsOf = sqlDataReader.GetDateTime(1);
|
||||
if (!sqlDataReader.IsDBNull(2)) fundamental.NextEarningsDate = sqlDataReader.GetDateTime(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) fundamental.Beta = sqlDataReader.GetDouble(3);
|
||||
if (!sqlDataReader.IsDBNull(4)) fundamental.Low52 = sqlDataReader.GetDouble(4);
|
||||
if (!sqlDataReader.IsDBNull(5)) fundamental.High52 = sqlDataReader.GetDouble(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) fundamental.Volume = sqlDataReader.GetInt64(6);
|
||||
if (!sqlDataReader.IsDBNull(7)) fundamental.MarketCap = sqlDataReader.GetDouble(7);
|
||||
if (!sqlDataReader.IsDBNull(8)) fundamental.PE = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) fundamental.EPS = sqlDataReader.GetDouble(9);
|
||||
if (!sqlDataReader.IsDBNull(10)) fundamental.PEG = sqlDataReader.GetDouble(10);
|
||||
if (!sqlDataReader.IsDBNull(11)) fundamental.ReturnOnAssets = sqlDataReader.GetDouble(11);
|
||||
if (!sqlDataReader.IsDBNull(12)) fundamental.ReturnOnEquity = sqlDataReader.GetDouble(12);
|
||||
if (!sqlDataReader.IsDBNull(13)) fundamental.TotalCash = sqlDataReader.GetDouble(13);
|
||||
if (!sqlDataReader.IsDBNull(14)) fundamental.TotalDebt = sqlDataReader.GetDouble(14);
|
||||
if (!sqlDataReader.IsDBNull(15)) fundamental.SharesOutstanding = sqlDataReader.GetDouble(15);
|
||||
if (!sqlDataReader.IsDBNull(16)) fundamental.Revenue = sqlDataReader.GetDouble(16);
|
||||
if (!sqlDataReader.IsDBNull(17)) fundamental.RevenuePerShare = sqlDataReader.GetDouble(17);
|
||||
if (!sqlDataReader.IsDBNull(18)) fundamental.QtrlyRevenueGrowth = sqlDataReader.GetDouble(18);
|
||||
if (!sqlDataReader.IsDBNull(19)) fundamental.GrossProfit = sqlDataReader.GetDouble(19);
|
||||
if (!sqlDataReader.IsDBNull(20)) fundamental.EBITDA = sqlDataReader.GetDouble(20);
|
||||
if (!sqlDataReader.IsDBNull(21)) fundamental.NetIncomeAvailableToCommon = sqlDataReader.GetDouble(21);
|
||||
if (!sqlDataReader.IsDBNull(22)) fundamental.BookValuePerShare = sqlDataReader.GetDouble(22);
|
||||
if (!sqlDataReader.IsDBNull(23)) fundamental.OperatingCashflow = sqlDataReader.GetDouble(23);
|
||||
if (!sqlDataReader.IsDBNull(24)) fundamental.LeveragedFreeCashflow = sqlDataReader.GetDouble(24);
|
||||
if (!sqlDataReader.IsDBNull(25)) fundamental.Equity = sqlDataReader.GetDouble(25);
|
||||
if (!sqlDataReader.IsDBNull(26)) fundamental.TrailingPE = sqlDataReader.GetDouble(26);
|
||||
if (!sqlDataReader.IsDBNull(27)) fundamental.EBIT = sqlDataReader.GetDouble(27);
|
||||
if (!sqlDataReader.IsDBNull(28)) fundamental.EnterpriseValue = sqlDataReader.GetDouble(28);
|
||||
if (!sqlDataReader.IsDBNull(29)) fundamental.Source = sqlDataReader.GetString(29);
|
||||
if (!double.IsNaN(totalStockHolderEquity) && !double.IsNaN(fundamental.TotalDebt))
|
||||
{
|
||||
if(0.00==totalStockHolderEquity)fundamental.TotalDebt=0.00;
|
||||
else fundamental.DebtToEquity=fundamental.TotalDebt/totalStockHolderEquity;
|
||||
}
|
||||
fundamentals.Add(fundamental);
|
||||
}
|
||||
return fundamentals;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public static bool InsertFundamentals(Fundamentals fundamentals)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
DeleteFundamentals(fundamentals, sqlConnection, sqlTransaction);
|
||||
for (int index = 0; index < fundamentals.Count; index++)
|
||||
{
|
||||
Fundamental fundamental = fundamentals[index];
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into fundamentals (symbol,asof,next_earnings_date,beta,low52,high52,volume,market_cap,pe,eps,peg,return_on_assets,return_on_equity,total_cash,total_debt,shares_outstanding,revenue,revenue_per_share,qtrly_revenue_growth,gross_profit,ebitda,net_income_available_to_common,book_value_per_share,operating_cashflow,leveraged_free_cashflow,trailing_pe,ebit,enterprise_value,source) ");
|
||||
sb.Append("values(");
|
||||
sb.Append("'").Append(fundamental.Symbol).Append("'").Append(",");
|
||||
sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(fundamental.AsOf)).Append("'").Append(",");
|
||||
if (Utility.IsEpoch(fundamental.NextEarningsDate)) sb.Append("null").Append(",");
|
||||
else sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(fundamental.NextEarningsDate)).Append("'").Append(",");
|
||||
if (!Double.IsNaN(fundamental.Beta)) sb.Append(fundamental.Beta).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.Low52)) sb.Append(fundamental.Low52).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.High52)) sb.Append(fundamental.High52).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
sb.Append(fundamental.Volume).Append(",");
|
||||
if (!Double.IsNaN(fundamental.MarketCap)) sb.Append(fundamental.MarketCap).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.PE)) sb.Append(fundamental.PE).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.EPS)) sb.Append(fundamental.EPS).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.PEG)) sb.Append(fundamental.PEG).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.ReturnOnAssets))sb.Append(fundamental.ReturnOnAssets).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.ReturnOnEquity)) sb.Append(fundamental.ReturnOnEquity).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.TotalCash)) sb.Append(fundamental.TotalCash).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.TotalDebt)) sb.Append(fundamental.TotalDebt).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.SharesOutstanding)) sb.Append(fundamental.SharesOutstanding).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.Revenue)) sb.Append(fundamental.Revenue).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.RevenuePerShare)) sb.Append(fundamental.RevenuePerShare).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.QtrlyRevenueGrowth)) sb.Append(fundamental.QtrlyRevenueGrowth).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.GrossProfit)) sb.Append(fundamental.GrossProfit).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.EBITDA)) sb.Append(fundamental.EBITDA).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.NetIncomeAvailableToCommon)) sb.Append(fundamental.NetIncomeAvailableToCommon).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.BookValuePerShare)) sb.Append(fundamental.BookValuePerShare).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.OperatingCashflow)) sb.Append(fundamental.OperatingCashflow).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.LeveragedFreeCashflow)) sb.Append(fundamental.LeveragedFreeCashflow).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.TrailingPE)) sb.Append(fundamental.TrailingPE).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.EBIT)) sb.Append(fundamental.EBIT).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.EnterpriseValue)) sb.Append(fundamental.EnterpriseValue).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (null != fundamental.Source) sb.Append(SqlUtils.AddQuotes(fundamental.Source));
|
||||
else sb.Append("null");
|
||||
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)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was '"+strQuery+"'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public static bool InsertFundamental(Fundamental fundamental)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
DeleteFundamental(fundamental, sqlConnection, sqlTransaction);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into fundamentals (symbol,asof,next_earnings_date,beta,low52,high52,volume,market_cap,pe,eps,peg,return_on_assets,return_on_equity,total_cash,total_debt,shares_outstanding,revenue,revenue_per_share,qtrly_revenue_growth,gross_profit,ebitda,net_income_available_to_common,book_value_per_share,operating_cashflow,leveraged_free_cashflow,trailing_pe,ebit,enterprise_value,source) ");
|
||||
sb.Append("values(");
|
||||
sb.Append("'").Append(fundamental.Symbol).Append("'").Append(",");
|
||||
sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(fundamental.AsOf)).Append("'").Append(",");
|
||||
if (Utility.IsEpoch(fundamental.NextEarningsDate)) sb.Append("null").Append(",");
|
||||
else sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(fundamental.NextEarningsDate)).Append("'").Append(",");
|
||||
if (!Double.IsNaN(fundamental.Beta)) sb.Append(fundamental.Beta).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.Low52)) sb.Append(fundamental.Low52).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.High52)) sb.Append(fundamental.High52).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
sb.Append(fundamental.Volume).Append(",");
|
||||
if (!Double.IsNaN(fundamental.MarketCap)) sb.Append(fundamental.MarketCap).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.PE)) sb.Append(fundamental.PE).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.EPS)) sb.Append(fundamental.EPS).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.PEG)) sb.Append(fundamental.PEG).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.ReturnOnAssets))sb.Append(fundamental.ReturnOnAssets).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.ReturnOnEquity)) sb.Append(fundamental.ReturnOnEquity).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.TotalCash)) sb.Append(fundamental.TotalCash).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.TotalDebt)) sb.Append(fundamental.TotalDebt).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.SharesOutstanding)) sb.Append(fundamental.SharesOutstanding).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.Revenue)) sb.Append(fundamental.Revenue).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.RevenuePerShare)) sb.Append(fundamental.RevenuePerShare).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.QtrlyRevenueGrowth)) sb.Append(fundamental.QtrlyRevenueGrowth).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.GrossProfit)) sb.Append(fundamental.GrossProfit).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.EBITDA)) sb.Append(fundamental.EBITDA).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.NetIncomeAvailableToCommon)) sb.Append(fundamental.NetIncomeAvailableToCommon).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.BookValuePerShare)) sb.Append(fundamental.BookValuePerShare).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.OperatingCashflow)) sb.Append(fundamental.OperatingCashflow).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.LeveragedFreeCashflow)) sb.Append(fundamental.LeveragedFreeCashflow).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.TrailingPE)) sb.Append(fundamental.TrailingPE).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.EBIT)) sb.Append(fundamental.EBIT).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(fundamental.EnterpriseValue)) sb.Append(fundamental.EnterpriseValue).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (null != fundamental.Source) sb.Append(SqlUtils.AddQuotes(fundamental.Source));
|
||||
else sb.Append("null");
|
||||
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)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was '"+strQuery+"'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static bool DeleteFundamentals(Fundamentals fundamentals, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
for (int index = 0; index < fundamentals.Count; index++)
|
||||
{
|
||||
DeleteFundamental(fundamentals[index], sqlConnection, sqlTransaction);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private static bool DeleteFundamental(Fundamental fundamental, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sb.Append("delete from fundamentals where ");
|
||||
sb.Append("symbol='").Append(fundamental.Symbol).Append("'");
|
||||
sb.Append(" and ");
|
||||
sb.Append("asof='").Append(Utility.DateTimeToStringYYYYHMMHDD(fundamental.AsOf)).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
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
434
MarketData/MarketDataLib/DataAccess/HeadlinesDA.cs
Executable file
434
MarketData/MarketDataLib/DataAccess/HeadlinesDA.cs
Executable file
@@ -0,0 +1,434 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class HeadlinesDA
|
||||
{
|
||||
private HeadlinesDA()
|
||||
{
|
||||
}
|
||||
public static DateTime GetMaxHeadlineDate()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
String strQuery = null;
|
||||
DateTime maxDate = Utility.Epoch;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select max(asof) from headlines");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (sqlDataReader.Read())
|
||||
{
|
||||
maxDate=sqlDataReader.GetDateTime(0);
|
||||
}
|
||||
return maxDate;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return Utility.Epoch;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static List<String> GetHeadlineDates()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
List<String> headlineDates=new List<String>();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select distinct asof from headlines order by 1 desc");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
headlineDates.Add(sqlDataReader.GetDateTime(0).ToShortDateString());
|
||||
}
|
||||
return headlineDates;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return headlineDates;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Headlines GetHeadlines(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
Headlines headlines=new Headlines();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select h.symbol, h.asof, h.headline, h.source, h.modified,sm.company from headlines h left outer join securitymaster sm on h.symbol=sm.symbol where h.symbol=").Append(SqlUtils.AddQuotes(symbol));
|
||||
sb.Append(" order by h.asof desc, h.modified desc");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
Headline headline=new Headline();
|
||||
headline.Symbol=symbol;
|
||||
headline.Date=sqlDataReader.GetDateTime(1);
|
||||
headline.Entry=sqlDataReader.GetString(2);
|
||||
headline.Source=sqlDataReader.GetString(3);
|
||||
headline.Modified=sqlDataReader.GetDateTime(4);
|
||||
headline.CompanyName=sqlDataReader.GetString(5);
|
||||
headlines.Add(headline);
|
||||
}
|
||||
return headlines;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return headlines;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Headlines GetHeadlines()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
Headlines headlines=new Headlines();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select h.symbol, h.asof, h.headline, h.source, h.modified, sm.company from headlines h left outer join securitymaster sm on h.symbol=sm.symbol");
|
||||
sb.Append(" order by h.asof desc,h.symbol,h.modified desc;");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
Headline headline=new Headline();
|
||||
headline.Symbol=sqlDataReader.GetString(0);
|
||||
headline.Date=sqlDataReader.GetDateTime(1);
|
||||
headline.Entry=sqlDataReader.GetString(2);
|
||||
headline.Source=sqlDataReader.GetString(3);
|
||||
headline.Modified=sqlDataReader.GetDateTime(4);
|
||||
headline.CompanyName=sqlDataReader.GetString(5);
|
||||
headlines.Add(headline);
|
||||
}
|
||||
return headlines;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return headlines;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
// This was authored for mobile app. It wants the sorting to match the WPF app.
|
||||
public static Headlines GetLatestHeadlines()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
String strQuery = null;
|
||||
Headlines headlines = new Headlines();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select h.symbol, h.asof, h.headline, h.source, h.modified, sm.company from headlines h left outer join securitymaster sm on h.symbol=sm.symbol");
|
||||
sb.Append(" where h.asof=(select max(asof) from headlines)");
|
||||
sb.Append(" order by h.asof desc,h.modified desc,h.symbol desc;");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
Headline headline = new Headline();
|
||||
headline.Symbol = sqlDataReader.GetString(0);
|
||||
headline.Date = sqlDataReader.GetDateTime(1);
|
||||
headline.Entry = sqlDataReader.GetString(2);
|
||||
headline.Source = sqlDataReader.GetString(3);
|
||||
headline.Modified = sqlDataReader.GetDateTime(4);
|
||||
headline.CompanyName = sqlDataReader.GetString(5);
|
||||
headlines.Add(headline);
|
||||
}
|
||||
return headlines;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return headlines;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Headlines GetHeadlines(String symbol,DateTime dateTime)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
Headlines headlines=new Headlines();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select h.symbol, h.asof, h.headline, h.source, h.modified, sm.company from headlines h left outer join securitymaster sm on h.symbol=sm.symbol where h.asof=").Append(SqlUtils.AddQuotes(SqlUtils.SqlDate(dateTime)));
|
||||
sb.Append(" and h.symbol=").Append(SqlUtils.AddQuotes(symbol));
|
||||
sb.Append("order by h.asof desc, h.modified desc");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
Headline headline=new Headline();
|
||||
headline.Symbol=sqlDataReader.GetString(0);
|
||||
headline.Date=sqlDataReader.GetDateTime(1);
|
||||
headline.Entry=sqlDataReader.GetString(2);
|
||||
headline.Source=sqlDataReader.GetString(3);
|
||||
headline.Modified=sqlDataReader.GetDateTime(4);
|
||||
headline.CompanyName=sqlDataReader.GetString(5);
|
||||
headlines.Add(headline);
|
||||
}
|
||||
return headlines;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return headlines;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Headlines GetHeadlines(DateTime dateTime)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
Headlines headlines=new Headlines();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select h.symbol, h.asof, h.headline, h.source, h.modified, sm.company from headlines h left outer join securitymaster sm on h.symbol=sm.symbol where h.asof=").Append(SqlUtils.AddQuotes(SqlUtils.SqlDate(dateTime)));
|
||||
sb.Append("order by h.modified desc,h.symbol");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
Headline headline=new Headline();
|
||||
headline.Symbol=sqlDataReader.GetString(0);
|
||||
headline.Date=sqlDataReader.GetDateTime(1);
|
||||
headline.Entry=sqlDataReader.GetString(2);
|
||||
headline.Source=sqlDataReader.GetString(3);
|
||||
headline.Modified=sqlDataReader.GetDateTime(4);
|
||||
headline.CompanyName=sqlDataReader.GetString(5);
|
||||
headlines.Add(headline);
|
||||
}
|
||||
return headlines;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return headlines;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool InsertHeadlines(Headlines headlines)
|
||||
{
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlTransaction sqlTransaction=null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == headlines || 0 == headlines.Count) return false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
headlines=new Headlines(headlines.Distinct(new HeadlinesEqualityComparer()).ToList());
|
||||
foreach(Headline headline in headlines)
|
||||
{
|
||||
if(!HeadlineExists(headline,sqlConnection,sqlTransaction))InsertHeadline(headline,sqlConnection,sqlTransaction);
|
||||
}
|
||||
sqlTransaction.Commit();
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static bool InsertHeadline(Headline headline,MySqlConnection sqlConnection,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == headline || null == headline.Symbol || null==headline.Entry) return false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into Headlines(symbol,asof,headline,source,modified) values(");
|
||||
sb.Append(SqlUtils.AddQuotes(headline.Symbol)).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(SqlUtils.SqlDate(headline.Date))).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(SqlUtils.SqlString(headline.Entry))).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(SqlUtils.SqlString(headline.Source))).Append(",");
|
||||
if(Utility.IsEpoch(headline.Modified))sb.Append(SqlUtils.AddQuotes(SqlUtils.ToSqlDateTime(DateTime.Now)));
|
||||
else sb.Append(SqlUtils.AddQuotes(SqlUtils.ToSqlDateTime(headline.Modified)));
|
||||
sb.Append(")");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
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();
|
||||
}
|
||||
}
|
||||
private static bool HeadlineExists(Headline headline,MySqlConnection sqlConnection,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == headline || null == headline.Symbol) return false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select count(*) from headlines where ");
|
||||
sb.Append(" asof=").Append(SqlUtils.AddQuotes(SqlUtils.SqlDate(headline.Date))).Append(" and ");
|
||||
sb.Append(" headline=").Append(SqlUtils.AddQuotes(SqlUtils.SqlString(headline.Entry)));
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
if(!sqlDataReader.Read())return false;
|
||||
return 0==sqlDataReader.GetInt32(0)?false:true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader){sqlDataReader.Close();sqlDataReader.Dispose();}
|
||||
}
|
||||
}
|
||||
//private static bool HeadlineExists(Headline headline,MySqlConnection sqlConnection,MySqlTransaction sqlTransaction)
|
||||
//{
|
||||
// MySqlDataReader sqlDataReader=null;
|
||||
// MySqlCommand sqlCommand=null;
|
||||
// String strQuery = null;
|
||||
|
||||
// try
|
||||
// {
|
||||
// if (null == headline || null == headline.Symbol) return false;
|
||||
// StringBuilder sb = new StringBuilder();
|
||||
// sb.Append("select count(*) from headlines where symbol=").Append(SqlUtils.AddQuotes(headline.Symbol)).Append(" and ");
|
||||
// sb.Append(" asof=").Append(SqlUtils.AddQuotes(SqlUtils.SqlDate(headline.Date))).Append(" and ");
|
||||
// sb.Append(" headline=").Append(SqlUtils.AddQuotes(SqlUtils.SqlString(headline.Entry)));
|
||||
// strQuery = sb.ToString();
|
||||
// sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
// sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
// sqlDataReader=sqlCommand.ExecuteReader();
|
||||
// if(!sqlDataReader.Read())return false;
|
||||
// return 0==sqlDataReader.GetInt32(0)?false:true;
|
||||
// }
|
||||
// catch (Exception exception)
|
||||
// {
|
||||
// MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
// return false;
|
||||
// }
|
||||
// finally
|
||||
// {
|
||||
// if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
// if(null!=sqlDataReader){sqlDataReader.Close();sqlDataReader.Dispose();}
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
191
MarketData/MarketDataLib/DataAccess/HistoricalDA.cs
Executable file
191
MarketData/MarketDataLib/DataAccess/HistoricalDA.cs
Executable file
@@ -0,0 +1,191 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class HistoricalDA
|
||||
{
|
||||
private HistoricalDA()
|
||||
{
|
||||
}
|
||||
public static TimeSeriesCollection GetTimeSeriesMaxAsOf(String symbol,DateTime asOf,TimeSeriesElement.ElementType elementType)
|
||||
{
|
||||
TimeSeriesCollection timeSeriesCollection =new TimeSeriesCollection();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,value,modified from historical where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'");
|
||||
sb.Append(" and type='").Append(TimeSeriesElement.StringForType(elementType)).Append("'");
|
||||
sb.Append(" and asof<='").Append(SqlUtils.FormatDate(asOf)).Append("'");
|
||||
sb.Append(" order by asof desc");
|
||||
sb.Append(";");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
TimeSeriesElement timeSeriesElement = new TimeSeriesElement();
|
||||
timeSeriesElement.Symbol = sqlDataReader.GetString(0);
|
||||
timeSeriesElement.AsOf = sqlDataReader.GetDateTime(1);
|
||||
timeSeriesElement.Type = elementType;
|
||||
if (!sqlDataReader.IsDBNull(2)) timeSeriesElement.Value = sqlDataReader.GetDouble(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) timeSeriesElement.Modified = sqlDataReader.GetDateTime(3);
|
||||
else timeSeriesElement.Modified=Utility.Epoch;
|
||||
timeSeriesCollection.Add(timeSeriesElement);
|
||||
}
|
||||
return timeSeriesCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static TimeSeriesCollection GetTimeSeries(String symbol,TimeSeriesElement.ElementType elementType)
|
||||
{
|
||||
TimeSeriesCollection timeSeriesCollection =new TimeSeriesCollection();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,value,modified from historical where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'");
|
||||
sb.Append(" and type='").Append(TimeSeriesElement.StringForType(elementType)).Append("'").Append(" order by asof desc");
|
||||
sb.Append(";");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
TimeSeriesElement timeSeriesElement = new TimeSeriesElement();
|
||||
timeSeriesElement.Symbol = sqlDataReader.GetString(0);
|
||||
timeSeriesElement.AsOf = sqlDataReader.GetDateTime(1);
|
||||
timeSeriesElement.Type = elementType;
|
||||
if (!sqlDataReader.IsDBNull(2)) timeSeriesElement.Value = sqlDataReader.GetDouble(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) timeSeriesElement.Modified = sqlDataReader.GetDateTime(3);
|
||||
else timeSeriesElement.Modified=Utility.Epoch;
|
||||
timeSeriesCollection.Add(timeSeriesElement);
|
||||
}
|
||||
return timeSeriesCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool InsertTimeSeries(TimeSeriesCollection timeSeriesCollection)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
String strQuery = null;
|
||||
DateTime modified=DateTime.Now;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
DeleteTimeSeries(timeSeriesCollection, sqlConnection, sqlTransaction);
|
||||
for (int index = 0; index < timeSeriesCollection.Count; index++)
|
||||
{
|
||||
TimeSeriesElement timeSeriesElement = timeSeriesCollection[index];
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into historical (symbol,asof,type,value,modified) ");
|
||||
sb.Append("values(");
|
||||
sb.Append("'").Append(timeSeriesElement.Symbol).Append("'").Append(",");
|
||||
sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(timeSeriesElement.AsOf)).Append("'").Append(",");
|
||||
sb.Append("'").Append(TimeSeriesElement.StringForType(timeSeriesElement.Type)).Append("'").Append(",");
|
||||
sb.Append(timeSeriesElement.Value).Append(",");
|
||||
sb.Append("'").Append(SqlUtils.ToSqlDateTime(modified)).Append("'");
|
||||
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)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was '"+strQuery+"'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static bool DeleteTimeSeries(TimeSeriesCollection timeSeriesCollection, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
for (int index = 0; index < timeSeriesCollection.Count; index++)
|
||||
{
|
||||
DeleteTimeSeries(timeSeriesCollection[index], sqlConnection, sqlTransaction);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private static bool DeleteTimeSeries(TimeSeriesElement timeSeriesElement, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sb.Append("delete from historical where ");
|
||||
sb.Append("symbol='").Append(timeSeriesElement.Symbol).Append("'");
|
||||
sb.Append(" and ");
|
||||
sb.Append("asof='").Append(Utility.DateTimeToStringYYYYHMMHDD(timeSeriesElement.AsOf)).Append("'");
|
||||
sb.Append(" and type='").Append(TimeSeriesElement.StringForType(timeSeriesElement.Type)).Append("'");
|
||||
strQuery = sb.ToString();
|
||||
MySqlCommand sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
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
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
159
MarketData/MarketDataLib/DataAccess/HolidayDA.cs
Executable file
159
MarketData/MarketDataLib/DataAccess/HolidayDA.cs
Executable file
@@ -0,0 +1,159 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class HolidayDA
|
||||
{
|
||||
private HolidayDA()
|
||||
{
|
||||
}
|
||||
public static String GetHolidayDescription(DateTime date)
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery=null;
|
||||
String description="";
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select description from MarketHolidays where date='").Append(Utils.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)) description=sqlDataReader.GetString(0);
|
||||
}
|
||||
return description;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return description;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader) sqlDataReader.Close();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DateTime GetMaxHolidayDate()
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery=null;
|
||||
DateTime maxHolidayDate=Utility.Epoch;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select max(date) from MarketHolidays");
|
||||
strQuery=sb.ToString();
|
||||
sqlCommand=new MySqlCommand(strQuery,sqlConnection);
|
||||
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
if(sqlDataReader.Read())
|
||||
{
|
||||
if(!sqlDataReader.IsDBNull(0)) maxHolidayDate=sqlDataReader.GetDateTime(0);
|
||||
}
|
||||
return maxHolidayDate;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return maxHolidayDate;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader) sqlDataReader.Close();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsMarketHoliday(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 MarketHolidays 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 0==recordCount?false:true;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader) sqlDataReader.Close();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Dictionary<DateTime,DateTime> GetHolidays()
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery=null;
|
||||
Dictionary<DateTime,DateTime> holidays=new Dictionary<DateTime,DateTime>();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select date from MarketHolidays order by date desc");
|
||||
strQuery=sb.ToString();
|
||||
sqlCommand=new MySqlCommand(strQuery,sqlConnection);
|
||||
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
DateTime holiday=sqlDataReader.GetDateTime(0);
|
||||
if(!holidays.ContainsKey(holiday))holidays.Add(holiday,holiday);
|
||||
}
|
||||
return holidays;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader) sqlDataReader.Close();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
522
MarketData/MarketDataLib/DataAccess/IncomeStatementDA.cs
Executable file
522
MarketData/MarketDataLib/DataAccess/IncomeStatementDA.cs
Executable file
@@ -0,0 +1,522 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class IncomeStatementDA
|
||||
{
|
||||
// ********************************************* I N C O M E S T A T E M E N T **************************************
|
||||
public static bool CheckIncomeStatementModifiedOn(String symbol, DateTime modified,IncomeStatement.PeriodType periodType)
|
||||
{
|
||||
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 count(*) from incomestatement");
|
||||
sb.Append(" where symbol='").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append(" and period=").Append(periodType.Equals(IncomeStatement.PeriodType.Annual)?0:1).Append("").Append(" ");
|
||||
sb.Append("and modified='").Append(Utility.DateTimeToStringYYYYHMMHDD(modified)).Append("'");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
sqlDataReader.Read();
|
||||
int recordCount = sqlDataReader.GetInt32(0);
|
||||
return 0 == recordCount ? false : true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static List<DateTime> GetIncomeStatementDates(String symbol,IncomeStatement.PeriodType periodType)
|
||||
{
|
||||
List<DateTime> incomeStatementDates = new List<DateTime>();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select asof from incomestatement where symbol='").Append(symbol).Append("' ");
|
||||
sb.Append("and period='").Append(periodType.Equals(IncomeStatement.PeriodType.Annual)?0:1).Append("'");
|
||||
sb.Append(" order by 1 asc;");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
DateTime asof = sqlDataReader.GetDateTime(0);
|
||||
incomeStatementDates.Add(asof);
|
||||
}
|
||||
return incomeStatementDates;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return incomeStatementDates;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DateTime? GetLatestIncomeStatementDate(String symbol,IncomeStatement.PeriodType periodType)
|
||||
{
|
||||
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(asof) from incomestatement");
|
||||
sb.Append(" where symbol='").Append(symbol).Append("' ");
|
||||
sb.Append(" and period=").Append(periodType.Equals(IncomeStatement.PeriodType.Annual)?0:1);
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (sqlDataReader.Read())
|
||||
{
|
||||
if (sqlDataReader.IsDBNull(0)) maxDate = null;
|
||||
else maxDate = sqlDataReader.GetDateTime(0);
|
||||
}
|
||||
return maxDate;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return maxDate;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static IncomeStatement GetIncomeStatement(String symbol,IncomeStatement.PeriodType periodType)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime? latestDate = GetLatestIncomeStatementDate(symbol,periodType);
|
||||
if(null==latestDate)return null;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,total_revenue,cost_of_revenue,income_tax_expense,gross_profit,net_income,sga,net_income_applicable_to_common_shares,ebit,operating_expenses,research_and_development,interest_expense,period,modified from incomestatement where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and period=").Append(periodType.Equals(IncomeStatement.PeriodType.Annual)?0:1).Append(" ");
|
||||
sb.Append("and asof=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(latestDate.Value)).Append("'").Append(";");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
IncomeStatement incomeStatement = new IncomeStatement();
|
||||
incomeStatement.Symbol = sqlDataReader.GetString(0);
|
||||
incomeStatement.AsOf = sqlDataReader.GetDateTime(1);
|
||||
if (!sqlDataReader.IsDBNull(2)) incomeStatement.TotalRevenue = sqlDataReader.GetDouble(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) incomeStatement.CostOfRevenue = sqlDataReader.GetDouble(3);
|
||||
if (!sqlDataReader.IsDBNull(4)) incomeStatement.IncomeTaxExpense = sqlDataReader.GetDouble(4);
|
||||
if (!sqlDataReader.IsDBNull(5)) incomeStatement.GrossProfit = sqlDataReader.GetDouble(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) incomeStatement.NetIncome = sqlDataReader.GetDouble(6);
|
||||
if (!sqlDataReader.IsDBNull(7)) incomeStatement.SGA = sqlDataReader.GetDouble(7);
|
||||
if (!sqlDataReader.IsDBNull(8)) incomeStatement.NetIncomeApplicableToCommonShares = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) incomeStatement.EBIT = sqlDataReader.GetDouble(9);
|
||||
if (!sqlDataReader.IsDBNull(10)) incomeStatement.OperatingExpenses = sqlDataReader.GetDouble(10);
|
||||
if (!sqlDataReader.IsDBNull(11)) incomeStatement.ResearchAndDevelopment = sqlDataReader.GetDouble(11);
|
||||
if (!sqlDataReader.IsDBNull(12)) incomeStatement.InterestExpense = sqlDataReader.GetDouble(12);
|
||||
if (!sqlDataReader.IsDBNull(13)) incomeStatement.Period = sqlDataReader.GetInt32(13)==0?IncomeStatement.PeriodType.Annual:IncomeStatement.PeriodType.Quarterly;
|
||||
if (!sqlDataReader.IsDBNull(14)) incomeStatement.Modified = sqlDataReader.GetDateTime(14);
|
||||
return incomeStatement;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static TimeSeriesCollection GetRevenue(String symbol,IncomeStatement.PeriodType period)
|
||||
{
|
||||
TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,total_revenue from incomestatement where symbol='").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append(" and period=").Append(period.Equals(IncomeStatement.PeriodType.Annual)?0:1).Append(" order by asof desc;");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
TimeSeriesElement timeSeriesElement = new TimeSeriesElement();
|
||||
timeSeriesElement.Symbol = sqlDataReader.GetString(0);
|
||||
timeSeriesElement.AsOf = sqlDataReader.GetDateTime(1);
|
||||
timeSeriesElement.Type = period.Equals(IncomeStatement.PeriodType.Quarterly)?TimeSeriesElement.ElementType.QuarterlyRevenue:TimeSeriesElement.ElementType.Revenue;
|
||||
if (!sqlDataReader.IsDBNull(2)) timeSeriesElement.Value = sqlDataReader.GetDouble(2);
|
||||
else continue;
|
||||
timeSeriesCollection.Add(timeSeriesElement);
|
||||
}
|
||||
return timeSeriesCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
// Profit Margin is calculated as a percentage.
|
||||
public static TimeSeriesCollection GetProfitMargin(String symbol,IncomeStatement.PeriodType period=IncomeStatement.PeriodType.Annual)
|
||||
{
|
||||
TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,total_revenue,gross_profit from incomestatement where symbol='").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append(" and period=").Append(period.Equals(IncomeStatement.PeriodType.Annual)?0:1).Append(" order by asof desc;");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
TimeSeriesElement timeSeriesElement = new TimeSeriesElement();
|
||||
timeSeriesElement.Symbol = sqlDataReader.GetString(0);
|
||||
timeSeriesElement.AsOf = sqlDataReader.GetDateTime(1);
|
||||
timeSeriesElement.Type = period.Equals(IncomeStatement.PeriodType.Quarterly)?TimeSeriesElement.ElementType.QuarterlyRevenue:TimeSeriesElement.ElementType.Revenue;
|
||||
|
||||
if(sqlDataReader.IsDBNull(2))continue;
|
||||
double total_revenue=sqlDataReader.GetDouble(2);
|
||||
if(sqlDataReader.IsDBNull(3))continue;
|
||||
double gross_profit=sqlDataReader.GetDouble(3);
|
||||
if(0==gross_profit)continue;
|
||||
timeSeriesElement.Value=(gross_profit/total_revenue)*100.00;
|
||||
timeSeriesCollection.Add(timeSeriesElement);
|
||||
}
|
||||
return timeSeriesCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
// Profit Margin is calculated as a percentage.
|
||||
public static TimeSeriesCollection GetProfitMarginMaxAsOf(String symbol,DateTime maxDate, IncomeStatement.PeriodType period = IncomeStatement.PeriodType.Annual)
|
||||
{
|
||||
TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,total_revenue,gross_profit from incomestatement where symbol='").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append(" and asof<='").Append(SqlUtils.SqlDate(maxDate)).Append("' ");
|
||||
sb.Append(" and period=").Append(period.Equals(IncomeStatement.PeriodType.Annual) ? 0 : 1).Append(" order by asof desc;");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
TimeSeriesElement timeSeriesElement = new TimeSeriesElement();
|
||||
timeSeriesElement.Symbol = sqlDataReader.GetString(0);
|
||||
timeSeriesElement.AsOf = sqlDataReader.GetDateTime(1);
|
||||
timeSeriesElement.Type = period.Equals(IncomeStatement.PeriodType.Quarterly) ? TimeSeriesElement.ElementType.QuarterlyRevenue : TimeSeriesElement.ElementType.Revenue;
|
||||
|
||||
if (sqlDataReader.IsDBNull(2)) continue;
|
||||
double total_revenue = sqlDataReader.GetDouble(2);
|
||||
if (sqlDataReader.IsDBNull(3)) continue;
|
||||
double gross_profit = sqlDataReader.GetDouble(3);
|
||||
if (0 == gross_profit) continue;
|
||||
timeSeriesElement.Value = (gross_profit / total_revenue) * 100.00;
|
||||
timeSeriesCollection.Add(timeSeriesElement);
|
||||
}
|
||||
return timeSeriesCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static IncomeStatement GetIncomeStatement(String symbol,DateTime asof,IncomeStatement.PeriodType periodType)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,total_revenue,cost_of_revenue,income_tax_expense,gross_profit,net_income,net_income_applicable_to_common_shares,sga,ebit,operating_expenses,research_and_development,interest_expense,period,modified from incomestatement where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and period=").Append(periodType.Equals(IncomeStatement.PeriodType.Annual) ? 0 : 1).Append(" ");
|
||||
sb.Append("and asof=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(asof)).Append("'").Append(";");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
IncomeStatement incomeStatement = new IncomeStatement();
|
||||
incomeStatement.Symbol = sqlDataReader.GetString(0);
|
||||
incomeStatement.AsOf = sqlDataReader.GetDateTime(1);
|
||||
if (!sqlDataReader.IsDBNull(2)) incomeStatement.TotalRevenue = sqlDataReader.GetDouble(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) incomeStatement.CostOfRevenue = sqlDataReader.GetDouble(3);
|
||||
if (!sqlDataReader.IsDBNull(4)) incomeStatement.IncomeTaxExpense = sqlDataReader.GetDouble(4);
|
||||
if (!sqlDataReader.IsDBNull(5)) incomeStatement.GrossProfit = sqlDataReader.GetDouble(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) incomeStatement.NetIncome = sqlDataReader.GetDouble(6);
|
||||
if (!sqlDataReader.IsDBNull(7)) incomeStatement.SGA = sqlDataReader.GetDouble(7);
|
||||
if (!sqlDataReader.IsDBNull(8)) incomeStatement.NetIncomeApplicableToCommonShares = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) incomeStatement.EBIT = sqlDataReader.GetDouble(9);
|
||||
if (!sqlDataReader.IsDBNull(10)) incomeStatement.OperatingExpenses = sqlDataReader.GetDouble(10);
|
||||
if (!sqlDataReader.IsDBNull(11)) incomeStatement.ResearchAndDevelopment = sqlDataReader.GetDouble(11);
|
||||
if (!sqlDataReader.IsDBNull(12)) incomeStatement.InterestExpense = sqlDataReader.GetDouble(12);
|
||||
if (!sqlDataReader.IsDBNull(13)) incomeStatement.Period = 0 == sqlDataReader.GetInt32(13) ? IncomeStatement.PeriodType.Annual : IncomeStatement.PeriodType.Quarterly;
|
||||
if (!sqlDataReader.IsDBNull(14)) incomeStatement.Modified = sqlDataReader.GetDateTime(14);
|
||||
return incomeStatement;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static IncomeStatement GetIncomeStatementMaxAsOf(String symbol,DateTime asof,IncomeStatement.PeriodType periodType)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,asof,total_revenue,cost_of_revenue,income_tax_expense,gross_profit,net_income,net_income_applicable_to_common_shares,sga,ebit,operating_expenses,research_and_development,interest_expense,period,modified from incomestatement where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and period=").Append(periodType.Equals(IncomeStatement.PeriodType.Annual) ? 0 : 1).Append(" ");
|
||||
sb.Append("and asof<=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(asof)).Append("'");
|
||||
sb.Append(" order by asof desc limit 1;");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
IncomeStatement incomeStatement = new IncomeStatement();
|
||||
incomeStatement.Symbol = sqlDataReader.GetString(0);
|
||||
incomeStatement.AsOf = sqlDataReader.GetDateTime(1);
|
||||
if (!sqlDataReader.IsDBNull(2)) incomeStatement.TotalRevenue = sqlDataReader.GetDouble(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) incomeStatement.CostOfRevenue = sqlDataReader.GetDouble(3);
|
||||
if (!sqlDataReader.IsDBNull(4)) incomeStatement.IncomeTaxExpense = sqlDataReader.GetDouble(4);
|
||||
if (!sqlDataReader.IsDBNull(5)) incomeStatement.GrossProfit = sqlDataReader.GetDouble(5);
|
||||
if (!sqlDataReader.IsDBNull(6)) incomeStatement.NetIncome = sqlDataReader.GetDouble(6);
|
||||
if (!sqlDataReader.IsDBNull(7)) incomeStatement.SGA = sqlDataReader.GetDouble(7);
|
||||
if (!sqlDataReader.IsDBNull(8)) incomeStatement.NetIncomeApplicableToCommonShares = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) incomeStatement.EBIT = sqlDataReader.GetDouble(9);
|
||||
if (!sqlDataReader.IsDBNull(10)) incomeStatement.OperatingExpenses = sqlDataReader.GetDouble(10);
|
||||
if (!sqlDataReader.IsDBNull(11)) incomeStatement.ResearchAndDevelopment = sqlDataReader.GetDouble(11);
|
||||
if (!sqlDataReader.IsDBNull(12)) incomeStatement.InterestExpense = sqlDataReader.GetDouble(12);
|
||||
if (!sqlDataReader.IsDBNull(13)) incomeStatement.Period = 0 == sqlDataReader.GetInt32(13) ? IncomeStatement.PeriodType.Annual : IncomeStatement.PeriodType.Quarterly;
|
||||
if (!sqlDataReader.IsDBNull(14)) incomeStatement.Modified = sqlDataReader.GetDateTime(14);
|
||||
return incomeStatement;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool InsertIncomeStatements(List<IncomeStatement> incomeStatements)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
DateTime modified = DateTime.Now;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
DeleteIncomeStatements(incomeStatements, sqlConnection, sqlTransaction);
|
||||
for (int index = 0; index < incomeStatements.Count; index++)
|
||||
{
|
||||
IncomeStatement incomeStatement = incomeStatements[index];
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into incomestatement (symbol,asof,total_revenue,cost_of_revenue,income_tax_expense,gross_profit,net_income,sga,net_income_applicable_to_common_shares,ebit,operating_expenses,research_and_development,interest_expense,period,modified) ");
|
||||
sb.Append("values(");
|
||||
sb.Append("'").Append(incomeStatement.Symbol).Append("'").Append(",");
|
||||
sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(incomeStatement.AsOf)).Append("'").Append(",");
|
||||
if (!Double.IsNaN(incomeStatement.TotalRevenue)) sb.Append(incomeStatement.TotalRevenue).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(incomeStatement.CostOfRevenue)) sb.Append(incomeStatement.CostOfRevenue).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(incomeStatement.IncomeTaxExpense)) sb.Append(incomeStatement.IncomeTaxExpense).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(incomeStatement.GrossProfit)) sb.Append(incomeStatement.GrossProfit).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(incomeStatement.NetIncome)) sb.Append(incomeStatement.NetIncome).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(incomeStatement.SGA)) sb.Append(incomeStatement.SGA).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(incomeStatement.NetIncomeApplicableToCommonShares))sb.Append(incomeStatement.NetIncomeApplicableToCommonShares).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(incomeStatement.EBIT)) sb.Append(incomeStatement.EBIT).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(incomeStatement.OperatingExpenses)) sb.Append(incomeStatement.OperatingExpenses).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(incomeStatement.ResearchAndDevelopment)) sb.Append(incomeStatement.ResearchAndDevelopment).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(incomeStatement.InterestExpense)) sb.Append(incomeStatement.InterestExpense).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
sb.Append(IncomeStatement.PeriodType.Annual.Equals(incomeStatement.Period)?0:1).Append(",");
|
||||
sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(modified)).Append("'");
|
||||
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)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was '" + strQuery + "'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static bool DeleteIncomeStatements(List<IncomeStatement> incomeStatements, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
for (int index = 0; index < incomeStatements.Count; index++)
|
||||
{
|
||||
DeleteIncomeStatement(incomeStatements[index], sqlConnection, sqlTransaction);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private static bool DeleteIncomeStatement(IncomeStatement incomeStatement, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sb.Append("delete from incomestatement where ");
|
||||
sb.Append("symbol='").Append(incomeStatement.Symbol).Append("'");
|
||||
sb.Append(" and ");
|
||||
sb.Append("asof='").Append(Utility.DateTimeToStringYYYYHMMHDD(incomeStatement.AsOf)).Append("'");
|
||||
sb.Append(" and period=").Append(incomeStatement.Period.Equals(IncomeStatement.PeriodType.Annual) ? 0 : 1);
|
||||
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
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
318
MarketData/MarketDataLib/DataAccess/InsiderTransactionDA.cs
Executable file
318
MarketData/MarketDataLib/DataAccess/InsiderTransactionDA.cs
Executable file
@@ -0,0 +1,318 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class InsiderTransactionDA
|
||||
{
|
||||
private InsiderTransactionDA()
|
||||
{
|
||||
}
|
||||
public static InsiderTransactionSummaries GetInsiderTransactionSummaries(String symbol,DateTime? minDate)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
InsiderTransactionSummaries insiderTransactionSummaries =new InsiderTransactionSummaries();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select transaction_date,sum(number_or_value_acquired_disposed) from InsiderTransaction where ");
|
||||
sb.Append(" symbol=").Append("'").Append(symbol).Append("' ");
|
||||
if(null!=minDate)sb.Append(" and transaction_date>=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(minDate.Value)).Append("'");
|
||||
sb.Append(" and number_or_value_acquired_disposed is not null group by transaction_date order by transaction_date desc ");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
InsiderTransactionSummary insiderTransactionSummary=new InsiderTransactionSummary();
|
||||
insiderTransactionSummary.Symbol=symbol;
|
||||
insiderTransactionSummary.TransactionDate=sqlDataReader.GetDateTime(0);
|
||||
insiderTransactionSummary.NumberOfSharesAcquiredDisposed=sqlDataReader.GetDouble(1);
|
||||
insiderTransactionSummaries.Add(insiderTransactionSummary);
|
||||
}
|
||||
return insiderTransactionSummaries;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("GetInsiderTransactionSummaries Exception: {0}",exception.ToString()));
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public static InsiderTransactions GetLatestInsiderTransactions()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
InsiderTransactions insiderTransactions=new InsiderTransactions();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("SELECT symbol,filing_date,transaction_date,insider_name,ownership_type,securities,nature_of_transaction,number_or_value_acquired_disposed,price,form,sec_accession_number,form_row_number,modified FROM insidertransaction WHERE TRANSACTION_DATE=(SELECT MAX(TRANSACTION_DATE) FROM insidertransaction WHERE TRANSACTION_DATE<=NOW() LIMIT 1)");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
InsiderTransaction insiderTransaction=new InsiderTransaction();
|
||||
insiderTransaction.Symbol=sqlDataReader.GetString(0);
|
||||
insiderTransaction.FilingDate=sqlDataReader.GetDateTime(1);
|
||||
insiderTransaction.TransactionDate=sqlDataReader.GetDateTime(2);
|
||||
insiderTransaction.InsiderName=sqlDataReader.GetString(3);
|
||||
insiderTransaction.OwnershipType=sqlDataReader.GetString(4);
|
||||
insiderTransaction.Securities=sqlDataReader.GetString(5);
|
||||
insiderTransaction.NatureOfTransaction=sqlDataReader.GetString(6);
|
||||
if(!sqlDataReader.IsDBNull(7))insiderTransaction.NumberOrValueAcquiredDisposed=sqlDataReader.GetDouble(7);
|
||||
else insiderTransaction.NumberOrValueAcquiredDisposed=Double.NaN;
|
||||
if(!sqlDataReader.IsDBNull(8))insiderTransaction.Price=sqlDataReader.GetDouble(8);
|
||||
else insiderTransaction.Price=Double.NaN;
|
||||
insiderTransaction.Form=sqlDataReader.GetString(9);
|
||||
insiderTransaction.SECAccessionNumber=sqlDataReader.GetString(10);
|
||||
insiderTransaction.FormRowNumber=sqlDataReader.GetString(11);
|
||||
insiderTransaction.Modified=sqlDataReader.GetDateTime(12);
|
||||
insiderTransactions.Add(insiderTransaction);
|
||||
}
|
||||
return insiderTransactions;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("GetLatestInsiderTransactions Exception: {0}",exception.ToString()));
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public static InsiderTransactions GetInsiderTransactions(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
InsiderTransactions insiderTransactions=new InsiderTransactions();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,filing_date,transaction_date,insider_name,ownership_type,securities,nature_of_transaction,number_or_value_acquired_disposed,price,form,sec_accession_number,form_row_number,modified from InsiderTransaction where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" order by filing_date desc");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
InsiderTransaction insiderTransaction=new InsiderTransaction();
|
||||
insiderTransaction.Symbol=sqlDataReader.GetString(0);
|
||||
insiderTransaction.FilingDate=sqlDataReader.GetDateTime(1);
|
||||
insiderTransaction.TransactionDate=sqlDataReader.GetDateTime(2);
|
||||
insiderTransaction.InsiderName=sqlDataReader.GetString(3);
|
||||
insiderTransaction.OwnershipType=sqlDataReader.GetString(4);
|
||||
insiderTransaction.Securities=sqlDataReader.GetString(5);
|
||||
insiderTransaction.NatureOfTransaction=sqlDataReader.GetString(6);
|
||||
if(!sqlDataReader.IsDBNull(7))insiderTransaction.NumberOrValueAcquiredDisposed=sqlDataReader.GetDouble(7);
|
||||
else insiderTransaction.NumberOrValueAcquiredDisposed=Double.NaN;
|
||||
if(!sqlDataReader.IsDBNull(8))insiderTransaction.Price=sqlDataReader.GetDouble(8);
|
||||
else insiderTransaction.Price=Double.NaN;
|
||||
insiderTransaction.Form=sqlDataReader.GetString(9);
|
||||
insiderTransaction.SECAccessionNumber=sqlDataReader.GetString(10);
|
||||
insiderTransaction.FormRowNumber=sqlDataReader.GetString(11);
|
||||
insiderTransaction.Modified=sqlDataReader.GetDateTime(12);
|
||||
insiderTransactions.Add(insiderTransaction);
|
||||
}
|
||||
return insiderTransactions;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("GetInsiderTransactions Exception: {0}",exception.ToString()));
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static bool InsertInsiderTransactions(InsiderTransactions insiderTransactions)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
Dictionary<String,InsiderTransaction> insiderTransactionsUniqueKey = new Dictionary<String,InsiderTransaction>();
|
||||
String strQuery = null;
|
||||
DateTime modified=DateTime.Now;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
if (!DeleteInsiderTransactions(insiderTransactions, sqlConnection)) return false;
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
foreach (InsiderTransaction insiderTransaction in insiderTransactions)
|
||||
{
|
||||
String insiderTransactionKey = insiderTransaction.Form + insiderTransaction.SECAccessionNumber + insiderTransaction.FormRowNumber;
|
||||
if (insiderTransactionsUniqueKey.ContainsKey(insiderTransactionKey))
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"*********************************************************************************************************");
|
||||
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Collection already contains a value for Form:{0} SECAccessionNumber:{1} FormRowNumber:{2}",
|
||||
insiderTransaction.Form,insiderTransaction.SECAccessionNumber,insiderTransaction.FormRowNumber));
|
||||
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Item attempting to add: Symbol:{0} InsiderName:{1} OwnershipType:{2} Securities:{3} NatureOfTransaction:{4} NumberOrValueAcquiredDisposed:{5} Price:{7} FilingDate:{7} TransactionDate:{8}",
|
||||
insiderTransaction.Symbol, insiderTransaction.InsiderName,insiderTransaction.OwnershipType,insiderTransaction.Securities,insiderTransaction.NatureOfTransaction,insiderTransaction.NumberOrValueAcquiredDisposed,insiderTransaction.Price,
|
||||
Utility.DateTimeToStringMMHDDHYYYY(insiderTransaction.FilingDate),Utility.DateTimeToStringMMSDDSYYYY(insiderTransaction.TransactionDate)));
|
||||
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Item already in collection: Symbol:{0} InsiderName:{1} OwnershipType:{2} Securities:{3} NatureOfTransaction:{4} NumberOrValueAcquiredDisposed:{5} Price:{6} FilingDate:{7} TransactionDate:{8}",
|
||||
insiderTransaction.Symbol,insiderTransaction.InsiderName,insiderTransaction.OwnershipType,insiderTransaction.Securities,insiderTransaction.NatureOfTransaction,insiderTransaction.NumberOrValueAcquiredDisposed,insiderTransaction.Price,
|
||||
Utility.DateTimeToStringMMHDDHYYYY(insiderTransaction.FilingDate),Utility.DateTimeToStringMMSDDSYYYY(insiderTransaction.TransactionDate)));
|
||||
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"*********************************************************************************************************");
|
||||
|
||||
continue;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into InsiderTransaction (symbol,filing_date,transaction_date,insider_name,ownership_type,securities,nature_of_transaction,number_or_value_acquired_disposed,price,form,sec_accession_number,form_row_number,modified) ");
|
||||
sb.Append("values(");
|
||||
sb.Append("'").Append(insiderTransaction.Symbol).Append("'").Append(",");
|
||||
sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(insiderTransaction.FilingDate)).Append("'").Append(",");
|
||||
sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(insiderTransaction.TransactionDate)).Append("'").Append(",");
|
||||
sb.Append("'").Append(SqlUtils.SqlString(insiderTransaction.InsiderName)).Append("'").Append(",");
|
||||
sb.Append("'").Append(SqlUtils.SqlString(insiderTransaction.OwnershipType)).Append("'").Append(",");
|
||||
sb.Append("'").Append(SqlUtils.SqlString(insiderTransaction.Securities)).Append("'").Append(",");
|
||||
sb.Append("'").Append(SqlUtils.SqlString(insiderTransaction.NatureOfTransaction)).Append("'").Append(",");
|
||||
if (!Double.IsNaN(insiderTransaction.NumberOrValueAcquiredDisposed)) sb.Append("'").Append(insiderTransaction.NumberOrValueAcquiredDisposed).Append("'").Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!Double.IsNaN(insiderTransaction.Price)) sb.Append("'").Append(insiderTransaction.Price).Append("'").Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
sb.Append("'").Append(insiderTransaction.Form).Append("'").Append(",");
|
||||
sb.Append("'").Append(insiderTransaction.SECAccessionNumber).Append("'").Append(",");
|
||||
sb.Append("'").Append(insiderTransaction.FormRowNumber).Append("'").Append(",");
|
||||
sb.Append("'").Append(SqlUtils.ToSqlDateTime(modified)).Append("'");
|
||||
sb.Append(")");
|
||||
strQuery = sb.ToString();
|
||||
MySqlCommand sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
insiderTransactionsUniqueKey.Add(insiderTransactionKey, insiderTransaction);
|
||||
sqlCommand.Dispose();
|
||||
}
|
||||
sqlTransaction.Commit();
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("InsertInsiderTransactions Exception: {0}",exception.ToString()));
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was '" + strQuery + "'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
}
|
||||
}
|
||||
private static bool DeleteInsiderTransactions(InsiderTransactions insiderTransactions, MySqlConnection sqlConnection)
|
||||
{
|
||||
StringBuilder sb = null;
|
||||
String strQuery = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == insiderTransactions) return false;
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
foreach (InsiderTransaction insiderTransaction in insiderTransactions)
|
||||
{
|
||||
sb = new StringBuilder();
|
||||
sb.Append("delete from InsiderTransaction where ");
|
||||
sb.Append("symbol='").Append(insiderTransaction.Symbol).Append("'").Append(" and ");
|
||||
sb.Append("form='").Append(insiderTransaction.Form).Append("'").Append(" and ");
|
||||
sb.Append("sec_accession_number='").Append(insiderTransaction.SECAccessionNumber).Append("'").Append(" and ");
|
||||
sb.Append("form_row_number='").Append(insiderTransaction.FormRowNumber).Append("'");
|
||||
strQuery = sb.ToString();
|
||||
MySqlCommand sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
}
|
||||
sqlTransaction.Commit();
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("DeleteInsiderTransactions Exception: {0}",exception.ToString()));
|
||||
if (null != strQuery) MDTrace.WriteLine(LogLevel.DEBUG,"Query was " + strQuery);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Delete InsiderTransaction for given symbol for all years>=given year.
|
||||
/// </summary>
|
||||
/// <param name="symbol">The symbol.</param>
|
||||
/// <param name="yearsGreaterEqual">All years greater then or equal to the year.</param>
|
||||
public static bool DeleteInsiderTransactionsYearsGreaterEqual(String symbol, int yearsGreaterEqual)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
StringBuilder sb = null;
|
||||
String strQuery = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == symbol) return false;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
sb = new StringBuilder();
|
||||
sb.Append("DELETE FROM InsiderTransaction WHERE symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'");
|
||||
sb.Append(" AND YEAR(filing_date)>=").Append(yearsGreaterEqual);
|
||||
strQuery = sb.ToString();
|
||||
MySqlCommand sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
sqlTransaction.Commit();
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("DeleteInsiderTransactionsYearsGreaterEqual Exception: {0}",exception.ToString()));
|
||||
if (null != strQuery) MDTrace.WriteLine(LogLevel.DEBUG,"Query was " + strQuery);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if(null!=sqlConnection)sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
305
MarketData/MarketDataLib/DataAccess/LexicalDA.cs
Executable file
305
MarketData/MarketDataLib/DataAccess/LexicalDA.cs
Executable file
@@ -0,0 +1,305 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class LexicalDA
|
||||
{
|
||||
private LexicalDA()
|
||||
{
|
||||
}
|
||||
public static bool TruncateLexicon()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("truncate table lexicon");
|
||||
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,"Query was '" + strQuery + "'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool InsertLexicalElement(LexicalElement lexicalElement)
|
||||
{
|
||||
LexicalCollection lexicalCollection=new LexicalCollection();
|
||||
lexicalCollection.Add(lexicalElement);
|
||||
InsertLexicalCollection(lexicalCollection);
|
||||
return true;
|
||||
}
|
||||
public static bool InsertLexicalCollection(LexicalCollection lexicalCollection)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime modified=DateTime.Now;
|
||||
DateTime created=modified;
|
||||
foreach(LexicalElement lexicalElement in lexicalCollection)
|
||||
{
|
||||
sb=new StringBuilder();
|
||||
if(ContainsLexicalElement(lexicalElement))continue;
|
||||
sb.Append("insert into lexicon (word,sentiment,part_of_speech) ");
|
||||
sb.Append("values(");
|
||||
sb.Append("'").Append(lexicalElement.Word).Append("'").Append(",");
|
||||
sb.Append("'").Append(lexicalElement.Sentiment).Append("'").Append(",");
|
||||
if(null!=lexicalElement.PartOfSpeech)sb.Append("'").Append(lexicalElement.PartOfSpeech).Append("'");
|
||||
else sb.Append("null");
|
||||
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,"Query was '" + strQuery + "'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool ContainsLexicalElement(LexicalElement lexicalElement)
|
||||
{
|
||||
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 lexicon where word='").Append(lexicalElement.Word).Append("' ");
|
||||
sb.Append("and sentiment='").Append(lexicalElement.Sentiment).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);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool UpdateLexicalElement(LexicalElement lexicalElement)
|
||||
{
|
||||
LexicalCollection lexicalCollection=new LexicalCollection();
|
||||
lexicalCollection.Add(lexicalElement);
|
||||
return UpdateLexicalElements(lexicalCollection);
|
||||
}
|
||||
public static bool UpdateLexicalElements(LexicalCollection lexicalCollection)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
foreach(LexicalElement lexicalElement in lexicalCollection)
|
||||
{
|
||||
if(!ContainsLexicalElement(lexicalElement))continue;
|
||||
if(null==lexicalElement.PartOfSpeech||0==lexicalElement.PartOfSpeech.Length)continue;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime modified=DateTime.Now;
|
||||
sb.Append("update lexicon set ");
|
||||
sb.Append("part_of_speech=").Append("'").Append(lexicalElement.PartOfSpeech).Append("'");
|
||||
sb.Append(" where word='").Append(lexicalElement.Word).Append("'").Append(" and ");
|
||||
sb.Append("sentiment='").Append(lexicalElement.Sentiment).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,"Query was '" + strQuery + "'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static LexicalCollection GetLexicalCollection(String sentiment)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
LexicalCollection lexicalCollection=new LexicalCollection();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime modified=DateTime.Now;
|
||||
sb.Append("select word,sentiment,part_of_speech from lexicon ");
|
||||
sb.Append(" where sentiment='").Append(sentiment).Append("'");
|
||||
sb.Append(" order by word desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
LexicalElement lexicalElement=new LexicalElement();
|
||||
lexicalElement.Word=sqlDataReader.GetString(0);
|
||||
lexicalElement.Sentiment=sqlDataReader.GetString(1);
|
||||
if(!sqlDataReader.IsDBNull(2))lexicalElement.PartOfSpeech=sqlDataReader.GetString(2);
|
||||
lexicalCollection.Add(lexicalElement);
|
||||
}
|
||||
return lexicalCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was '" + strQuery + "'");
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static LexicalCollection GetLexicalCollection()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
LexicalCollection lexicalCollection=new LexicalCollection();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime modified=DateTime.Now;
|
||||
sb.Append("select word,sentiment,part_of_speech from lexicon ");
|
||||
sb.Append(" order by word desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
LexicalElement lexicalElement=new LexicalElement();
|
||||
lexicalElement.Word=sqlDataReader.GetString(0);
|
||||
lexicalElement.Sentiment=sqlDataReader.GetString(1);
|
||||
if(!sqlDataReader.IsDBNull(2))lexicalElement.PartOfSpeech=sqlDataReader.GetString(2);
|
||||
lexicalCollection.Add(lexicalElement);
|
||||
}
|
||||
return lexicalCollection;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was '" + strQuery + "'");
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static bool DeleteLexicalCollection(LexicalCollection lexicalCollection, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
foreach(LexicalElement lexicalElement in lexicalCollection)DeleteLexicalElement(lexicalElement,sqlConnection,sqlTransaction);
|
||||
return true;
|
||||
}
|
||||
private static bool DeleteLexicalElement(LexicalElement lexicalElement, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sb.Append("delete from lexicon where ");
|
||||
sb.Append("word='").Append(lexicalElement.Word).Append("'");
|
||||
sb.Append(" and ");
|
||||
sb.Append("sentiment='").Append(lexicalElement.Sentiment).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
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
181
MarketData/MarketDataLib/DataAccess/MStarSecurityDA.cs
Executable file
181
MarketData/MarketDataLib/DataAccess/MStarSecurityDA.cs
Executable file
@@ -0,0 +1,181 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class MStarSecurityDA
|
||||
{
|
||||
private MStarSecurityDA()
|
||||
{
|
||||
}
|
||||
public static String GetSecurityId(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
String strQuery = null;
|
||||
String securityIdentifier=null;
|
||||
|
||||
try
|
||||
{
|
||||
if(!HasSecurityId(symbol))return null;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select security_identifier from mstarsecurityid where symbol=").Append(Utility.AddQuotes(symbol));
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (sqlDataReader.Read())
|
||||
{
|
||||
securityIdentifier=sqlDataReader.GetString(0);
|
||||
}
|
||||
return securityIdentifier;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static void PutSecurityId(String symbol,String securityId)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction=null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
if(!HasSecurityId(symbol))InsertSecurityId(symbol,securityId,sqlConnection,sqlTransaction);
|
||||
else UpdateSecurityId(symbol,securityId,sqlConnection,sqlTransaction);
|
||||
sqlTransaction= sqlConnection.BeginTransaction();
|
||||
sqlTransaction.Commit();
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlTransaction)
|
||||
{
|
||||
sqlTransaction.Dispose();
|
||||
}
|
||||
if(null!=sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static bool UpdateSecurityId(String symbol,String securityIdentifier, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == securityIdentifier || null == symbol) return false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("update mstarsecurityid set security_identifier=").Append(Utility.AddQuotes(securityIdentifier));
|
||||
sb.Append(" where symbol=").Append(Utility.AddQuotes(symbol));
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
private static bool InsertSecurityId(String symbol,String securityIdentifier, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == securityIdentifier || null == symbol) return false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into mstarsecurityid(symbol,security_identifier,modified) values(");
|
||||
sb.Append(SqlUtils.AddQuotes(symbol)).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(securityIdentifier)).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(SqlUtils.ToSqlDateTime(DateTime.Now)));
|
||||
sb.Append(")");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
public static bool HasSecurityId(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
String strQuery = null;
|
||||
int count = 0;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select count(*) from mstarsecurityid where symbol=");
|
||||
sb.Append(Utility.AddQuotes(symbol));
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
sqlDataReader.Read();
|
||||
count=sqlDataReader.GetInt32(0);
|
||||
return count>0?true:false;
|
||||
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
433
MarketData/MarketDataLib/DataAccess/OptionsDA.cs
Executable file
433
MarketData/MarketDataLib/DataAccess/OptionsDA.cs
Executable file
@@ -0,0 +1,433 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
using MarketData.Numerical;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class OptionsDA
|
||||
{
|
||||
private OptionsDA()
|
||||
{
|
||||
}
|
||||
public static List<String> GetOptionsSymbolIn(List<String> symbols)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
List<String> optionSymbols = new List<String>();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select distinct symbol from options where symbol in ").Append(SqlUtils.CreateInClause(symbols)).Append(" order by symbol asc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
optionSymbols.Add(sqlDataReader.GetString(0));
|
||||
}
|
||||
return optionSymbols;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Options GetOptions(String symbol, DateTime earliestDate)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
Options options = new Options();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol, expiration_date,option_type,strike,bid,ask,volume,open_interest,modified from options ");
|
||||
sb.Append("where symbol='").Append(symbol).Append("'");
|
||||
sb.Append(" and expiration_date>='").Append(Utility.DateTimeToStringYYYYHMMHDD(earliestDate)).Append("'");
|
||||
sb.Append(" order by option_type,expiration_date asc, strike desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
Option option = new Option();
|
||||
option.Symbol = sqlDataReader.GetString(0);
|
||||
option.Expiration = sqlDataReader.GetDateTime(1);
|
||||
option.Type = ("C".Equals(sqlDataReader.GetString(2)) ? OptionTypeEnum.CallOption : OptionTypeEnum.PutOption);
|
||||
if (!sqlDataReader.IsDBNull(3)) option.Strike = sqlDataReader.GetDouble(3);
|
||||
else option.Strike = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(4)) option.Bid = sqlDataReader.GetDouble(4);
|
||||
else option.Bid = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(5)) option.Ask = sqlDataReader.GetDouble(5);
|
||||
else option.Ask = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(6)) option.Volume = sqlDataReader.GetInt32(6);
|
||||
else option.Ask = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(7)) option.OpenInterest = sqlDataReader.GetDouble(7);
|
||||
else option.Ask = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(8)) option.Modified = sqlDataReader.GetDateTime(8);
|
||||
else option.Modified = Utility.Epoch;
|
||||
options.Add(option);
|
||||
}
|
||||
return options;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Options GetOptions(String symbol, DateTime earliestDate,OptionTypeEnum optionType)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
Options options = new Options();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol, expiration_date,option_type,strike,bid,ask,volume,open_interest,modified from options ");
|
||||
sb.Append("where symbol='").Append(symbol).Append("'");
|
||||
sb.Append(" and expiration_date>='").Append(Utility.DateTimeToStringYYYYHMMHDD(earliestDate)).Append("'");
|
||||
sb.Append(" and option_type='").Append(optionType.Equals(OptionTypeEnum.CallOption) ? "C" : "P").Append("'");
|
||||
sb.Append(" order by option_type,expiration_date asc, strike desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
Option option = new Option();
|
||||
option.Symbol = sqlDataReader.GetString(0);
|
||||
option.Expiration = sqlDataReader.GetDateTime(1);
|
||||
option.Type = ("C".Equals(sqlDataReader.GetString(2)) ? OptionTypeEnum.CallOption : OptionTypeEnum.PutOption);
|
||||
if (!sqlDataReader.IsDBNull(3)) option.Strike = sqlDataReader.GetDouble(3);
|
||||
else option.Strike = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(4)) option.Bid = sqlDataReader.GetDouble(4);
|
||||
else option.Bid = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(5)) option.Ask = sqlDataReader.GetDouble(5);
|
||||
else option.Ask = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(6)) option.Volume = sqlDataReader.GetInt32(6);
|
||||
else option.Ask = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(7)) option.OpenInterest = sqlDataReader.GetDouble(7);
|
||||
else option.Ask = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(8)) option.Modified = sqlDataReader.GetDateTime(8);
|
||||
else option.Modified = Utility.Epoch;
|
||||
options.Add(option);
|
||||
}
|
||||
return options;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Options GetOptions(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
Options options = new Options();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol, expiration_date,option_type,strike,bid,ask,volume,open_interest,modified from options where symbol='").Append(symbol).Append("' order by option_type,expiration_date asc, strike desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
Option option = new Option();
|
||||
option.Symbol = sqlDataReader.GetString(0);
|
||||
option.Expiration = sqlDataReader.GetDateTime(1);
|
||||
option.Type = ("C".Equals(sqlDataReader.GetString(2)) ?OptionTypeEnum.CallOption : OptionTypeEnum.PutOption);
|
||||
if (!sqlDataReader.IsDBNull(3)) option.Strike = sqlDataReader.GetDouble(3);
|
||||
else option.Strike = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(4)) option.Bid = sqlDataReader.GetDouble(4);
|
||||
else option.Bid = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(5)) option.Ask = sqlDataReader.GetDouble(5);
|
||||
else option.Ask = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(6)) option.Volume = sqlDataReader.GetInt32(6);
|
||||
else option.Ask = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(7)) option.OpenInterest = sqlDataReader.GetDouble(7);
|
||||
else option.Ask = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(8)) option.Modified = sqlDataReader.GetDateTime(8);
|
||||
else option.Modified = Utility.Epoch;
|
||||
options.Add(option);
|
||||
}
|
||||
return options;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Options GetOptions(String symbol,OptionTypeEnum optionType)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
Options options = new Options();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol, expiration_date,option_type,strike,bid,ask,volume,open_interest,modified from options where symbol='").Append(symbol).Append("'");
|
||||
sb.Append(" and option_type='").Append(optionType.Equals(OptionTypeEnum.CallOption)?"C":"P").Append("'");
|
||||
sb.Append(" order by option_type,expiration_date asc, strike desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
Option option = new Option();
|
||||
option.Symbol = sqlDataReader.GetString(0);
|
||||
option.Expiration = sqlDataReader.GetDateTime(1);
|
||||
option.Type = ("C".Equals(sqlDataReader.GetString(2)) ? OptionTypeEnum.CallOption : OptionTypeEnum.PutOption);
|
||||
if (!sqlDataReader.IsDBNull(3)) option.Strike = sqlDataReader.GetDouble(3);
|
||||
else option.Strike = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(4)) option.Bid = sqlDataReader.GetDouble(4);
|
||||
else option.Bid = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(5)) option.Ask = sqlDataReader.GetDouble(5);
|
||||
else option.Ask = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(6)) option.Volume = sqlDataReader.GetInt32(6);
|
||||
else option.Ask = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(7)) option.OpenInterest = sqlDataReader.GetDouble(7);
|
||||
else option.Ask = double.NaN;
|
||||
if (!sqlDataReader.IsDBNull(8)) option.Modified = sqlDataReader.GetDateTime(8);
|
||||
else option.Modified = Utility.Epoch;
|
||||
options.Add(option);
|
||||
}
|
||||
return options;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool DeleteOptions(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
String strQuery = null;
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("delete from options where symbol='").Append(symbol).Append("' ");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
sqlCommand.Dispose();
|
||||
sqlCommand = null;
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) { sqlCommand.Dispose(); sqlCommand = null; }
|
||||
if (null != sqlConnection) { sqlConnection.Close(); sqlConnection = null; }
|
||||
}
|
||||
}
|
||||
private static bool DeleteOption(Option option, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
String strQuery = null;
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("delete from options where symbol=").Append(SqlUtils.AddQuotes(option.Symbol));
|
||||
sb.Append(" and expiration_date=").Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(option.Expiration)));
|
||||
sb.Append(" and strike=").Append(Utility.FormatNumber(option.Strike,2));
|
||||
sb.Append(" and option_type=").Append(SqlUtils.AddQuotes(option.Type.Equals(OptionTypeEnum.CallOption) ? "C" : "P"));
|
||||
strQuery = sb.ToString();
|
||||
// MDTrace.WriteLine(LogLevel.DEBUG,String.Format("DeleteOption {0},{1},{2},{3}",option.Symbol,Utility.DateTimeToStringMMHDDHYYYY(option.Expiration),option.Type.Equals(OptionTypeEnum.CallOption)?"Call":"Put",option.Strike));
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
sqlCommand.Dispose();
|
||||
sqlCommand = null;
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) { sqlCommand.Dispose(); sqlCommand = null; }
|
||||
if (null != sqlConnection) { sqlConnection.Close(); sqlConnection = null; }
|
||||
}
|
||||
}
|
||||
// private static bool DeleteOption(Option option)
|
||||
// {
|
||||
// MySqlConnection sqlConnection = null;
|
||||
// MySqlCommand sqlCommand = null;
|
||||
// MySqlTransaction sqlTransaction=null;
|
||||
// String strQuery = null;
|
||||
// try
|
||||
// {
|
||||
// StringBuilder sb = new StringBuilder();
|
||||
// sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
// sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
// sb.Append("delete from options where symbol=").Append(SqlUtils.AddQuotes(option.Symbol));
|
||||
// sb.Append(" and expiration_date=").Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(option.Expiration)));
|
||||
// sb.Append(" and strike=").Append(Utility.FormatNumber(option.Strike,2));
|
||||
// sb.Append(" and option_type=").Append(SqlUtils.AddQuotes(option.Type.Equals(OptionTypeEnum.CallOption) ? "C" : "P"));
|
||||
// strQuery = sb.ToString();
|
||||
//// MDTrace.WriteLine(LogLevel.DEBUG,String.Format("DeleteOption {0},{1},{2},{3}",option.Symbol,Utility.DateTimeToStringMMHDDHYYYY(option.Expiration),option.Type.Equals(OptionTypeEnum.CallOption)?"Call":"Put",option.Strike));
|
||||
// sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
// sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
// sqlCommand.ExecuteNonQuery();
|
||||
// sqlTransaction.Commit();
|
||||
// sqlCommand.Dispose();
|
||||
// sqlCommand = null;
|
||||
// return true;
|
||||
// }
|
||||
// catch (Exception exception)
|
||||
// {
|
||||
// MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
// return false;
|
||||
// }
|
||||
// finally
|
||||
// {
|
||||
// if (null != sqlCommand) { sqlCommand.Dispose(); sqlCommand = null; }
|
||||
// if (null != sqlConnection) { sqlConnection.Close(); sqlConnection.Dispose();sqlConnection = null; }
|
||||
// if(null!=sqlTransaction){sqlTransaction.Dispose();sqlTransaction=null;}
|
||||
// }
|
||||
// }
|
||||
public static bool AddOptions(Options options)
|
||||
{
|
||||
foreach (Option option in options)
|
||||
{
|
||||
AddOption(option);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public static bool AddOption(Option option)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
// MDTrace.WriteLine(LogLevel.DEBUG,"[AddOption]Enter");
|
||||
DateTime modified = DateTime.Now;
|
||||
if (null == option) return false;
|
||||
// DeleteOption(option);
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
DeleteOption(option,sqlTransaction);
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.Append("insert into options(symbol,expiration_date,option_type,strike,last_price,change_price,bid,ask,volume,open_interest,modified)values(");
|
||||
sb.Append(SqlUtils.AddQuotes(option.Symbol)).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes((Utility.DateTimeToStringYYYYHMMHDD(option.Expiration)))).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(option.Type.Equals(OptionTypeEnum.CallOption)?"C":"P")).Append(",");
|
||||
if (!double.IsNaN(option.Strike)) sb.Append(Utility.FormatNumber(option.Strike,2)).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!double.IsNaN(option.Last)) sb.Append(option.Last).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!double.IsNaN(option.Change)) sb.Append(option.Change).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!double.IsNaN(option.Bid)) sb.Append(option.Bid).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!double.IsNaN(option.Ask)) sb.Append(option.Ask).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!double.IsNaN(option.Volume)) sb.Append(option.Volume).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!double.IsNaN(option.OpenInterest)) sb.Append(option.OpenInterest).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(modified)));
|
||||
sb.Append(")");
|
||||
strQuery = sb.ToString();
|
||||
// MDTrace.WriteLine(LogLevel.DEBUG,String.Format("AddOption {0},{1},{2},{3}",option.Symbol,Utility.DateTimeToStringMMHDDHYYYY(option.Expiration),option.Type.Equals(OptionTypeEnum.CallOption)?"Call":"Put",option.Strike));
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
sqlTransaction.Commit();
|
||||
sqlTransaction.Dispose();
|
||||
sqlTransaction=null;
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
// MDTrace.WriteLine(LogLevel.DEBUG,"[AddOption]LEAVE");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
933
MarketData/MarketDataLib/DataAccess/PortfolioDA.cs
Executable file
933
MarketData/MarketDataLib/DataAccess/PortfolioDA.cs
Executable file
@@ -0,0 +1,933 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class PortfolioDA
|
||||
{
|
||||
private PortfolioDA()
|
||||
{
|
||||
}
|
||||
// **********************************************************************************************************************************************************************************
|
||||
// ****************************************************************************************** S T O P L I M I T S ****************************************************************
|
||||
// **********************************************************************************************************************************************************************************
|
||||
public static StopLimit GetStopLimit(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery=null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
if(null==symbol)return null;
|
||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select symbol,stop_limit,shares,stop_type,active from stoplimits ");
|
||||
sb.Append("where symbol='").Append(symbol).Append("'");
|
||||
sb.Append(" and active=1");
|
||||
strQuery=sb.ToString();
|
||||
sqlCommand=new MySqlCommand(strQuery,sqlConnection);
|
||||
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
if(!sqlDataReader.Read())return null;
|
||||
StopLimit stopLimit=new StopLimit();
|
||||
stopLimit.Symbol=sqlDataReader.GetString(0);
|
||||
stopLimit.StopPrice=sqlDataReader.GetDouble(1);
|
||||
stopLimit.Shares=sqlDataReader.GetDouble(2);
|
||||
stopLimit.StopType=sqlDataReader.GetString(3);
|
||||
stopLimit.Active=sqlDataReader.GetInt32(4);
|
||||
return stopLimit;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader) sqlDataReader.Close();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool InsertUpdateStopLimit(StopLimit stopLimit)
|
||||
{
|
||||
if(null==stopLimit||null==stopLimit.Symbol||double.IsNaN(stopLimit.StopPrice))return false;
|
||||
if(!HasStopLimit(stopLimit.Symbol))return InsertStopLimit(stopLimit);
|
||||
return UpdateStopLimit(stopLimit);
|
||||
}
|
||||
private static bool InsertStopLimit(StopLimit stopLimit)
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlTransaction sqlTransaction=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery=null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sqlTransaction=sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.Append("insert into stoplimits(symbol,stop_limit,shares,stop_type,active)values(");
|
||||
sb.Append(SqlUtils.AddQuotes(stopLimit.Symbol)).Append(",");
|
||||
sb.Append(stopLimit.StopPrice).Append(",");
|
||||
sb.Append(stopLimit.Shares).Append(",");
|
||||
if(null!=stopLimit.StopType) sb.Append(SqlUtils.AddQuotes(stopLimit.StopType)).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
sb.Append(stopLimit.Active);
|
||||
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);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
||||
if(null!=sqlTransaction) sqlTransaction.Dispose();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static bool UpdateStopLimit(StopLimit stopLimit)
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlTransaction sqlTransaction=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery=null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sqlTransaction=sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.Append("update stoplimits ");
|
||||
sb.Append("set ");
|
||||
if(null!=stopLimit.StopType) sb.Append("stop_type=").Append(SqlUtils.AddQuotes(stopLimit.StopType)).Append(", ");
|
||||
sb.Append("stop_limit=").Append(stopLimit.StopPrice).Append(", ");
|
||||
sb.Append("shares=").Append(stopLimit.Shares).Append(", ");
|
||||
sb.Append("active=").Append(stopLimit.Active).Append("");
|
||||
sb.Append(" where symbol=").Append(SqlUtils.AddQuotes(stopLimit.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);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
if(null!=sqlTransaction) sqlTransaction.Dispose();
|
||||
}
|
||||
}
|
||||
public static bool HasStopLimit(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery=null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
if(null==symbol) return false;
|
||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select count(*) from stoplimits ");
|
||||
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()) return false;
|
||||
int count=sqlDataReader.GetInt32(0);
|
||||
return count>0?true:false;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader) sqlDataReader.Close();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool DeleteStopLimit(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery=null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
if(null==symbol) return false;
|
||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("delete from stoplimits ");
|
||||
sb.Append("where symbol='").Append(symbol).Append("'");
|
||||
strQuery=sb.ToString();
|
||||
sqlCommand=new MySqlCommand(strQuery,sqlConnection);
|
||||
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
return true;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
// **********************************************************************************************************************************************************************
|
||||
public static bool UpdateTrade(PortfolioTrade trade)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("update trades ");
|
||||
sb.Append("set ");
|
||||
sb.Append("symbol=").Append(SqlUtils.AddQuotes(trade.Symbol)).Append(", ");
|
||||
sb.Append("trade_date=").Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(trade.TradeDate))).Append(", ");
|
||||
sb.Append("shares=").Append(trade.Shares).Append(", ");
|
||||
sb.Append("price=").Append(trade.Price).Append(", ");
|
||||
sb.Append("buysell=").Append(SqlUtils.AddQuotes(trade.BuySell)).Append(", ");
|
||||
sb.Append("account=").Append(SqlUtils.AddQuotes(trade.Account)).Append(", ");
|
||||
sb.Append("status=").Append(SqlUtils.AddQuotes(trade.Status)).Append(", ");
|
||||
sb.Append("commission=").Append(trade.Commission);
|
||||
if (!Utility.IsEpoch(trade.SellDate))sb.Append(", date_sold=").Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(trade.SellDate))).Append(" ");
|
||||
if (!double.IsNaN(trade.SellPrice) && !double.IsInfinity(trade.SellPrice))sb.Append(", sell_price=").Append(trade.SellPrice).Append(" ");
|
||||
sb.Append(" where trade_id=").Append(trade.TradeId);
|
||||
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);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
}
|
||||
}
|
||||
public static int AddTrade(PortfolioTrade trade)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into trades(symbol,trade_date,shares,price,buysell,account,status,commission,date_sold,sell_price)values(");
|
||||
sb.Append(SqlUtils.AddQuotes(trade.Symbol)).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(trade.TradeDate))).Append(",");
|
||||
sb.Append(trade.Shares).Append(",");
|
||||
sb.Append(trade.Price).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(trade.BuySell)).Append(",");
|
||||
sb.Append("'").Append(trade.Account).Append("'").Append(",");
|
||||
sb.Append("'").Append(trade.Status).Append("'").Append(",");
|
||||
sb.Append("'").Append(trade.Commission).Append("'").Append(",");
|
||||
if (!Utility.IsEpoch(trade.SellDate)) sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(trade.SellDate))).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if (!double.IsNaN(trade.SellPrice)&&!double.IsInfinity(trade.SellPrice)) sb.Append(trade.SellPrice);
|
||||
else sb.Append("null");
|
||||
sb.Append(")");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
sqlTransaction.Commit();
|
||||
return GetMaxTradeId();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return -1;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static PortfolioTrades GetTrades(String symbol=null)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
PortfolioTrades portfolioTrades = new PortfolioTrades();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select trade_id,symbol,trade_date,shares,price,buysell,account,status,commission,date_sold,sell_price from trades ");
|
||||
if(null!=symbol)sb.Append("where symbol='").Append(symbol).Append("'");
|
||||
sb.Append(" order by trade_date asc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
PortfolioTrade portfolioTrade = new PortfolioTrade();
|
||||
portfolioTrade.TradeId = sqlDataReader.GetInt32(0);
|
||||
portfolioTrade.Symbol = sqlDataReader.GetString(1);
|
||||
portfolioTrade.TradeDate = sqlDataReader.GetDateTime(2);
|
||||
portfolioTrade.Shares = sqlDataReader.GetDouble(3);
|
||||
portfolioTrade.Price = sqlDataReader.GetDouble(4);
|
||||
portfolioTrade.BuySell = sqlDataReader.GetString(5);
|
||||
portfolioTrade.Account = sqlDataReader.GetString(6);
|
||||
portfolioTrade.Status = sqlDataReader.GetString(7);
|
||||
portfolioTrade.Commission = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) portfolioTrade.SellDate = sqlDataReader.GetDateTime(9);
|
||||
else portfolioTrade.SellDate = Utility.Epoch;
|
||||
if (!sqlDataReader.IsDBNull(10)) portfolioTrade.SellPrice = sqlDataReader.GetDouble(10);
|
||||
else portfolioTrade.SellPrice = double.NaN;
|
||||
portfolioTrades.Add(portfolioTrade);
|
||||
}
|
||||
return portfolioTrades;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static PortfolioTrades GetTradesForAccount(String account)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
PortfolioTrades portfolioTrades = new PortfolioTrades();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select trade_id,symbol,trade_date,shares,price,buysell,account,status,commission,date_sold,sell_price from trades ");
|
||||
sb.Append("where account='").Append(account).Append("'");
|
||||
sb.Append(" order by trade_date asc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
PortfolioTrade portfolioTrade = new PortfolioTrade();
|
||||
portfolioTrade.TradeId = sqlDataReader.GetInt32(0);
|
||||
portfolioTrade.Symbol = sqlDataReader.GetString(1);
|
||||
portfolioTrade.TradeDate = sqlDataReader.GetDateTime(2);
|
||||
portfolioTrade.Shares = sqlDataReader.GetDouble(3);
|
||||
portfolioTrade.Price = sqlDataReader.GetDouble(4);
|
||||
portfolioTrade.BuySell = sqlDataReader.GetString(5);
|
||||
portfolioTrade.Account = sqlDataReader.GetString(6);
|
||||
portfolioTrade.Status = sqlDataReader.GetString(7);
|
||||
portfolioTrade.Commission = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) portfolioTrade.SellDate = sqlDataReader.GetDateTime(9);
|
||||
else portfolioTrade.SellDate = Utility.Epoch;
|
||||
if (!sqlDataReader.IsDBNull(10)) portfolioTrade.SellPrice = sqlDataReader.GetDouble(10);
|
||||
else portfolioTrade.SellPrice = double.NaN;
|
||||
portfolioTrades.Add(portfolioTrade);
|
||||
}
|
||||
return portfolioTrades;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static PortfolioTrades GetTradesForAccounts(List<String> accounts)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
PortfolioTrades portfolioTrades = new PortfolioTrades();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select trade_id,symbol,trade_date,shares,price,buysell,account,status,commission,date_sold,sell_price from trades ");
|
||||
sb.Append("where account in ").Append(SqlUtils.CreateInClause(accounts));
|
||||
sb.Append(" order by trade_date asc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
PortfolioTrade portfolioTrade = new PortfolioTrade();
|
||||
portfolioTrade.TradeId = sqlDataReader.GetInt32(0);
|
||||
portfolioTrade.Symbol = sqlDataReader.GetString(1);
|
||||
portfolioTrade.TradeDate = sqlDataReader.GetDateTime(2);
|
||||
portfolioTrade.Shares = sqlDataReader.GetDouble(3);
|
||||
portfolioTrade.Price = sqlDataReader.GetDouble(4);
|
||||
portfolioTrade.BuySell = sqlDataReader.GetString(5);
|
||||
portfolioTrade.Account = sqlDataReader.GetString(6);
|
||||
portfolioTrade.Status = sqlDataReader.GetString(7);
|
||||
portfolioTrade.Commission = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) portfolioTrade.SellDate = sqlDataReader.GetDateTime(9);
|
||||
else portfolioTrade.SellDate = Utility.Epoch;
|
||||
if (!sqlDataReader.IsDBNull(10)) portfolioTrade.SellPrice = sqlDataReader.GetDouble(10);
|
||||
else portfolioTrade.SellPrice = double.NaN;
|
||||
portfolioTrades.Add(portfolioTrade);
|
||||
}
|
||||
return portfolioTrades;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static PortfolioTrades GetOpenTrades()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
PortfolioTrades portfolioTrades = new PortfolioTrades();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select trade_id,symbol,trade_date,shares,price,buysell,account,status,commission,date_sold,sell_price from trades ");
|
||||
sb.Append(" where status='").Append("OPEN").Append("'");
|
||||
sb.Append(" order by trade_date asc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
PortfolioTrade portfolioTrade = new PortfolioTrade();
|
||||
portfolioTrade.TradeId = sqlDataReader.GetInt32(0);
|
||||
portfolioTrade.Symbol = sqlDataReader.GetString(1);
|
||||
portfolioTrade.TradeDate = sqlDataReader.GetDateTime(2);
|
||||
portfolioTrade.Shares = sqlDataReader.GetDouble(3);
|
||||
portfolioTrade.Price = sqlDataReader.GetDouble(4);
|
||||
portfolioTrade.BuySell = sqlDataReader.GetString(5);
|
||||
portfolioTrade.Account = sqlDataReader.GetString(6);
|
||||
portfolioTrade.Status = sqlDataReader.GetString(7);
|
||||
portfolioTrade.Commission = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) portfolioTrade.SellDate = sqlDataReader.GetDateTime(9);
|
||||
else portfolioTrade.SellDate = Utility.Epoch;
|
||||
if (!sqlDataReader.IsDBNull(10)) portfolioTrade.SellPrice = sqlDataReader.GetDouble(10);
|
||||
else portfolioTrade.SellPrice = double.NaN;
|
||||
portfolioTrades.Add(portfolioTrade);
|
||||
}
|
||||
return portfolioTrades;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
// Retrieves the symbols for open trades
|
||||
public static List<String> GetOpenSymbols()
|
||||
{
|
||||
try
|
||||
{
|
||||
PortfolioTrades portfolioTrades=GetOpenTrades();
|
||||
if(null==portfolioTrades)return null;
|
||||
return portfolioTrades.Symbols;
|
||||
}
|
||||
catch(Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
// This is used by ValueAtRisk
|
||||
// Notes: Added the conditional after "OR" clause. The reason being that the query was not returning trades which are CLOSED but OPEN on the asOf date.
|
||||
// The behaviour was observed while running the VaR and examining 07/30/2019 vs 07/31/2019 and noticing that ENSG, which was still OPEN on 07/30, was not being included in the VaR
|
||||
public static PortfolioTrades GetOpenTradesAsOf(DateTime asOf)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
PortfolioTrades portfolioTrades = new PortfolioTrades();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select trade_id,symbol,trade_date,shares,price,buysell,account,status,commission,date_sold,sell_price from trades ");
|
||||
sb.Append(" where (trade_date<='").Append(Utility.DateTimeToStringYYYYHMMHDD(asOf)).Append("' ").Append(" and status='").Append("OPEN").Append("')");
|
||||
sb.Append(" or (trade_date<='").Append(Utility.DateTimeToStringYYYYHMMHDD(asOf)).Append("' and status='CLOSED' and date_sold is not null and date_sold>='").Append(Utility.DateTimeToStringYYYYHMMHDD(asOf)).Append("')");
|
||||
sb.Append(" order by trade_date asc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
PortfolioTrade portfolioTrade = new PortfolioTrade();
|
||||
portfolioTrade.TradeId = sqlDataReader.GetInt32(0);
|
||||
portfolioTrade.Symbol = sqlDataReader.GetString(1);
|
||||
portfolioTrade.TradeDate = sqlDataReader.GetDateTime(2);
|
||||
portfolioTrade.Shares = sqlDataReader.GetDouble(3);
|
||||
portfolioTrade.Price = sqlDataReader.GetDouble(4);
|
||||
portfolioTrade.BuySell = sqlDataReader.GetString(5);
|
||||
portfolioTrade.Account = sqlDataReader.GetString(6);
|
||||
portfolioTrade.Status = sqlDataReader.GetString(7);
|
||||
portfolioTrade.Commission = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) portfolioTrade.SellDate = sqlDataReader.GetDateTime(9);
|
||||
else portfolioTrade.SellDate = Utility.Epoch;
|
||||
if (!sqlDataReader.IsDBNull(10)) portfolioTrade.SellPrice = sqlDataReader.GetDouble(10);
|
||||
else portfolioTrade.SellPrice = double.NaN;
|
||||
portfolioTrades.Add(portfolioTrade);
|
||||
}
|
||||
return portfolioTrades;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static PortfolioTrades GetOpenTradesSymbolBetween(String symbol,DateTime minDate,DateTime maxDate)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
PortfolioTrades portfolioTrades = new PortfolioTrades();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select trade_id,symbol,trade_date,shares,price,buysell,account,status,commission,date_sold,sell_price from trades ");
|
||||
sb.Append(" where trade_date>='").Append(Utility.DateTimeToStringYYYYHMMHDD(minDate)).Append("' ");
|
||||
sb.Append(" and trade_date<='").Append(Utility.DateTimeToStringYYYYHMMHDD(maxDate)).Append("' ");
|
||||
sb.Append(" and symbol='").Append(symbol).Append("'");
|
||||
sb.Append(" and status='").Append("OPEN").Append("'");
|
||||
sb.Append(" order by trade_date asc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
PortfolioTrade portfolioTrade = new PortfolioTrade();
|
||||
portfolioTrade.TradeId = sqlDataReader.GetInt32(0);
|
||||
portfolioTrade.Symbol = sqlDataReader.GetString(1);
|
||||
portfolioTrade.TradeDate = sqlDataReader.GetDateTime(2);
|
||||
portfolioTrade.Shares = sqlDataReader.GetDouble(3);
|
||||
portfolioTrade.Price = sqlDataReader.GetDouble(4);
|
||||
portfolioTrade.BuySell = sqlDataReader.GetString(5);
|
||||
portfolioTrade.Account = sqlDataReader.GetString(6);
|
||||
portfolioTrade.Status = sqlDataReader.GetString(7);
|
||||
portfolioTrade.Commission = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) portfolioTrade.SellDate = sqlDataReader.GetDateTime(9);
|
||||
else portfolioTrade.SellDate = Utility.Epoch;
|
||||
if (!sqlDataReader.IsDBNull(10)) portfolioTrade.SellPrice = sqlDataReader.GetDouble(10);
|
||||
else portfolioTrade.SellPrice = double.NaN;
|
||||
portfolioTrades.Add(portfolioTrade);
|
||||
}
|
||||
return portfolioTrades;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static PortfolioTrades GetOpenTradesSymbol(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
PortfolioTrades portfolioTrades = new PortfolioTrades();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select trade_id,symbol,trade_date,shares,price,buysell,account,status,commission,date_sold,sell_price from trades ");
|
||||
sb.Append(" where symbol='").Append(symbol).Append("'");
|
||||
sb.Append(" and status='").Append("OPEN").Append("'");
|
||||
sb.Append(" order by trade_date asc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
PortfolioTrade portfolioTrade = new PortfolioTrade();
|
||||
portfolioTrade.TradeId = sqlDataReader.GetInt32(0);
|
||||
portfolioTrade.Symbol = sqlDataReader.GetString(1);
|
||||
portfolioTrade.TradeDate = sqlDataReader.GetDateTime(2);
|
||||
portfolioTrade.Shares = sqlDataReader.GetDouble(3);
|
||||
portfolioTrade.Price = sqlDataReader.GetDouble(4);
|
||||
portfolioTrade.BuySell = sqlDataReader.GetString(5);
|
||||
portfolioTrade.Account = sqlDataReader.GetString(6);
|
||||
portfolioTrade.Status = sqlDataReader.GetString(7);
|
||||
portfolioTrade.Commission = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) portfolioTrade.SellDate = sqlDataReader.GetDateTime(9);
|
||||
else portfolioTrade.SellDate = Utility.Epoch;
|
||||
if (!sqlDataReader.IsDBNull(10)) portfolioTrade.SellPrice = sqlDataReader.GetDouble(10);
|
||||
else portfolioTrade.SellPrice = double.NaN;
|
||||
portfolioTrades.Add(portfolioTrade);
|
||||
}
|
||||
return portfolioTrades;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static PortfolioTrades GetTradesSymbol(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
PortfolioTrades portfolioTrades = new PortfolioTrades();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select trade_id,symbol,trade_date,shares,price,buysell,account,status,commission,date_sold,sell_price from trades ");
|
||||
sb.Append(" where symbol='").Append(symbol).Append("'");
|
||||
sb.Append(" order by trade_date asc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
PortfolioTrade portfolioTrade = new PortfolioTrade();
|
||||
portfolioTrade.TradeId = sqlDataReader.GetInt32(0);
|
||||
portfolioTrade.Symbol = sqlDataReader.GetString(1);
|
||||
portfolioTrade.TradeDate = sqlDataReader.GetDateTime(2);
|
||||
portfolioTrade.Shares = sqlDataReader.GetDouble(3);
|
||||
portfolioTrade.Price = sqlDataReader.GetDouble(4);
|
||||
portfolioTrade.BuySell = sqlDataReader.GetString(5);
|
||||
portfolioTrade.Account = sqlDataReader.GetString(6);
|
||||
portfolioTrade.Status = sqlDataReader.GetString(7);
|
||||
portfolioTrade.Commission = sqlDataReader.GetDouble(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) portfolioTrade.SellDate = sqlDataReader.GetDateTime(9);
|
||||
else portfolioTrade.SellDate = Utility.Epoch;
|
||||
if (!sqlDataReader.IsDBNull(10)) portfolioTrade.SellPrice = sqlDataReader.GetDouble(10);
|
||||
else portfolioTrade.SellPrice = double.NaN;
|
||||
portfolioTrades.Add(portfolioTrade);
|
||||
}
|
||||
return BreakoutTrades(portfolioTrades);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static PortfolioTrades BreakoutTrades(PortfolioTrades portfolioTrades)
|
||||
{
|
||||
PortfolioTrades allTrades = new PortfolioTrades();
|
||||
foreach (PortfolioTrade portfolioTrade in portfolioTrades)
|
||||
{
|
||||
if (portfolioTrade.IsOpen) allTrades.Add(portfolioTrade);
|
||||
else
|
||||
{
|
||||
PortfolioTrade openTrade = new PortfolioTrade();
|
||||
PortfolioTrade closedTrade = new PortfolioTrade();
|
||||
openTrade.Symbol = portfolioTrade.Symbol;
|
||||
openTrade.Shares = portfolioTrade.Shares;
|
||||
openTrade.Account = portfolioTrade.Account;
|
||||
openTrade.BuySell = "B";
|
||||
openTrade.Status = "OPEN";
|
||||
openTrade.Commission = 0;
|
||||
openTrade.Price = portfolioTrade.Price;
|
||||
openTrade.TradeDate = portfolioTrade.TradeDate;
|
||||
openTrade.TradeId=portfolioTrade.TradeId;
|
||||
allTrades.Add(openTrade);
|
||||
closedTrade.Symbol = portfolioTrade.Symbol;
|
||||
closedTrade.Shares = portfolioTrade.Shares;
|
||||
closedTrade.Account = portfolioTrade.Account;
|
||||
closedTrade.BuySell = "S";
|
||||
closedTrade.Status = "CLOSED";
|
||||
closedTrade.Commission = 0;
|
||||
closedTrade.Price = portfolioTrade.SellPrice; // closed trade gets a price of the sell price
|
||||
closedTrade.SellPrice = portfolioTrade.SellPrice; // closed trade also gets a sell price of sell price. Ref:CombineLots() function depends on the SellPrice being set.
|
||||
closedTrade.TradeDate = portfolioTrade.SellDate;
|
||||
closedTrade.TradeId=portfolioTrade.TradeId;
|
||||
allTrades.Add(closedTrade);
|
||||
}
|
||||
}
|
||||
return allTrades;
|
||||
}
|
||||
public static List<DateTime> GetOpenTradeDates()
|
||||
{
|
||||
Profiler profiler = new Profiler();
|
||||
List<DateTime> openTradeDates = new List<DateTime>();
|
||||
try
|
||||
{
|
||||
DateGenerator dateGenerator = new DateGenerator();
|
||||
DateTime minTradeDate = GetMinTradeDate();
|
||||
DateTime maxTradeDate = PricingDA.GetLatestDate(); // use the latest date for which we have pricing information
|
||||
List<DateTime> historicalDates = PricingDA.GetPricingDates(minTradeDate);
|
||||
foreach (DateTime historicalDate in historicalDates)
|
||||
{
|
||||
PortfolioTrades portfolioTrades = GetOpenTradesAsOf(historicalDate);
|
||||
if (null == portfolioTrades || 0 == portfolioTrades.Count) continue;
|
||||
openTradeDates.Add(historicalDate);
|
||||
}
|
||||
return openTradeDates;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return openTradeDates;
|
||||
}
|
||||
finally
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, String.Format("PortfolioDA.GetOpenTradeDates: Done, took {0}(ms)", profiler.End()));
|
||||
}
|
||||
}
|
||||
public static DateTime GetMinTradeDate()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
DateTime minTradeDate=Utility.Epoch;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select min(trade_date) from trades");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (sqlDataReader.Read()) minTradeDate = sqlDataReader.GetDateTime(0);
|
||||
return minTradeDate;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return minTradeDate;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static int GetMaxTradeId()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
int tradeId = -1;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select max(trade_id) from trades");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (sqlDataReader.Read()) tradeId = sqlDataReader.GetInt32(0);
|
||||
return tradeId;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return -1;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool DeleteTrade(int tradeId)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("delete from trades where trade_id=");
|
||||
sb.Append(tradeId.ToString());
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static List<String> GetAccounts()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
List<String> watchList = new List<String>();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select account from accounts order by 1 desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
String symbol = sqlDataReader.GetString(0);
|
||||
watchList.Add(symbol);
|
||||
}
|
||||
return watchList;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static List<String> GetAccountsWithOpenTrades()
|
||||
{
|
||||
try
|
||||
{
|
||||
PortfolioTrades portfolioTrades=GetOpenTrades();
|
||||
List<String> accounts=(from portfolioTrade in portfolioTrades select portfolioTrade.Account).Distinct().ToList();
|
||||
return accounts;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
281
MarketData/MarketDataLib/DataAccess/PremarketDA.cs
Executable file
281
MarketData/MarketDataLib/DataAccess/PremarketDA.cs
Executable file
@@ -0,0 +1,281 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class PremarketDA
|
||||
{
|
||||
private PremarketDA()
|
||||
{
|
||||
}
|
||||
//SELECT market, change_value,change_percent,timestamp, STR_TO_DATE(SUBSTRING(TIMESTAMP,12,11),'%h:%i:%s %p') time_of_day,CONVERT(TIMESTAMP,DATE) AS the_day
|
||||
//FROM premarket WHERE market='S&P' ORDER BY the_day DESC, time_of_day DESC
|
||||
|
||||
public static bool AddElements(PremarketElements premarketElements)
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlTransaction sqlTransaction=null;
|
||||
String strQuery=null;
|
||||
|
||||
try
|
||||
{
|
||||
if(null==premarketElements||0==premarketElements.Count) return false;
|
||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction=sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
for(int index=0;index<premarketElements.Count;index++)
|
||||
{
|
||||
PremarketElement premarketElement=premarketElements[index];
|
||||
if(String.IsNullOrEmpty(premarketElement.Market))continue;
|
||||
if(Utility.IsEpoch(premarketElement.Timestamp))continue;
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.Append("insert into premarket (market,change_value,change_percent,timestamp)");
|
||||
sb.Append("values(");
|
||||
sb.Append("'").Append(premarketElement.Market).Append("'").Append(",");
|
||||
if(double.NaN.Equals(premarketElement.ChangeValue)) sb.Append("null").Append(",");
|
||||
else sb.Append(premarketElement.ChangeValue).Append(","); ;
|
||||
if(double.NaN.Equals(premarketElement.ChangePercent)) sb.Append("null").Append(",");
|
||||
else sb.Append(premarketElement.ChangePercent).Append(",");
|
||||
sb.Append("'").Append(SqlUtils.ToSqlDateTimeTT(premarketElement.Timestamp)).Append("'");
|
||||
|
||||
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 PremarketElements GetLatestPremarketData()
|
||||
{
|
||||
PremarketElements premarketElements=new PremarketElements();
|
||||
List<String> markets=GetDistinctMarkets();
|
||||
if(null==markets)return null;
|
||||
foreach(String market in markets)
|
||||
{
|
||||
PremarketElement premarketElement=GetLatestPremarketData(market);
|
||||
if(null==premarketElement)continue;
|
||||
premarketElements.Add(premarketElement);
|
||||
|
||||
}
|
||||
return premarketElements;
|
||||
}
|
||||
public static PremarketElement GetLatestPremarketData(String market)
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery=null;
|
||||
PremarketElement premarketElement=new PremarketElement();
|
||||
|
||||
try
|
||||
{
|
||||
List<DateTime> availableMarketDates=GetAvailableMarketDates(market);
|
||||
if(null==availableMarketDates || 0==availableMarketDates.Count)return null;
|
||||
DateTime marketDate=availableMarketDates.Take(1).FirstOrDefault();
|
||||
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select market,change_value,change_percent,timestamp,STR_TO_DATE(SUBSTRING(TIMESTAMP,12,11),'%h:%i:%s %p') MILITARY_TIME,CONVERT(TIMESTAMP,DATE) DATE from premarket ");
|
||||
sb.Append("WHERE MARKET='").Append(market).Append("' and CONVERT(TIMESTAMP,DATE)=").Append(SqlUtils.AddQuotes(SqlUtils.SqlString(SqlUtils.FormatDate(marketDate))));
|
||||
sb.Append("ORDER BY MILITARY_TIME DESC LIMIT 10");
|
||||
strQuery=sb.ToString();
|
||||
sqlCommand=new MySqlCommand(strQuery,sqlConnection);
|
||||
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
if(!sqlDataReader.Read())return null;
|
||||
if(!sqlDataReader.IsDBNull(0)) premarketElement.Market=sqlDataReader.GetString(0);
|
||||
if(!sqlDataReader.IsDBNull(1)) premarketElement.ChangeValue=sqlDataReader.GetDouble(1);
|
||||
if(!sqlDataReader.IsDBNull(2)) premarketElement.ChangePercent=sqlDataReader.GetDouble(2);
|
||||
if(!sqlDataReader.IsDBNull(3)) premarketElement.Timestamp=Utility.ParseDate(sqlDataReader.GetString(3));
|
||||
if(String.IsNullOrEmpty(premarketElement.Market)) return null;
|
||||
return premarketElement;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader) sqlDataReader.Close();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DateTime GetLatestMarketDate()
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery=null;
|
||||
DateTime latestMarketDate=Utility.Epoch;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("SELECT MAX(CONVERT(TIMESTAMP,DATE)) FROM premarket ");
|
||||
strQuery=sb.ToString();
|
||||
sqlCommand=new MySqlCommand(strQuery,sqlConnection);
|
||||
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
if(sqlDataReader.Read())
|
||||
{
|
||||
latestMarketDate=sqlDataReader.GetDateTime(0);
|
||||
}
|
||||
return latestMarketDate;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return latestMarketDate;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader) sqlDataReader.Close();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static List<DateTime> GetAvailableMarketDates(String market)
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery=null;
|
||||
List<DateTime> availableMarketDates=new List<DateTime>();
|
||||
DateGenerator dateGenerator=new DateGenerator();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("SELECT DISTINCT(CONVERT(TIMESTAMP,DATE)) FROM premarket where market=").Append(SqlUtils.AddQuotes(market)).Append(" ORDER BY 1 DESC");
|
||||
strQuery=sb.ToString();
|
||||
sqlCommand=new MySqlCommand(strQuery,sqlConnection);
|
||||
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
DateTime marketDate=sqlDataReader.GetDateTime(0);
|
||||
if(!dateGenerator.IsMarketOpen(marketDate)) continue;
|
||||
availableMarketDates.Add(marketDate);
|
||||
}
|
||||
return availableMarketDates;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader) sqlDataReader.Close();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static PremarketElements GetLatestPremarketData(String market,DateTime date,bool constrainToMarketHours=true)
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery=null;
|
||||
PremarketElements premarketElements=new PremarketElements();
|
||||
DateGenerator dateGenerator=new DateGenerator();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select market,change_value,change_percent,timestamp,STR_TO_DATE(SUBSTRING(TIMESTAMP,12,11),'%h:%i:%s %p') MILITARY_TIME,CONVERT(TIMESTAMP,DATE) DATE from premarket ");
|
||||
sb.Append("WHERE MARKET='").Append(market).Append("' and CONVERT(TIMESTAMP,DATE)=").Append(SqlUtils.AddQuotes(SqlUtils.FormatDate(date)));
|
||||
if(constrainToMarketHours)sb.Append(" AND STR_TO_DATE(SUBSTRING(TIMESTAMP,12,11),'%h:%i:%s %p')<'16:30:00' ");
|
||||
sb.Append(" ORDER BY MILITARY_TIME ASC");
|
||||
strQuery=sb.ToString();
|
||||
sqlCommand=new MySqlCommand(strQuery,sqlConnection);
|
||||
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
PremarketElement premarketElement=new PremarketElement();
|
||||
if(!sqlDataReader.IsDBNull(0)) premarketElement.Market=sqlDataReader.GetString(0);
|
||||
if(!sqlDataReader.IsDBNull(1)) premarketElement.ChangeValue=sqlDataReader.GetDouble(1);
|
||||
if(!sqlDataReader.IsDBNull(2)) premarketElement.ChangePercent=sqlDataReader.GetDouble(2);
|
||||
if(!sqlDataReader.IsDBNull(3)) premarketElement.Timestamp=Utility.ParseDate(sqlDataReader.GetString(3));
|
||||
if(String.IsNullOrEmpty(premarketElement.Market)) continue;
|
||||
if(!dateGenerator.IsMarketOpen(premarketElement.Timestamp))continue;
|
||||
premarketElements.Add(premarketElement);
|
||||
}
|
||||
return premarketElements;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader) sqlDataReader.Close();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static List<String> GetDistinctMarkets()
|
||||
{
|
||||
MySqlConnection sqlConnection=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery=null;
|
||||
List<String> markets=new List<String>();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select distinct market from premarket order by 1 asc");
|
||||
strQuery=sb.ToString();
|
||||
sqlCommand=new MySqlCommand(strQuery,sqlConnection);
|
||||
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
markets.Add(sqlDataReader.GetString(0));
|
||||
}
|
||||
return markets;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
||||
if(null!=sqlDataReader) sqlDataReader.Close();
|
||||
if(null!=sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
1518
MarketData/MarketDataLib/DataAccess/PricingDA.cs
Executable file
1518
MarketData/MarketDataLib/DataAccess/PricingDA.cs
Executable file
File diff suppressed because it is too large
Load Diff
485
MarketData/MarketDataLib/DataAccess/SECFilingDA.cs
Executable file
485
MarketData/MarketDataLib/DataAccess/SECFilingDA.cs
Executable file
@@ -0,0 +1,485 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class SECFilingDA
|
||||
{
|
||||
private SECFilingDA()
|
||||
{
|
||||
}
|
||||
public static List<String> GetDistinctFilingSymbols()
|
||||
{
|
||||
List<String> distinctSymbols = new List<String>();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select distinct(symbol) from secfilings order by symbol asc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
distinctSymbols.Add(sqlDataReader.GetString(0));
|
||||
}
|
||||
return distinctSymbols;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection = null;
|
||||
}
|
||||
if (null != sqlDataReader)
|
||||
{
|
||||
sqlDataReader.Close();
|
||||
sqlDataReader = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static List<DateTime> GetDistinctFilingDatesForSymbol(String symbol)
|
||||
{
|
||||
List<DateTime> distinctFilingDates = new List<DateTime>();
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select distinct(filing_date) from secfilings where symbol=");
|
||||
sb.Append(SqlUtils.AddQuotes(symbol));
|
||||
sb.Append(" order by 1 desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
distinctFilingDates.Add(sqlDataReader.GetDateTime(0));
|
||||
}
|
||||
return distinctFilingDates;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection = null;
|
||||
}
|
||||
if (null != sqlDataReader)
|
||||
{
|
||||
sqlDataReader.Close();
|
||||
sqlDataReader = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static SEC13Info GetSEC13Info(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
SEC13Info sec13Info=new SEC13Info();
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sec13Info.Symbol=symbol;
|
||||
sb.Append(" select symbol,max(filing_date),count(*) from secfilings where symbol='").Append(symbol).Append("' and (form like '%13D%' or form like '%13G%')");
|
||||
sb.Append(" group by symbol having datediff(curdate(),max(filing_date))<180");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return sec13Info;
|
||||
sec13Info.SEC13=true;
|
||||
sec13Info.MostRecentFilingDate=sqlDataReader.GetDateTime(1);
|
||||
sec13Info.Filings=sqlDataReader.GetInt32(2);
|
||||
return sec13Info;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return sec13Info;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection = null;
|
||||
}
|
||||
if (null != sqlDataReader)
|
||||
{
|
||||
sqlDataReader.Close();
|
||||
sqlDataReader = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static bool HaveSECFiling(SECFiling secFiling)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select count(*) from secfilings ");
|
||||
sb.Append("where symbol=").Append(SqlUtils.AddQuotes(secFiling.Symbol)).Append(" ");
|
||||
sb.Append("and filing_date=").Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(secFiling.FilingDate))).Append(" ");
|
||||
sb.Append("and sec_accession_number=").Append(SqlUtils.AddQuotes(secFiling.SECAccessionNumber)).Append(" ");
|
||||
sb.Append("and sequence=").Append(secFiling.Sequence).Append(" ");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return false;
|
||||
int recordCount = sqlDataReader.GetInt32(0);
|
||||
return recordCount > 0 ? true : false;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection = null;
|
||||
}
|
||||
if (null != sqlDataReader)
|
||||
{
|
||||
sqlDataReader.Close();
|
||||
sqlDataReader = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static bool DeleteSECFiling(SECFiling secFiling,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand =null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("delete from secfilings ");
|
||||
sb.Append("where symbol=").Append(SqlUtils.AddQuotes(secFiling.Symbol)).Append(" ");
|
||||
sb.Append("and filing_date=").Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(secFiling.FilingDate)));
|
||||
sb.Append("and sec_accession_number=").Append(SqlUtils.AddQuotes(secFiling.SECAccessionNumber));
|
||||
sb.Append("and sequence=").Append(secFiling.Sequence);
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection,sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection = null;
|
||||
}
|
||||
if (null != sqlDataReader)
|
||||
{
|
||||
sqlDataReader.Close();
|
||||
sqlDataReader = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static bool InsertOrUpdateSECFilings(SECFilings secFilings)
|
||||
{
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlConnection sqlConnection = null;
|
||||
|
||||
try
|
||||
{
|
||||
secFilings=secFilings.Distinct();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
for (int index = 0; index < secFilings.Count; index++)
|
||||
{
|
||||
SECFiling secFiling = secFilings[index];
|
||||
if (HaveSECFiling(secFiling)) DeleteSECFiling(secFiling,sqlTransaction);
|
||||
InsertSECFiling(secFiling,sqlConnection,sqlTransaction);
|
||||
}
|
||||
sqlTransaction.Commit();
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlTransaction)
|
||||
{
|
||||
sqlTransaction.Dispose();
|
||||
sqlTransaction = null;
|
||||
}
|
||||
if (null != sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static bool InsertSECFilings(SECFilings secFilings)
|
||||
{
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlConnection sqlConnection = null;
|
||||
|
||||
try
|
||||
{
|
||||
secFilings=secFilings.Distinct();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
for (int index = 0; index < secFilings.Count; index++)
|
||||
{
|
||||
SECFiling secFiling = secFilings[index];
|
||||
if (HaveSECFiling(secFiling)) continue;
|
||||
InsertSECFiling(secFiling, sqlConnection, sqlTransaction);
|
||||
}
|
||||
sqlTransaction.Commit();
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlTransaction)
|
||||
{
|
||||
sqlTransaction.Dispose();
|
||||
sqlTransaction = null;
|
||||
}
|
||||
if (null != sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
private static bool InsertSECFiling(SECFiling secFiling,MySqlConnection sqlConnection,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
MySqlCommand sqlCommand = null;
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlCommand = new MySqlCommand();
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.Connection = sqlConnection;
|
||||
sb.Append("insert into secfilings(sec_accession_number,sequence,symbol,filing_date,form,form_text,description,sec_filing_url)");
|
||||
sb.Append(" values(?sec_accession_number,?sequence,?symbol,?filing_date,?form,?form_text,?description,?secFilingUrl);");
|
||||
MySqlParameter secAccessionNumberParameter = new MySqlParameter("?sec_accession_number", MySqlDbType.VarChar,50);
|
||||
MySqlParameter sequenceParameter = new MySqlParameter("?sequence", MySqlDbType.Int32);
|
||||
MySqlParameter symbolParameter = new MySqlParameter("?symbol", MySqlDbType.VarChar, 15);
|
||||
MySqlParameter filingDateParameter = new MySqlParameter("?filing_date", MySqlDbType.DateTime);
|
||||
MySqlParameter formParameter = new MySqlParameter("?form", MySqlDbType.VarChar,25);
|
||||
MySqlParameter formTextParameter = new MySqlParameter("?form_text", MySqlDbType.Blob, secFiling.FormText.Length);
|
||||
MySqlParameter descriptionParameter = new MySqlParameter("?description", MySqlDbType.VarChar, 128);
|
||||
MySqlParameter secFilingUrlParameter = new MySqlParameter("?secFilingUrl", MySqlDbType.VarChar, 255);
|
||||
secAccessionNumberParameter.Value = secFiling.SECAccessionNumber;
|
||||
sequenceParameter.Value = secFiling.Sequence;
|
||||
symbolParameter.Value = secFiling.Symbol;
|
||||
filingDateParameter.Value=Utility.DateTimeToStringYYYYHMMHDD(secFiling.FilingDate);
|
||||
formParameter.Value=secFiling.Form;
|
||||
formTextParameter.Value = Utility.Compress(secFiling.FormText);
|
||||
descriptionParameter.Value = secFiling.Description;
|
||||
secFilingUrlParameter.Value = secFiling.SecFilingUrl;
|
||||
sqlCommand.Parameters.Add(secAccessionNumberParameter);
|
||||
sqlCommand.Parameters.Add(symbolParameter);
|
||||
sqlCommand.Parameters.Add(filingDateParameter);
|
||||
sqlCommand.Parameters.Add(formParameter);
|
||||
sqlCommand.Parameters.Add(formTextParameter);
|
||||
sqlCommand.Parameters.Add(descriptionParameter);
|
||||
sqlCommand.Parameters.Add(secFilingUrlParameter);
|
||||
sqlCommand.Parameters.Add(sequenceParameter);
|
||||
sqlCommand.CommandText = sb.ToString();
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand)
|
||||
{
|
||||
sqlCommand.Dispose();
|
||||
sqlCommand = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static SECFilings GetSECFilings(String symbol)
|
||||
{
|
||||
MySqlCommand sqlCommand = null;
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
SECFilings secFilings = new SECFilings();
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlCommand = null;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,filing_date,form,length(form_text),form_text,description,sec_filing_url,sequence,sec_accession_number from secfilings ");
|
||||
sb.Append("where symbol='").Append(symbol).Append("' order by filing_date desc");
|
||||
String strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
SECFiling secFiling = new SECFiling();
|
||||
secFiling.Symbol = sqlDataReader.GetString(0);
|
||||
secFiling.FilingDate = sqlDataReader.GetDateTime(1);
|
||||
secFiling.Form = sqlDataReader.GetString(2);
|
||||
int lengthFormText=sqlDataReader.GetInt32(3);
|
||||
byte[] formTextBytes=new byte[lengthFormText];
|
||||
sqlDataReader.GetBytes(4,0,formTextBytes,0,lengthFormText);
|
||||
secFiling.FormText=Utility.Decompress(formTextBytes);
|
||||
secFiling.Description = sqlDataReader.GetString(5);
|
||||
secFiling.SecFilingUrl = sqlDataReader.GetString(6);
|
||||
secFiling.Sequence = sqlDataReader.GetInt32(7);
|
||||
secFiling.SECAccessionNumber = sqlDataReader.GetString(8);
|
||||
secFilings.Add(secFiling);
|
||||
}
|
||||
return secFilings;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection = null;
|
||||
}
|
||||
if (null != sqlCommand)
|
||||
{
|
||||
sqlCommand.Dispose();
|
||||
sqlCommand = null;
|
||||
}
|
||||
if (null != sqlDataReader)
|
||||
{
|
||||
sqlDataReader.Close();
|
||||
sqlDataReader = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static SECFilings GetSECFilings(String symbol,DateTime filingDate)
|
||||
{
|
||||
MySqlCommand sqlCommand = null;
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
SECFilings secFilings = new SECFilings();
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlCommand = null;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,filing_date,form,length(form_text),form_text,description,sec_filing_url,sequence,sec_accession_number from secfilings ");
|
||||
sb.Append("where symbol=").Append(SqlUtils.AddQuotes(symbol));
|
||||
sb.Append(" and filing_date=").Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(filingDate)));
|
||||
sb.Append(" order by filing_date desc");
|
||||
String strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
SECFiling secFiling = new SECFiling();
|
||||
secFiling.Symbol = sqlDataReader.GetString(0);
|
||||
secFiling.FilingDate = sqlDataReader.GetDateTime(1);
|
||||
secFiling.Form = sqlDataReader.GetString(2);
|
||||
int lengthFormText = sqlDataReader.GetInt32(3);
|
||||
byte[] formTextBytes = new byte[lengthFormText];
|
||||
sqlDataReader.GetBytes(4, 0, formTextBytes, 0, lengthFormText);
|
||||
secFiling.FormText = Utility.Decompress(formTextBytes);
|
||||
secFiling.Description = sqlDataReader.GetString(5);
|
||||
secFiling.SecFilingUrl = sqlDataReader.GetString(6);
|
||||
secFiling.Sequence = sqlDataReader.GetInt32(7);
|
||||
secFiling.SECAccessionNumber = sqlDataReader.GetString(8);
|
||||
secFilings.Add(secFiling);
|
||||
}
|
||||
return secFilings;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlConnection)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
sqlConnection = null;
|
||||
}
|
||||
if (null != sqlCommand)
|
||||
{
|
||||
sqlCommand.Dispose();
|
||||
sqlCommand = null;
|
||||
}
|
||||
if (null != sqlDataReader)
|
||||
{
|
||||
sqlDataReader.Close();
|
||||
sqlDataReader = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
335
MarketData/MarketDataLib/DataAccess/SplitsDA.cs
Executable file
335
MarketData/MarketDataLib/DataAccess/SplitsDA.cs
Executable file
@@ -0,0 +1,335 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class SplitsDA
|
||||
{
|
||||
private SplitsDA()
|
||||
{
|
||||
}
|
||||
public static bool InsertSplit(Split split)
|
||||
{
|
||||
Splits splits=new Splits();
|
||||
splits.Add(split);
|
||||
InsertSplits(splits);
|
||||
return true;
|
||||
}
|
||||
public static bool ContainsSplit(Split split)
|
||||
{
|
||||
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 splits where symbol='").Append(split.Symbol).Append("' ");
|
||||
sb.Append("and effective_date='").Append(SqlUtils.SqlDate(split.EffectiveDate)).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);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool UpdateSplit(Split split)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if(!ContainsSplit(split))return false;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime modified=DateTime.Now;
|
||||
if(split.Applied&&(Utility.IsEpoch(split.AppliedLeastRecent)||Utility.IsEpoch(split.AppliedMostRecent)))
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Applied split requires AppliedRecent and AppliedMostRecent dates.");
|
||||
return false;
|
||||
}
|
||||
if(!split.Applied)return false;
|
||||
sb.Append("update splits set ");
|
||||
sb.Append("applied=").Append(split.Applied).Append(", ");
|
||||
sb.Append("applied_least_recent='").Append(SqlUtils.SqlDate(split.AppliedLeastRecent)).Append("'").Append(", ");
|
||||
sb.Append("applied_most_recent='").Append(SqlUtils.SqlDate(split.AppliedMostRecent)).Append("'").Append(", ");
|
||||
sb.Append("modified='").Append(SqlUtils.SqlDate(modified)).Append("'").Append(" ");
|
||||
sb.Append(" where symbol='").Append(split.Symbol).Append("'").Append(" and ");
|
||||
sb.Append("effective_date='").Append(SqlUtils.SqlDate(split.EffectiveDate)).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,"Query was '" + strQuery + "'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Splits GetUnappliedSplits()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
String strQuery = null;
|
||||
Splits splits=new Splits();
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime modified=DateTime.Now;
|
||||
sb.Append("select symbol,effective_date,ratio,applied,applied_least_recent,applied_most_recent,modified,created from splits where Symbol in(select symbol from securitymaster) and applied=0 order by symbol,effective_date desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
Split split=new Split();
|
||||
split.Symbol=sqlDataReader.GetString(0);
|
||||
split.EffectiveDate=sqlDataReader.GetDateTime(1);
|
||||
split.StrRatio=sqlDataReader.GetString(2);
|
||||
split.Applied=sqlDataReader.GetInt32(3)==0?false:true;
|
||||
if(!sqlDataReader.IsDBNull(4))split.AppliedLeastRecent=sqlDataReader.GetDateTime(4);
|
||||
if(!sqlDataReader.IsDBNull(5))split.AppliedMostRecent=sqlDataReader.GetDateTime(5);
|
||||
if(!sqlDataReader.IsDBNull(6))split.Modified=sqlDataReader.GetDateTime(6);
|
||||
if(!sqlDataReader.IsDBNull(7))split.Created=sqlDataReader.GetDateTime(7);
|
||||
splits.Add(split);
|
||||
}
|
||||
return splits;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was '" + strQuery + "'");
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Splits GetSplits(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
String strQuery = null;
|
||||
Splits splits=new Splits();
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime modified=DateTime.Now;
|
||||
sb.Append("select symbol,effective_date,ratio,applied,applied_least_recent,applied_most_recent,modified,created from splits ");
|
||||
sb.Append(" where symbol='").Append(symbol).Append("'");
|
||||
sb.Append(" order by symbol,effective_date desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
Split split=new Split();
|
||||
split.Symbol=sqlDataReader.GetString(0);
|
||||
split.EffectiveDate=sqlDataReader.GetDateTime(1);
|
||||
split.StrRatio=sqlDataReader.GetString(2);
|
||||
split.Applied=sqlDataReader.GetInt32(3)==0?false:true;
|
||||
if(!sqlDataReader.IsDBNull(4))split.AppliedLeastRecent=sqlDataReader.GetDateTime(4);
|
||||
if(!sqlDataReader.IsDBNull(5))split.AppliedMostRecent=sqlDataReader.GetDateTime(5);
|
||||
if(!sqlDataReader.IsDBNull(6))split.Modified=sqlDataReader.GetDateTime(6);
|
||||
if(!sqlDataReader.IsDBNull(7))split.Created=sqlDataReader.GetDateTime(7);
|
||||
splits.Add(split);
|
||||
}
|
||||
return splits;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was '" + strQuery + "'");
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Splits GetSplits()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlDataReader sqlDataReader=null;
|
||||
String strQuery = null;
|
||||
Splits splits=new Splits();
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime modified=DateTime.Now;
|
||||
sb.Append("select symbol,effective_date,ratio,applied,applied_least_recent,applied_most_recent,modified,created from splits order by symbol,effective_date desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader=sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
Split split=new Split();
|
||||
split.Symbol=sqlDataReader.GetString(0);
|
||||
split.EffectiveDate=sqlDataReader.GetDateTime(1);
|
||||
split.StrRatio=sqlDataReader.GetString(2);
|
||||
split.Applied=sqlDataReader.GetInt32(3)==0?false:true;
|
||||
if(!sqlDataReader.IsDBNull(4))split.AppliedLeastRecent=sqlDataReader.GetDateTime(4);
|
||||
if(!sqlDataReader.IsDBNull(5))split.AppliedMostRecent=sqlDataReader.GetDateTime(5);
|
||||
if(!sqlDataReader.IsDBNull(6))split.Modified=sqlDataReader.GetDateTime(6);
|
||||
if(!sqlDataReader.IsDBNull(7))split.Created=sqlDataReader.GetDateTime(7);
|
||||
splits.Add(split);
|
||||
}
|
||||
return splits;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Query was '" + strQuery + "'");
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool InsertSplits(Splits splits)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
DateTime modified=DateTime.Now;
|
||||
DateTime created=modified;
|
||||
foreach(Split split in splits)
|
||||
{
|
||||
sb=new StringBuilder();
|
||||
if(ContainsSplit(split))continue;
|
||||
sb.Append("insert into splits (symbol,effective_date,ratio,applied,applied_least_recent,applied_most_recent,modified,created) ");
|
||||
sb.Append("values(");
|
||||
sb.Append("'").Append(split.Symbol).Append("'").Append(",");
|
||||
sb.Append("'").Append(SqlUtils.SqlDate(split.EffectiveDate)).Append("'").Append(",");
|
||||
sb.Append("'").Append(split.StrRatio).Append("'").Append(",");
|
||||
sb.Append("").Append(split.Applied).Append("").Append(",");
|
||||
if(split.Applied&&(Utility.IsEpoch(split.AppliedLeastRecent)||Utility.IsEpoch(split.AppliedMostRecent)))
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,"Applied split requires LeastRecentDate and MostRecentDate to be set.");
|
||||
continue;
|
||||
}
|
||||
if(!Utility.IsEpoch(split.AppliedLeastRecent))sb.Append("'").Append(SqlUtils.SqlDate(split.AppliedLeastRecent)).Append("'").Append(",");
|
||||
else sb.Append("null,");
|
||||
if(!Utility.IsEpoch(split.AppliedLeastRecent))sb.Append("'").Append(SqlUtils.SqlDate(split.AppliedMostRecent)).Append("'").Append(",");
|
||||
else sb.Append("null,");
|
||||
sb.Append("'").Append(SqlUtils.SqlDate(modified)).Append("'").Append(",");
|
||||
sb.Append("'").Append(SqlUtils.SqlDate(modified)).Append("'").Append("");
|
||||
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,"Query was '" + strQuery + "'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static bool DeleteSplits(Splits splits, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
foreach(Split split in splits)DeleteSplit(split,sqlConnection,sqlTransaction);
|
||||
return true;
|
||||
}
|
||||
private static bool DeleteSplit(Split split, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
sb.Append("delete from splits where ");
|
||||
sb.Append("symbol='").Append(split.Symbol).Append("'");
|
||||
sb.Append(" and ");
|
||||
sb.Append("effective_date='").Append(SqlUtils.SqlDate(split.EffectiveDate)).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
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
135
MarketData/MarketDataLib/DataAccess/UserDA.cs
Executable file
135
MarketData/MarketDataLib/DataAccess/UserDA.cs
Executable file
@@ -0,0 +1,135 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.Utils;
|
||||
using MarketData.MarketDataModel.User;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class UserDA
|
||||
{
|
||||
private UserDA()
|
||||
{
|
||||
}
|
||||
|
||||
public static bool UserExists(String username)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if(String.IsNullOrEmpty(username))return false;
|
||||
username = username.ToLower();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("user_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select count(*) from users where username=").Append("'").Append(username).Append("'");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if(!sqlDataReader.Read())return false;
|
||||
int count = sqlDataReader.GetInt32(0);
|
||||
return count>0?true:false;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null != sqlDataReader)sqlDataReader.Dispose();
|
||||
if(null != sqlCommand)sqlCommand.Dispose();
|
||||
if(null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public static User GetUser(String username)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
String strQuery = null;
|
||||
User user = new User();
|
||||
|
||||
try
|
||||
{
|
||||
if(null == username)return null;
|
||||
if(!UserExists(username))return null;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("user_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select username, salt, hash from users where ");
|
||||
sb.Append("username=").Append("'").Append(username).Append("'");
|
||||
sb.Append(";");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if(sqlDataReader.Read())
|
||||
{
|
||||
user.Username = sqlDataReader.GetString(0);
|
||||
user.Salt = sqlDataReader.GetString(1);
|
||||
user.Hash = sqlDataReader.GetString(2);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlDataReader)sqlDataReader.Dispose();
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public static bool AddUser(User user)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if(null == user || null == user.Username || null == user.Hash || null == user.Salt)return false;
|
||||
if(UserExists(user.Username))return false;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("user_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into users(username,hash,salt,created_by,created_on,modified_by,modified_on) values(");
|
||||
sb.Append(SqlUtils.AddQuotes(user.Username)).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(user.Hash)).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(user.Salt)).Append(",");
|
||||
sb.Append("CURRENT_USER").Append(",");
|
||||
sb.Append("CURRENT_TIMESTAMP").Append(",");
|
||||
sb.Append("CURRENT_USER").Append(",");
|
||||
sb.Append("CURRENT_TIMESTAMP").Append("");
|
||||
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);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
437
MarketData/MarketDataLib/DataAccess/ValuationDA.cs
Executable file
437
MarketData/MarketDataLib/DataAccess/ValuationDA.cs
Executable file
@@ -0,0 +1,437 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class ValuationDA
|
||||
{
|
||||
private ValuationDA()
|
||||
{
|
||||
}
|
||||
public static List<DateTime> GetValuationDates()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
List<DateTime> valuationDates = new List<DateTime>();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select distinct(valuation_date) from valuations order by valuation_date desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
valuationDates.Add(sqlDataReader.GetDateTime(0));
|
||||
}
|
||||
return valuationDates;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DateTime GetLatestValuationDate()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
DateTime valuationDate=Utility.Epoch;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select max(valuation_date) from valuations");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (sqlDataReader.Read()) valuationDate = sqlDataReader.GetDateTime(0);
|
||||
return valuationDate;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return valuationDate;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static Valuations GetValuation(DateTime valuationDate)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
Valuations valuations = new Valuations();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (Utility.IsEpoch(valuationDate)) return null;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("select valuation_date,symbol,company,next_earnings_date,long_term_debt,revenue,debt_load,roic_avg,bvps_dates,avg_equity_growth,avg_equity_growth_2y,avg_equity_growth_4y,eps_dates,avg_eps_growth,avg_eps_growth_2y,avg_eps_growth_4y,revenue_dates,avg_revenue_growth,avg_revenue_growth_2y,avg_revenue_growth_4y,avg_freecashflow_growth,avg_operating_cashflow,avg_working_capital,bvps,eps,pe,low_pe,trailing_pe,avg_low_trailing,current_stock_estimate_price,price_estimate_10y,todays_price_for_required_return,mos,mos_80,intrinsic_value,rgv,latest_price,upside_pcnt,downside_pcnt,mean_target_price,low_target_price,high_target_price,bargain_mos,bargain_mos_80,price_to_book,peg,implied_earnings_growth,beta90,beta2y,roic_slope,roic_dates,market_cap,earnings_yield,ebit,enterprise_value,tlb_rank_roic,fundamental_value,net_current_asset_value_per_share,roic_latest,sector,industry,debt_to_equity,sec_13,sec_13_filing_date,roc_latest,tlb_rank_roc,operating_earnings,am_rank,acquirers_multiple,dividend_yield,operating_cashflow,shares_outstanding,modified").Append(" ");
|
||||
sb.Append("from valuations where valuation_date=").Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(valuationDate)));
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
valuations.ValuationDate = valuationDate;
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
Valuation valuation = new Valuation();
|
||||
valuation.Symbol = sqlDataReader.GetString(1);
|
||||
valuation.Company = sqlDataReader.GetString(2);
|
||||
if (!sqlDataReader.IsDBNull(3)) valuation.NextEarningsDate = sqlDataReader.GetDateTime(3);
|
||||
else valuation.NextEarningsDate = Utility.Epoch;
|
||||
if (!sqlDataReader.IsDBNull(4)) valuation.LongTermDebt = sqlDataReader.GetDouble(4);
|
||||
if (!sqlDataReader.IsDBNull(5)) valuation.Revenue = sqlDataReader.GetDouble(5);
|
||||
valuation.DebtLoad = sqlDataReader.GetString(6);
|
||||
if (!sqlDataReader.IsDBNull(7)) valuation.AverageROIC = sqlDataReader.GetDouble(7);
|
||||
if (!sqlDataReader.IsDBNull(8)) valuation.BVPSDates = sqlDataReader.GetString(8);
|
||||
if (!sqlDataReader.IsDBNull(9)) valuation.AverageEquityGrowth = sqlDataReader.GetDouble(9);
|
||||
if (!sqlDataReader.IsDBNull(10)) valuation.AverageEquityGrowth2Y = sqlDataReader.GetDouble(10);
|
||||
if (!sqlDataReader.IsDBNull(11)) valuation.AverageEquityGrowth4Y = sqlDataReader.GetDouble(11);
|
||||
valuation.EPSDates = sqlDataReader.GetString(12);
|
||||
if (!sqlDataReader.IsDBNull(13)) valuation.AverageEPSGrowth = sqlDataReader.GetDouble(13);
|
||||
if (!sqlDataReader.IsDBNull(14)) valuation.AverageEPSGrowth2Y = sqlDataReader.GetDouble(14);
|
||||
if (!sqlDataReader.IsDBNull(15)) valuation.AverageEPSGrowth4Y = sqlDataReader.GetDouble(15);
|
||||
valuation.RevenueDates = sqlDataReader.GetString(16);
|
||||
if (!sqlDataReader.IsDBNull(17)) valuation.AverageRevenueGrowth = sqlDataReader.GetDouble(17);
|
||||
if (!sqlDataReader.IsDBNull(18)) valuation.AverageRevenueGrowth2Y = sqlDataReader.GetDouble(18);
|
||||
if (!sqlDataReader.IsDBNull(19)) valuation.AverageRevenueGrowth4Y = sqlDataReader.GetDouble(19);
|
||||
if (!sqlDataReader.IsDBNull(20)) valuation.AverageFreeCashflowGrowth = sqlDataReader.GetDouble(20);
|
||||
if (!sqlDataReader.IsDBNull(21)) valuation.AverageOperatingCashflow = sqlDataReader.GetDouble(21);
|
||||
if (!sqlDataReader.IsDBNull(22)) valuation.AverageWorkingCapital = sqlDataReader.GetDouble(22);
|
||||
if (!sqlDataReader.IsDBNull(23)) valuation.BVPS = sqlDataReader.GetDouble(23);
|
||||
if (!sqlDataReader.IsDBNull(24)) valuation.EPS = sqlDataReader.GetDouble(24);
|
||||
if (!sqlDataReader.IsDBNull(25)) valuation.PE = sqlDataReader.GetDouble(25);
|
||||
if (!sqlDataReader.IsDBNull(26)) valuation.LowPE = sqlDataReader.GetDouble(26);
|
||||
if (!sqlDataReader.IsDBNull(27)) valuation.TrailingPE = sqlDataReader.GetDouble(27);
|
||||
if (!sqlDataReader.IsDBNull(28)) valuation.AverageLowTrailing = sqlDataReader.GetDouble(28);
|
||||
if (!sqlDataReader.IsDBNull(29)) valuation.CurrentStockEstimatePrice = sqlDataReader.GetDouble(29);
|
||||
if (!sqlDataReader.IsDBNull(30)) valuation.PriceEstimate10Y = sqlDataReader.GetDouble(30);
|
||||
if (!sqlDataReader.IsDBNull(31)) valuation.TodaysPriceForRequiredReturn = sqlDataReader.GetDouble(31);
|
||||
if (!sqlDataReader.IsDBNull(32)) valuation.MOS = sqlDataReader.GetDouble(32);
|
||||
if (!sqlDataReader.IsDBNull(33)) valuation.MOS80 = sqlDataReader.GetDouble(33);
|
||||
if (!sqlDataReader.IsDBNull(34)) valuation.IntrinsicValue = sqlDataReader.GetDouble(34);
|
||||
if (!sqlDataReader.IsDBNull(35)) valuation.RGV = sqlDataReader.GetDouble(35);
|
||||
if (!sqlDataReader.IsDBNull(36)) valuation.LatestPrice = sqlDataReader.GetDouble(36);
|
||||
if (!sqlDataReader.IsDBNull(37)) valuation.UpsidePcnt = sqlDataReader.GetDouble(37);
|
||||
if (!sqlDataReader.IsDBNull(38)) valuation.DownsidePcnt = sqlDataReader.GetDouble(38);
|
||||
if (!sqlDataReader.IsDBNull(39)) valuation.MeanTargetPrice = sqlDataReader.GetDouble(39);
|
||||
if (!sqlDataReader.IsDBNull(40)) valuation.LowTargetPrice = sqlDataReader.GetDouble(40);
|
||||
if (!sqlDataReader.IsDBNull(41)) valuation.HighTargetPrice = sqlDataReader.GetDouble(41);
|
||||
valuation.Bargain = sqlDataReader.GetBoolean(42);
|
||||
valuation.Bargain80 = sqlDataReader.GetBoolean(43);
|
||||
if (!sqlDataReader.IsDBNull(44)) valuation.PBVPS = sqlDataReader.GetDouble(44);
|
||||
if (!sqlDataReader.IsDBNull(45)) valuation.PEG = sqlDataReader.GetDouble(45);
|
||||
if (!sqlDataReader.IsDBNull(46)) valuation.ImpliedEarningsGrowth = sqlDataReader.GetDouble(46);
|
||||
if (!sqlDataReader.IsDBNull(47)) valuation.Beta90 = sqlDataReader.GetDouble(47);
|
||||
if (!sqlDataReader.IsDBNull(48)) valuation.Beta720 = sqlDataReader.GetDouble(48);
|
||||
if (!sqlDataReader.IsDBNull(49)) valuation.ROICSlope = sqlDataReader.GetDouble(49);
|
||||
if (!sqlDataReader.IsDBNull(50)) valuation.ROICDates = sqlDataReader.GetString(50);
|
||||
if (!sqlDataReader.IsDBNull(51)) valuation.MarketCap = sqlDataReader.GetDouble(51);
|
||||
if (!sqlDataReader.IsDBNull(52)) valuation.EarningsYield = sqlDataReader.GetDouble(52);
|
||||
if (!sqlDataReader.IsDBNull(53)) valuation.EBIT = sqlDataReader.GetDouble(53);
|
||||
if (!sqlDataReader.IsDBNull(54)) valuation.EnterpriseValue = sqlDataReader.GetDouble(54);
|
||||
if (!sqlDataReader.IsDBNull(55)) valuation.TLBRankROIC = sqlDataReader.GetDouble(55);
|
||||
if (!sqlDataReader.IsDBNull(56)) valuation.FundamentalValue = sqlDataReader.GetDouble(56);
|
||||
if (!sqlDataReader.IsDBNull(57)) valuation.NetCurrentAssetValuePerShare = sqlDataReader.GetDouble(57);
|
||||
if (!sqlDataReader.IsDBNull(58)) valuation.LatestROIC = sqlDataReader.GetDouble(58);
|
||||
if (!sqlDataReader.IsDBNull(59)) valuation.Sector=sqlDataReader.GetString(59);
|
||||
if (!sqlDataReader.IsDBNull(60)) valuation.Industry = sqlDataReader.GetString(60);
|
||||
if (!sqlDataReader.IsDBNull(61)) valuation.DebtToEquity = sqlDataReader.GetDouble(61);
|
||||
if (!sqlDataReader.IsDBNull(62)) valuation.SEC13 = sqlDataReader.GetBoolean(62);
|
||||
if (!sqlDataReader.IsDBNull(63)) valuation.SEC13FilingDate = sqlDataReader.GetDateTime(63);
|
||||
if (!sqlDataReader.IsDBNull(64)) valuation.LatestROC = sqlDataReader.GetDouble(64);
|
||||
if (!sqlDataReader.IsDBNull(65)) valuation.TLBRankROC = sqlDataReader.GetDouble(65);
|
||||
if (!sqlDataReader.IsDBNull(66)) valuation.OperatingEarnings = sqlDataReader.GetDouble(66);
|
||||
if (!sqlDataReader.IsDBNull(67)) valuation.AMRank = sqlDataReader.GetDouble(67);
|
||||
if (!sqlDataReader.IsDBNull(68)) valuation.AcquirersMultiple = sqlDataReader.GetDouble(68);
|
||||
if (!sqlDataReader.IsDBNull(69)) valuation.DividendYield = sqlDataReader.GetDouble(69);
|
||||
if (!sqlDataReader.IsDBNull(70)) valuation.OperatingCashflow = sqlDataReader.GetDouble(70);
|
||||
if (!sqlDataReader.IsDBNull(71)) valuation.SharesOutstanding = sqlDataReader.GetDouble(71);
|
||||
valuation.Modified = sqlDataReader.GetDateTime(72);
|
||||
valuations.Add(valuation);
|
||||
}
|
||||
return valuations;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool AddValuations(Valuations valuations)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
String strQuery = null;
|
||||
DateTime modified = DateTime.Now;
|
||||
|
||||
try
|
||||
{
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
RemoveValuation(valuations.ValuationDate,sqlTransaction);
|
||||
foreach(Valuation valuation in valuations)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into valuations(valuation_date,symbol,company,next_earnings_date,long_term_debt,revenue,debt_load,roic_avg,roic_slope,roic_dates,bvps_dates,avg_equity_growth,avg_equity_growth_2y,avg_equity_growth_4y,eps_dates,avg_eps_growth,avg_eps_growth_2y,avg_eps_growth_4y,revenue_dates,avg_revenue_growth,avg_revenue_growth_2y,avg_revenue_growth_4y,avg_freecashflow_growth,avg_operating_cashflow,avg_working_capital,bvps,eps,pe,low_pe,trailing_pe,avg_low_trailing,current_stock_estimate_price,price_estimate_10y,todays_price_for_required_return,mos,mos_80,intrinsic_value,rgv,latest_price,upside_pcnt,downside_pcnt,mean_target_price,low_target_price,high_target_price,bargain_mos,bargain_mos_80,price_to_book,peg,implied_earnings_growth,beta90,beta2y,market_cap,earnings_yield,ebit,enterprise_value,tlb_rank_roic,fundamental_value,net_current_asset_value_per_share,roic_latest,sector,industry,debt_to_equity,sec_13,sec_13_filing_date,roc_latest,tlb_rank_roc,operating_earnings,am_rank,acquirers_multiple,dividend_yield,operating_cashflow,shares_outstanding,modified)values(");
|
||||
sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(valuations.ValuationDate))).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(valuation.Symbol)).Append(",");
|
||||
if (null == valuation.Company) sb.Append("null").Append(",");
|
||||
else sb.Append(SqlUtils.AddQuotes(SqlUtils.SqlString(valuation.Company))).Append(",");
|
||||
if (Utility.IsEpoch(valuation.NextEarningsDate)) sb.Append("null").Append(",");
|
||||
else sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(valuation.NextEarningsDate))).Append(",");
|
||||
if (double.IsNaN(valuation.LongTermDebt) || double.IsInfinity(valuation.LongTermDebt)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.LongTermDebt).Append(",");
|
||||
if (double.IsNaN(valuation.Revenue) || double.IsInfinity(valuation.Revenue)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.Revenue).Append(",");
|
||||
if (null == valuation.DebtLoad) sb.Append("null").Append(",");
|
||||
else sb.Append(SqlUtils.AddQuotes(valuation.DebtLoad)).Append(",");
|
||||
if (double.IsNaN(valuation.AverageROIC) || double.IsInfinity(valuation.AverageROIC)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AverageROIC).Append(",");
|
||||
if (double.IsNaN(valuation.ROICSlope) || double.IsInfinity(valuation.ROICSlope)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.ROICSlope).Append(",");
|
||||
if (null == valuation.ROICDates) sb.Append("null").Append(",");
|
||||
else sb.Append(SqlUtils.AddQuotes(valuation.ROICDates)).Append(",");
|
||||
if (null == valuation.BVPSDates) sb.Append("null").Append(",");
|
||||
else sb.Append(SqlUtils.AddQuotes(valuation.BVPSDates)).Append(",");
|
||||
if (double.IsNaN(valuation.AverageEquityGrowth) || double.IsInfinity(valuation.AverageEquityGrowth)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AverageEquityGrowth).Append(",");
|
||||
if (double.IsNaN(valuation.AverageEquityGrowth2Y) || double.IsInfinity(valuation.AverageEquityGrowth2Y)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AverageEquityGrowth2Y).Append(",");
|
||||
if (double.IsNaN(valuation.AverageEquityGrowth4Y) || double.IsInfinity(valuation.AverageEquityGrowth4Y)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AverageEquityGrowth4Y).Append(",");
|
||||
if (null == valuation.EPSDates) sb.Append("null").Append(",");
|
||||
else sb.Append(SqlUtils.AddQuotes(valuation.EPSDates)).Append(",");
|
||||
if (double.IsNaN(valuation.AverageEPSGrowth) || double.IsInfinity(valuation.AverageEPSGrowth)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AverageEPSGrowth).Append(",");
|
||||
if (double.IsNaN(valuation.AverageEPSGrowth2Y) || double.IsInfinity(valuation.AverageEPSGrowth2Y)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AverageEPSGrowth2Y).Append(",");
|
||||
if (double.IsNaN(valuation.AverageEPSGrowth4Y) || double.IsInfinity(valuation.AverageEPSGrowth4Y)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AverageEPSGrowth4Y).Append(",");
|
||||
if (null == valuation.RevenueDates) sb.Append("null").Append(",");
|
||||
else sb.Append(SqlUtils.AddQuotes(valuation.RevenueDates)).Append(",");
|
||||
if (double.IsNaN(valuation.AverageRevenueGrowth) || double.IsInfinity(valuation.AverageRevenueGrowth)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AverageRevenueGrowth).Append(",");
|
||||
// if (double.IsNaN(valuation.AverageRevenueGrowthQtr) || double.IsInfinity(valuation.AverageRevenueGrowthQtr)) sb.Append("null").Append(",");
|
||||
// else sb.Append(valuation.AverageRevenueGrowthQtr).Append(",");
|
||||
if (double.IsNaN(valuation.AverageRevenueGrowth2Y) || double.IsInfinity(valuation.AverageRevenueGrowth2Y)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AverageRevenueGrowth2Y).Append(",");
|
||||
if (double.IsNaN(valuation.AverageRevenueGrowth4Y) || double.IsInfinity(valuation.AverageRevenueGrowth4Y)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AverageRevenueGrowth4Y).Append(",");
|
||||
if (double.IsNaN(valuation.AverageFreeCashflowGrowth) || double.IsInfinity(valuation.AverageFreeCashflowGrowth)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AverageFreeCashflowGrowth).Append(",");
|
||||
if (double.IsNaN(valuation.AverageOperatingCashflow) || double.IsInfinity(valuation.AverageOperatingCashflow)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AverageOperatingCashflow).Append(",");
|
||||
if (double.IsNaN(valuation.AverageWorkingCapital) || double.IsInfinity(valuation.AverageWorkingCapital)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AverageWorkingCapital).Append(",");
|
||||
if (double.IsNaN(valuation.BVPS) || double.IsInfinity(valuation.BVPS)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.BVPS).Append(",");
|
||||
if (double.IsNaN(valuation.EPS) || double.IsInfinity(valuation.EPS)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.EPS).Append(",");
|
||||
if (double.IsNaN(valuation.PE) || double.IsInfinity(valuation.PE)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.PE).Append(",");
|
||||
if (double.IsNaN(valuation.LowPE) || double.IsInfinity(valuation.LowPE)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.LowPE).Append(",");
|
||||
if (double.IsNaN(valuation.TrailingPE) || double.IsInfinity(valuation.TrailingPE)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.TrailingPE).Append(",");
|
||||
if (double.IsNaN(valuation.AverageLowTrailing) || double.IsInfinity(valuation.AverageLowTrailing)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AverageLowTrailing).Append(",");
|
||||
if (double.IsNaN(valuation.CurrentStockEstimatePrice) || double.IsInfinity(valuation.CurrentStockEstimatePrice)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.CurrentStockEstimatePrice).Append(",");
|
||||
if (valuation.PriceEstimate10Y<0 || double.IsNaN(valuation.PriceEstimate10Y) || double.IsInfinity(valuation.PriceEstimate10Y) || Utility.OutOfRange(valuation.PriceEstimate10Y)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.PriceEstimate10Y).Append(",");
|
||||
if (double.IsNaN(valuation.TodaysPriceForRequiredReturn) || double.IsInfinity(valuation.TodaysPriceForRequiredReturn) || Utility.OutOfRange(valuation.TodaysPriceForRequiredReturn)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.TodaysPriceForRequiredReturn).Append(",");
|
||||
if (double.IsNaN(valuation.MOS) || double.IsInfinity(valuation.MOS) || Utility.OutOfRange(valuation.MOS)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.MOS).Append(",");
|
||||
if (double.IsNaN(valuation.MOS80) || double.IsInfinity(valuation.MOS80) || Utility.OutOfRange(valuation.MOS80)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.MOS80).Append(",");
|
||||
if (double.IsNaN(valuation.IntrinsicValue) || double.IsInfinity(valuation.IntrinsicValue) || Utility.OutOfRange(valuation.IntrinsicValue)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.IntrinsicValue).Append(",");
|
||||
if (double.IsNaN(valuation.RGV) || double.IsInfinity(valuation.RGV) || Utility.OutOfRange(valuation.RGV)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.RGV).Append(",");
|
||||
if (double.IsNaN(valuation.LatestPrice) || double.IsInfinity(valuation.LatestPrice)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.LatestPrice).Append(",");
|
||||
if (double.IsNaN(valuation.UpsidePcnt) || double.IsInfinity(valuation.UpsidePcnt)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.UpsidePcnt).Append(",");
|
||||
if (double.IsNaN(valuation.DownsidePcnt) || double.IsInfinity(valuation.DownsidePcnt)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.DownsidePcnt).Append(",");
|
||||
if (double.IsNaN(valuation.MeanTargetPrice) || double.IsInfinity(valuation.MeanTargetPrice)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.MeanTargetPrice).Append(",");
|
||||
if (double.IsNaN(valuation.LowTargetPrice) || double.IsInfinity(valuation.LowTargetPrice)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.LowTargetPrice).Append(",");
|
||||
if (double.IsNaN(valuation.HighTargetPrice) || double.IsInfinity(valuation.HighTargetPrice)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.HighTargetPrice).Append(",");
|
||||
sb.Append(valuation.Bargain).Append(",");
|
||||
sb.Append(valuation.Bargain80).Append(",");
|
||||
if (double.IsNaN(valuation.PBVPS) || double.IsInfinity(valuation.PBVPS)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.PBVPS).Append(",");
|
||||
if (double.IsNaN(valuation.PEG) || double.IsInfinity(valuation.PEG)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.PEG).Append(",");
|
||||
if (double.IsNaN(valuation.ImpliedEarningsGrowth) || double.IsInfinity(valuation.ImpliedEarningsGrowth)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.ImpliedEarningsGrowth).Append(",");
|
||||
if (double.IsNaN(valuation.Beta90) || double.IsInfinity(valuation.Beta90)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.Beta90).Append(",");
|
||||
if (double.IsNaN(valuation.Beta720) || double.IsInfinity(valuation.Beta720)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.Beta720).Append(",");
|
||||
if (double.IsNaN(valuation.MarketCap) || double.IsInfinity(valuation.MarketCap)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.MarketCap).Append(",");
|
||||
if (double.IsNaN(valuation.EarningsYield) || double.IsInfinity(valuation.EarningsYield)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.EarningsYield).Append(",");
|
||||
if (double.IsNaN(valuation.EBIT) || double.IsInfinity(valuation.EBIT)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.EBIT).Append(",");
|
||||
if (double.IsNaN(valuation.EnterpriseValue) || double.IsInfinity(valuation.EnterpriseValue)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.EnterpriseValue).Append(",");
|
||||
if (double.IsNaN(valuation.TLBRankROIC) || double.IsInfinity(valuation.TLBRankROIC)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.TLBRankROIC).Append(",");
|
||||
if (double.IsNaN(valuation.FundamentalValue) || double.IsInfinity(valuation.FundamentalValue)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.FundamentalValue).Append(",");
|
||||
if (double.IsNaN(valuation.NetCurrentAssetValuePerShare) || double.IsInfinity(valuation.NetCurrentAssetValuePerShare)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.NetCurrentAssetValuePerShare).Append(",");
|
||||
if (double.IsNaN(valuation.LatestROIC) || double.IsInfinity(valuation.LatestROIC)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.LatestROIC).Append(",");
|
||||
if (null==valuation.Sector) sb.Append("null").Append(",");
|
||||
else sb.Append(SqlUtils.AddQuotes(valuation.Sector)).Append(",");
|
||||
if (null==valuation.Industry) sb.Append("null").Append(",");
|
||||
else sb.Append(SqlUtils.AddQuotes(valuation.Industry)).Append(",");
|
||||
if (double.IsNaN(valuation.DebtToEquity) || double.IsInfinity(valuation.DebtToEquity)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.DebtToEquity).Append(",");
|
||||
sb.Append(valuation.SEC13).Append(",");
|
||||
if (Utility.IsEpoch(valuation.SEC13FilingDate)) sb.Append("null").Append(",");
|
||||
else sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(valuation.SEC13FilingDate))).Append(",");
|
||||
if (double.IsNaN(valuation.LatestROC) || double.IsInfinity(valuation.LatestROC)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.LatestROC).Append(",");
|
||||
if (double.IsNaN(valuation.TLBRankROC) || double.IsInfinity(valuation.TLBRankROC)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.TLBRankROC).Append(",");
|
||||
if (double.IsNaN(valuation.OperatingEarnings) || double.IsInfinity(valuation.OperatingEarnings)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.OperatingEarnings).Append(",");
|
||||
if (double.IsNaN(valuation.AMRank) || double.IsInfinity(valuation.AMRank)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AMRank).Append(",");
|
||||
if (double.IsNaN(valuation.AcquirersMultiple) || double.IsInfinity(valuation.AcquirersMultiple)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.AcquirersMultiple).Append(",");
|
||||
if (double.IsNaN(valuation.DividendYield) || double.IsInfinity(valuation.DividendYield)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.DividendYield).Append(",");
|
||||
if (double.IsNaN(valuation.OperatingCashflow) || double.IsInfinity(valuation.OperatingCashflow)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.OperatingCashflow).Append(",");
|
||||
if (double.IsNaN(valuation.SharesOutstanding) || double.IsInfinity(valuation.SharesOutstanding)) sb.Append("null").Append(",");
|
||||
else sb.Append(valuation.SharesOutstanding).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(modified))).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,strQuery);
|
||||
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static void RemoveValuationBySymbol(String symbol,DateTime valuationDate)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
MySqlTransaction sqlTransaction=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction=sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
sb.Append("delete from valuations where symbol='").Append(symbol).Append("'");
|
||||
sb.Append(" and valuation_date=").Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(valuationDate)));
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection,sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
sqlTransaction.Commit();
|
||||
return;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool RemoveValuation(DateTime valuationDate,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("delete from valuations where valuation_date=");
|
||||
sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(valuationDate)));
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection,sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
239
MarketData/MarketDataLib/DataAccess/WatchListDA.cs
Executable file
239
MarketData/MarketDataLib/DataAccess/WatchListDA.cs
Executable file
@@ -0,0 +1,239 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class WatchListDA
|
||||
{
|
||||
private WatchListDA()
|
||||
{
|
||||
}
|
||||
public static bool AddToWatchList(String symbol, String watchListName = "Valuations")
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == symbol) return false;
|
||||
symbol = symbol.ToUpper();
|
||||
WatchListItem watchListItem = GetWatchListItem(watchListName);
|
||||
if (null == watchListItem) return false;
|
||||
if (IsInWatchList(symbol, watchListName)) return true;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into watchlist(watch_list_id,symbol)values(");
|
||||
sb.Append(watchListItem.WatchListId).Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(symbol));
|
||||
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);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool RemoveFromWatchList(String symbol, String watchListName = "Valuations")
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == symbol) return false;
|
||||
symbol = symbol.ToUpper();
|
||||
WatchListItem watchListItem = GetWatchListItem(watchListName);
|
||||
if (null == watchListItem) return false;
|
||||
List<String> watchListSymbols = GetWatchList(watchListName);
|
||||
if (null == watchListSymbols.Find(x => x.Equals(symbol))) return true;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("delete from watchlist where watch_list_id=").Append(watchListItem.WatchListId).Append(" and ").Append("symbol='").Append(symbol).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);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static bool IsInWatchList(String symbol, String watchListName = "Valuations")
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select count(*) from watchlist wl, watchlists wls where wl.watch_list_id=wls.watch_list_id and wls.watch_list_name=");
|
||||
sb.Append("'").Append(watchListName).Append("'").Append(" ");
|
||||
sb.Append("and wl.symbol=").Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append(" order by 1 asc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return false;
|
||||
int count = sqlDataReader.GetInt32(0);
|
||||
return count > 0 ? true : false;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static List<String> GetWatchList(String watchListName)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
List<String> watchList = new List<String>();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select wl.symbol from watchlist wl, watchlists wls where wl.watch_list_id=wls.watch_list_id and wls.watch_list_name=");
|
||||
sb.Append("'").Append(watchListName).Append("'").Append(" order by 1 asc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
String symbol = sqlDataReader.GetString(0);
|
||||
watchList.Add(symbol);
|
||||
}
|
||||
return watchList;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static WatchListItem GetWatchListItem(String watchListName)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
WatchListItem watchListItem = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select watch_list_name,watch_list_id from watchlists where watch_list_name='").Append(watchListName).Append("'");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (sqlDataReader.Read())
|
||||
{
|
||||
watchListItem = new WatchListItem();
|
||||
watchListItem.WatchListName = sqlDataReader.GetString(0);
|
||||
watchListItem.WatchListId = sqlDataReader.GetInt32(1);
|
||||
}
|
||||
return watchListItem;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static List<String> GetWatchLists()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand = null;
|
||||
List<String> watchList = new List<String>();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||
sb.Append("select watch_list_name from watchlists");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
String symbol = sqlDataReader.GetString(0);
|
||||
watchList.Add(symbol);
|
||||
}
|
||||
return watchList;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlCommand) sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
454
MarketData/MarketDataLib/DataAccess/YieldCurveDA.cs
Executable file
454
MarketData/MarketDataLib/DataAccess/YieldCurveDA.cs
Executable file
@@ -0,0 +1,454 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class YieldCurveDA
|
||||
{
|
||||
private YieldCurveDA()
|
||||
{
|
||||
}
|
||||
private static bool DeleteYieldCurve(YieldCurve yieldCurve, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
|
||||
{
|
||||
String strQuery = null;
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("delete from yieldcurve ");
|
||||
sb.Append("where date in ").Append(SqlUtils.CreateInClauseYear(yieldCurve.GetDistinctDates()));
|
||||
strQuery = sb.ToString();
|
||||
MySqlCommand sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
sqlCommand.Dispose();
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
}
|
||||
}
|
||||
public static bool InsertOrUpdate(YieldCurve yieldCurve)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (null == yieldCurve || 0 == yieldCurve.Count) return false;
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
DeleteYieldCurve(yieldCurve,sqlConnection,sqlTransaction);
|
||||
DateTime modified = DateTime.Now;
|
||||
for (int index = 0; index < yieldCurve.Count; index++)
|
||||
{
|
||||
YieldCurveData yieldCurveData = yieldCurve[index];
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into yieldcurve(date,1mo,3mo,6mo,1yr,2yr,3yr,5yr,7yr,10yr,20yr,30yr,modified)values(");
|
||||
sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(yieldCurveData.Date))).Append(",");
|
||||
if(!double.IsNaN(yieldCurveData.Mo1))sb.Append(yieldCurveData.Mo1).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if(!double.IsNaN(yieldCurveData.Mo3))sb.Append(yieldCurveData.Mo3).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if(!double.IsNaN(yieldCurveData.Mo3))sb.Append(yieldCurveData.Mo6).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if(!double.IsNaN(yieldCurveData.Yr1))sb.Append(yieldCurveData.Yr1).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if(!double.IsNaN(yieldCurveData.Yr2))sb.Append(yieldCurveData.Yr2).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if(!double.IsNaN(yieldCurveData.Yr3))sb.Append(yieldCurveData.Yr3).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if(!double.IsNaN(yieldCurveData.Yr5))sb.Append(yieldCurveData.Yr5).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if(!double.IsNaN(yieldCurveData.Yr7))sb.Append(yieldCurveData.Yr7).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if(!double.IsNaN(yieldCurveData.Yr10))sb.Append(yieldCurveData.Yr10).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if(!double.IsNaN(yieldCurveData.Yr20))sb.Append(yieldCurveData.Yr20).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
if(!double.IsNaN(yieldCurveData.Yr30))sb.Append(yieldCurveData.Yr30).Append(",");
|
||||
else sb.Append("null").Append(",");
|
||||
sb.Append(SqlUtils.AddQuotes(Utility.DateTimeToStringYYYYHMMHDD(modified)));
|
||||
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)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != sqlTransaction) sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static YieldCurve GetYieldCurve(DateTime startDate, int days)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
YieldCurve yieldCurve = new YieldCurve();
|
||||
|
||||
String strQuery = null;
|
||||
|
||||
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,1mo,3mo,6mo,1yr,2yr,3yr,5yr,7yr,10yr,20yr,30yr from yieldcurve ");
|
||||
sb.Append("where date<='").Append(Utility.DateTimeToStringYYYYHMMHDD(startDate)).Append("' ");
|
||||
sb.Append("and date>='").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())
|
||||
{
|
||||
YieldCurveData yieldCurveData = new YieldCurveData();
|
||||
yieldCurveData.Date = sqlDataReader.GetDateTime(0);
|
||||
yieldCurveData.Mo1 = sqlDataReader.GetDouble(1);
|
||||
yieldCurveData.Mo3 = sqlDataReader.GetDouble(2);
|
||||
yieldCurveData.Mo6 = sqlDataReader.GetDouble(3);
|
||||
yieldCurveData.Yr1 = sqlDataReader.GetDouble(4);
|
||||
yieldCurveData.Yr2 = sqlDataReader.GetDouble(5);
|
||||
yieldCurveData.Yr3 = sqlDataReader.GetDouble(6);
|
||||
yieldCurveData.Yr5 = sqlDataReader.GetDouble(7);
|
||||
yieldCurveData.Yr7 = sqlDataReader.GetDouble(8);
|
||||
yieldCurveData.Yr10 = sqlDataReader.GetDouble(9);
|
||||
yieldCurveData.Yr20 = sqlDataReader.GetDouble(10);
|
||||
yieldCurveData.Yr30 = sqlDataReader.GetDouble(11);
|
||||
yieldCurve.Add(yieldCurveData);
|
||||
}
|
||||
return yieldCurve;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static YieldCurveData GetYieldCurve(DateTime effectiveDate)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
YieldCurveData yieldCurveData = null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select date,1mo,3mo,6mo,1yr,2yr,3yr,5yr,7yr,10yr,20yr,30yr from yieldcurve ");
|
||||
sb.Append("where date='").Append(Utility.DateTimeToStringYYYYHMMHDD(effectiveDate)).Append("' ");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read())return null;
|
||||
yieldCurveData = new YieldCurveData();
|
||||
yieldCurveData.Date = sqlDataReader.GetDateTime(0);
|
||||
yieldCurveData.Mo1 = sqlDataReader.GetDouble(1);
|
||||
yieldCurveData.Mo3 = sqlDataReader.GetDouble(2);
|
||||
yieldCurveData.Mo6 = sqlDataReader.GetDouble(3);
|
||||
yieldCurveData.Yr1 = sqlDataReader.GetDouble(4);
|
||||
yieldCurveData.Yr2 = sqlDataReader.GetDouble(5);
|
||||
yieldCurveData.Yr3 = sqlDataReader.GetDouble(6);
|
||||
yieldCurveData.Yr5 = sqlDataReader.GetDouble(7);
|
||||
yieldCurveData.Yr7 = sqlDataReader.GetDouble(8);
|
||||
yieldCurveData.Yr10 = sqlDataReader.GetDouble(9);
|
||||
yieldCurveData.Yr20 = sqlDataReader.GetDouble(10);
|
||||
yieldCurveData.Yr30 = sqlDataReader.GetDouble(11);
|
||||
return yieldCurveData;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static YieldCurve GetYieldCurve(int dayCount)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
YieldCurve yieldCurve = new YieldCurve();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select date,1mo,3mo,6mo,1yr,2yr,3yr,5yr,7yr,10yr,20yr,30yr from yieldcurve ");
|
||||
sb.Append(" order by date desc");
|
||||
sb.Append(" limit ").Append(dayCount);
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
YieldCurveData yieldCurveData = new YieldCurveData();
|
||||
yieldCurveData.Date = sqlDataReader.GetDateTime(0);
|
||||
yieldCurveData.Mo1 = sqlDataReader.GetDouble(1);
|
||||
yieldCurveData.Mo3 = sqlDataReader.GetDouble(2);
|
||||
yieldCurveData.Mo6 = sqlDataReader.GetDouble(3);
|
||||
yieldCurveData.Yr1 = sqlDataReader.GetDouble(4);
|
||||
yieldCurveData.Yr2 = sqlDataReader.GetDouble(5);
|
||||
yieldCurveData.Yr3 = sqlDataReader.GetDouble(6);
|
||||
yieldCurveData.Yr5 = sqlDataReader.GetDouble(7);
|
||||
yieldCurveData.Yr7 = sqlDataReader.GetDouble(8);
|
||||
yieldCurveData.Yr10 = sqlDataReader.GetDouble(9);
|
||||
yieldCurveData.Yr20 = sqlDataReader.GetDouble(10);
|
||||
yieldCurveData.Yr30 = sqlDataReader.GetDouble(11);
|
||||
yieldCurve.Add(yieldCurveData);
|
||||
}
|
||||
return yieldCurve;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static YieldCurve GetYieldCurve()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
YieldCurve yieldCurve = new YieldCurve();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select date,1mo,3mo,6mo,1yr,2yr,3yr,5yr,7yr,10yr,20yr,30yr from yieldcurve ");
|
||||
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())
|
||||
{
|
||||
YieldCurveData yieldCurveData = new YieldCurveData();
|
||||
yieldCurveData.Date=sqlDataReader.GetDateTime(0);
|
||||
yieldCurveData.Mo1=sqlDataReader.GetDouble(1);
|
||||
yieldCurveData.Mo3=sqlDataReader.GetDouble(2);
|
||||
yieldCurveData.Mo6=sqlDataReader.GetDouble(3);
|
||||
yieldCurveData.Yr1=sqlDataReader.GetDouble(4);
|
||||
yieldCurveData.Yr2=sqlDataReader.GetDouble(5);
|
||||
yieldCurveData.Yr3=sqlDataReader.GetDouble(6);
|
||||
yieldCurveData.Yr5=sqlDataReader.GetDouble(7);
|
||||
yieldCurveData.Yr7=sqlDataReader.GetDouble(8);
|
||||
yieldCurveData.Yr10=sqlDataReader.GetDouble(9);
|
||||
yieldCurveData.Yr20=sqlDataReader.GetDouble(10);
|
||||
yieldCurveData.Yr30 = sqlDataReader.GetDouble(11);
|
||||
yieldCurve.Add(yieldCurveData);
|
||||
}
|
||||
return yieldCurve;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null != sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static double GetRiskFreeRate1Yr(DateTime effectiveDate,int lookAhead)
|
||||
{
|
||||
DateGenerator dateGenerator=new DateGenerator();
|
||||
YieldCurveData yieldCurveData=null;
|
||||
for(int index=0;index<lookAhead;index++)
|
||||
{
|
||||
yieldCurveData=GetYieldCurve(effectiveDate);
|
||||
if(null!=yieldCurveData)break;
|
||||
effectiveDate=dateGenerator.FindNextBusinessDay(effectiveDate);
|
||||
}
|
||||
if(null==yieldCurveData)return double.NaN;
|
||||
return yieldCurveData.Yr1;
|
||||
}
|
||||
public static double GetRiskFreeRate(DateTime asOf)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
double riskFreeRate = double.NaN;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select 3mo from yieldcurve ");
|
||||
sb.Append("where date<='").Append(SqlUtils.FormatDate(asOf)).Append("'").Append(" order by date desc limit 1");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
riskFreeRate = sqlDataReader.GetDouble(0);
|
||||
}
|
||||
return riskFreeRate;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return riskFreeRate;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static double GetRiskFreeRate()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
double riskFreeRate = double.NaN;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select 3mo from yieldcurve order by date desc limit 1");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while (sqlDataReader.Read())
|
||||
{
|
||||
riskFreeRate = sqlDataReader.GetDouble(0);
|
||||
}
|
||||
return riskFreeRate;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return riskFreeRate;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static DateTime GetMaxYieldCurveDate()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select max(date) from yieldcurve");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if(!sqlDataReader.Read())return Utility.Epoch;
|
||||
return sqlDataReader.GetDateTime(0);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return Utility.Epoch;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
public static List<DateTime> GetYieldCurveDates()
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
List<DateTime> yieldCurveDates=new List<DateTime>();
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select date from yieldcurve order by date desc");
|
||||
strQuery = sb.ToString();
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
while(sqlDataReader.Read())
|
||||
{
|
||||
yieldCurveDates.Add(sqlDataReader.GetDateTime(0));
|
||||
}
|
||||
return yieldCurveDates;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return new List<DateTime>();
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
225
MarketData/MarketDataLib/DataAccess/ZacksRankDA.cs
Executable file
225
MarketData/MarketDataLib/DataAccess/ZacksRankDA.cs
Executable file
@@ -0,0 +1,225 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MarketData.MarketDataModel;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.DataAccess
|
||||
{
|
||||
public class ZacksRankDA
|
||||
{
|
||||
private ZacksRankDA()
|
||||
{
|
||||
}
|
||||
// Get the latest rank for symbol that falls on or before the specified date
|
||||
public static ZacksRank GetZacksRankOnOrBefore(String symbol, DateTime date)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
DateTime? maxDate=GetMaxDateOnOrBefore(symbol,date);
|
||||
if(null==maxDate)return null;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select symbol,zacks_rank,date,type from zacksrank where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and date=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(maxDate.Value)).Append("'");
|
||||
sb.Append(" limit 1").Append(";");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
ZacksRank zacksRank=new ZacksRank();
|
||||
zacksRank.Symbol=sqlDataReader.GetString(0);
|
||||
zacksRank.Rank=sqlDataReader.GetString(1);
|
||||
zacksRank.Date=sqlDataReader.GetDateTime(2);
|
||||
if(!sqlDataReader.IsDBNull(3))zacksRank.Type=sqlDataReader.GetString(3);
|
||||
return zacksRank;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
// Get the most recent rank date on or before the given date for specified symbol
|
||||
private static DateTime? GetMaxDateOnOrBefore(String symbol, DateTime date)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
DateTime? maxDate=null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select date from zacksrank where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and date<=").Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(date)).Append("' ");
|
||||
sb.Append(" order by date desc ");
|
||||
sb.Append(" limit 1").Append(";");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
if(!sqlDataReader.IsDBNull(0))maxDate=sqlDataReader.GetDateTime(0);
|
||||
return maxDate;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
// Get the latest rank for symbol
|
||||
public static ZacksRank GetZacksRank(String symbol)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlDataReader sqlDataReader = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sb.Append("select date,symbol,zacks_rank,type from zacksrank where symbol=");
|
||||
sb.Append("'").Append(symbol).Append("'").Append(" ");
|
||||
sb.Append("and date=").Append(" (select max(date) from zacksrank where symbol='").Append(symbol).Append("')");
|
||||
strQuery = sb.ToString(); ;
|
||||
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||
sqlDataReader = sqlCommand.ExecuteReader();
|
||||
if (!sqlDataReader.Read()) return null;
|
||||
ZacksRank zacksRank=new ZacksRank();
|
||||
zacksRank.Date=sqlDataReader.GetDateTime(0);
|
||||
zacksRank.Symbol=sqlDataReader.GetString(1);
|
||||
zacksRank.Rank=sqlDataReader.GetString(2);
|
||||
if(!sqlDataReader.IsDBNull(3))zacksRank.Type=sqlDataReader.GetString(3);
|
||||
return zacksRank;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if (null != sqlDataReader) sqlDataReader.Close();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
// Insert a rank if the latest rank that we have is different from the one to be added or we do not have a rank
|
||||
public static bool InsertZacksRank(ZacksRank zacksRank)
|
||||
{
|
||||
MySqlConnection sqlConnection = null;
|
||||
MySqlTransaction sqlTransaction = null;
|
||||
MySqlCommand sqlCommand=null;
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if(null==zacksRank||null==zacksRank.Symbol||null==zacksRank.Rank||"".Equals(zacksRank.Rank))return false;
|
||||
ZacksRank latestRank=GetZacksRank(zacksRank.Symbol);
|
||||
if(null!=latestRank&&latestRank.Rank.Equals(zacksRank.Rank))return true;
|
||||
zacksRank.Type=GetChangeType(latestRank,zacksRank);
|
||||
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
|
||||
sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
|
||||
DeleteZacksRank(zacksRank, sqlConnection, sqlTransaction);
|
||||
sqlTransaction.Commit();
|
||||
sqlTransaction = sqlConnection.BeginTransaction();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("insert into zacksrank (symbol,date,zacks_rank,type) ");
|
||||
sb.Append("values(");
|
||||
sb.Append("'").Append(zacksRank.Symbol).Append("'").Append(",");
|
||||
sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(zacksRank.Date)).Append("'").Append(",");
|
||||
sb.Append("'").Append(zacksRank.Rank).Append("'").Append(",");
|
||||
if(null==zacksRank.Type)sb.Append("null");
|
||||
else sb.Append("'").Append(zacksRank.Type).Append("'");
|
||||
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,"Query was '" + strQuery + "'");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=sqlCommand)sqlCommand.Dispose();
|
||||
if(null!=sqlTransaction)sqlTransaction.Dispose();
|
||||
if (null != sqlConnection) sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
private static String GetChangeType(ZacksRank latestRank,ZacksRank newRank)
|
||||
{
|
||||
if(null==latestRank)return "Initial";
|
||||
if(null==latestRank.Rank||null==newRank||null==newRank.Rank)return null;
|
||||
int latestRankNumber=int.Parse(latestRank.Rank.Substring(0,1));
|
||||
int newRankNumber=int.Parse(newRank.Rank.Substring(0,1));
|
||||
if(newRankNumber>latestRankNumber)return "Downgrades";
|
||||
else if(newRankNumber<latestRankNumber)return "Upgrades";
|
||||
return "NoChange";
|
||||
|
||||
}
|
||||
// Delete a ranking on symbol and date
|
||||
private static bool DeleteZacksRank(ZacksRank zacksRank,MySqlConnection sqlConnection,MySqlTransaction sqlTransaction)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String strQuery = null;
|
||||
|
||||
try
|
||||
{
|
||||
if(null==zacksRank||null==zacksRank.Symbol||null==zacksRank.Rank||"".Equals(zacksRank.Rank))return false;
|
||||
sb.Append("delete from zacksrank where ");
|
||||
sb.Append("symbol='").Append(zacksRank.Symbol).Append("'");
|
||||
sb.Append(" and ");
|
||||
sb.Append("date='").Append(Utility.DateTimeToStringYYYYHMMHDD(zacksRank.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
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user