示例#1
0
文件: Alert.cs 项目: dreamsql/Outter
        internal bool Hit(Quotation quotaion)
        {
            bool isHit = false;

            if (this.Condition == AlertCondition.LessThanBid)
            {
                isHit = this.Price > quotaion.Bid;
                if (isHit)
                {
                    this.HitPrice = quotaion.Bid;
                }
            }
            else if (this.Condition == AlertCondition.GreaterThanBid)
            {
                isHit = this.Price < quotaion.Bid;
                if (isHit)
                {
                    this.HitPrice = quotaion.Bid;
                }
            }
            else if (this.Condition == AlertCondition.LessThanAsk)
            {
                isHit = this.Price > quotaion.Ask;
                if (isHit)
                {
                    this.HitPrice = quotaion.Ask;
                }
            }
            else if (this.Condition == AlertCondition.GreaterThanAsk)
            {
                isHit = this.Price < quotaion.Ask;
                if (isHit)
                {
                    this.HitPrice = quotaion.Ask;
                }
            }

            if (isHit)
            {
                this.State             = AlertState.Hit;
                this.HitPriceTimestamp = quotaion.Timestamp;
            }

            return(isHit);
        }
示例#2
0
        private List <Guid> UpdateQuotation(List <AlertOverridedQuotation> overridedQs)
        {
            List <Guid> quotationChangedInstruments = new List <Guid>();

            foreach (AlertOverridedQuotation quotaion in overridedQs)
            {
                if (!quotationChangedInstruments.Contains(quotaion.Instrument.Id))
                {
                    quotationChangedInstruments.Add(quotaion.Instrument.Id);
                }

                Dictionary <Guid, Quotation> quotations = null;
                if (!this.instrument2Quotations.TryGetValue(quotaion.Instrument.Id, out quotations))
                {
                    quotations = new Dictionary <Guid, Quotation>();
                    this.instrument2Quotations.Add(quotaion.Instrument.Id, quotations);
                }
                quotations[quotaion.QuotePolicyID] = Quotation.From(quotaion);
            }
            return(quotationChangedInstruments);
        }
示例#3
0
        private void HitAlerts(object state)
        {
            while (true)
            {
                this.marketQuotationsEvent.WaitOne();

                try
                {
                    while (true)
                    {
                        List <Alert> hitedAlerts   = new List <Alert>();
                        List <Alert> expiredAlerts = new List <Alert>();

                        lock (this.lockObj)
                        {
                            if (this.marketQuotations.Count == 0)
                            {
                                break;
                            }

                            DateTime now = DateTime.Now;
                            List <AlertOverridedQuotation> overridedQs = this.marketQuotations.Dequeue();
                            List <Guid> quotationChangedInstruments    = this.UpdateQuotation(overridedQs);
                            foreach (Guid instrumentId in quotationChangedInstruments)
                            {
                                ICollection <Alert> alerts = null;
                                if (this.instrument2Alerts.TryGetValue(instrumentId, out alerts) && alerts.Count > 0)
                                {
                                    foreach (Alert alert in alerts)
                                    {
                                        if (alert.QuotePolicyId == null)
                                        {
                                            continue;
                                        }
                                        Quotation quotation = this.GetQuotation(alert.InstrumentId, alert.QuotePolicyId.Value);
                                        if (quotation == null)
                                        {
                                            continue;
                                        }
                                        if (alert.State == AlertState.Pending)
                                        {
                                            if (alert.Expire(now))
                                            {
                                                Logger.InfoFormat("alert expire {0}", alert);
                                                expiredAlerts.Add(alert);
                                            }
                                            else if (alert.Hit(quotation))
                                            {
                                                Logger.InfoFormat("alert hit {0}", alert);
                                                hitedAlerts.Add(alert);
                                            }
                                        }
                                    }
                                }
                            }

                            foreach (Alert alert in hitedAlerts)
                            {
                                Logger.Info(string.Format("PriceAlertManager.HitAlerts remove hit alert {0}", alert));
                                this.Remove(alert.Id);
                            }

                            foreach (Alert alert in expiredAlerts)
                            {
                                Logger.Info(string.Format("PriceAlertManager.HitAlerts remove expired alert {0}", alert));
                                this.Remove(alert.Id);
                            }
                        }

                        if (hitedAlerts.Count > 0)
                        {
                            this.SaveAlerts(hitedAlerts);
                            this.FireAlertHit(hitedAlerts);
                        }

                        if (expiredAlerts.Count > 0)
                        {
                            this.SaveAlerts(expiredAlerts);
                            this.FireAlertExpried(expiredAlerts);
                        }

                        if (this.marketQuotations.Count > 0)
                        {
                            Thread.Sleep(10);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                catch (Exception exception)
                {
                    Logger.Error("PriceAlertManager.HitAlerts error", exception);
                }
            }
        }