예제 #1
0
        public IEnumerable <Tick> QueryQuoteHistoryTicksRange(DateTime from, DateTime to, string symbol, bool level2)
        {
            if (!IsConnected)
            {
                throw new Exception("Client is not connected!");
            }

            if (to < from)
            {
                DateTime temp = from;
                from = to;
                to   = temp;
            }

            var timestamp = from;
            var last      = timestamp;

            var filename      = level2 ? "ticks level2" : "ticks";
            var formatter     = level2 ? (IFormatter <TickValue>)FeedTickLevel2Formatter.Instance : FeedTickFormatter.Instance;
            var zipSerializer = new ItemsZipSerializer <TickValue, TickValueList>(formatter, filename);
            var txtSerializer = new ItemsTextSerializer <TickValue, TickValueList>(formatter, filename);

            do
            {
                List <byte[]> content = QueryQuoteHistoryTicksFilesInternal(timestamp, symbol, level2);
                foreach (var file in content)
                {
                    TickValueList historyTicks;
                    if ((file.Length >= 4) && (file[0] == 0x50) && (file[1] == 0x4b) && (file[2] == 0x03) && (file[3] == 0x04))
                    {
                        historyTicks = zipSerializer.Deserialize(file);
                    }
                    else
                    {
                        historyTicks = txtSerializer.Deserialize(file);
                    }

                    foreach (var historyTick in historyTicks)
                    {
                        if (historyTick.Time < from)
                        {
                            continue;
                        }
                        if (historyTick.Time > to)
                        {
                            break;
                        }

                        if (historyTick.Time <= last)
                        {
                            continue;
                        }
                        last = historyTick.Time;

                        var tick = new Tick()
                        {
                            Id = new TickId
                            {
                                Time  = historyTick.Id.Time,
                                Index = historyTick.Id.Index
                            },
                            Level2 = new Level2Collection()
                        };

                        foreach (var level2record in historyTick.Level2)
                        {
                            if (level2record.Type == FxPriceType.Bid)
                            {
                                var bid = new Level2Value
                                {
                                    Price  = (decimal)level2record.Price,
                                    Volume = (decimal)level2record.Volume
                                };
                                tick.Level2.Bids.Insert(0, bid);
                            }
                            if (level2record.Type == FxPriceType.Ask)
                            {
                                var ask = new Level2Value
                                {
                                    Price  = (decimal)level2record.Price,
                                    Volume = (decimal)level2record.Volume
                                };
                                tick.Level2.Asks.Add(ask);
                            }
                        }

                        yield return(tick);
                    }
                }

                timestamp = timestamp.AddHours(1);
            } while (timestamp < to);
        }
예제 #2
0
        public IEnumerable <Bar> QueryQuoteHistoryBarsRange(DateTime from, DateTime to, string symbol, string pereodicity, PriceType priceType)
        {
            if (!IsConnected)
            {
                throw new Exception("Client is not connected!");
            }

            if (to < from)
            {
                DateTime temp = from;
                from = to;
                to   = temp;
            }

            var periodicity = Periodicity.Parse(pereodicity);
            var timestamp   = periodicity.GetPeriodStartTime(from);
            var last        = timestamp;

            var filename      = periodicity + " " + priceType.ToString("g").ToLowerInvariant();
            var zipSerializer = new ItemsZipSerializer <HistoryBar, List <HistoryBar> >(BarFormatter.Default, filename);
            var txtSerializer = new ItemsTextSerializer <HistoryBar, List <HistoryBar> >(BarFormatter.Default, filename);

            do
            {
                List <byte[]> content = QueryQuoteHistoryBarsFilesInternal(timestamp, symbol, pereodicity, priceType);
                foreach (var file in content)
                {
                    List <HistoryBar> historyBars;
                    if ((file.Length >= 4) && (file[0] == 0x50) && (file[1] == 0x4b) && (file[2] == 0x03) && (file[3] == 0x04))
                    {
                        historyBars = zipSerializer.Deserialize(file);
                    }
                    else
                    {
                        historyBars = txtSerializer.Deserialize(file);
                    }

                    foreach (var historyBar in historyBars)
                    {
                        if (historyBar.Time < from)
                        {
                            continue;
                        }
                        if (historyBar.Time > to)
                        {
                            break;
                        }

                        if (historyBar.Time <= last)
                        {
                            continue;
                        }
                        last = historyBar.Time;

                        yield return(new Bar
                        {
                            Time = historyBar.Time,
                            Open = historyBar.Open,
                            High = historyBar.Hi,
                            Low = historyBar.Low,
                            Close = historyBar.Close,
                            Volume = (decimal)historyBar.Volume
                        });
                    }
                }

                if (periodicity.Interval == TimeInterval.Second)
                {
                    if (periodicity.IntervalsCount < 10)
                    {
                        timestamp = timestamp.AddHours(1);
                    }
                    else
                    {
                        timestamp = timestamp.AddDays(1);
                    }
                }
                else if (periodicity.Interval == TimeInterval.Minute)
                {
                    if (periodicity.IntervalsCount < 5)
                    {
                        timestamp = timestamp.AddDays(1);
                    }
                    else
                    {
                        timestamp = timestamp.AddMonths(1);
                    }
                }
                else
                {
                    timestamp = timestamp.AddMonths(1);
                }
            } while (timestamp < to);
        }