/// <summary> /// Send a single feed to redis /// </summary> /// <param name="feed"></param> /// <param name="exchange"></param> /// <returns></returns> public bool SendFeed(Feed feed, string exchange) { ISubscriber sub = connection.GetSubscriber(); string text = Convert.ToBase64String(ObjectSerialization.SerializeToStream(feed).ToArray()); sub.PublishAsync(exchange, text); return true; }
/// <summary> /// Method for notifying all subscribers that feed has arrived. /// </summary> /// <param name="symbolId">Id of the symbol for which feed has arrived</param> /// <param name="fd">Feed</param> private void Notify(int symbolId, Feed fd) { lock (_lockSubscription) { if (notifyList.ContainsKey(symbolId)) { foreach (OnFeedReceived hndl in notifyList[symbolId]) { try { //send a copy hndl((Feed)fd.Clone()); } catch { //ignore... } } } } }
/// <summary> /// Method for generating fake data /// </summary> private void UpdateData() { // Method to change the values of all the stocks randomly in a fixed range List<StockModel.Symbol> symbols = InMemoryObjects.ExchangeSymbolList.SingleOrDefault(x => x.Exchange == Exchange.FAKE_NASDAQ).Symbols; Feed[] feedsArray = new Feed[symbols.Count]; List<SymbolFeeds> symbolFeeds = new List<SymbolFeeds>(); while (true) { Thread.Sleep(updateDurationTime); Parallel.ForEach(symbols, (symbol) => { SymbolFeeds feeds = new SymbolFeeds(); feeds.SymbolId = symbol.Id; double changePercent = random.NextDouble() * (Constants.MAX_CHANGE_PERC - Constants.MIN_CHANGE_PERC) + Constants.MIN_CHANGE_PERC; symbol.DefaultVal = symbol.DefaultVal + symbol.DefaultVal * changePercent / 100; Feed feed = new Feed(); feed.SymbolId = symbol.Id; feed.LTP = symbol.DefaultVal; feed.TimeStamp = Convert.ToInt64((DateTime.Now - epoch).TotalMilliseconds); if (FeedArrived != null) FeedArrived((Feed)feed.Clone()); //notify subscribers - later to be changed to only notify if there is any new data Notify(symbol.Id, feed); }); } }
/// <summary> /// Method for requesting latest data from the yahoo server against a given exchange/symbol /// </summary> /// <param name="symbol">Symbol for which data is to be fetched</param> /// <param name="symbolId">Unique symbol id for the symbol</param> /// <param name="exchange">Exchange used</param> /// <returns></returns> private Feed GetFeed(string symbol, int symbolId, string exchange) { string url = string.Format("{0}?q={1}&format={2}&env={3}", baseYFinanceURL, HttpUtility.UrlPathEncode(string.Format(yql, symbol, exchange)), HttpUtility.UrlPathEncode(format), Uri.EscapeDataString(HttpUtility.UrlPathEncode(env)) ); WebRequest request = WebRequest.Create(url); WebResponse response = request.GetResponse(); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); RootObject rootData = JsonConvert.DeserializeObject<RootObject>(responseFromServer); Feed fd = new Feed(); fd.SymbolId = symbolId; fd.Id = symbolId; fd.High = Convert.ToDouble(string.IsNullOrEmpty(rootData.query.results.quote.DaysHigh) ? "0" : rootData.query.results.quote.DaysHigh); fd.Low = Convert.ToDouble(string.IsNullOrEmpty(rootData.query.results.quote.DaysLow) ? "0" : rootData.query.results.quote.DaysHigh); fd.LTP = Convert.ToDouble(string.IsNullOrEmpty(rootData.query.results.quote.LastTradePriceOnly) ? "0" : rootData.query.results.quote.DaysHigh); fd.Open = Convert.ToDouble(string.IsNullOrEmpty(rootData.query.results.quote.Open) ? "0" : rootData.query.results.quote.DaysHigh); fd.TimeStamp = Convert.ToInt64((DateTime.Now - epoch).TotalMilliseconds); return fd; }