Example #1
0
        public Importer(string formatString)
        {
            string[] subStrings = formatString.Split(seperators, StringSplitOptions.None);
            if (subStrings.Length == 1)
            {
                subStrings = formatString.Split(new char[] { ' ' });
            }

            int    secondStart = formatString.IndexOf(subStrings[1]);
            string temp        = formatString.Substring(subStrings[0].Length, secondStart - subStrings[0].Length);

            if (temp.Length != 1)
            {
                throw new Exception("Splittor is missing or not detected!");
            }

            Splitter = temp.ToCharArray();

            if (findIndicator(subStrings, dateIndicator) == -1)
            {
                subStrings = DefaultFormat;
            }

            this.paraDict = new Dictionary <string, int>();

            foreach (KeyValuePair <string, string[]> kvp in KeywordsDictionary)
            {
                this.paraDict.Add(kvp.Key, findIndicator(subStrings, kvp.Value));
            }

            dateIndex   = this[dateIndicator[0]];
            openIndex   = this[openIndicator[0]];
            highIndex   = this[highIndicator[0]];
            lowIndex    = this[lowIndicator[0]];
            closeIndex  = this[closeIndicator[0]];
            volumeIndex = this[volumeIndicator[0]];

            tryParseTime = new ParseDateTimeDelegate(DateTime.TryParse);
        }
Example #2
0
        public DayItem FromFirstString(string line)
        {
            DateTime      time;
            List <Double> values = new List <Double>();
            double        temp;

            String[] subStr = line.Split(Splitter);

            if (dateIndex == -1 || subStr[dateIndex] == "")
            {
                throw new ArgumentException("Fail to get date from: " + line);
            }
            else if (!DateTime.TryParse(subStr[dateIndex], out time))
            {
                string dateStr = subStr[dateIndex];
                switch (dateStr.Length)
                {
                case 6:
                    dateTimeFormat = "yyMMdd";
                    break;

                case 8:
                    dateTimeFormat = "yyyyMMdd";
                    break;

                default:
                    throw new NotImplementedException();
                }

                if (parseDateTime(dateStr, out time))
                {
                    tryParseTime = new ParseDateTimeDelegate(parseDateTime);
                }
                else
                {
                    throw new ArgumentException("Fail to get date from: " + line);
                }
            }

            if (openIndex == -1 || subStr[openIndex] == "" || !Double.TryParse(subStr[openIndex], out temp))
            {
                throw new Exception("Open value is not as expected!");
            }
            else
            {
                values.Add(temp);
            }

            if (highIndex == -1 || subStr[highIndex] == "" || !Double.TryParse(subStr[highIndex], out temp))
            {
                throw new Exception("Top value is not as expected!");
            }
            else
            {
                values.Add(temp);
            }

            if (lowIndex == -1 || subStr[lowIndex] == "" || !Double.TryParse(subStr[lowIndex], out temp))
            {
                throw new Exception("Bottom value is not as expected!");
            }
            else
            {
                values.Add(temp);
            }

            if (closeIndex == -1 || subStr[closeIndex] == "" || !Double.TryParse(subStr[closeIndex], out temp))
            {
                throw new Exception("Close value is not as expected!");
            }
            else
            {
                values.Add(temp);
            }

            if (volumeIndex == -1 || subStr[volumeIndex] == "" || !Double.TryParse(subStr[volumeIndex], out temp))
            {
                values.Add(0);
            }
            else
            {
                values.Add(temp);
            }

            return(new DayItem(time, values));
        }