/// <summary> /// Add economic data to the queue. /// </summary> /// <param name="economicDataPoint"></param> public void AddEconomicPointToQueue(EconomicDataPoint economicDataPoint) { lock (m_EconomicDataPointQueue) { m_EconomicDataPointQueue.Enqueue(economicDataPoint); } m_WaitHandle.Set(); }
/// <summary> /// Generate update query code for particular ticker and date. /// </summary> /// <param name="ticker"></param> /// <param name="date"></param> /// <returns></returns> private string GetUpdateQueryCode(string ticker, string date) { StringBuilder stringBuilder = new StringBuilder(); EconomicDataPoint economicDataPoint = m_EconomicDataManager.EconomicDataSeriesByTicker[ticker].EconomicDataPointByDate[date]; stringBuilder.AppendFormat("update {0} set {1}", m_EconomicDataTable, WriteDataToUpdateQueryString(economicDataPoint)); stringBuilder.AppendFormat("where tickerId = '{0}' and dateStr = '{1}';", m_DataBaseReader.ReportTickerID(ticker), date); return(stringBuilder.ToString()); }
/// <summary> /// Add the future economic data to data series. /// </summary> /// <param name="economicDataPoint"></param> public void AddFutureEconomicPoint(EconomicDataPoint economicDataPoint) { // Add economic data point to economic data series for this ticker as future economic data point. string date = economicDataPoint.Date; if (Functions.StringToDateTime(date) != DateTime.MaxValue && Functions.StringToDateTime(date) != DateTime.MinValue && date != Logging.m_EmptySign) { m_EconomicDataPointByDate.TryAdd(date, economicDataPoint); } }
/// <summary> /// Generate insert query code for particular ticker and date. /// </summary> /// <param name="ticker"></param> /// <param name="date"></param> /// <returns></returns> private string GetInsertQueryCode(string ticker, string date) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendFormat("insert into {0} (ts,tickerId,dateStr,timeStr,unixT,tzStr,tzBbg,eventName,shortName,securityName,surveyLastPrice,surveyHigh", m_EconomicDataTable); stringBuilder.Append(",surveyLow,surveyMedian,surveyAverage,surveyObservations,surveyStandardDeviation,indexUpdateFrequency,relevanceValue,observationPeriod) values ("); EconomicDataPoint economicDataPoint = m_EconomicDataManager.EconomicDataSeriesByTicker[ticker].EconomicDataPointByDate[date]; stringBuilder.AppendFormat("{0});", WriteDataToInsertQueryString(economicDataPoint)); return(stringBuilder.ToString()); }
/// <summary> /// Get input from the historical economic data listener and create data point. /// </summary> /// <param name="economicDataPoint"></param> /// <param name="releaseDate"></param> /// <param name="surveyLastPrice"></param> /// <param name="surveyHigh"></param> /// <param name="surveyLow"></param> /// <param name="surveyMedian"></param> /// <param name="surveyAverage"></param> /// <param name="surveyobservations"></param> /// <param name="futureStandardDeviation"></param> /// <returns></returns> public EconomicDataPoint CreateHistoricalEconomicPoint(EconomicDataPoint economicDataPoint, string releaseDate, string surveyLastPrice, string surveyHigh, string surveyLow, string surveyMedian, string surveyAverage, string surveyobservations, string futureStandardDeviation) { // Copy constant members from the latest economic data point as the first input for this function. m_CountryCode = economicDataPoint.m_CountryCode; m_Ticker = economicDataPoint.m_Ticker; m_EcoDate = releaseDate; m_EcoTime = economicDataPoint.m_EcoTime; m_EventName = economicDataPoint.m_EventName; m_ShortName = economicDataPoint.m_ShortName; m_SecurityName = economicDataPoint.m_SecurityName; m_IndexUpdateFrequency = economicDataPoint.m_IndexUpdateFrequency; m_RelevanceValue = economicDataPoint.m_RelevanceValue; m_TimeZoneCode = economicDataPoint.m_TimeZoneCode; m_ObservationPeriod = m_EmptySign; // Calculate the following economic variables. try { m_UnixTimeStamp = Convert.ToString(Functions.ConvertToUnixTimeStamp(m_EcoDate, m_EcoTime, Convert.ToInt32(m_TimeZoneCode))); m_ExchangeLocationName = Functions.ConvertToExchangeLocationName(Convert.ToInt32(m_TimeZoneCode)); } catch (Exception ex) { string errorInfo = ex.ToString(); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); } // Set corresponding historical economic values to other members. m_SurveyLastPrice = surveyLastPrice; m_SurveyHigh = surveyHigh; m_SurveyLow = surveyLow; m_SurveyMedian = surveyMedian; m_SurveyAverage = surveyAverage; m_SurveyObservations = surveyobservations; m_FutureStandardDeviation = futureStandardDeviation; if (m_EcoDate.Length < 8) { string errorInfo = string.Format("EconomicDataPoint_CreateHistoricalEconomicPoint:The economic release date for the ticker {0} has some problem and the date is {1}", m_Ticker, m_EcoDate); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); } if (m_EcoTime.Length < 12) { string errorInfo = string.Format("EconomicDataPoint_CreateHistoricalEconomicPoint:The economic release time for the ticker {0} has some problem and the time is {1}", m_Ticker, m_EcoTime); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); } return(this); }
/// <summary> /// Write information for this data point to the database. /// </summary> /// <param name="economicDataPoint"></param> public void TryWritePoint(EconomicDataPoint economicDataPoint) { lock (m_DataBaseWriteLock) { ConcurrentDictionary <string, ConcurrentDictionary <string, List <string> > > economicDataCollections = m_DataBaseReader.DataBaseEconomicDataByTicker; string ticker = economicDataPoint.Ticker; string date = economicDataPoint.Date; // Check the existence of the data in the database. bool NeedUpdate = CheckExistenceOfRow(economicDataCollections, ticker, date); if (NeedUpdate) { // Only update all the data in that row. string updateQuery = GetUpdateQueryCode(ticker, date); Logging.WriteQueryLog(updateQuery); try { MySqlCommand mySqlCommand = new MySqlCommand(updateQuery, m_MySqlEconomicDataTableConnection); mySqlCommand.ExecuteNonQuery(); } catch (MySqlException ex) { Console.WriteLine(ex.ToString()); Logging.WriteErrorLog(ex.ToString()); } } else { // Only Insert the data row. string insertQuery = GetInsertQueryCode(ticker, date); Logging.WriteQueryLog(insertQuery); try { MySqlCommand mySqlCommand = new MySqlCommand(insertQuery, m_MySqlEconomicDataTableConnection); mySqlCommand.ExecuteNonQuery(); } catch (MySqlException ex) { Console.WriteLine(ex.ToString()); Logging.WriteErrorLog(ex.ToString()); } } } }
// ***************************************************************** // **** Private Methods **** // ***************************************************************** // // /// <summary> /// Run thread to keep writing. /// </summary> private void RunWritting() { //m_MySqlEconomicDataTableConnection.Open(); while (m_ContinueWorking) { m_WaitHandle.WaitOne(); lock (m_EconomicDataPointQueue) { while (m_EconomicDataPointQueue.Count > 0) { EconomicDataPoint economicDataPoint = m_EconomicDataPointQueue.Dequeue(); TryWritePoint(economicDataPoint); } } } }
/// <summary> /// Update the future economic point that has date historically. /// </summary> /// <param name="economicDataPoint"></param> public void UpdatePoint(EconomicDataPoint economicDataPoint) { lock (m_DataBaseWriteLock) { ConcurrentDictionary <string, ConcurrentDictionary <string, List <string> > > economicDataCollections = m_DataBaseReader.DataBaseEconomicDataByTicker; string ticker = economicDataPoint.Ticker; string date = economicDataPoint.Date; // Only update all the data in that row. string updateQuery = GetUpdateQueryCode(ticker, date); MySqlCommand mySqlCommand = new MySqlCommand(updateQuery, m_MySqlEconomicDataTableConnection); Logging.WriteQueryLog(updateQuery); mySqlCommand.ExecuteNonQuery(); } }
/// <summary> /// Write latest economic data points for each ticker to string. /// </summary> /// <returns></returns> private string WriteLatestDataToString() { EconomicDataSeries dataSeries; StringBuilder stringBuilder = new StringBuilder(); // Loop to find latest economic data point in the dictionary. foreach (string ticker in m_EconomicDataManager.EconomicDataSeriesByTicker.Keys) { m_EconomicDataManager.EconomicDataSeriesByTicker.TryGetValue(ticker, out dataSeries); EconomicDataPoint economicDataPoint = dataSeries.LatestEconomicPoint; if (economicDataPoint != null) { stringBuilder.AppendLine(economicDataPoint.WriteDetaToString()); } } return(stringBuilder.ToString()); }
/// <summary> /// Add the packaged historical economic data point from listeners to the corresponding ticker economic data series. /// </summary> /// <param name="economicDataPoint"></param> public void AddHistoricalEconomicPoint(EconomicDataPoint economicDataPoint) { // Check existence of ticker. EconomicDataSeries economicDataSeries; // Check whether the latest economic point exists in the series. if (!m_EconomicDataSeriesByTicker.ContainsKey(economicDataPoint.Ticker)) { string errorInfo = string.Format("The latest economic data point does not exist in the economic data manager for ticker:{0} with date {1}." , economicDataPoint.Ticker, economicDataPoint.Date); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); } // Add economic data point to corresponding economic data series. m_EconomicDataSeriesByTicker.TryGetValue(economicDataPoint.Ticker, out economicDataSeries); if (DateInRange(economicDataPoint.Date)) { economicDataSeries.AddHistoricalEconomicPoint(economicDataPoint, m_NewTickerListenComplete); } }
/// <summary> /// Update the future economic data field besides the release date and release time. /// </summary> private void UpdateFutureEconomicData() { // The future data is first listened to and so they need to be updated finally. foreach (string ticker in m_EconomicDataManager.EconomicDataSeriesByTicker.Keys) { // Find all the points in series that have date in the future and do the change accordingly. EconomicDataSeries dataSeries = m_EconomicDataManager.EconomicDataSeriesByTicker[ticker]; foreach (string date in dataSeries.EconomicDataPointByDate.Keys) { if (Functions.StringToDateTime(date) < DateTime.Now) { continue; } else { EconomicDataPoint dataPoint = dataSeries.EconomicDataPointByDate[date]; m_EconomicDataManager.UpdateFutureDataPoint(dataPoint); } } } }
/// <summary> /// Add the packaged future economic data point from listeners to the corresponding ticker economic data series. /// </summary> /// <param name="economicDataPoint"></param> public void AddFutureEconomicPoint(EconomicDataPoint economicDataPoint) { // Future listen is the first to be launched, so instantiate the data series in the dictionary. EconomicDataSeries economicDataSeries = new EconomicDataSeries(); string ticker = economicDataPoint.Ticker; // Add the economic data series object to the corresponding ticker series dictionary. if (!m_EconomicDataSeriesByTicker.ContainsKey(ticker)) { m_EconomicDataSeriesByTicker.TryAdd(ticker, economicDataSeries); // Set the writer manager for that ticker. It is ensured to only set once. economicDataSeries.SetWriter(m_Writer); } // Add economic data point to corresponding economic data series. m_EconomicDataSeriesByTicker.TryGetValue(ticker, out economicDataSeries); if (DateInRange(economicDataPoint.Date)) { economicDataSeries.AddFutureEconomicPoint(economicDataPoint); } }
// ***************************************************************** // **** Public Methods **** // ***************************************************************** // // // // // // /// <summary> /// Add the latest economic data to data series. /// </summary> /// <param name="economicDataPoint"></param> public void AddLatestEconomicPoint(EconomicDataPoint economicDataPoint, bool newTickerCompleteFlag) { // Find this data point in the dictionary if it already exists. string date = economicDataPoint.Date; EconomicDataPoint dataPoint; m_EconomicDataPointByDate.TryGetValue(date, out dataPoint); // Add latest economic data point to economic data series for this ticker. if (dataPoint != null) { // It shows that the future data listener has requested the same date. Replace the stored data point with the latest economic data point. m_EconomicDataPointByDate[date] = economicDataPoint; if (Functions.StringToDateTime(date) != DateTime.MaxValue && Functions.StringToDateTime(date) != DateTime.MinValue && date != Logging.m_EmptySign) { m_Writer.TryWritePoint(m_EconomicDataPointByDate[date]); } } else { // There must be invalid field for the ticker or the date time does't match. string text = string.Format("There is no date match for the latest data and future data for ticker:{0}.", economicDataPoint.Ticker); Console.WriteLine(text); Logging.WriteLog(text); } // Set latest economic point and its date for this ticker. m_LatestEconomicDataPoint = economicDataPoint; DateTime dateTime = Functions.StringToDateTime(m_LatestEconomicDataPoint.Date); if (dateTime == DateTime.MinValue || dateTime == DateTime.MaxValue) { m_LatestDate = DateTime.Now.ToString("yyyyMMdd"); } else { m_LatestDate = dateTime.ToString("yyyyMMdd"); } if (!newTickerCompleteFlag) { // Update all other missing fields for all economic data points foreach (string aDate in m_EconomicDataPointByDate.Keys) { // Update those economic data points and also write to the data base. EconomicDataPoint point = m_EconomicDataPointByDate[aDate]; if (Functions.StringToDateTime(aDate) != Functions.StringToDateTime(m_LatestDate)) { point.UpdateMissingFutureDataPointFields(m_LatestEconomicDataPoint); if (Functions.StringToDateTime(point.Date) != DateTime.MinValue && Functions.StringToDateTime(point.Date) != DateTime.MaxValue) { m_Writer.TryWritePoint(point); } } } } else { // Update all other missing fields for all future economic data points foreach (string aDate in m_EconomicDataPointByDate.Keys) { // Update those economic data points and also write to the data base. EconomicDataPoint point = m_EconomicDataPointByDate[aDate]; if (Functions.StringToDateTime(aDate) > Functions.StringToDateTime(m_LatestDate)) { point.UpdateMissingFutureDataPointFields(m_LatestEconomicDataPoint); if (Functions.StringToDateTime(point.Date) != DateTime.MaxValue) { m_Writer.TryWritePoint(point); } } } } }
/// <summary> /// Record future economic data information. /// </summary> /// <param name="eventObj"></param> private void RecordFutureEconomicData(Event eventObj) { StringBuilder stringBuilder = new StringBuilder(); foreach (Message message in eventObj) { stringBuilder.AppendLine(message.ToString()); Element response = message.AsElement; if (response.HasElement("responseError")) { string errorInfo = string.Format("FutureDataListener_RecordFutureEconomicData:There is response error here!The message content is {0}.", message.ToString()); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } if (!response.HasElement("securityData")) { string errorInfo = string.Format("FutureDataListener_RecordFutureEconomicData:There is no securityData here!The message content is {0}.", message.ToString()); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } Element securityDataArray = response.GetElement("securityData"); int numberOfSecurityData = securityDataArray.NumValues; if (numberOfSecurityData == 0) { string errorInfo = string.Format("FutureDataListener_RecordFutureEconomicData:There is no data for any securities here!The message content is {0}.", message.ToString()); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } for (int i = 0; i < numberOfSecurityData; ++i) { Element securityData = securityDataArray.GetValueAsElement(i); string ticker = string.Empty; if (securityData.HasElement("security")) ticker = securityData.GetElementAsString("security"); else { string errorInfo = string.Format("FutureDataListener_RecordFutureEconomicData:There is no ticker!The message content is {0}.", message.ToString()); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } if (securityData.HasElement("securityError")) { string errorInfo = string.Format("FutureDataListener_RecordFutureEconomicData:There is securityError!The error ticker is {0}.", ticker); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } if (securityData.HasElement("fieldException")) { string errorInfo = string.Format("FutureDataListener_RecordFutureEconomicData:There is field exception!The error ticker is {0}.", ticker); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } if (!securityData.HasElement("fieldData")) { string errorInfo = string.Format("FutureDataListener_RecordFutureEconomicData:There is no field data!The error ticker is {0}.", ticker); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } else { Element fields = securityData.GetElement("fieldData"); int numberOfFields = fields.NumElements; if (numberOfFields == 0) { string errorInfo = string.Format("FutureDataListener_RecordFutureEconomicData:There is no field for this security here!The error ticker is {0}.", ticker); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } else { int numberOfField = fields.NumElements; if (numberOfField == 0) { string errorInfo = string.Format("FutureDataListener_RecordFutureEconomicData:There is no field for this security here!The error ticker is {0}.", ticker); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); m_EconomicDataManager.m_BadTickerList.Add(ticker); continue; } else { Element field = fields.GetElement(0); if (field.NumValues > 0) { for (int k = 0; k < field.NumValues; k++) { Element temp = field.GetValueAsElement(k); Element temptemp = temp.GetElement(0); string accurateTime = temptemp.GetValueAsString(); string futureReleaseDate = TransformDateStringFormat(accurateTime); string futureReleaseTime = TransformTimeStringFormat(accurateTime); EconomicDataPoint economicDataPoint = new EconomicDataPoint(); economicDataPoint.CreateFutureEconomicPoint( ticker, futureReleaseDate, futureReleaseTime ); m_EconomicDataManager.AddFutureEconomicPoint(economicDataPoint); m_EconomicDataManager.m_RowCount++; string text = string.Format("FutureDataListener_RecordFutureEconomicData:Bloomberg gave {0} rows already. Message:{1}", m_EconomicDataManager.m_RowCount, economicDataPoint.WriteDetaToString()); stringBuilder.AppendLine(text); Console.WriteLine(text); } } else { string errorInfo = string.Format("FutureDataListener_RecordFutureEconomicData:The array returned has 0 dimension. The error ticker is {0}.", ticker); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); m_EconomicDataManager.m_BadTickerList.Add(ticker); continue; } } } } } } // Write to log all the message details. Logging.WriteLog(stringBuilder.ToString()); }
/// <summary> /// Record historical economic data information. /// </summary> /// <param name="eventObj"></param> private void RecordHistoricalEconomicData(Event eventObj) { StringBuilder stringBuilder = new StringBuilder(); foreach (Message message in eventObj) { stringBuilder.AppendLine(message.ToString()); Element response = message.AsElement; if (response.HasElement("responseError")) { string errorInfo = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:There is response error here!The message content is {0}.", message.ToString()); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } if (!response.HasElement("securityData")) { string errorInfo = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:There is no securityData here!The message content is {0}.", message.ToString()); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } Element securityData = response.GetElement("securityData"); if (securityData.HasElement("securityError")) { string errorInfo = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:There is security error here!The message content is {0}.", message.ToString()); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } if (securityData.HasElement("fieldException")) { string errorInfo = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:There is field exception here!The message content is {0}.", message.ToString()); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } string ticker = string.Empty; if (securityData.HasElement("security")) { ticker = securityData.GetElementAsString("security"); } else { string errorInfo = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:There is no ticker!The message content is {0}.", message.ToString()); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } if (!securityData.HasElement("fieldData")) { string errorInfo = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:There is no field data!The error ticker is {0}.", ticker); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } else { Element fieldDataArray = securityData.GetElement("fieldData"); int numberOfFieldRows = fieldDataArray.NumValues; if (numberOfFieldRows == 0) { string errorInfo = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:There is no field data history! The error ticker name is {0}.", ticker); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } else { EconomicDataSeries economicDataSeries; m_EconomicDataManager.EconomicDataSeriesByTicker.TryGetValue(ticker, out economicDataSeries); if (economicDataSeries != null) { EconomicDataPoint latestEconomicDataPoint = economicDataSeries.LatestEconomicPoint; if (latestEconomicDataPoint == null) { string text = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:There is no latest economic data point for this ticker:{0}.", ticker); Console.WriteLine(text); Logging.WriteErrorLog(text); continue; } if (numberOfFieldRows > 0) { for (int i = 0; i < numberOfFieldRows; ++i) { Element fieldData = fieldDataArray.GetValueAsElement(i); string releaseDate = m_EmptySign; string surveyActualRelease = m_EmptySign; string surveyHigh = m_EmptySign; string surveyLow = m_EmptySign; string surveyMedian = m_EmptySign; string surveyAverage = m_EmptySign; string surveyObservations = m_EmptySign; string futureStandardDeviation = m_EmptySign; if (fieldData.HasElement(m_EcoDateString)) { releaseDate = fieldData.GetElementAsString(m_EcoDateString); } else { string text = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:Ticker:{0} does not have field {1}.", ticker, m_EcoDateString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fieldData.HasElement(m_SurveyActualRelease)) { surveyActualRelease = fieldData.GetElementAsString(m_SurveyActualRelease); } else { string text = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:Ticker:{0} does not have field {1}.", ticker, m_SurveyActualRelease); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fieldData.HasElement(m_SurveyHighString)) { surveyHigh = fieldData.GetElementAsString(m_SurveyHighString); } else { string text = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:Ticker:{0} does not have field {1}.", ticker, m_SurveyHighString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fieldData.HasElement(m_SurveyLowString)) { surveyLow = fieldData.GetElementAsString(m_SurveyLowString); } else { string text = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:Ticker:{0} does not have field {1}.", ticker, m_SurveyLowString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fieldData.HasElement(m_SurveyMedianString)) { surveyMedian = fieldData.GetElementAsString(m_SurveyMedianString); } else { string text = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:Ticker:{0} does not have field {1}.", ticker, m_SurveyMedianString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fieldData.HasElement(m_SurveyAverageString)) { surveyAverage = fieldData.GetElementAsString(m_SurveyAverageString); } else { string text = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:Ticker:{0} does not have field {1}.", ticker, m_SurveyAverageString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fieldData.HasElement(m_SurveyObservationsString)) { surveyObservations = fieldData.GetElementAsString(m_SurveyObservationsString); } else { string text = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:Ticker:{0} does not have field {1}.", ticker, m_SurveyObservationsString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fieldData.HasElement(m_FutureStandardDeviationString)) { futureStandardDeviation = fieldData.GetElementAsString(m_FutureStandardDeviationString); } else { string text = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:Ticker:{0} does not have field {1}.", ticker, m_FutureStandardDeviationString); Console.WriteLine(text); Logging.WriteErrorLog(text); } EconomicDataPoint economicDataPoint = new EconomicDataPoint(); economicDataPoint.CreateHistoricalEconomicPoint( latestEconomicDataPoint, releaseDate, surveyActualRelease, surveyHigh, surveyLow, surveyMedian, surveyAverage, surveyObservations, futureStandardDeviation ); m_EconomicDataManager.AddHistoricalEconomicPoint(economicDataPoint); m_EconomicDataManager.m_RowCount++; // Append message that we record. string textMessage = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:Bloomberg gave {0} rows already. Message:{1}" , m_EconomicDataManager.m_RowCount, economicDataPoint.WriteDetaToString()); stringBuilder.AppendLine(textMessage); Console.WriteLine(textMessage); } } else { string errorInfo = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:The returned array dimension is 0. The error ticker name is {0}.", ticker); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } } else { string errorInfo = string.Format("HistoricalDataListener_RecordHistoricalEconomicData:There is no economic series for this ticker here! The error ticker is {0}.", ticker); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } } } } // Write to log all the message details. Logging.WriteLog(stringBuilder.ToString()); }
/// <summary> /// Record latest economic data information. /// </summary> /// <param name="eventObj"></param> private void RecordLatestEconomicData(Event eventObj) { StringBuilder stringBuilder = new StringBuilder(); foreach (Message message in eventObj) { stringBuilder.AppendLine(message.ToString()); Element response = message.AsElement; if (response.HasElement("responseError")) { string errorInfo = string.Format("LatestDataListener_RecordLatestEconomicData:There is response error here!The message content is {0}.", message.ToString()); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } if (!response.HasElement("securityData")) { string errorInfo = string.Format("LatestDataListener_RecordLatestEconomicData:There is no securityData here!The message content is {0}.", message.ToString()); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } Element securityDataArray = response.GetElement("securityData"); int numberOfSecurityData = securityDataArray.NumValues; if (numberOfSecurityData == 0) { string errorInfo = string.Format("LatestDataListener_RecordLatestEconomicData:There is no data for any securities here! The message content is {0}.", message.ToString()); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } for (int i = 0; i < numberOfSecurityData; ++i) { Element securityData = securityDataArray.GetValueAsElement(i); string ticker = string.Empty; if (securityData.HasElement("security")) { ticker = securityData.GetElementAsString("security"); } else { string errorInfo = string.Format("LatestDataListener_RecordLatestEconomicData:There is no ticker! The message content is {0}.", message.ToString()); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } if (securityData.HasElement("securityError")) { string errorInfo = string.Format("LatestDataListener_RecordLatestEconomicData:There is securityError!The error ticker is {0}.", ticker); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } if (securityData.HasElement("fieldException")) { string errorInfo = string.Format("LatestDataListener_RecordLatestEconomicData:There is field exception!The error ticker is {0}.", ticker); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } if (!securityData.HasElement("fieldData")) { string errorInfo = string.Format("LatestDataListener_RecordLatestEconomicData:There is no field data!The error ticker is {0}.", ticker); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } else { Element fields = securityData.GetElement("fieldData"); int numberOfFields = fields.NumElements; if (numberOfFields == 0) { string errorInfo = string.Format("LatestDataListener_RecordLatestEconomicData:There is no field for this security here!The error ticker is {0}.", ticker); Console.WriteLine(errorInfo); Logging.WriteErrorLog(errorInfo); continue; } else { string countryID = m_EmptySign; string ecoDate = m_EmptySign; string ecoTime = m_EmptySign; string eventName = m_EmptySign; string shortName = m_EmptySign; string securityName = m_EmptySign; string surveyLast = m_EmptySign; string surveyHigh = m_EmptySign; string surveyLow = m_EmptySign; string surveyMedian = m_EmptySign; string surveyAverage = m_EmptySign; string surveyObservations = m_EmptySign; string futureStandardDeviation = m_EmptySign; string indexUpdateFrequency = m_EmptySign; string relevanceValue = m_EmptySign; string timeZoneCode = m_EmptySign; string observationPeriod = m_EmptySign; if (fields.HasElement(m_CountryCodeString)) { countryID = fields.GetElementAsString(m_CountryCodeString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:Ticker:{0} does not have field {1}.", ticker, m_CountryCodeString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_EcoDateString)) { ecoDate = fields.GetElementAsString(m_EcoDateString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:For the ticker:{0}, the Bloomberg does not know field {1}.", ticker, m_EcoDateString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_EcoTimeString)) { ecoTime = fields.GetElementAsString(m_EcoTimeString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:For the ticker:{0}, the Bloomberg does not know field {1}.", ticker, m_EcoTimeString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_EventNameString)) { eventName = fields.GetElementAsString(m_EventNameString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:Ticker:{0} does not have field {1}.", ticker, m_EventNameString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_LongCompNameString)) { string longCompName = fields.GetElementAsString(m_LongCompNameString); if (longCompName != null && longCompName != "" && longCompName != string.Empty) { eventName = longCompName; } } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:Ticker:{0} does not have field {1}.", ticker, m_LongCompNameString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_ShortNameString)) { shortName = fields.GetElementAsString(m_ShortNameString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:Ticker:{0} does not have field {1}.", ticker, m_ShortNameString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_SecurityNameString)) { securityName = fields.GetElementAsString(m_SecurityNameString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:Ticker:{0} does not have field {1}.", ticker, m_SecurityNameString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_SurveyLastPriceString)) { surveyLast = fields.GetElementAsString(m_SurveyLastPriceString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:Ticker:{0} does not have field {1}.", ticker, m_SurveyLastPriceString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_SurveyHighString)) { surveyHigh = fields.GetElementAsString(m_SurveyHighString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:Ticker:{0} does not have field {1}.", ticker, m_SurveyHighString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_SurveyLowString)) { surveyLow = fields.GetElementAsString(m_SurveyLowString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:Ticker:{0} does not have field {1}.", ticker, m_SurveyLowString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_SurveyMedianString)) { surveyMedian = fields.GetElementAsString(m_SurveyMedianString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:Ticker:{0} does not have field {1}.", ticker, m_SurveyMedianString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_SurveyAverageString)) { surveyAverage = fields.GetElementAsString(m_SurveyAverageString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:Ticker:{0} does not have field {1}.", ticker, m_SurveyAverageString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_SurveyObservationsString)) { surveyObservations = fields.GetElementAsString(m_SurveyObservationsString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:Ticker:{0} does not have field {1}.", ticker, m_SurveyObservationsString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_FutureStandardDeviationString)) { futureStandardDeviation = fields.GetElementAsString(m_FutureStandardDeviationString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData;Ticker:{0} does not have field {1}.", ticker, m_FutureStandardDeviationString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_IndexUpdateFrequencyString)) { indexUpdateFrequency = fields.GetElementAsString(m_IndexUpdateFrequencyString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:Ticker:{0} does not have field {1}.", ticker, m_IndexUpdateFrequencyString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_RelevanceValueString)) { relevanceValue = fields.GetElementAsString(m_RelevanceValueString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:Ticker:{0} does not have field {1}.", ticker, m_RelevanceValueString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_TimeZoneCodeString)) { timeZoneCode = fields.GetElementAsString(m_TimeZoneCodeString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:Ticker:{0} does not have field {1}.", ticker, m_TimeZoneCodeString); Console.WriteLine(text); Logging.WriteErrorLog(text); } if (fields.HasElement(m_ObservationPeriodString)) { observationPeriod = fields.GetElementAsString(m_ObservationPeriodString); } else { string text = string.Format("LatestDataListener_RecordLatestEconomicData:Ticker:{0} does not have field {1}.", ticker, m_ObservationPeriodString); Console.WriteLine(text); Logging.WriteErrorLog(text); } ecoDate = TransformDateStringFormat(ecoDate); EconomicDataPoint economicDataPoint = new EconomicDataPoint(); economicDataPoint.CreateLatestEconomicPoint( countryID, ticker, ecoDate, ecoTime, eventName, shortName, securityName, surveyLast, surveyHigh, surveyLow, surveyMedian, surveyAverage, surveyObservations, futureStandardDeviation, indexUpdateFrequency, relevanceValue, timeZoneCode, observationPeriod ); m_EconomicDataManager.AddLatestEconomicPoint(economicDataPoint); m_EconomicDataManager.m_RowCount++; string textMessage = string.Format("LatestDataListener_RecordLatestEconomicData:Bloomberg gave {0} rows already. Message:{1}" , m_EconomicDataManager.m_RowCount, economicDataPoint.WriteDetaToString()); stringBuilder.AppendLine(textMessage); Console.WriteLine(textMessage); } } } } // Write to log all the message details. Logging.WriteLog(stringBuilder.ToString()); }