// // // private static bool TryCreateExchange(MySqlDataReader reader, out string ExchangeName, out int ExchangeID) { bool isSuccesful = true; ExchangeName = null; ExchangeID = -1; string[] fieldNames = new string[reader.FieldCount]; for (int i = 0; i < fieldNames.Length; i++) { fieldNames[i] = reader.GetName(i); } TableInfo.ExchangesTableInfo table = new TableInfo.ExchangesTableInfo(); try { ExchangeName = reader.GetString(table.ExchangeName); ExchangeID = reader.GetInt32(ExchangeID); } catch (Exception) { isSuccesful = false; } return(isSuccesful); }
// // /// <summary> /// Try and find the uniquie Instrument ID from a mySQL database for a given InstrumentName. /// Warning : This function is extremely dangerous and written very poorly. /// </summary> /// <param name="dataBase"></param> /// <param name="instrument"></param> /// <param name="mySQLInstrumentID"></param> /// <returns></returns> public static bool TryGetMySQLInstrumentId(DatabaseInfo dataBase, InstrumentName instrument, out int mySQLInstrumentID) { // Validate mySQLInstrumentID = -1; if (dataBase.Instruments == null) { return(false); } Dictionary <int, string> possibleMySQLInstrumendIds = new Dictionary <int, string>(); TableInfo.InstrumentsTableInfo instrumentTable = dataBase.Instruments; TableInfo.ExchangesTableInfo exchangeTable = dataBase.Exchanges; MySqlConnection conn = null; bool isSuccess = true; string expiryCode; // ie Z3 or H4 if (dataBase.IsTryToConnect(ref conn) && UV.Lib.Utilities.QTMath.TryConvertMonthYearToCodeY(instrument.SeriesName, out expiryCode)) { MySqlDataReader reader = null; StringBuilder query = new StringBuilder(); query.AppendFormat("SELECT {0},{1} FROM {2} ", instrumentTable.InstrumentID, instrumentTable.SpreadComposition, dataBase.Instruments.TableNameFull); // add restrictions here. (example below) /* 0 1 2 3 4 5 6 7 8 * select instrID from instrumentInfo where exchangeId in (select exchangeId from exchangeInfo where exchangeNameTT ='CME') and prodNameTT = "CL" and prodExpiry='X2'; */ query.AppendFormat("WHERE {0} in (select {1} from {2} where {3} =\'{4}\') and {5} =\'{6}\' and {7} =\'{8}\' and {9} = \'{10}\'", instrumentTable.ExchangeID, // 0 instrumentTable.ExchangeID, // 1 exchangeTable.TableNameFull, // 2 exchangeTable.ExchangeName, // 3 instrument.Product.Exchange, // 4 instrumentTable.Product, // 5 instrument.Product.ProductName, // 6 instrumentTable.ExpirySymbol, // 7 expiryCode, // 8 instrumentTable.ProductType, // 9 instrument.Product.Type.ToString().ToLower()); // 10 #region BAD CODE FIX ME! if (instrument.Product.Type == ProductTypes.Spread) { // handling special cases here in a BAD WAY! if (instrument.Product.ProductName == "KEZW" || instrument.Product.ProductName == "MWEZW" || instrument.Product.ProductName == "MWEKE") { // these are ICS spreads and we only want the "Matched" expirations string composition = instrument.SeriesName; string[] compSplit = composition.Split(new char[] { ':' }); List <string> expiryCodes = new List <string>(); string matchExpiries = @"[a-zA-Z]{3}[0-9]{2}"; foreach (string partialString in compSplit) { foreach (System.Text.RegularExpressions.Match match in System.Text.RegularExpressions.Regex.Matches(partialString, matchExpiries)) { expiryCodes.Add(match.Value); } } if (expiryCodes.Count == 2 && expiryCodes[0] == expiryCodes[1]) { } else { isSuccess = false; return(isSuccess); } } } #endregion query.AppendFormat("ORDER BY {0};", instrumentTable.InstrumentID); try { MySqlCommand cmd = new MySqlCommand(query.ToString(), conn); reader = cmd.ExecuteReader(); while (reader.Read()) { int Id = -1; string spreadComposition; if (!TryReadMySQLInstrumentID(reader, out Id, out spreadComposition)) { dataBase.Errors.Enqueue("TryGetMySQLInstrumentId(): Failed to get mySQL instrument."); isSuccess = false; break; } possibleMySQLInstrumendIds.Add(Id, spreadComposition); } } catch (Exception e) { dataBase.Errors.Enqueue(string.Format("Instrument.Create(): MySql exception {0}", e.Message)); isSuccess = false; } finally { if (reader != null && !reader.IsClosed) { reader.Close(); } conn.Close(); } if (possibleMySQLInstrumendIds.Count > 1) {// we need to find out which instrument id is correct, very simliar instruments! // Warning: This is horrible code. I am not sure yet how to create something more generic dealing with // a place were were keep similiar spreads as different products and TT believes them to be instruments. // This section is VERY specific and not general in any way. It will probably break. Need to come up // with a much better well though out solution #region //Bad Code! Match match = Regex.Match(instrument.SeriesName, ": [0-9] x [0-9]"); // make sure we know how to deal with these instruments if (match.Success) { // find our ratios from our TT product to compare to our database product string[] stringRatios = Regex.Split(match.Value.Substring(2), "x"); // split these ratios into their components so we can compare int[] intRatios = new int[stringRatios.Length]; for (int i = 0; i < stringRatios.Length; i++) { int ratio; if (int.TryParse(stringRatios[i], out ratio)) // create them as ints and save { intRatios[i] = ratio; } } // compare with each database product to find the correct match foreach (int id in possibleMySQLInstrumendIds.Keys) { bool isMatch = false; string spreadCompString = possibleMySQLInstrumendIds[id]; MatchCollection allMatches = Regex.Matches(spreadCompString, "[0-9]x[A-Z]"); if (match.Success) { int ratio; int count = 0; foreach (Match newMatch in allMatches) { if (int.TryParse(newMatch.Value.Substring(0, 1), out ratio)) // parse out the ints to compare { if (ratio == intRatios[count]) { isMatch = true; } else { isMatch = false; } } else { isMatch = false; } count++; } } if (isMatch) { // we found a ratio match so assign the correct id and move on mySQLInstrumentID = id; break; } } } } #endregion // Bad Code! else if (possibleMySQLInstrumendIds.Count == 1) { foreach (int id in possibleMySQLInstrumendIds.Keys) { mySQLInstrumentID = id; } } else { isSuccess = false; } // exit } //if connected return(isSuccess && mySQLInstrumentID > -1); } //TryCreate().