コード例 #1
0
ファイル: StockStream.cs プロジェクト: cloudcell/Qsh2Bin
        // **********************************************************************

        public override void Read(bool push)
        {
            int n = dr.ReadPackInt();

            for (int i = 0; i < n; i++)
            {
                int p = dr.ReadRelative(ref basePrice);
                int v = dr.ReadPackInt();

                if (v == 0)
                {
                    rawQuotes.Remove(p);
                }
                else
                {
                    rawQuotes[p] = v;
                }
            }

            if (push && Handler != null)
            {
                Quote[] quotes;
                Spread  spread;

                rawQuotes.GetQuotes(out quotes, out spread);

                Handler(Security.Key, quotes, spread);
            }
        }
コード例 #2
0
        // **********************************************************************

        public override void Read(bool push)
        {
            int n = (int)dr.ReadLeb128();

            for (int i = 0; i < n; i++)
            {
                lastPrice += (int)dr.ReadLeb128();
                int v = (int)dr.ReadLeb128();

                if (v == 0)
                {
                    rawQuotes.Remove(lastPrice);
                }
                else
                {
                    rawQuotes[lastPrice] = v;
                }
            }

            if (push && Handler != null)
            {
                Quote[] quotes;
                Spread  spread;

                rawQuotes.GetQuotes(out quotes, out spread);

                Handler(Security.Key, quotes, spread);
            }
        }
コード例 #3
0
ファイル: QuotesStream.cs プロジェクト: MMZhirkov/Trading
        // **********************************************************************

        public override void Read(bool push)
        {
            int n = (int)dr.ReadLeb128();

            for (int i = 0; i < n; i++)
            {
                lastPrice += (int)dr.ReadLeb128();
                int v = (int)dr.ReadLeb128();

                if (v == 0)
                {
                    rawQuotes.Remove(lastPrice);
                }
                else
                {
                    rawQuotes[lastPrice] = v;
                }
            }

            if (push && Handler != null)
            {
                Handler(rawQuotes.GetQuotes());
            }
        }
