Пример #1
0
        public void CatchUpDailyCandles(int MarketId)
        {
            using (MarketsDBEntities mdb = new MarketsDBEntities())
            {

                //load the current daily candles

                var CompiledCandles = mdb.Candles.Where(x => x.MarketId == MarketId && x.PeriodId == 5);

                //load from the data

                var RawData = mdb.DailyCandles.Where(x => x.MarketId == MarketId);

                //compare lengths, if same then return

                Console.WriteLine(CompiledCandles.Count());
                Console.WriteLine(RawData.Count());

                if (CompiledCandles.Count() < RawData.Count())
                {

                    //if not same, need to add some candles
                    HashSet<DateTime> processedDates = new HashSet<DateTime>(CompiledCandles.Select(s => s.StartDate));

                    var results = RawData.Where(m => !processedDates.Contains(m.Date));

                    foreach (var dc in results)
                    {
                        Candle c = new Candle();
                        c.Close = dc.Close;
                        c.EndDate = dc.Date;
                        c.High = dc.High;
                        c.Low = dc.Low;
                        c.MarketId = dc.MarketId;
                        c.Open = dc.Open;
                        c.PeriodId = 5;
                        c.StartDate = dc.Date;
                        c.Volume = dc.Volume;

                        using (MarketsDBEntities mdbSaver = new MarketsDBEntities())
                        {

                            mdbSaver.Candles.Add(c);
                            mdbSaver.SaveChanges();
                        }

                    }
                }

            }
        }
Пример #2
0
        public void GenerateMonthlyCandles(int marketId)
        {
            using (MarketsDBEntities mde = new MarketsDBEntities())
            {
                var candles = mde.DailyCandles.Where(x => x.MarketId == marketId)
                    .OrderBy(x => x.Date)
                    .GroupBy(x => x.Date.Month.ToString() + "-" + x.Date.Year.ToString())
                    .Select(x => new CandleListItem { MarketId = marketId,
                        StartDate = x.Min(y => y.Date),
                        High = x.Max(y => y.High),
                        Low = x.Min(y => y.High),
                        Open = 0,
                        Close = 0,
                        Volume = x.Sum(y => y.Volume),

                    });

                foreach (CandleListItem candle in candles)
                {
                    //get open and close

                    var dailyCandles = mde.DailyCandles.Where(x => x.MarketId == marketId && x.Date.Month == candle.StartDate.Month && x.Date.Year == candle.StartDate.Year).OrderBy(x => x.Date);
                    candle.Open = dailyCandles.First().Open;
                    candle.Close = dailyCandles.OrderByDescending(x => x.Date).First().Close;

                    Candle c = new Candle();
                    c.Close = candle.Close;
                    c.EndDate = dailyCandles.OrderByDescending(x => x.Date).First().Date;
                    c.High = candle.High;
                    c.Low = candle.Low;
                    c.MarketId = marketId;
                    c.Open = candle.Open;
                    c.PeriodId = 2;
                    c.StartDate = candle.StartDate;
                    c.Volume = candle.Volume;

                    if (mde.Candles.Where(x=>x.MarketId == marketId && x.StartDate == c.StartDate && x.PeriodId == c.PeriodId).Count() == 0)
                    {
                        using (MarketsDBEntities mdeSaver = new MarketsDBEntities())
                        {
                            mdeSaver.Candles.Add(c);
                            mdeSaver.SaveChanges();
                        }
                    }

                    Console.WriteLine(candle.StartDate.ToShortDateString() + " H:" + candle.High);
                }

            }
        }