Exemple #1
0
        public void UpdateSubscription(string userId)
        {
            SymbolSubscriptions.RemoveAll(x => x.UserId == userId);

            var subscriptions = _subscriptionManager.GetSubscriptionsByUser(userId);

            foreach (var subscription in subscriptions)
            {
                SymbolSubscriptions.Add(subscription);
            }
        }
Exemple #2
0
        public void ResetCache(string userId, string symbolId = null)
        {
            var subscriptions = SymbolSubscriptions.Where(x => x.UserId == userId);

            if (!string.IsNullOrEmpty(symbolId))
            {
                subscriptions = subscriptions.Where(x => x.SymbolId == symbolId).ToList();
            }

            foreach (var subscription in subscriptions)
            {
                subscription.LastNotifiedPrice = decimal.Zero;
                subscription.LastNotifiedDate  = DateTime.Now;
            }
        }
Exemple #3
0
        private void NotifyPriceAsync(SymbolModel symbol, decimal currentPrice)
        {
            var symbolSubscriptions = SymbolSubscriptions.Where(x => x.SymbolId == symbol.Id);

            foreach (var symbolSubscription in symbolSubscriptions)
            {
                if (symbolSubscription.LastNotifiedPrice == decimal.Zero)
                {
                    symbolSubscription.LastNotifiedPrice = currentPrice;
                    symbolSubscription.LastNotifiedDate  = DateTime.Now;
                }
                else
                {
                    decimal userPrice  = symbolSubscription.LastNotifiedPrice;
                    decimal userChange = (currentPrice - userPrice) / userPrice;

                    if (Math.Abs(userChange) >= symbolSubscription.MinimumChange)
                    {
                        var user = Users.FirstOrDefault(x => x.Id == symbolSubscription.UserId);

                        string message = string.Format("{0}: {1} [{2}%{3}]",
                                                       symbol.FriendlyName,
                                                       currentPrice,
                                                       (userChange * 100).ToString("+0.00;-0.00;0"),
                                                       GetTimeSpanString(DateTime.Now - symbolSubscription.LastNotifiedDate));

                        _userManager.SendMessage(user.Id, message);

                        // Console.WriteLine("Notified {0}: {1}", user.Id, message);

                        symbolSubscription.LastNotifiedPrice = currentPrice;
                        symbolSubscription.LastNotifiedDate  = DateTime.Now;

                        _subscriptionManager.UpdateAsNotified(user.Id, symbol.Id, currentPrice, DateTime.Now);
                    }
                }
            }
        }