void WatcherChanged(object sender, FileSystemEventArgs e) { try { string contents = File.ReadAllText(e.FullPath); Regex regex = new Regex(@"(.+):(.+),(.+),([\d.]+)"); Match match = regex.Match(contents); if (match.Success == true) { string matchMarket = match.Groups[1].Value; string matchSymbol = match.Groups[2].Value; string matchDate = match.Groups[3].Value; string matchPrice = match.Groups[4].Value; DateTime date = new DateTime(1970, 1, 1, 0, 0, 0); date = date.AddSeconds(Convert.ToInt32(matchDate)); string symbol = matchSymbol; double price = Convert.ToDouble(matchPrice); // Only report this tick value once then wait until it changes if (!_lastKnownPrices.ContainsKey(symbol)) _lastKnownPrices.Add(symbol, 0.0); if (_lastKnownPrices[symbol] != price) { _lastKnownPrices[symbol] = price; Tick tick = new Tick { Price = Convert.ToDecimal(price), Date = date, Instrument = AllInstruments.GetInstrument(matchMarket, matchSymbol) }; if (ReceivedTick != null) ReceivedTick(this, tick); } } } catch (Exception) { //Console.WriteLine("Error: {0}", ex.Message); } }
private void GotTick(object sender, Tick tick) { _alertManager.NewTick(tick); Persist(); }
/// <summary> /// A new tick has come in for the provided instrument, poll all alerts /// </summary> public void NewTick(Tick tick) { DateTime date = tick.Date; Instrument instrument = tick.Instrument; decimal price = tick.Price; if (instrument == null) return; var alerts = _alerts.Where(a => a.Instrument == instrument && !a.Finished); foreach (var alert in alerts) { if (alert.Finished) continue; if (alert.HitsOrExceedsTarget(price)) { alert.Finished = true; ThrowEvent(this, new AlertEventArgs { Alert = alert, Instrument = instrument, Price = price, EventType = AlertEventType.TargetReached }); } // Check if new closest known price since the plan started else if (alert.IsCloserToTarget(price)) { alert.ClosestPrice = price; ThrowEvent(this, new AlertEventArgs { Alert = alert, Instrument = instrument, Price = price, EventType = AlertEventType.CloserToTarget }); } } }