Exemple #1
0
        private static void SyncHistory()
        {
            var             symbols           = GetSymbols();
            var             historyDictionary = new Dictionary <string, List <Historic> >();
            var             oneYearAgo        = DateTime.Now.AddYears(-1);
            List <Historic> historics         = null;
            var             flag = false;

            foreach (var symbol in symbols)
            {
                if (symbol == "OVBC" || flag)
                {
                    flag = true;
                    using (var db = new PeabodyDataContext())
                    {
                        //Delete stale data (more than a year old)
                        var dbStaleHistories = db.Histories.Where(h => h.Symbol == symbol && h.Date < oneYearAgo);
                        db.Histories.RemoveRange(dbStaleHistories);
                        db.SaveChanges();

                        var dbHistories = db.Histories.Where(h => h.Symbol == symbol);

                        // If there's no history for the symbol on the new list, add a year of history
                        if (dbHistories.Count() == 0)
                        {
                            historics = GetHistory(symbol, DateTime.Now.AddYears(-1), DateTime.Now);
                        }
                        else
                        {
                            //if (dbHistories.OrderByDescending(t => t.Date).First().Date == DateTime.Now.Date)
                            //{
                            //    continue;
                            //}

                            // Add history beginning from it's earliest record
                            historics = GetHistory(symbol, dbHistories.OrderByDescending(t => t.Date).First().Date.AddDays(1), DateTime.Now);
                        }

                        // If we were able to pull records from yahoo, add or update database
                        if (historics != null)
                        {
                            var histories = AutoMapper.Mapper.Map <IEnumerable <Historic>, IEnumerable <History> >(historics);

                            db.Histories.AddOrUpdate(histories.ToArray());

                            try
                            {
                                db.SaveChanges();

                                System.Console.WriteLine("{0}", symbol);
                            }
                            catch (DbEntityValidationException e)
                            {
                                foreach (var eve in e.EntityValidationErrors)
                                {
                                    foreach (var ve in eve.ValidationErrors)
                                    {
                                        System.Console.WriteLine("Main --> {0}", ve.ErrorMessage);
                                    }
                                }
                                throw;
                            }
                        }
                    }

                    Thread.Sleep(2000);
                }
            }
        }
Exemple #2
0
        private static void FillStockInfoRealTime(XmlReader xr, IEnumerable <string> symbols)
        {
            try
            {
                XDocument master = XDocument.Load(xr);

                using (var db = new PeabodyDataContext())
                {
                    foreach (var symbol in symbols)
                    {
                        IEnumerable <XElement> elements =
                            from c in master.Root.Elements("results").Descendants("quote")
                            where c.Attribute("symbol").Value.ToLower() == symbol.ToLower()
                            select c;

                        elements = elements.Elements();

                        decimal lastTradePriceOnly;

                        var element = elements.FirstOrDefault(x => x.Name == "LastTradeRealtimeWithTime");

                        if (element != null && !string.IsNullOrEmpty(element.Value))
                        {
                            lastTradePriceOnly = ValueUtils.GetValidDecimal(element);
                        }
                        else
                        {
                            lastTradePriceOnly = ValueUtils.GetValidDecimal(elements.FirstOrDefault(x => x.Name == "LastTradePriceOnly"));
                        }

                        var daysHigh      = ValueUtils.GetValidDecimal(elements.FirstOrDefault(x => x.Name == "DaysHigh"));
                        var daysLow       = ValueUtils.GetValidDecimal(elements.FirstOrDefault(x => x.Name == "DaysLow"));
                        var volume        = ValueUtils.GetValidInteger(elements.FirstOrDefault(x => x.Name == "Volume"));
                        var open          = ValueUtils.GetValidDecimal(elements.FirstOrDefault(x => x.Name == "Open"));
                        var previousClose = ValueUtils.GetValidDecimal(elements.FirstOrDefault(x => x.Name == "PreviousClose"));

                        var dateStamp = DateTime.Now.Date;

                        // In case this was left running
                        if (dateStamp.DayOfWeek == DayOfWeek.Saturday)
                        {
                            dateStamp = dateStamp.AddDays(-1);
                        }
                        else if (dateStamp.DayOfWeek == DayOfWeek.Sunday)
                        {
                            dateStamp = dateStamp.AddDays(-2);
                        }

                        db.Histories.AddOrUpdate(h => new { h.Symbol, h.Date }, new StockBuddy.Core.Domain.History {
                            AdustedClosePrice = lastTradePriceOnly, Date = dateStamp, ClosePrice = lastTradePriceOnly, HighPrice = daysHigh, LowPrice = daysLow, OpenPrice = open, PreviousClosePrice = previousClose, Symbol = symbol, Volume = volume
                        });
                    }

                    db.SaveChanges();
                }
            }
            catch
            { }

            finally
            {
                HttpUtils.CloseConnections(xr, true);
            }
        }