Beispiel #1
0
        private void CheckBarSubscribers(Tick aTick, Level1Subscribers subscribers)
        {
            List <AlertSubscription> alertSubscriptions;

            lock (subscribers)
            {
                alertSubscriptions = new List <AlertSubscription>(subscribers.AlertSubscribers);
            }

            for (int i = 0; i < alertSubscriptions.Count; i++)
            {
                bool fire = alertSubscriptions[i].AppendTick(aTick);
                if (fire)
                {
                    AlertSubscribeResponse response = new AlertSubscribeResponse
                    {
                        Id        = alertSubscriptions[i].Id,
                        AlertName = alertSubscriptions[i].AlertName,
                        Alert     = new Alert
                        {
                            Name      = alertSubscriptions[i].Name,
                            Symbol    = alertSubscriptions[i].Symbol,
                            Timestamp = aTick.Date,
                        },
                        User = DataServer.MessageRouter.gMessageRouter.GetUserInfoByLogin(alertSubscriptions[i].Login)
                    };
                    alertSubscriptions[i].AddFiredAlertToHistory(response.Alert);
                    if (response.User != null)
                    {
                        PushResponses(response.User.ID, new ResponseMessage[] { response });
                    }
                    else
                    {
                        //send remote notification
                        DataServer.MessageRouter.gMessageRouter.PushRemoteNotification(alertSubscriptions[i].Login, alertSubscriptions[i].Id, alertSubscriptions[i].AlertName, response.Alert);
                    }
                }
            }
        }
Beispiel #2
0
        private void SubscribeRequest(SubscribeRequest aRequest)
        {
            Level1Subscribers aLevel1Subscribers;
            Tick aTick;

            Validate(aRequest);
            lock (m_Level1SubscribersBySymbols)
            {
                if (!m_Level1SubscribersBySymbols.TryGetValue(aRequest.Symbol, out aLevel1Subscribers))
                {
                    aLevel1Subscribers = new Level1Subscribers();

                    aLevel1Subscribers.Subscribers      = new List <string>();
                    aLevel1Subscribers.AlertSubscribers = new List <AlertSubscription>();
                    m_Level1SubscribersBySymbols.Add(aRequest.Symbol, aLevel1Subscribers);
                }
            }
            lock (aLevel1Subscribers)
            {
                if (!aLevel1Subscribers.Subscribers.Contains(aRequest.User.ID))
                {
                    aLevel1Subscribers.Subscribers.Add(aRequest.User.ID);
                }
                aTick = aLevel1Subscribers.Tick;
            }
            if (aTick != null)
            {
                PushTick(aRequest.User.ID, aTick);
            }

            IDataFeed aDataFeed = GetDataFeedByName(aRequest.Symbol.DataFeed);

            if (aDataFeed != null)
            {
                aDataFeed.Subscribe(aRequest.Symbol);
            }
        }
Beispiel #3
0
        private void SubscribeAlert(AlertSubscribeRequest aRequest)
        {
            var errorString = TradeScript.Validate(aRequest.Script);

            if (aRequest.Script == string.Empty)
            {
                errorString = "Script can not be empty";
            }

            if (errorString != string.Empty)
            {
                var response = new AlertSubscribeResponse
                {
                    Id        = aRequest.Id,
                    Error     = errorString,
                    AlertName = aRequest.AlertName,
                    Alert     = new Alert
                    {
                        Name   = aRequest.Name,
                        Symbol = aRequest.Symbol
                    },
                    User = aRequest.User
                };
                PushResponses(aRequest.User.ID, new ResponseMessage[] { response });
                return;
            }

            var       selection = new HistoryParameters(aRequest.Id, aRequest.Symbol, aRequest.Periodicity, aRequest.Interval, aRequest.BarsCount);
            IDataFeed aDataFeed = GetDataFeedByName(aRequest.Symbol.DataFeed);

            if (aDataFeed != null)
            {
                GetHistoryCtx aCtx = GetHistoryCtx(selection, aDataFeed.TimeZoneInfo);
                aCtx.Request  = new HistoryRequest(selection);
                aCtx.DataFeed = aDataFeed;
                ThreadPool.QueueUserWorkItem(o =>
                {
                    aDataFeed.GetHistory(aCtx, (ctx, bars) =>
                    {
                        Level1Subscribers subscribers = null;
                        var symbolItem = new SymbolItem()
                        {
                            DataFeed = aRequest.Symbol.DataFeed,
                            Exchange = aRequest.Symbol.Exchange,
                            Symbol   = aRequest.Symbol.Symbol,
                            Type     = aRequest.Symbol.Type
                        };
                        lock (m_Level1SubscribersBySymbols)
                        {
                            if (!m_Level1SubscribersBySymbols.TryGetValue(aRequest.Symbol, out subscribers))
                            {
                                subscribers                  = new Level1Subscribers();
                                subscribers.Subscribers      = new List <string>();
                                subscribers.AlertSubscribers = new List <AlertSubscription>();
                                m_Level1SubscribersBySymbols.Add(aRequest.Symbol, subscribers);
                            }
                        }
                        AlertSubscription alert = new AlertSubscription()
                        {
                            Id              = aRequest.Id,
                            AlertName       = aRequest.AlertName,
                            Symbol          = symbolItem,
                            Name            = aRequest.Name,
                            Periodicity     = aRequest.Periodicity,
                            Interval        = aRequest.Interval,
                            Script          = aRequest.Script,
                            UserSessionId   = aRequest.User.ID,
                            Login           = aRequest.User.Login,
                            CalculationType = aRequest.CalculationType
                        };
                        alert.InitAlert(bars);
                        lock (subscribers.AlertSubscribers)
                        {
                            subscribers.AlertSubscribers.Add(alert);
                        }
                        aDataFeed.Subscribe(aRequest.Symbol);
                    });
                });
            }
        }