diff --git a/MarketData/MarketDataLib/DataAccess/PortfolioDA.cs b/MarketData/MarketDataLib/DataAccess/PortfolioDA.cs index 70c93e4..0c4d870 100755 --- a/MarketData/MarketDataLib/DataAccess/PortfolioDA.cs +++ b/MarketData/MarketDataLib/DataAccess/PortfolioDA.cs @@ -10,241 +10,6 @@ namespace MarketData.DataAccess 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();sqlDataReader.Dispose();} - 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 Dictionary HasStopLimit(List symbols) - { - MySqlConnection sqlConnection=null; - MySqlDataReader sqlDataReader=null; - MySqlCommand sqlCommand=null; - Dictionary hasStopLimit = new Dictionary(); - - try - { - if(null == symbols || 0==symbols.Count)return hasStopLimit; - StringBuilder sb=new StringBuilder(); - if(null==symbols) return null; - sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data")); - sb.Append("select symbol, count(*) from stoplimits "); - sb.Append("where symbol in "); - sb.Append(SqlUtils.CreateInClause(symbols)); - sb.Append(" and active=1 group by 1"); - String strQuery=sb.ToString(); - sqlCommand=new MySqlCommand(strQuery,sqlConnection); - sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT; - sqlDataReader=sqlCommand.ExecuteReader(); - while(sqlDataReader.Read()) - { - String symbol = sqlDataReader.GetString(0); - symbol = symbol.ToUpper(); - hasStopLimit.Add(symbol, true); - } - return hasStopLimit; - } - 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 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("'"); - 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 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();sqlDataReader.Dispose();} - 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; diff --git a/MarketData/MarketDataLib/DataAccess/StopLimitDA.cs b/MarketData/MarketDataLib/DataAccess/StopLimitDA.cs new file mode 100644 index 0000000..831527a --- /dev/null +++ b/MarketData/MarketDataLib/DataAccess/StopLimitDA.cs @@ -0,0 +1,241 @@ +using System.Text; +using MySql.Data.MySqlClient; +using MarketData.MarketDataModel; +using MarketData.Utils; + +namespace MarketData.DataAccess +{ + public class StopLimitDA + { + private StopLimitDA() + { + } + + 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 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 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); + return stopLimit; + } + 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 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)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)); + 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); + 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); + 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 Dictionary HasStopLimit(List symbols) + { + MySqlConnection sqlConnection = null; + MySqlDataReader sqlDataReader = null; + MySqlCommand sqlCommand = null; + Dictionary hasStopLimit = new Dictionary(); + + try + { + if (null == symbols || 0 == symbols.Count) return hasStopLimit; + StringBuilder sb = new StringBuilder(); + if (null == symbols) return null; + sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data")); + sb.Append("select symbol, count(*) from stoplimits "); + sb.Append("where symbol in "); + sb.Append(SqlUtils.CreateInClause(symbols)); + sb.Append(" group by 1"); + String strQuery = sb.ToString(); + sqlCommand = new MySqlCommand(strQuery, sqlConnection); + sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; + sqlDataReader = sqlCommand.ExecuteReader(); + while (sqlDataReader.Read()) + { + String symbol = sqlDataReader.GetString(0); + symbol = symbol.ToUpper(); + hasStopLimit.Add(symbol, true); + } + return hasStopLimit; + } + 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 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(); sqlDataReader.Dispose(); } + 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(); + } + } + } +} diff --git a/MarketData/MarketDataLib/MarketDataModel/GainLoss/GainLossSummaryItemCollection.cs b/MarketData/MarketDataLib/MarketDataModel/GainLoss/GainLossSummaryItemCollection.cs index 5bcb4e5..5fa04da 100755 --- a/MarketData/MarketDataLib/MarketDataModel/GainLoss/GainLossSummaryItemCollection.cs +++ b/MarketData/MarketDataLib/MarketDataModel/GainLoss/GainLossSummaryItemCollection.cs @@ -27,7 +27,7 @@ namespace MarketData.MarketDataModel.GainLoss if(null==gainLossGenerator || null==activeGainLossGenerator)return; Dictionary companyNames = PricingDA.GetNamesForSymbols(symbols); - Dictionary stopLimits = PortfolioDA.HasStopLimit(symbols); + Dictionary stopLimits = StopLimitDA.HasStopLimit(symbols); foreach(String symbol in symbols) { @@ -99,7 +99,7 @@ namespace MarketData.MarketDataModel.GainLoss public GainLossSummaryItemCollection(PortfolioTrades portfolioTrades,DateTime? maxDateRef=null) { List symbols=portfolioTrades.Symbols; - Dictionary stopLimits = PortfolioDA.HasStopLimit(symbols); + Dictionary stopLimits = StopLimitDA.HasStopLimit(symbols); Dictionary namesDictionary = PricingDA.GetNamesForSymbols(symbols); foreach(String symbol in symbols) diff --git a/MarketData/MarketDataLib/MarketDataModel/StopLimit.cs b/MarketData/MarketDataLib/MarketDataModel/StopLimit.cs index fb3d6b7..a4a775d 100755 --- a/MarketData/MarketDataLib/MarketDataModel/StopLimit.cs +++ b/MarketData/MarketDataLib/MarketDataModel/StopLimit.cs @@ -57,42 +57,34 @@ namespace MarketData.MarketDataModel public double StopPrice{get;set;} public double Shares{get;set;} public String StopType{get;set;} - public int Active{get;set;} public DateTime EffectiveDate{get;set;} // if the EffectiveDate is Epoch then the StopLimit is taken to be in effect and is the most recent. Otherwise it is considered an historical stop limit public StopLimit() { } - public StopLimit(String symbol,double stopPrice,int shares,String stopType,int active) - { - this.Symbol=symbol; - this.StopPrice=stopPrice; - this.Shares=shares; - this.StopType=stopType; - this.Active=active; - } + public override String ToString() { - StringBuilder sb=new StringBuilder(); + StringBuilder sb = new StringBuilder(); sb.Append(Symbol).Append(","); sb.Append(Utility.FormatCurrency(StopPrice)).Append(","); - sb.Append(Utility.FormatNumber(Shares,3)).Append(","); + sb.Append(Utility.FormatNumber(Shares, 3)).Append(","); sb.Append(StopType).Append(","); - sb.Append(Active.ToString()).Append(","); sb.Append(EffectiveDate.ToShortDateString()); return sb.ToString(); } + public virtual NVPCollection ToNVPCollection() { - NVPCollection nvpCollection=new NVPCollection(); - nvpCollection.Add(new NVP("Symbol",Symbol.ToString())); - nvpCollection.Add(new NVP("StopPrice",StopPrice.ToString())); - nvpCollection.Add(new NVP("Shares",Shares.ToString())); - nvpCollection.Add(new NVP("StopType",StopType.ToString())); - nvpCollection.Add(new NVP("Active",Active.ToString())); - nvpCollection.Add(new NVP("EffectiveDate",EffectiveDate.ToShortDateString())); + NVPCollection nvpCollection = new NVPCollection(); + nvpCollection.Add(new NVP("Symbol", Symbol.ToString())); + nvpCollection.Add(new NVP("StopPrice", StopPrice.ToString())); + nvpCollection.Add(new NVP("Shares", Shares.ToString())); + nvpCollection.Add(new NVP("StopType", StopType.ToString())); + nvpCollection.Add(new NVP("EffectiveDate", EffectiveDate.ToShortDateString())); return nvpCollection; } + public static StopLimit FromNVPCollection(NVPCollection nvpCollection) { StopLimit stopLimit=new StopLimit(); diff --git a/MarketDataServer/Controllers/PortfolioController.cs b/MarketDataServer/Controllers/PortfolioController.cs index f45cf3b..defd578 100755 --- a/MarketDataServer/Controllers/PortfolioController.cs +++ b/MarketDataServer/Controllers/PortfolioController.cs @@ -98,7 +98,7 @@ namespace MarketDataServer.Controllers { MDTrace.WriteLine(LogLevel.DEBUG,$"Start"); if(!Authorizations.GetInstance().IsAuthorized(token)) return null; - StopLimit stopLimit=PortfolioDA.GetStopLimit(symbol); + StopLimit stopLimit=StopLimitDA.GetStopLimit(symbol); return stopLimit; } catch(Exception exception)