internal SummarySample(DateTime ts, Stream stream) : base(ts)
            {
                var flags = SecDBPrimitives.ReadByte(stream);

                IsBidQty    = (flags & (1)) != 0;
                IsAskQty    = (flags & (1 << 1)) != 0;
                IsPositions = (flags & (1 << 2)) != 0;
                IsRisk      = (flags & (1 << 3)) != 0;

                if (IsBidQty)
                {
                    BidQty = SecDBPrimitives.ReadULEB128(stream);
                }
                if (IsAskQty)
                {
                    AskQty = SecDBPrimitives.ReadULEB128(stream);
                }
                if (IsPositions)
                {
                    Positions = SecDBPrimitives.ReadSLEB128(stream);
                }
                if (IsRisk)
                {
                    RiskAmt = SecDBPrimitives.ReadDouble(stream);
                }
            }
            internal InfoSample(DateTime ts, Stream stream) : base(ts)
            {
                var tp = SecDBPrimitives.ReadByte(stream);

                if (tp == 0)
                {
                    Body = SecDBPrimitives.ReadString(stream);
                }
                else
                {
                    ResID = SecDBPrimitives.ReadULEB128(stream);
                }
            }
Beispiel #3
0
        private void parseCandlesMeta(Stream stream)
        {
            var bt = SecDBPrimitives.ReadByte(stream);

            if (bt != 0x03)//0x3 -  Candles metadata header
            {
                throw new FinancialException("CandlesMeta.Header != 0x03");
            }

            //Filler
            bt = SecDBPrimitives.ReadByte(stream);

            //HdrCount
            var icount = SecDBPrimitives.ReadUInt16(stream);

            var chdrs = new CandleHeader[icount];

            //CandleHeader
            for (var i = 0; i < icount; i++)
            {
                bt = SecDBPrimitives.ReadByte(stream);
                if (bt != 0x04)//0x4 -  Candle Header
                {
                    throw new FinancialException("CandleHeader.Header != 0x04");
                }

                //Filler
                bt = SecDBPrimitives.ReadByte(stream);

                var resolution = SecDBPrimitives.ReadUInt16(stream);
                var starttime  = SecDBPrimitives.ReadUInt32(stream);
                var count      = SecDBPrimitives.ReadUInt32(stream);
                var offset     = SecDBPrimitives.ReadUInt32(stream);


                if (resolution == 0)
                {
                    throw new FinancialException("CandleHeader.Resolution == 0x00");
                }

                var sdt = new DateTime(m_SystemHeader.Date.Year,
                                       m_SystemHeader.Date.Month,
                                       m_SystemHeader.Date.Day,
                                       0, 0, 0, DateTimeKind.Utc).AddSeconds(starttime);

                var ch = new CandleHeader(this, resolution, starttime, sdt, count, offset);
                chdrs[i] = ch;
            }
            m_Candles_Meta = new CandlesMeta(this, chdrs);
        }
            internal QuoteSample(SecDBFileReader file, QuoteSample ps, DateTime ts, Stream stream) : base(ts)
            {
                var bt = SecDBPrimitives.ReadByte(stream);

                BidCount = bt & 0xf;
                AskCount = (bt & 0xf0) >> 4;
                var cnt = BidCount + AskCount;

                if (cnt == 0)
                {
                    throw new FinancialException(StringConsts.SECDB_FILE_HEADER_ERROR + "QuoteSample: no px levels");
                }

                PriceLevels = new PxLevel[cnt];

                var pxStep = file.SystemHeader.PriceStep;

                var currentPrice = 0L;

                for (var i = 0; i < cnt; i++)
                {
                    var price = SecDBPrimitives.ReadSLEB128(stream);
                    if (i == 0)
                    {
                        if (ps == null)
                        {
                            currentPrice = price;
                        }
                        else
                        {
                            currentPrice  = ps.PriceLevels[0].PriceStep;
                            currentPrice += price;
                        }
                    }
                    else
                    {
                        currentPrice += price;
                    }

                    var qty = SecDBPrimitives.ReadSLEB128(stream);

                    var pl = new PxLevel {
                        PriceStep = currentPrice, Price = currentPrice * pxStep, Quantity = qty
                    };
                    PriceLevels[i] = pl;
                }
            }
            internal OrderSample(SecDBFileReader file, OrderSample ps, DateTime ts, Stream stream) : base(ts)
            {
                var flags = SecDBPrimitives.ReadByte(stream);

                InternalOrder = (flags & (1)) != 0;
                CancelAll     = (flags >> 1) != 0;

                if (!CancelAll)
                {
                    IsActive      = (flags >> 2) != 0;
                    IsReplacement = (flags >> 3) != 0;
                    Side          = (SideType)((flags >> 4) & 0x1);

                    IsTakeProfit = (flags >> 5) != 0;
                    IsStopLoss   = (flags >> 6) != 0;

                    OrderID = SecDBPrimitives.ReadSLEB128(stream);

                    if (IsActive)
                    {
                        var price = SecDBPrimitives.ReadSLEB128(stream);
                        if (ps == null)
                        {
                            PriceStep = price;
                        }
                        else
                        {
                            PriceStep = ps.PriceStep + price;
                        }

                        Price = PriceStep * file.SystemHeader.PriceStep;

                        Qty = SecDBPrimitives.ReadSLEB128(stream);
                    }

                    if (IsReplacement)
                    {
                        OldOrderID = SecDBPrimitives.ReadSLEB128(stream);
                    }
                }
            }
            internal TradeSample(SecDBFileReader file, TradeSample ps, DateTime ts, Stream stream) : base(ts)
            {
                var flags = SecDBPrimitives.ReadByte(stream);

                InternalTrade = (flags & (1)) != 0;
                Aggressor     = (AggressorType)((flags >> 1) & 0x3);
                Side          = (SideType)((flags >> 3) & 0x1);

                IsQty     = (flags & (1 << 4)) != 0;
                IsTradeID = (flags & (1 << 5)) != 0;
                IsOrderID = (flags & (1 << 6)) != 0;

                var price = SecDBPrimitives.ReadSLEB128(stream);

                if (ps == null)
                {
                    PriceStep = price;
                }
                else
                {
                    PriceStep = ps.PriceStep + price;
                }

                Price = PriceStep * file.SystemHeader.PriceStep;

                if (IsQty)
                {
                    Qty = SecDBPrimitives.ReadSLEB128(stream);
                }
                if (IsTradeID)
                {
                    TradeID = SecDBPrimitives.ReadULEB128(stream);
                }
                if (IsOrderID)
                {
                    OrderID = SecDBPrimitives.ReadULEB128(stream);
                }
            }
