Changes to handle multiple stop limits for a single symbol. The primary key in the tableis now symbol + shares.
This commit is contained in:
@@ -15,50 +15,95 @@ namespace MarketData.DataAccess
|
|||||||
// **********************************************************************************************************************************************************************************
|
// **********************************************************************************************************************************************************************************
|
||||||
// ****************************************************************************************** S T O P L I M I T S ****************************************************************
|
// ****************************************************************************************** S T O P L I M I T S ****************************************************************
|
||||||
// **********************************************************************************************************************************************************************************
|
// **********************************************************************************************************************************************************************************
|
||||||
public static StopLimit GetStopLimit(String symbol)
|
public static StopLimits GetStopLimits(String symbol)
|
||||||
{
|
{
|
||||||
MySqlConnection sqlConnection=null;
|
MySqlConnection sqlConnection = null;
|
||||||
MySqlDataReader sqlDataReader=null;
|
MySqlDataReader sqlDataReader = null;
|
||||||
MySqlCommand sqlCommand=null;
|
MySqlCommand sqlCommand = null;
|
||||||
String strQuery=null;
|
String strQuery = null;
|
||||||
|
StopLimits stopLimits = new StopLimits();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
StringBuilder sb=new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
if(null==symbol)return null;
|
if (null == symbol) return null;
|
||||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||||
sb.Append("select symbol,stop_limit,shares,stop_type from stoplimits ");
|
sb.Append("select symbol,stop_limit,shares,stop_type from stoplimits ");
|
||||||
sb.Append("where symbol='").Append(symbol).Append("'");
|
sb.Append("where symbol='").Append(symbol).Append("'");
|
||||||
strQuery=sb.ToString();
|
strQuery = sb.ToString();
|
||||||
sqlCommand=new MySqlCommand(strQuery,sqlConnection);
|
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||||
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
|
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||||
sqlDataReader=sqlCommand.ExecuteReader();
|
sqlDataReader = sqlCommand.ExecuteReader();
|
||||||
if(!sqlDataReader.Read())return null;
|
while (sqlDataReader.Read())
|
||||||
StopLimit stopLimit=new StopLimit();
|
{
|
||||||
stopLimit.Symbol=sqlDataReader.GetString(0);
|
StopLimit stopLimit = new StopLimit();
|
||||||
stopLimit.StopPrice=sqlDataReader.GetDouble(1);
|
stopLimit.Symbol = sqlDataReader.GetString(0);
|
||||||
stopLimit.Shares=sqlDataReader.GetDouble(2);
|
stopLimit.StopPrice = sqlDataReader.GetDouble(1);
|
||||||
stopLimit.StopType=sqlDataReader.GetString(3);
|
stopLimit.Shares = sqlDataReader.GetDouble(2);
|
||||||
return stopLimit;
|
stopLimit.StopType = sqlDataReader.GetString(3);
|
||||||
|
stopLimits.Add(stopLimit);
|
||||||
|
}
|
||||||
|
return stopLimits;
|
||||||
}
|
}
|
||||||
catch(Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||||
return null;
|
return stopLimits;
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
if (null != sqlCommand) sqlCommand.Dispose();
|
||||||
if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
|
if (null != sqlDataReader) { sqlDataReader.Close(); sqlDataReader.Dispose(); }
|
||||||
if(null!=sqlConnection) sqlConnection.Close();
|
if (null != sqlConnection) sqlConnection.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static StopLimit GetStopLimit(String symbol, double shares)
|
||||||
|
{
|
||||||
|
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("'");
|
||||||
|
sb.Append(" ");
|
||||||
|
sb.Append(" and shares=").Append(Utility.FormatNumber(shares, 2));
|
||||||
|
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)
|
public static bool InsertUpdateStopLimit(StopLimit stopLimit)
|
||||||
{
|
{
|
||||||
if(null==stopLimit || null==stopLimit.Symbol || double.IsNaN(stopLimit.StopPrice))return false;
|
if(null==stopLimit || null==stopLimit.Symbol || double.IsNaN(stopLimit.StopPrice))return false;
|
||||||
if(!HasStopLimit(stopLimit.Symbol))return InsertStopLimit(stopLimit);
|
if(!HasStopLimit(stopLimit.Symbol,stopLimit.Shares))return InsertStopLimit(stopLimit);
|
||||||
return UpdateStopLimit(stopLimit);
|
return UpdateStopLimit(stopLimit);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool InsertStopLimit(StopLimit stopLimit)
|
private static bool InsertStopLimit(StopLimit stopLimit)
|
||||||
{
|
{
|
||||||
MySqlConnection sqlConnection=null;
|
MySqlConnection sqlConnection=null;
|
||||||
@@ -97,6 +142,7 @@ namespace MarketData.DataAccess
|
|||||||
if(null!=sqlConnection) sqlConnection.Close();
|
if(null!=sqlConnection) sqlConnection.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool UpdateStopLimit(StopLimit stopLimit)
|
private static bool UpdateStopLimit(StopLimit stopLimit)
|
||||||
{
|
{
|
||||||
MySqlConnection sqlConnection=null;
|
MySqlConnection sqlConnection=null;
|
||||||
@@ -115,6 +161,7 @@ namespace MarketData.DataAccess
|
|||||||
sb.Append("stop_limit=").Append(stopLimit.StopPrice).Append(", ");
|
sb.Append("stop_limit=").Append(stopLimit.StopPrice).Append(", ");
|
||||||
sb.Append("shares=").Append(stopLimit.Shares);
|
sb.Append("shares=").Append(stopLimit.Shares);
|
||||||
sb.Append(" where symbol=").Append(SqlUtils.AddQuotes(stopLimit.Symbol));
|
sb.Append(" where symbol=").Append(SqlUtils.AddQuotes(stopLimit.Symbol));
|
||||||
|
sb.Append(" and shares=").Append(Utility.FormatNumber(stopLimit.Shares,2));
|
||||||
strQuery=sb.ToString();
|
strQuery=sb.ToString();
|
||||||
sqlCommand=new MySqlCommand(strQuery,sqlConnection,sqlTransaction);
|
sqlCommand=new MySqlCommand(strQuery,sqlConnection,sqlTransaction);
|
||||||
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
|
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
|
||||||
@@ -177,7 +224,7 @@ namespace MarketData.DataAccess
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool HasStopLimit(String symbol)
|
public static bool HasStopLimit(String symbol,double shares)
|
||||||
{
|
{
|
||||||
MySqlConnection sqlConnection=null;
|
MySqlConnection sqlConnection=null;
|
||||||
MySqlDataReader sqlDataReader=null;
|
MySqlDataReader sqlDataReader=null;
|
||||||
@@ -191,6 +238,8 @@ namespace MarketData.DataAccess
|
|||||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||||
sb.Append("select count(*) from stoplimits ");
|
sb.Append("select count(*) from stoplimits ");
|
||||||
sb.Append("where symbol='").Append(symbol).Append("'");
|
sb.Append("where symbol='").Append(symbol).Append("'");
|
||||||
|
sb.Append(" and ");
|
||||||
|
sb.Append("shares=").Append(Utility.FormatNumber(shares,2));
|
||||||
strQuery=sb.ToString();
|
strQuery=sb.ToString();
|
||||||
sqlCommand=new MySqlCommand(strQuery,sqlConnection);
|
sqlCommand=new MySqlCommand(strQuery,sqlConnection);
|
||||||
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
|
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
|
||||||
@@ -211,34 +260,37 @@ namespace MarketData.DataAccess
|
|||||||
if(null!=sqlConnection) sqlConnection.Close();
|
if(null!=sqlConnection) sqlConnection.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static bool DeleteStopLimit(String symbol)
|
|
||||||
|
public static bool DeleteStopLimit(String symbol,double shares)
|
||||||
{
|
{
|
||||||
MySqlConnection sqlConnection=null;
|
MySqlConnection sqlConnection = null;
|
||||||
MySqlCommand sqlCommand=null;
|
MySqlCommand sqlCommand = null;
|
||||||
String strQuery=null;
|
String strQuery = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
StringBuilder sb=new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
if(null==symbol) return false;
|
if (null == symbol) return false;
|
||||||
sqlConnection=SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
|
||||||
sb.Append("delete from stoplimits ");
|
sb.Append("delete from stoplimits ");
|
||||||
sb.Append("where symbol='").Append(symbol).Append("'");
|
sb.Append("where symbol='").Append(symbol).Append("'");
|
||||||
strQuery=sb.ToString();
|
sb.Append(" ");
|
||||||
sqlCommand=new MySqlCommand(strQuery,sqlConnection);
|
sb.Append("and shares=").Append(Utility.FormatNumber(shares,3));
|
||||||
sqlCommand.CommandTimeout=SqlUtils.COMMAND_TIMEOUT;
|
strQuery = sb.ToString();
|
||||||
|
sqlCommand = new MySqlCommand(strQuery, sqlConnection);
|
||||||
|
sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
|
||||||
sqlCommand.ExecuteNonQuery();
|
sqlCommand.ExecuteNonQuery();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch(Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
MDTrace.WriteLine(LogLevel.DEBUG, exception);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if(null!=sqlCommand) sqlCommand.Dispose();
|
if (null != sqlCommand) sqlCommand.Dispose();
|
||||||
if(null!=sqlConnection) sqlConnection.Close();
|
if (null != sqlConnection) sqlConnection.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user