// constructor public aStock(DateTime start, DateTime end, aPeriodType period, List<aCandlestick> candleStick) { periodType = period; startingDate = start; endingDate = end; cStick = candleStick; }
public aStock(string resolution, DateTime startTime, DateTime endTime, string ticker) { //aPeriodType PeriodType if (resolution.ToUpper() == "DAILY") { PeriodType = aPeriodType.DAILY; } else if (resolution.ToUpper() == "WEEKLY") { PeriodType = aPeriodType.WEEKLY; } else if (resolution.ToUpper() == "MONTHLY") { PeriodType = aPeriodType.MONTHLY; } StartingDate = startTime; EndingDate = endTime; companyTicker = ticker; }
/// <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; }