コード例 #4
0
        // **********************************************************************

        public override void Read(bool push)
        {
            DateTime dateTime;
            long     orderId;
            int      amountRest;
            long     dealId;
            int      dealPrice;
            int      oi;

            // ------------------------------------------------------------

            OrdLogEntryFlags flags       = (OrdLogEntryFlags)dr.ReadByte();
            OrdLogFlags      ordLogFlags = (OrdLogFlags)dr.ReadUInt16();

            bool isAdd  = (ordLogFlags & OrdLogFlags.Add) != 0;
            bool isFill = (ordLogFlags & OrdLogFlags.Fill) != 0;

            bool isBuy  = (ordLogFlags & OrdLogFlags.Buy) != 0;
            bool isSell = (ordLogFlags & OrdLogFlags.Sell) != 0;

            // ------------------------------------------------------------

            if ((flags & OrdLogEntryFlags.DateTime) != 0)
            {
                lastMilliseconds = dr.ReadGrowing(lastMilliseconds);
            }

            dateTime = DateTimeHelper.FromMs(lastMilliseconds);

            if ((flags & OrdLogEntryFlags.OrderId) == 0)
            {
                orderId = lastOrderId;
            }
            else if (isAdd)
            {
                orderId = lastOrderId = dr.ReadGrowing(lastOrderId);
            }
            else
            {
                orderId = lastOrderId + dr.ReadLeb128();
            }

            if ((flags & OrdLogEntryFlags.Price) != 0)
            {
                lastPrice += (int)dr.ReadLeb128();
            }

            if ((flags & OrdLogEntryFlags.Amount) != 0)
            {
                lastAmount = (int)dr.ReadLeb128();
            }

            if (isFill)
            {
                if ((flags & OrdLogEntryFlags.AmountRest) != 0)
                {
                    amountRest = lastAmountRest = (int)dr.ReadLeb128();
                }
                else
                {
                    amountRest = lastAmountRest;
                }

                if ((flags & OrdLogEntryFlags.DealId) != 0)
                {
                    dealId = lastDealId = (int)dr.ReadGrowing(lastDealId);
                }
                else
                {
                    dealId = lastDealId;
                }

                if ((flags & OrdLogEntryFlags.DealPrice) != 0)
                {
                    dealPrice = lastDealPrice += (int)dr.ReadLeb128();
                }
                else
                {
                    dealPrice = lastDealPrice;
                }

                if ((flags & OrdLogEntryFlags.OI) != 0)
                {
                    oi = lastOI += (int)dr.ReadLeb128();
                }
                else
                {
                    oi = lastOI;
                }
            }
            else
            {
                amountRest = isAdd ? lastAmount : 0;
                dealId     = 0;
                dealPrice  = 0;
                oi         = 0;
            }

            // ------------------------------------------------------------

            if (Handler != null && push)
            {
                Handler(new OrdLogEntry(
                            ordLogFlags, dateTime, orderId, lastPrice,
                            lastAmount, amountRest, dealId, dealPrice, oi));
            }

            // ------------------------------------------------------------

            if ((ordLogFlags & OrdLogFlags.FlowStart) != 0)
            {
                rawQuotes.Clear();
            }

            if (!(isBuy ^ isSell) ||
                (ordLogFlags & OrdLogFlags.NonSystem) != 0 ||
                (ordLogFlags & OrdLogFlags.NonZeroReplAct) != 0)
            {
                return;
            }

            // ------------------------------------------------------------

            int quantity;

            rawQuotes.TryGetValue(lastPrice, out quantity);

            if (isAdd ? isSell : isBuy)
            {
                quantity += lastAmount;
            }
            else
            {
                quantity -= lastAmount;
            }

            if (quantity == 0)
            {
                rawQuotes.Remove(lastPrice);
            }
            else
            {
                rawQuotes[lastPrice] = quantity;
            }

            // ------------------------------------------------------------

            if (push)
            {
                if ((ordLogFlags & OrdLogFlags.EndOfTransaction) != 0)
                {
                    // В большинстве случаев нет необходимости делать все,
                    // что в этом блоке, на каждый тик. Целесообразнее
                    // отрпавлять эти данные по таймеру, раз в 5-15 мс.

                    if (quotesHandler != null)
                    {
                        quotesHandler(rawQuotes.GetQuotes());
                    }

                    if (auxInfoHandler != null)
                    {
                        int askTotal = 0;
                        int bidTotal = 0;

                        foreach (KeyValuePair <int, int> kvp in rawQuotes)
                        {
                            if (kvp.Value > 0)
                            {
                                askTotal += kvp.Value;
                            }
                            else
                            {
                                bidTotal -= kvp.Value;
                            }
                        }

                        auxInfoHandler(new AuxInfo(
                                           dateTime, lastDealPrice, askTotal,
                                           bidTotal, lastOI, 0, 0, 0, 0, null));
                    }
                }

                if (lastPushedDealId < dealId)
                {
                    if (dealHandler != null)
                    {
                        dealHandler(new Deal()
                        {
                            Type     = isSell ? DealType.Sell : DealType.Buy,
                            Id       = dealId,
                            DateTime = dateTime,
                            Price    = dealPrice,
                            Volume   = lastAmount,
                            OI       = oi
                        });
                    }

                    lastPushedDealId = dealId;
                }
            }
        }
