Exemplo n.º 1
0
            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);
                }
            }
Exemplo n.º 2
0
            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);
                }
            }
Exemplo n.º 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);
        }
Exemplo n.º 4
0
            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;
                }
            }
Exemplo n.º 5
0
            internal CandleData(SecDBFileReader file, int sampleNumber, DateTime timestamp, Stream stream)
            {
                File = file;

                SampleNumber = sampleNumber;
                TimeStamp    = timestamp;

                OpenSteps  = SecDBPrimitives.ReadInt32(stream);
                HighSteps  = SecDBPrimitives.ReadInt32(stream);
                LowSteps   = SecDBPrimitives.ReadInt32(stream);
                CloseSteps = SecDBPrimitives.ReadInt32(stream);

                BuyVolume  = SecDBPrimitives.ReadInt32(stream);
                SellVolume = SecDBPrimitives.ReadInt32(stream);

                FirstStreamOffset = SecDBPrimitives.ReadUInt64(stream);
            }
Exemplo n.º 6
0
            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);
                    }
                }
            }
Exemplo n.º 7
0
            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);
                }
            }
Exemplo n.º 8
0
        private IEnumerable <StreamSample> enumerateStreamData(enumerateStreamDataArgs args, FileSystemFile file)
        {
            var candle = args.Candle;

            using (var stream = file.FileStream)
            {
                //Stream data start per candle
                if (candle.IsAssigned)
                {
                    stream.Seek((long)candle.FirstStreamOffset, SeekOrigin.Begin);
                }
                else
                {
                    stream.Seek((long)(m_Streams_DataOffset + sizeof(int)), SeekOrigin.Begin);
                }

                DateTime currentTS = candle.TimeStamp;

                StreamSample[] prior = new StreamSample[(int)StreamID.MAX_ID + 1];
                while (true)
                {
                    var hdr = stream.ReadByte();
                    if (hdr == -1)
                    {
                        yield break; //END OF FILE
                    }
                    var isDelta = (hdr & 0x80) != 0;
                    var sid     = hdr & 0x7f;
                    if ((StreamID)sid > StreamID.MAX_ID || sid >= STREAM_SAMPLE_FACTORIES.Length)
                    {
                        throw new FinancialException(StringConsts.SECDB_STREAM_CORRUPTED_ERROR + "enumerateStreamData(sid>StreamIS.MAX_ID)");
                    }

                    if (sid == (int)StreamID.Seconds)
                    {
                        int ts = SecDBPrimitives.ReadMidnightSecond(stream);
                        currentTS = new DateTime(m_SystemHeader.Date.Year,
                                                 m_SystemHeader.Date.Month,
                                                 m_SystemHeader.Date.Day,
                                                 0, 0, 0, DateTimeKind.Utc).AddSeconds(ts);
                        //Second resets all other
                        for (var i = 0; i < prior.Length; i++)
                        {
                            prior[i] = null;
                        }
                    }
                    else
                    {
                        var mcsDiff = SecDBPrimitives.ReadDiffTimeMcs(stream);
                        var ticks   = mcsDiff * 10;
                        currentTS = currentTS.AddTicks((long)ticks);
                    }


                    var priorSampleOfThisType = prior[sid];
                    if (priorSampleOfThisType == null && isDelta)
                    {
                        throw new FinancialException(StringConsts.SECDB_STREAM_CORRUPTED_ERROR + "prior sample of '{0}' type is null, but IsDelta = 1".Args((StreamID)sid));
                    }

                    var priorSecond = prior[0] as SecondSample;
                    if (sid != 0 && priorSecond == null)
                    {
                        throw new FinancialException(StringConsts.SECDB_STREAM_CORRUPTED_ERROR + "missing SecondSample before '{0}' sample".Args((StreamID)sid));
                    }


                    var sample = STREAM_SAMPLE_FACTORIES[sid](this, priorSampleOfThisType, currentTS, stream);
                    yield return(sample);

                    prior[sid] = sample;
                }
            }//using stream
        }
Exemplo n.º 9
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");
            }
        }