Esempio n. 1
0
        private static void SyncHistory()
        {
            Console.WriteLine("Getting Symbols...");
            var symbols = StockUtils.GetSymbols();

            Console.WriteLine("");
            Console.WriteLine("Found {0} symbols", symbols.Count());
            List <Historic> historics = null;

            foreach (var symbol in symbols)
            {
                IHistoryService _historyService = new HistoryService();

                //Delete stale data (more than a year old)
                Console.WriteLine("{0} -- Deleting stale data...", symbol);

                _historyService.DeleteStaleHistory(symbol);

                var dbHistories = _historyService.GetHistories(symbol);

                // If there's no history for the symbol on the new list, add a year of history
                if (dbHistories.Count() == 0)
                {
                    Console.WriteLine("{0} -- No history found. Getting a years worth...", symbol);

                    historics = GetHistory(symbol, DateTime.Now.AddYears(-1), DateTime.Now);

                    if (historics != null && historics.Count < 210)
                    {
                        Console.WriteLine("{0} -- Not enough historic records found on yahoo ({1}). [SKIPPED]...", symbol, historics.Count);

                        continue;
                    }
                }
                else
                {
                    //if (dbHistories.OrderByDescending(t => t.Date).First().Date == DateTime.Now.Date)
                    //{
                    //    continue;
                    //}

                    // Add history beginning from it's latest record
                    //var latestHistoryDate = dbHistories.OrderByDescending(t => t.Date).First().Date;

                    //Going a month back (re-calibrating)
                    var latestHistoryDate = dbHistories.OrderByDescending(t => t.Date).First().Date.AddMonths(-1);

                    Console.WriteLine("{0} -- DB History found ({1}). Getting Yahoo history starting at {2}...", symbol, dbHistories.Count(), latestHistoryDate.AddDays(1).ToString());
                    historics = GetHistory(symbol, latestHistoryDate.AddDays(1), DateTime.Now);
                }

                // If we were able to pull records from yahoo, add/update database
                if (historics != null)
                {
                    Console.WriteLine("{0} -- Yahoo History found. Adding {1} records...", symbol, historics.Count());

                    _historyService.AddOrUpdateHistorics(historics);
                }

                _historyService.Dispose();
                Console.WriteLine("~~~ Sleeping ~~~");
                Thread.Sleep(2000);
            }
        }