static void Main(string[] args) { string[] stocks = InvestingToolkit.GetEquityGroupAsync(EquityGroup.SP500).Result; foreach (string s in stocks) { HistoricalDataProvider hdp = new HistoricalDataProvider(); Console.Write("Working on " + s + "... "); hdp.DownloadHistoricalDataAsync(s, new DateTime(2010, 1, 1), new DateTime(2011, 1, 1)).Wait(); if (hdp.DownloadResult == HistoricalDataDownloadResult.Successful) { Console.WriteLine(hdp.HistoricalData.Length.ToString()); } else if (hdp.DownloadResult == HistoricalDataDownloadResult.DataDoesNotExistForSpecifiedTimePeriod) { Console.WriteLine("Data did not exist at the time!"); } else if (hdp.DownloadResult == HistoricalDataDownloadResult.Unauthorized) { Console.WriteLine("Unauthorized!!!!!"); } else if (hdp.DownloadResult == HistoricalDataDownloadResult.NoDataFound) { Console.WriteLine("Stock symbol not found."); } else { Console.WriteLine("Idk... " + hdp.DownloadResult.ToString()); } } }
private async Task <float> StockPriceClosestToDateAsync(string symbol, DateTime target) { HistoricalDataProvider hdp = new HistoricalDataProvider(); await hdp.DownloadHistoricalDataAsync(symbol, target.AddDays(-5), target.AddDays(5), 20); //Throw an error if not successful if (hdp.DownloadResult != HistoricalDataDownloadResult.Successful) { throw new Exception("Unable to download historical data for '" + symbol.ToUpper().Trim() + "'. Status: " + hdp.DownloadResult.ToString()); } //Find the closest int closest_measure = int.MaxValue; HistoricalDataRecord winner = hdp.HistoricalData[0]; foreach (HistoricalDataRecord hdr in hdp.HistoricalData) { TimeSpan ts = hdr.Date - target; if (Math.Abs(ts.Days) < closest_measure) { closest_measure = Math.Abs(ts.Days); winner = hdr; } } return(winner.AdjustedClose); }
public static void Main(string[] args) { // header Console.WriteLine("***********************"); Console.WriteLine("* Test Load Data *"); Console.WriteLine("***********************"); var HistoricalData = new HistoricalDataProvider(); DateTime from = new DateTime(2010, 01, 01); DateTime maturity = new DateTime(2010, 04, 12); Share share1 = new Share("ACCOR SA", "AC FP"); Share share2 = new Share("CREDIT AGRICOLE SA", "ACA FP"); Share[] underlyingShares = new Share[] { share1, share2 }; double[] weights = new double[] { 0.3, 0.7 }; double strike = 7000; IOption option = new BasketOption("panier", underlyingShares, weights, maturity, strike); List <DataFeed> lst = HistoricalData.GetDataFeeds(option, from); foreach (DataFeed element in lst) { Console.WriteLine("\n\n" + element.Date.ToString() + "\n" + string.Join(",", element.PriceList.Select(kv => kv.Key + "=" + kv.Value).ToArray())); } Console.WriteLine("**************End of Loading****************"); String[] id = lst[0].PriceList.Keys.ToArray(); Console.WriteLine("les ids : " + id[0] + " et " + id[1]); Console.ReadKey(true); }
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++; } } } }