Ejemplo n.º 1
0
        //
        //
        //
        /// <summary>
        /// This function is the inverse of the above.
        /// It will convert U4 to Sep14 like this.
        /// </summary>
        /// <param name="monthCodeYear"></param>
        /// <param name="MMMYY"></param>
        /// <returns></returns>
        public static bool TryParseMonthCodeToMMMYY(string monthCodeYear, out string MMMYY)
        {
            bool isSuccess = false;

            MMMYY = null;
            if (monthCodeYear.Length == 2)
            {
                string monthCode = monthCodeYear.Substring(0, 1);
                string year      = monthCodeYear.Substring(1, 1);
                int    yearNumber;
                if (int.TryParse(year, out yearNumber))
                {
                    double years = (DateTime.Now.Year - 2000) / 10;
                    yearNumber += 10 * (int)Math.Floor(years);
                    int    monthNumber = BaseQTMath.GetMonthNumberFromCode(monthCode);
                    string monthName;
                    if (BaseQTMath.TryGetMonthName(monthNumber, out monthName))
                    {
                        MMMYY     = string.Format("{0}{1}", monthName, yearNumber);
                        isSuccess = true;
                    }
                }
            }
            return(isSuccess);
        }
Ejemplo n.º 2
0
        //
        //
        //
        /// <summary>
        /// Convert instrument name to instrument data base name.
        /// </summary>
        /// <param name="instrumentName"></param>
        /// <param name="instrumentNameDatabase"></param>
        /// <returns></returns>
        public static bool TryConvertInstrumentNameToInstrumentNameDatabase(InstrumentName instrumentName, LogHub log, out string instrumentNameDatabase)
        {
            bool isSuccess = false;

            instrumentNameDatabase = null;
            string       productName  = instrumentName.Product.ProductName;
            string       seriesName   = instrumentName.SeriesName;
            ProductTypes productTypes = instrumentName.Product.Type;
            string       monthYearCode;

            switch (productTypes)
            {
            case ProductTypes.Future:

                if (BaseQTMath.TryConvertMonthYearToCodeY(seriesName, out monthYearCode))
                {
                    instrumentNameDatabase = string.Format("1x{0}_{1}", productName, monthYearCode);
                    isSuccess = true;
                }
                else
                {
                    log.NewEntry(LogLevel.Error, "Parse month year code wrong for string {0}.", seriesName);
                    return(isSuccess);
                }
                break;

            case ProductTypes.Spread:
                // Target: Calendar: 1xGE Sep14:-1xDec14
                List <string> matchedResults = new List <string>();
                if (seriesName.Contains("Calendar") || seriesName.Contains("Butterfly") || seriesName.Contains("Double Butterfly"))
                {
                    string          pattern         = @"-?\d+x[^:]+(?=(:?))";
                    MatchCollection matchCollection = Regex.Matches(seriesName, pattern);
                    foreach (Match match in matchCollection)
                    {
                        string matchedValue = match.Value;
                        matchedResults.Add(matchedValue);
                    }

                    StringBuilder instrumentDatabaseNameBuilder = new StringBuilder();
                    bool          isFirst = true;
                    foreach (string matchedString in matchedResults)
                    {
                        char     seperator = 'x';
                        string[] parts     = matchedString.Split(new char[] { seperator }, StringSplitOptions.RemoveEmptyEntries);
                        if (parts.Length == 2)
                        {
                            string weightString = parts[0];
                            int    weight;
                            if (int.TryParse(weightString, out weight))
                            {
                                string monthYear = parts[1];
                                pattern         = @"(?!x)[a-zA-Z]{3}[0-9]{2}\z";
                                matchCollection = Regex.Matches(monthYear, pattern);
                                string matchedValue = null;
                                foreach (Match match in matchCollection)
                                {
                                    if (match.Success)
                                    {
                                        matchedValue = match.Value;
                                        break;
                                    }
                                }
                                if (!string.IsNullOrEmpty(matchedValue))
                                {
                                    if (BaseQTMath.TryConvertMonthYearToCodeY(matchedValue, out monthYearCode))
                                    {
                                        if (!isFirst)
                                        {
                                            instrumentDatabaseNameBuilder.Append(".");
                                        }
                                        else
                                        {
                                            isFirst = false;
                                        }
                                        instrumentDatabaseNameBuilder.AppendFormat("{0}x{1}_{2}", weight, productName, monthYearCode);
                                    }
                                    else
                                    {
                                        log.NewEntry(LogLevel.Error, "month year parsed wrong for {0}.", matchedValue);
                                        return(isSuccess);
                                    }
                                }
                                else
                                {
                                    log.NewEntry(LogLevel.Error, "month year matched wrong for {0}.", monthYear);
                                    return(isSuccess);
                                }
                            }
                            else
                            {
                                log.NewEntry(LogLevel.Error, "weight parsed wrong for {0}.", weightString);
                                return(isSuccess);
                            }
                        }
                        else
                        {
                            log.NewEntry(LogLevel.Error, "string {0} does not contain weight and month year.", matchedString);
                            return(isSuccess);
                        }
                    }
                    instrumentNameDatabase = instrumentDatabaseNameBuilder.ToString();
                    isSuccess = true;
                }
                else
                {
                    log.NewEntry(LogLevel.Error, "Unhandled product type for string {0}.", seriesName);
                    return(isSuccess);
                }
                break;

            default:
                break;
            }
            return(isSuccess);
        }