Beispiel #7
0
        private void parseMetadata(Stream stream)
        {
            var bt = SecDBPrimitives.ReadByte(stream);

            if (bt != 0x01)//0x1 - Streams header
            {
                throw new FinancialException("StreamsMeta.Header != 0x01");
            }

            //Compression
            bt = SecDBPrimitives.ReadByte(stream);
            if (bt == 0)
            {
                m_Streams_CompressionType = CompressionType.None;
            }
            else if (bt == 1)
            {
                m_Streams_CompressionType = CompressionType.GZip;
            }
            else
            {
                throw new FinancialException("StreamsMeta.Compression != 0 | 1");
            }

            //DataOffset
            m_Streams_DataOffset = SecDBPrimitives.ReadUInt32(stream);

            //Stream Count
            var scount = SecDBPrimitives.ReadByte(stream);

            m_Streams_Metas    = new StreamMeta[scount + 1];
            m_Streams_Metas[0] = new StreamMeta(StreamID.Seconds);

            //StreamMeta
            for (var i = 1; i <= scount; i++) //<= as scount does not include zeros element which is mandatory
            {
                bt = SecDBPrimitives.ReadByte(stream);
                if (bt != 0x02)//0x2 - Stream header
                {
                    throw new FinancialException("StreamMeta.Header != 0x02");
                }

                //StreamID
                bt = SecDBPrimitives.ReadByte(stream);
                if (bt > (byte)StreamID.MAX_ID)
                {
                    throw new FinancialException("StreamMeta.StreamID {0} = invalid".Args(bt));
                }

                var sid = (StreamID)bt;
                m_Streams_Metas[i] = new StreamMeta(sid);
            }

            //CandlesMeta
            parseCandlesMeta(stream);

            //Verify data start
            //Beginning of Stream Data Marker
            stream.Seek(m_Streams_DataOffset, SeekOrigin.Begin);
            var magic = SecDBPrimitives.ReadUInt32(stream);

            if (magic != 0xABBABABA)
            {
                throw new FinancialException("StreamDataMarker != 0xABBABABA");
            }
        }