static void TestSp500() { string[] sp500 = InvestingToolkit.GetEquityGroupAsync(EquityGroup.SP500).Result; List <string> Errors = new List <string>(); int t = 0; foreach (string s in sp500) { Console.Write("Working on " + s + "... "); t = t + 1; try { EquityStatisticalData esd = EquityStatisticalData.CreateAsync(s).Result; float pdone = (float)t / (float)sp500.Length; Console.WriteLine("success... " + pdone.ToString("#0.0%")); } catch (Exception e) { Console.WriteLine("FAILURE! " + e.Message); Errors.Add(s); } } Console.WriteLine("Done!"); Console.WriteLine("Critical errors:"); foreach (string s in Errors) { Console.WriteLine(s); } }
static void TestOne() { do { Console.WriteLine("Symbol?"); string s = Console.ReadLine(); try { EquityStatisticalData esd = EquityStatisticalData.CreateAsync(s).Result; string json = JsonConvert.SerializeObject(esd); Console.WriteLine(json); } catch { Console.WriteLine("Failure!"); } } while (true); }
public async static Task <HttpResponseMessage> GetStockStatisticalData([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req, ILogger log) { log.LogInformation("New request received"); string symbol = req.Query["symbol"]; if (symbol == null || symbol == "") { log.LogInformation("Parameter 'symbol' not present as part of request. Ending."); HttpResponseMessage hrm = new HttpResponseMessage(HttpStatusCode.BadRequest); hrm.Content = new StringContent("Fatal failure. Parameter 'symbol' not provided. You must provide a stock symbol to return data for."); return(hrm); } symbol = symbol.Trim().ToUpper(); log.LogInformation("Symbol requested: '" + symbol + "'"); //Download data EquityStatisticalData ToReturn = null; try { log.LogInformation("Attemping to download statistical data for '" + symbol + "'."); ToReturn = await EquityStatisticalData.CreateAsync(symbol.Trim().ToUpper()); } catch { log.LogInformation("Error while downloading statistical data for stock '" + symbol + "'."); HttpResponseMessage hrm = new HttpResponseMessage(HttpStatusCode.InternalServerError); hrm.Content = new StringContent("Fatal failure. Unable to download statistical data for stock with symbol '" + symbol + "'."); return(hrm); } //Return it string json = JsonConvert.SerializeObject(ToReturn); HttpResponseMessage fhrm = new HttpResponseMessage(HttpStatusCode.OK); fhrm.Content = new StringContent(json, Encoding.UTF8, "application/json"); return(fhrm); }
public async static Task <HttpResponseMessage> GetMultipleStockStatisticalData([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req, ILogger log) { StreamReader sr = new StreamReader(req.Body); string content = await sr.ReadToEndAsync(); //Check for blank if (content == null || content == "") { HttpResponseMessage hrm = new HttpResponseMessage(HttpStatusCode.BadRequest); hrm.Content = new StringContent("Your request body was blank. Please include a body of an array of strings (stock symbols), encoded in JSON format."); return(hrm); } //Deserialize the body into an array of strings string[] symbols = null; try { symbols = JsonConvert.DeserializeObject <string[]>(content); } catch { HttpResponseMessage hrm = new HttpResponseMessage(HttpStatusCode.BadRequest); hrm.Content = new StringContent("Fatal error while parsing request body to JSON string array."); return(hrm); } //Set them up HttpClient hc = new HttpClient(); List <Task <HttpResponseMessage> > Requests = new List <Task <HttpResponseMessage> >(); foreach (string s in symbols) { string url = "http://papertradesim.azurewebsites.net/api/StockStatisticalData?symbol=" + s.Trim().ToLower(); Requests.Add(hc.GetAsync(url)); } //Wait for all responses HttpResponseMessage[] responses = await Task.WhenAll(Requests); //Parse into all List <EquityStatisticalData> datas = new List <EquityStatisticalData>(); foreach (HttpResponseMessage hrm in responses) { if (hrm.StatusCode == HttpStatusCode.OK) { try { string cont = await hrm.Content.ReadAsStringAsync(); EquityStatisticalData esd = JsonConvert.DeserializeObject <EquityStatisticalData>(cont); datas.Add(esd); } catch { } } } //return as Json string as_json = JsonConvert.SerializeObject(datas.ToArray()); HttpResponseMessage rhrm = new HttpResponseMessage(HttpStatusCode.OK); rhrm.Content = new StringContent(as_json, Encoding.UTF8, "application/json"); return(rhrm); }