Пример #1
0
        private void Work(object sender, DoWorkEventArgs args)
        {
            var    newsRobotFileName = ExecutablePath.ExecPath + "\\plugin\\NewsRobot.dll.xml";
            string error;
            var    settings = NewsRobot.CurrencySettings.LoadCurrencySettings(newsRobotFileName, out error);

            if (error != null)
            {
                AddPendingMessage(error);
                return;
            }
            var calc = new CurrencyIndexCalculator();
            var selectedCurrencies = new List <string>();

            foreach (var selectedCurrency in workingCurrencies)
            {
                var c = selectedCurrency.Split(new[] { ' ' })[1];
                if (!selectedCurrencies.Contains(c))
                {
                    selectedCurrencies.Add(c);
                }
            }

            //if (fileQuotes.Count == 0)
            if (quotesFileNames.Count != 0)
            {
                for (var i = 0; i < quotesFileNames.Count; i++)
                {
                    var fileName = quotesFileNames[i];
                    if (string.IsNullOrEmpty(fileName))
                    {
                        continue;
                    }
                    var info = new FileInfo(fileName);
                    if (!info.Exists)
                    {
                        AddPendingMessage(string.Format("Файл не найден: {0}", fileName));
                        continue;
                    }
                    var smb     = info.Name.Replace(info.Extension, null);
                    var candles = CandleData.LoadFromFile(fileName, smb);
                    fileQuotes[smb] = candles;
                    AddPendingMessage(string.Format("Прочитано котировок {0}: {1} из {2}", smb, candles.Count, fileName));
                    bw.ReportProgress((i + 1) * 100 / quotesFileNames.Count);
                }
            }
            else
            {
                // составляем валютные пары по выбранным валютам
                var tickersToUpdate = new List <string>();

                /*var allTickers = DalSpot.Instance.GetTickerNames();
                 * foreach (var с in selectedCurrencies)
                 * {
                 *  var currency = с;
                 *  tickersToUpdate.AddRange(allTickers.Where(t => t.StartsWith(currency)));
                 *  tickersToUpdate.AddRange(allTickers.Where(t => t.EndsWith(currency)));
                 * }*/
                foreach (var c in selectedCurrencies)
                {
                    tickersToUpdate.AddRange(calc.GetIndexDependencies(c, out error));
                    AddPendingMessage(error);
                }
                // обновляем данные по валютным парам
                var tickersAndDates = new Dictionary <string, DateTime>();
                foreach (var ticker in tickersToUpdate)
                {
                    if (!tickersAndDates.ContainsKey(ticker))
                    {
                        tickersAndDates.Add(ticker, startTime);
                    }
                }
                AddPendingMessage("Получение котировок");
                MainForm.Instance.UpdateTickersCacheForRobots(tickersAndDates, 60);
            }

            // читаем новости в кэш
            if (robotNews.Count == 0)
            {
                AddPendingMessage("Получение новостей");
                for (var day = startTime; day <= endTime; day += new TimeSpan(1, 0, 0, 0))
                {
                    List <string> errors;
                    var           dailyNews = NewsRobot.NewsRobot.GrabNews(day, out errors);
                    robotNews.AddRange(dailyNews);
                    AddPendingMessages(errors);
                    bw.ReportProgress((int)((day - startTime).TotalDays + 1) * 100 /
                                      (int)((endTime - startTime).TotalDays + 1));
                }
            }

            // обрабатываем новости
            AddPendingMessage("Обработка новостей");
            for (var day = startTime; day <= endTime; day += new TimeSpan(1, 0, 0, 0))
            {
                var newsDay   = day;
                var dailyNews = robotNews.Where(news => news.Time.Date == newsDay.Date);
                foreach (var dn in dailyNews)
                {
                    var news = dn;
                    if (news.Valuable != onlyValuableNews)
                    {
                        continue;
                    }
                    var countrySettings = settings.Where(s => s.CountryCode == news.CountryCode).ToList();
                    if (countrySettings.Count() == 0)
                    {
                        //AddPendingMessage("CurrencyIndexCalculator:ActivateScript: no associated currency with country " + news.CountryCode);
                        continue;
                    }
                    var currencyCode = countrySettings[0].CurrencyCode;
                    if (!selectedCurrencies.Contains(currencyCode))
                    {
                        continue;
                    }
                    List <string> errors;
                    var           delta = calc.GetIndexDelta(currencyCode, news.Time, 15, fileQuotes, out errors);
                    //AddPendingMessages(errors);
                    if (delta.HasValue)
                    {
                        var newsValueSign = 0;
                        if (news.Value > news.ProjectedValue)
                        {
                            newsValueSign = 1;
                        }
                        else if (news.Value < news.ProjectedValue)
                        {
                            newsValueSign = -1;
                        }
                        if (newsValueSign != 0 && delta.Value != 0)
                        {
                            AddStat(news.CountryCode, news.Title, delta.Value, (delta.Value > 0) == (news.Value > news.ProjectedValue), news.Time);
                        }
                    }
                }
                if (bw.CancellationPending)
                {
                    args.Cancel = true;
                    break;
                }
                bw.ReportProgress((int)((day - startTime).TotalDays + 1) * 100 / (int)((endTime - startTime).TotalDays + 1));
            }
        }
Пример #2
0
        private void Work(object sender, DoWorkEventArgs args)
        {
            var newsRobotFileName = ExecutablePath.ExecPath + "\\plugin\\NewsRobot.dll.xml";
            string error;
            var settings = NewsRobot.CurrencySettings.LoadCurrencySettings(newsRobotFileName, out error);
            if (error != null)
            {
                AddPendingMessage(error);
                return;
            }
            var calc = new CurrencyIndexCalculator();
            var selectedCurrencies = new List<string>();
            foreach (var selectedCurrency in workingCurrencies)
            {
                var c = selectedCurrency.Split(new[] { ' ' })[1];
                if (!selectedCurrencies.Contains(c))
                    selectedCurrencies.Add(c);
            }

            //if (fileQuotes.Count == 0)
            if(quotesFileNames.Count != 0)
            {
                for (var i = 0; i < quotesFileNames.Count; i++)
                {
                    var fileName = quotesFileNames[i];
                    if (string.IsNullOrEmpty(fileName))
                        continue;
                    var info = new FileInfo(fileName);
                    if (!info.Exists)
                    {
                        AddPendingMessage(string.Format("Файл не найден: {0}", fileName));
                        continue;
                    }
                    var smb = info.Name.Replace(info.Extension, null);
                    var candles = CandleData.LoadFromFile(fileName, smb);
                    fileQuotes[smb] = candles;
                    AddPendingMessage(string.Format("Прочитано котировок {0}: {1} из {2}", smb, candles.Count, fileName));
                    bw.ReportProgress((i + 1) * 100 / quotesFileNames.Count);
                }
            }
            else
            {
                // составляем валютные пары по выбранным валютам
                var tickersToUpdate = new List<string>();
                /*var allTickers = DalSpot.Instance.GetTickerNames();
                foreach (var с in selectedCurrencies)
                {
                    var currency = с;
                    tickersToUpdate.AddRange(allTickers.Where(t => t.StartsWith(currency)));
                    tickersToUpdate.AddRange(allTickers.Where(t => t.EndsWith(currency)));
                }*/
                foreach (var c in selectedCurrencies)
                {
                    tickersToUpdate.AddRange(calc.GetIndexDependencies(c, out error));
                    AddPendingMessage(error);
                }
                // обновляем данные по валютным парам
                var tickersAndDates = new Dictionary<string, DateTime>();
                foreach (var ticker in tickersToUpdate)
                {
                    if (!tickersAndDates.ContainsKey(ticker))
                        tickersAndDates.Add(ticker, startTime);
                }
                AddPendingMessage("Получение котировок");
                MainForm.Instance.UpdateTickersCacheForRobots(tickersAndDates, 60);
            }

            // читаем новости в кэш
            if(robotNews.Count == 0)
            {
                AddPendingMessage("Получение новостей");
                for (var day = startTime; day <= endTime; day += new TimeSpan(1, 0, 0, 0))
                {
                    List<string> errors;
                    var dailyNews = NewsRobot.NewsRobot.GrabNews(day, out errors);
                    robotNews.AddRange(dailyNews);
                    AddPendingMessages(errors);
                    bw.ReportProgress((int) ((day - startTime).TotalDays + 1) * 100 /
                                      (int) ((endTime - startTime).TotalDays + 1));
                }
            }

            // обрабатываем новости
            AddPendingMessage("Обработка новостей");
            for (var day = startTime; day <= endTime; day += new TimeSpan(1, 0, 0, 0))
            {
                var newsDay = day;
                var dailyNews = robotNews.Where(news => news.Time.Date == newsDay.Date);
                foreach (var dn in dailyNews)
                {
                    var news = dn;
                    if (news.Valuable != onlyValuableNews)
                        continue;
                    var countrySettings = settings.Where(s => s.CountryCode == news.CountryCode).ToList();
                    if (countrySettings.Count() == 0)
                    {
                        //AddPendingMessage("CurrencyIndexCalculator:ActivateScript: no associated currency with country " + news.CountryCode);
                        continue;
                    }
                    var currencyCode = countrySettings[0].CurrencyCode;
                    if (!selectedCurrencies.Contains(currencyCode))
                        continue;
                    List<string> errors;
                    var delta = calc.GetIndexDelta(currencyCode, news.Time, 15, fileQuotes, out errors);
                    //AddPendingMessages(errors);
                    if (delta.HasValue)
                    {
                        var newsValueSign = 0;
                        if (news.Value > news.ProjectedValue)
                            newsValueSign = 1;
                        else if (news.Value < news.ProjectedValue)
                            newsValueSign = -1;
                        if (newsValueSign != 0 && delta.Value != 0)
                            AddStat(news.CountryCode, news.Title, delta.Value, (delta.Value > 0) == (news.Value > news.ProjectedValue), news.Time);
                    }
                }
                if (bw.CancellationPending)
                {
                    args.Cancel = true;
                    break;
                }
                bw.ReportProgress((int)((day - startTime).TotalDays + 1) * 100 / (int)((endTime - startTime).TotalDays + 1));
            }
        }