Пример #1
0
        public void LoadStockDailyOrMinute()
        {
            Candles.Clear();
            CandlesBasic.Clear();
            CandlesHistory.Clear();

            try
            {
                string fName = LoadPathFileCsv();

                bool bDate1 = false;
                bool bDate2 = false;
                bool fileOrderDescending;

                DateTime date1 = DateTime.Today;
                DateTime date2 = DateTime.Today;
                string[] time1 = null;
                string[] time2 = null;
                int      candleInfoIndex;

                StreamReader stream = new StreamReader(fName);
                string       row;
                bool         goodSoFar = false;

                while ((row = stream.ReadLine()) != null)
                {
                    string[] splitRow = row.Split(';');

                    if (splitRow.Length < 6 || splitRow[0].Equals("#"))
                    {
                        continue;
                    }

                    if (!bDate1)
                    {
                        DateTime.TryParse(splitRow[1], out date1);
                        bDate1 = true;
                        time1  = splitRow[2].Split(':');
                        continue;
                    }

                    if (!bDate2)
                    {
                        DateTime.TryParse(splitRow[1], out date2);
                        bDate2    = true;
                        time2     = splitRow[2].Split(':');
                        goodSoFar = true;
                        stream.Close();
                        break;
                    }
                }

                if (!goodSoFar)
                {
                    throw new Exception("Invalid CSV File - 1 Each row must be Symbol, Date, Time (optional), Open, High, Low, Close, and Volume (optional).");
                }

                if (time1.Length == 3 && time2.Length == 3)
                {
                    candleInfoIndex = 3;

                    DateTime date1Tmp = new DateTime(date1.Year, date1.Month, date1.Day,
                                                     Convert.ToInt32(time1[0]), Convert.ToInt32(time1[1]), Convert.ToInt32(time1[2]));
                    DateTime date2Tmp = new DateTime(date2.Year, date2.Month, date2.Day,
                                                     Convert.ToInt32(time2[0]), Convert.ToInt32(time2[1]), Convert.ToInt32(time2[2]));

                    fileOrderDescending = (date1Tmp > date2Tmp) ? false : true;
                }
                else if (time1.Length == 1 && time2.Length == 1)
                {
                    candleInfoIndex     = 2;
                    fileOrderDescending = (date1 > date2) ? false : true;
                }
                else
                {
                    throw new Exception("Invalid CSV File" + " 2 Each row must be Symbol, Date, Time (optional), Open, High, Low, Close, and Volume (optional).");
                }

                int hr;
                int mn;
                int sc;

                stream = new StreamReader(fName);

                List <string> listRowCollection = new List <string>();

                while ((row = stream.ReadLine()) != null)
                {
                    listRowCollection.Add(row);
                }

                stream.Close();

                if (fileOrderDescending)
                {
                    foreach (string t in listRowCollection)
                    {
                        string[] splitRow = t.Split(';');

                        if (splitRow.Length < 6 || splitRow[0].Equals("#"))
                        {
                            continue;
                        }

                        DateTime dt;
                        if (!DateTime.TryParseExact(splitRow[1], "dd/MM/yyyy", null, DateTimeStyles.None, out dt))
                        {
                            continue;
                        }

                        hr = dt.Hour;
                        mn = dt.Minute;
                        sc = dt.Second;
                        if (dt.Hour == 0)
                        {
                            hr = 0;
                            mn = 0;
                            sc = 0;
                        }

                        if (candleInfoIndex == 3)
                        {
                            string[] strTime = splitRow[2].Split(':');
                            if (strTime.Length == 3)
                            {
                                hr = Convert.ToInt16(strTime[0]);
                                mn = Convert.ToInt16(strTime[1]);
                                sc = Convert.ToInt16(strTime[2]);
                            }
                        }

                        CandlesBasic.Add(new RegCandle
                        {
                            Date   = new DateTime(dt.Year, dt.Month, dt.Day, hr, mn, sc),
                            Open   = Convert.ToDouble(splitRow[candleInfoIndex]),
                            High   = Convert.ToDouble(splitRow[candleInfoIndex + 1]),
                            Low    = Convert.ToDouble(splitRow[candleInfoIndex + 2]),
                            Close  = Convert.ToDouble(splitRow[candleInfoIndex + 3]),
                            Volume = Convert.ToDouble(splitRow[candleInfoIndex + 4])
                        });
                    }
                }
                else
                {
                    for (int i = listRowCollection.Count - 1; i >= 0; i--)
                    {
                        string[] splitRow = listRowCollection[i].Split(';');

                        if (splitRow.Length < 6 || splitRow[0].Equals("#"))
                        {
                            continue;
                        }

                        DateTime dt;
                        if (!DateTime.TryParseExact(splitRow[1], "dd/MM/yyyy", null, DateTimeStyles.None, out dt))
                        {
                            continue;
                        }

                        hr = dt.Hour;
                        mn = dt.Minute;
                        sc = dt.Second;

                        if (dt.Hour == 0)
                        {
                            hr = 0;
                            mn = 0;
                            sc = 0;
                        }

                        if (candleInfoIndex == 3)
                        {
                            string[] strTime = splitRow[2].Split(':');
                            if (strTime.Length == 3)
                            {
                                hr = Convert.ToInt16(strTime[0]);
                                mn = Convert.ToInt16(strTime[1]);
                                sc = Convert.ToInt16(strTime[2]);
                            }
                        }

                        CandlesBasic.Add(new RegCandle
                        {
                            Date   = new DateTime(dt.Year, dt.Month, dt.Day, hr, mn, sc),
                            Open   = Convert.ToDouble(splitRow[candleInfoIndex]),
                            High   = Convert.ToDouble(splitRow[candleInfoIndex + 1]),
                            Low    = Convert.ToDouble(splitRow[candleInfoIndex + 2]),
                            Close  = Convert.ToDouble(splitRow[candleInfoIndex + 3]),
                            Volume = Convert.ToDouble(splitRow[candleInfoIndex + 4])
                        });
                    }
                }

                if (((Periodicity == Periodicity.Daily) || ((Periodicity == Periodicity.Minutely))) &&
                    (Interval == 1))
                {
                    Candles.AddRange(CandlesBasic);
                }
                else
                {
                    Transform();
                }

                CandlesHistory.AddRange((Candles.Count - History) > 0
                                            ? Candles.GetRange(Candles.Count - History, History)
                                            : Candles);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }