private void LoadHistoricalData(BootstrapForm bootstrap) { historicalDataResolutions = new Dictionary <string, bool>(); historicalResolutionSource = new Dictionary <int, string>(); if (historicalDataManager.All.Count > 0) { //historicalDataResolutions = new Dictionary<string, bool>(); //historicalResolutionSource = new Dictionary<int, string>(); string temp; bootstrap.MainLabel = "Carregando Bases Históricas..."; if (historicalDataManager.Config.AppSettings.Settings["historicalDataProvider"] == null) { historicalDataManager.Config.AppSettings.Settings.Add("historicalDataProvider", historicalDataManager.All[0].Identifier); historicalDataManager.Config.Save(ConfigurationSaveMode.Modified); } HistoricalDataProvider database = historicalDataManager.Get("DB_HISTORICAL_PROVIDER"); HistoricalDataProvider updater = historicalDataManager.Get(historicalDataManager.Config.AppSettings.Settings["historicalDataProvider"].Value); foreach (KeyValueConfigurationElement configValue in historicalDataManager.Config.AppSettings.Settings) { //bootstrap.Debug(configValue.Key + "\n", true); if (configValue.Key.Contains("HistoricalDataResolution_")) { historicalDataResolutions.Add(configValue.Key.Split('_')[1], configValue.Value == "1" ? true : false); } } List <int> resolutions = new List <int>(); foreach (KeyValuePair <string, bool> entry in historicalDataResolutions) { if (entry.Value) { int tmp = HistoricalDataResolutionStringToSeconds(entry.Key); historicalResolutionSource.Add(tmp, entry.Key); resolutions.Add(tmp); } } bootstrap.SubProgress = 0; bootstrap.SubProgressMax = stockManager.All.Count * resolutions.Count; bool updateNeeded; bool autoUpdate = false; string query; StringBuilder sb; List <string>[] results; DateTime lastWeekDay = DateTime.Now; //new DateTime(1970,1,1,1,); lastWeekDay = new DateTime(lastWeekDay.Year, lastWeekDay.Month, lastWeekDay.Day, 17, 0, 0); //pra pegar o ultimo dia útil. while (lastWeekDay.DayOfWeek == DayOfWeek.Sunday || lastWeekDay.DayOfWeek == DayOfWeek.Saturday) { lastWeekDay = lastWeekDay.Subtract(TimeSpan.FromDays(1)); } if (historicalDataManager.Config.AppSettings.Settings["historicalDataAutoUpdate"] == null) { historicalDataManager.Config.AppSettings.Settings.Add("historicalDataAutoUpdate", "0"); historicalDataManager.Config.Save(ConfigurationSaveMode.Modified); } if (historicalDataManager.Config.AppSettings.Settings["historicalDataAutoUpdate"].Value == "1") { autoUpdate = true; } foreach (Stock stock in stockManager.All) { foreach (int resolution in resolutions) { //checar se os dados históricos estão atualizados. query = "SELECT timestamp FROM period WHERE stock_id = "; query += stock.Id + " AND periodLength = " + resolution; query += " ORDER BY timestamp DESC LIMIT 1"; updateNeeded = false; results = DB.Select(query); if (results[0].Count == 0) //se não tiver nada no bd { updateNeeded = true; } else { DateTime lastUpdateDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0); lastUpdateDateTime = lastUpdateDateTime.AddSeconds(long.Parse(results[0][0])); if (lastUpdateDateTime.AddDays(1).DayOfWeek == DayOfWeek.Saturday) { //ultimo foi sexta //Clipboard.SetText(lastWeekDay.AddDays(2).Day.ToString()); if ((lastWeekDay.AddDays(1) - lastUpdateDateTime).TotalDays >= 1) { updateNeeded = true; } } else if ((lastWeekDay - lastUpdateDateTime).TotalHours >= 1) //se o ultimo periodo do db for de um dia anterior a ontem, atualizar. { updateNeeded = true; } } historicalResolutionSource.TryGetValue(resolution, out temp); if (updateNeeded && autoUpdate) //precisa atualizar { bootstrap.SubLabel = "Baixando dados históricos para " + stock.Ticker + ":" + temp + " de " + updater.Name + "..."; stock.HistoricalDataManager.SetList(resolution, updater.GetAllFrom("BVMF", stock.Ticker, resolution) ); if (stock.HistoricalDataManager.GetCandleList(resolution).Count > 0) { bootstrap.SubLabel = "Atualizando dados históricos de " + stock.Ticker + ":" + temp + "..."; sb = new StringBuilder(); sb.Append("INSERT INTO period VALUES "); foreach (Candle candle in stock.HistoricalData(resolution)) { sb.Append( "(" + stock.Id + ";" + candle.Timestamp + ";" + resolution + ";" + candle.Open + ";" + candle.Close + ";" + candle.High + ";" + candle.Low + ";" + candle.Volume + ");" ); } query = sb.ToString(); query = query.Replace(",", "."); query = query.Replace(";", ","); query += "@@@"; query = query.Replace(",@@@", " ON DUPLICATE KEY UPDATE stock_id=stock_id"); DB.Query(query); } } bootstrap.SubLabel = "Carregando dados históricos para " + stock.Ticker + ":" + temp + " de " + database.Name + "..."; stock.HistoricalDataManager.SetList( resolution, database.GetAllFrom(stock.Id, resolution) ); bootstrap.SubProgress++; } } } }