Exemple #1
0
            public override void OnTicksReport(ClientSession session, TicksRequestClientContext TicksRequestClientContext, QueryTicksReport report)
            {
                var context = (QueryQuoteHistoryTicksAsyncContext)TicksRequestClientContext;
                var result  = new List <Tick>();

                for (int i = 0; i < report.Ticks.Length; i++)
                {
                    var sourceTick = report.Ticks[i];
                    var tick       = new Tick()
                    {
                        Id = new TickId
                        {
                            Time  = sourceTick.Id.Time,
                            Index = sourceTick.Id.Index
                        },
                        Level2 = new Level2Collection()
                    };

                    for (int j = 0; j < sourceTick.Level2.Bids.Length; j++)
                    {
                        var sourceBid = sourceTick.Level2.Bids[j];
                        var bid       = new Level2Value
                        {
                            Price  = (decimal)sourceBid.Price,
                            Volume = (decimal)sourceBid.Volume
                        };
                        tick.Level2.Bids.Add(bid);
                    }

                    for (int j = 0; j < sourceTick.Level2.Asks.Length; j++)
                    {
                        var sourceAsk = sourceTick.Level2.Asks[j];
                        var ask       = new Level2Value
                        {
                            Price  = (decimal)sourceAsk.Price,
                            Volume = (decimal)sourceAsk.Volume
                        };
                        tick.Level2.Asks.Add(ask);
                    }

                    result.Add(tick);
                }
                context.Tcs.SetResult(result);
            }
Exemple #2
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);
        }