internal QuotationPair Fetch()
        {
            QuotationPair result = null;

            if (!this.High.IsEmpty)
            {
                if (this.Low.IsEmpty || this.High.OverridedQuotation.Timestamp < this.Low.OverridedQuotation.Timestamp)
                {
                    result = this.High.Clon();
                    this.High.Clear();
                }
                else
                {
                    result = this.Low.Clon();
                    this.Low.Clear();
                }
            }
            else if (!this.Low.IsEmpty)
            {
                result = this.Low.Clon();
                this.Low.Clear();
            }
            else if (!this.Last.IsEmpty)
            {
                result = this.Last.Clon();
                this.Last.Clear();
            }

            return(result);
        }
        internal QuotationPair Clon()
        {
            QuotationPair clon = new QuotationPair();

            clon.OverridedQuotation = this.OverridedQuotation;
            clon.OriginQuotation    = this.OriginQuotation;
            clon.Token = this.Token;

            return(clon);
        }
        internal QuotationForBroadcast MergeAndGetQuotationToBroadcast(Queue <QuotationForBroadcast> pendingQuotations)
        {
            if (pendingQuotations.Count > 0)
            {
                if (pendingQuotations.Count > 3)
                {
                    string warning = string.Format("Too much {0} quotation wait for sending to TransactionServer", pendingQuotations.Count);
                    AppDebug.LogEvent("StateServer.QuotationBroadcastHelper", warning, EventLogEntryType.Warning);
                }

                foreach (QuotationForBroadcast pendingQuotation in pendingQuotations)
                {
                    if (pendingQuotation.OverridedQuotations == null)
                    {
                        continue;
                    }

                    foreach (OverridedQuotation overridedQuotation in pendingQuotation.OverridedQuotations)
                    {
                        if (string.IsNullOrEmpty(overridedQuotation.Ask) || string.IsNullOrEmpty(overridedQuotation.Bid))
                        {
                            continue;
                        }

                        string mergedQuotationKey = overridedQuotation.InstrumentID.ToString() + overridedQuotation.QuotePolicyID.ToString();

                        MergedQuotation mergedQuotation = null;
                        if (!this.mergedQuotaions.TryGetValue(mergedQuotationKey, out mergedQuotation))
                        {
                            mergedQuotation = new MergedQuotation(overridedQuotation.InstrumentID, overridedQuotation.QuotePolicyID);
                            this.mergedQuotaions.Add(mergedQuotationKey, mergedQuotation);
                        }

                        OriginQuotation originQuotation = null;
                        if (pendingQuotation.OriginQuotations != null)
                        {
                            originQuotation = pendingQuotation.OriginQuotations.FirstOrDefault <OriginQuotation>(t => { return(t.InstrumentID == overridedQuotation.InstrumentID); });
                        }
                        mergedQuotation.Merge(pendingQuotation.Token, overridedQuotation, originQuotation);
                    }
                }
            }


            List <OverridedQuotation> overridedQuotations = null;
            List <OriginQuotation>    originQuotations    = null;
            Token token = null;

            foreach (MergedQuotation mergedQuotation in this.mergedQuotaions.Values)
            {
                QuotationPair pair = mergedQuotation.Fetch();
                if (pair != null)
                {
                    if (overridedQuotations == null)
                    {
                        overridedQuotations = new List <OverridedQuotation>();
                    }
                    overridedQuotations.Add(pair.OverridedQuotation);
                    if (token != null)
                    {
                        token = pair.Token;
                    }

                    if (pair.OriginQuotation != null)
                    {
                        if (originQuotations == null)
                        {
                            originQuotations = new List <OriginQuotation>();
                        }
                        if (!originQuotations.Contains(pair.OriginQuotation))
                        {
                            originQuotations.Add(pair.OriginQuotation);
                        }
                    }
                }
            }

            if (overridedQuotations != null)
            {
                QuotationForBroadcast result = new QuotationForBroadcast();
                result.OverridedQuotations = overridedQuotations.ToArray();
                result.Token            = token;
                result.OriginQuotations = originQuotations == null ? null : originQuotations.ToArray();
                return(result);
            }
            else
            {
                return(null);
            }
        }