コード例 #5
0
ファイル: OrdLogStream.cs プロジェクト: maxl85/Qsh2Bin
        // **********************************************************************

        public override void Read(bool push)
        {
            DateTime dateTime;
            long     orderId;
            int      amountRest;
            long     dealId;
            int      dealPrice;
            int      oi;

            // ------------------------------------------------------------

            OrdLogEntryFlags flags       = (OrdLogEntryFlags)dr.ReadByte();
            OrdLogFlags      ordLogFlags = (OrdLogFlags)dr.ReadUInt16();

            bool isAdd  = (ordLogFlags & OrdLogFlags.Add) != 0;
            bool isFill = (ordLogFlags & OrdLogFlags.Fill) != 0;

            bool isBuy  = (ordLogFlags & OrdLogFlags.Buy) != 0;
            bool isSell = (ordLogFlags & OrdLogFlags.Sell) != 0;

            // ------------------------------------------------------------

            if ((flags & OrdLogEntryFlags.DateTime) != 0)
            {
                lastMilliseconds = dr.ReadGrowing(lastMilliseconds);
            }

            dateTime = DateTimeHelper.FromMs(lastMilliseconds);

            if ((flags & OrdLogEntryFlags.OrderId) == 0)
            {
                orderId = lastOrderId;
            }
            else if (isAdd)
            {
                orderId = lastOrderId = dr.ReadGrowing(lastOrderId);
            }
            else
            {
                orderId = lastOrderId + dr.ReadLeb128();
            }

            if ((flags & OrdLogEntryFlags.Price) != 0)
            {
                lastPrice += (int)dr.ReadLeb128();
            }

            if ((flags & OrdLogEntryFlags.Amount) != 0)
            {
                lastAmount = (int)dr.ReadLeb128();
            }

            if (isFill)
            {
                if ((flags & OrdLogEntryFlags.AmountRest) != 0)
                {
                    amountRest = lastAmountRest = (int)dr.ReadLeb128();
                }
                else
                {
                    amountRest = lastAmountRest;
                }

                if ((flags & OrdLogEntryFlags.DealId) != 0)
                {
                    dealId = lastDealId = (int)dr.ReadGrowing(lastDealId);
                }
                else
                {
                    dealId = lastDealId;
                }

                if ((flags & OrdLogEntryFlags.DealPrice) != 0)
                {
                    dealPrice = lastDealPrice += (int)dr.ReadLeb128();
                }
                else
                {
                    dealPrice = lastDealPrice;
                }

                if ((flags & OrdLogEntryFlags.OI) != 0)
                {
                    oi = lastOI += (int)dr.ReadLeb128();
                }
                else
                {
                    oi = lastOI;
                }
            }
            else
            {
                amountRest = isAdd ? lastAmount : 0;
                dealId     = 0;
                dealPrice  = 0;
                oi         = 0;
            }

            // ------------------------------------------------------------

            if (Handler != null && push)
            {
                Handler(Security.Key, new OrdLogEntry(
                            ordLogFlags, dateTime, orderId, lastPrice,
                            lastAmount, amountRest, dealId, dealPrice, oi));
            }

            // ------------------------------------------------------------

            if ((ordLogFlags & OrdLogFlags.SessIdChanged) != 0)
            {
                rawQuotes.Clear();
            }

            if (!(isBuy ^ isSell) ||
                (ordLogFlags & OrdLogFlags.NonSystem) != 0 ||
                (ordLogFlags & OrdLogFlags.NonZeroReplAct) != 0)
            {
                return;
            }

            // ------------------------------------------------------------

            int quantity;

            rawQuotes.TryGetValue(lastPrice, out quantity);

            if (isAdd ? isSell : isBuy)
            {
                quantity += lastAmount;
            }
            else
            {
                quantity -= lastAmount;
            }

            if (quantity == 0)
            {
                rawQuotes.Remove(lastPrice);
            }
            else
            {
                rawQuotes[lastPrice] = quantity;
            }

            // ------------------------------------------------------------

            if (push)
            {
                if ((ordLogFlags & OrdLogFlags.EndOfTransaction) != 0)
                {
                    DateTime now = DateTime.UtcNow;

                    if (now - lastQuotesPush > quotesPushInterval)
                    {
                        lastQuotesPush = now;

                        if (quotesHandler != null)
                        {
                            Quote[] quotes;
                            Spread  spread;

                            rawQuotes.GetQuotes(out quotes, out spread);

                            if (spread.Ask > 0 && spread.Bid > 0)
                            {
                                quotesHandler(Security.Key, quotes, spread);
                            }
                        }

                        if (auxInfoHandler != null)
                        {
                            int askTotal = 0;
                            int bidTotal = 0;

                            foreach (KeyValuePair <int, int> kvp in rawQuotes)
                            {
                                if (kvp.Value > 0)
                                {
                                    askTotal += kvp.Value;
                                }
                                else
                                {
                                    bidTotal -= kvp.Value;
                                }
                            }

                            auxInfoHandler(Security.Key, new AuxInfo(
                                               dateTime, lastDealPrice, askTotal,
                                               bidTotal, lastOI, 0, 0, 0, 0, null));
                        }
                    }
                }

                if (lastPushedDealId < dealId)
                {
                    if (dealHandler != null)
                    {
                        dealHandler(new Deal()
                        {
                            SecKey   = Security.Key,
                            Type     = isSell ? DealType.Sell : DealType.Buy,
                            Id       = dealId,
                            DateTime = dateTime,
                            Price    = dealPrice,
                            Volume   = lastAmount,
                            OI       = oi
                        });
                    }

                    lastPushedDealId = dealId;
                }
            }
        }