Пример #1
0
        /// <summary>
        /// uses LINQ to generate monthly and weekly data from daily data
        /// </summary>
        /// <param name="pt"></param>
        /// <returns></returns>
        public aStock generateOtherPeriod(aPeriodType pt)
        {
            //// create new aCandlestick object and set it to cStick
            List<aCandlestick> candlesticks = new List<aCandlestick>();

            // create new aStock object
            aStock newstock = new aStock(startingDate, endingDate, pt, candlesticks);

            if (pt == (aStock.aPeriodType)Enum.Parse(typeof(aStock.aPeriodType), "MONTHLY"))
            {
                // group candlesticks by year and month using LINQ
                var dateGroup = from candlestick in cStick // take the candlesticks...
                                group candlestick by candlestick.StartingDate.Year into ygroup // and GROUP them by year (LINQ)
                                from mgroup in
                                    (from candlestick in ygroup // take year groups...
                                     group candlestick by candlestick.StartingDate.Month)
                                group mgroup by ygroup.Key; // and GROUP them by month (LINQ)

                // now add the grouped candlesticks to a list
                foreach (var candlestickYear in dateGroup)
                {
                    // for each inner month group inside of outer year group
                    foreach (var candlestickMonth in candlestickYear)
                    {
                        // set candlestick values
                        decimal newlow = 1000000;
                        decimal newhigh = 0;
                        DateTime newdate = candlestickMonth.Last().StartingDate;
                        decimal newopen = candlestickMonth.First().Open;
                        decimal newclose = candlestickMonth.Last().Close;
                        double newvolume = 0;

                        // find the highest, lowest, and sum the volumes
                        foreach (var stick in candlestickMonth)
                        {
                            newvolume += stick.Volume;

                            if (stick.Low < newlow)
                            {
                                newlow = stick.Low;
                            }

                            if (stick.High > newhigh)
                            {
                                newhigh = stick.High;
                            }
                        }

                        // create a new aCandlestick and populate it with candlestick data
                        aCandlestick outputStick = new aCandlestick(newdate, newopen, newhigh, newlow, newclose, newvolume);

                        // add it to the newstock object
                        newstock.Candlestick.Add(outputStick);
                    }

                }

            }
            else if (pt == (aStock.aPeriodType)Enum.Parse(typeof(aStock.aPeriodType), "WEEKLY")) // if period is weekly
            {
                // set calendar
                Calendar cal = new GregorianCalendar();
                DayOfWeek firstDay = DayOfWeek.Sunday;

                // set calendar rule
                CalendarWeekRule rule;
                rule = CalendarWeekRule.FirstDay;

                // group candlesticks by year and week using LINQ
                var dateGroup = from candlestick in cStick // take the candlesticks...
                                group candlestick by candlestick.StartingDate.Year into ygroup // and GROUP them by year (LINQ)
                                from wgroup in
                                    (from candlestick in ygroup // take the year groups...
                                     group candlestick by cal.GetWeekOfYear(candlestick.StartingDate, rule, firstDay))
                                group wgroup by ygroup.Key; // and GROUP them by week of the year (LINQ)

                // now add the grouped candlesticks to a list
                foreach (var candlestickYear in dateGroup)
                {
                    // for each inner week group inside of outer year group
                    foreach (var candlestickWeek in candlestickYear)
                    {
                        // set candlesick values
                        decimal newlow = 1000000;
                        decimal newhigh = 0;
                        DateTime newdate = candlestickWeek.Last().StartingDate;
                        decimal newopen = candlestickWeek.First().Open;
                        decimal newclose = candlestickWeek.Last().Close;
                        double newvolume = 0;

                        // find the highest, lowest, and sum the volumes
                        foreach (var stick in candlestickWeek)
                        {
                            newvolume += stick.Volume;

                            if (stick.Low < newlow)
                            {
                                newlow = stick.Low;
                            }

                            if (stick.High > newhigh)
                            {
                                newhigh = stick.High;
                            }
                        }

                        // create a new aCandlestick and populate it with candlestick data
                        aCandlestick outputStick = new aCandlestick(newdate, newopen, newhigh, newlow, newclose, newvolume);

                        // add it to the newstock object
                        newstock.Candlestick.Add(outputStick);
                    }

                }

            }

            // return the newstock to the caller, to be displayed
            return newstock;
        }
Пример #2
0
        /// <summary>
        /// read aStock data from a CSV file
        /// </summary>
        /// <param name="filename"></param>
        public void ReadFromFile(string filename)
        {
            // read data from CSV file using StreamReader
            using (StreamReader sr = new StreamReader(filename))
            {
                sr.ReadLine();
                string current;
                while ((current = sr.ReadLine()) != null)
                {
                    string[] csv_value = current.Split(','); //parse each line and pull the high/low/open/close values from it
                    List<string> csv_values = new List<string>();

                    // only read lines that have a date that are between the start and end dates
                    if (DateTime.Parse(csv_value[0]) <= endingDate && DateTime.Parse(csv_value[0]) >= startingDate)
                    {
                        csv_value[0] = csv_value[0].Replace(" 0:00", ""); // remove the time from date
                        for (int i = 0; i < 6; ++i) // read first 6 columns (date - volume)
                        {
                            csv_values.Add(csv_value[i]);
                        }

                        // add data from csv list to aCandlestick object
                        aCandlestick candlestick = new aCandlestick(DateTime.Parse(csv_values[0]), decimal.Parse(csv_values[1]), decimal.Parse(csv_values[2]), decimal.Parse(csv_values[3]), decimal.Parse(csv_values[4]), double.Parse(csv_values[5]));
                        Candlestick.Add(candlestick);
                    } // end if

                } // end while
            } // end using StreamReader
        }