public static void UpdateData() { TimeSpan start = new TimeSpan(14, 30, 0); // 9:30am TimeSpan end = new TimeSpan(23, 30, 0); // 5:00pm TimeSpan now = DateTime.UtcNow.TimeOfDay; Console.WriteLine(now.ToString()); if (!File.Exists(_TkPath)) { Console.WriteLine("Could not find token file: " + _TkPath); return; } if ((now > start) && (now < end)) { if (_Basing.Count < 1 || _Basing == null) { if (File.Exists(_CachedDataFile)) { Console.WriteLine("Pulling from cached data..."); _Basing = JsonConvert.DeserializeObject <Dictionary <string, TickerData> >(File.ReadAllText(_CachedDataFile)); } else { DownloadNewHistData(); } } foreach (var(_, value) in _Basing) { InstantData instantData = DownloadIEXInstant(value); Console.WriteLine(value + " Instant Data: " + instantData); if (instantData != null) { ProcessIEXInstant(instantData, value); CurrentData[value.Ticker] = value; Console.WriteLine(value.Ticker + " added to current data."); } } _GottenNewData = false; } else { if (!_GottenNewData) { if (File.Exists(_CachedDataFile)) { File.Delete(_CachedDataFile); } DownloadNewHistData(); _GottenNewData = true; } } }
private static InstantData DownloadIEXInstant(TickerData tickerData) { string token; try { token = File.ReadAllText(_TkPath).Replace("\n", ""); } catch (FileNotFoundException e) { Console.WriteLine("Could not find " + _TkPath); Console.WriteLine(e.Message); return(null); } InstantData jsonData = null; if (tickerData == null) { return(null); } RestClient client = new RestClient("https://cloud.iexapis.com/stable/stock/" + tickerData.Ticker + "/quote?token=" + token); RestRequest request = new RestRequest("", Method.GET); IRestResponse response = client.Execute(request); if (!response.IsSuccessful) { return(null); } try { jsonData = JsonConvert.DeserializeObject <InstantData>(response.Content); } catch (Exception e) { Console.WriteLine("Error encoding data for " + tickerData.Ticker); Console.WriteLine(e.ToString()); } return(jsonData); }
private static void ProcessIEXInstant(InstantData instantData, TickerData tickerData) { bool volSpiking = false; bool priceSpikingUp = false; bool priceSpikingDown = false; double volDelta = instantData.latestVolume - _PreviousVolume[tickerData.Ticker]; double volGainPercentage = volDelta / instantData.avgTotalVolume; if (volGainPercentage >= _VOLUMESPIKECAP) { volSpiking = true; } if (instantData.changePercent >= _VOLUMESPIKECAP) { priceSpikingUp = true; } else if (instantData.changePercent < _VOLUMESPIKECAP) { priceSpikingDown = true; } if (tickerData.Deviation > -1 && tickerData.Deviation <= 6) { tickerData.Volatility = "Steady"; } else if (tickerData.Deviation > 6 && tickerData.Deviation <= 12) { tickerData.Volatility = "Moderate"; } else if (tickerData.Deviation > 12) { tickerData.Volatility = "Volatile"; } if (volSpiking) { if (priceSpikingUp) { tickerData.Breaking = "Breaking Up!"; } else if (priceSpikingDown) { tickerData.Breaking = "Breaking Down!"; } else { tickerData.Breaking = "Volume Spike but no price change!"; } } else if (priceSpikingUp) { tickerData.Breaking = "Breaking Up but no volume change"; } else if (priceSpikingDown) { tickerData.Breaking = "Breaking Down but no volume change"; } else { tickerData.Breaking = ""; } tickerData.Price = (float)instantData.latestPrice; _PreviousVolume[tickerData.Ticker] = instantData.latestVolume; }