private void ListToKdb(List <PbTickView> list, bool SaveQuote)
        {
            foreach (var l in list)
            {
                string date      = getDate(l.ActionDay);
                string time      = getTime(l);
                string trade_str = string.Format("`trade insert({0}T{1};`$\"{2}\";{3}f;{4}j;{5}j)", date, time, l.Static.Symbol, l.LastPrice, l.Volume, l.OpenInterest);
                c.ks(trade_str);

                if (!SaveQuote)
                {
                    continue;
                }

                double bid   = 0;
                double ask   = 0;
                int    bsize = 0;
                int    asize = 0;

                int count = l.DepthList == null ? 0 : l.DepthList.Count;
                if (count > 0)
                {
                    int AskPos = DepthListHelper.FindAsk1Position(l.DepthList, l.AskPrice1);
                    int BidPos = AskPos - 1;
                    if (BidPos >= 0)
                    {
                        bid   = l.DepthList[BidPos].Price;
                        bsize = l.DepthList[BidPos].Size;
                    }
                    if (AskPos < count)
                    {
                        ask   = l.DepthList[AskPos].Price;
                        asize = l.DepthList[AskPos].Size;
                    }
                }

                string quote_str = string.Format("`quote insert({0}T{1};`$\"{2}\";{3}f;{4}f;{5};{6})", date, time, l.Static.Symbol, bid, ask, bsize, asize);

                c.ks(quote_str);
            }
        }
        private void ListToCSV(List <PbTickView> list, FileInfo file_output)
        {
            using (TextWriter stream = new StreamWriter(file_output.FullName))
            {
                StringBuilder data = new StringBuilder();
                // 头行第一部分
                for (int i = 0; i < CSVHeaderLine.Length; ++i)
                {
                    data.Append(CSVHeaderLine[i]);
                    data.Append(",");
                }
                // 第二部分根据档位计算
                for (int i = 1; i <= numericUpDown_DepthLevel.Value; ++i)
                {
                    foreach (var s in CSVHeaderLineEx)
                    {
                        data.Append(s);
                        data.Append(i);
                        data.Append(",");
                    }
                }
                stream.WriteLine(data);

                foreach (var l in list)
                {
                    stream.Write(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},",
                                               l.TradingDay,
                                               l.ActionDay,
                                               getTime(l),
                                               l.LastPrice,
                                               l.Volume,
                                               l.OpenInterest,
                                               l.Turnover,
                                               l.AveragePrice
                                               ));
                    StringBuilder s = new StringBuilder();
                    for (int i = 0; i < numericUpDown_DepthLevel.Value; ++i)
                    {
                        int count = l.DepthList == null ? 0 : l.DepthList.Count;
                        if (count > 0)
                        {
                            int AskPos = DepthListHelper.FindAsk1Position(l.DepthList, l.AskPrice1);
                            int BidPos = AskPos - 1;
                            if (BidPos - i >= 0)
                            {
                                s.Append(l.DepthList[BidPos - i].Price);
                                s.Append(",");
                                s.Append(l.DepthList[BidPos - i].Size);
                                s.Append(",");
                            }
                            else
                            {
                                s.Append(",,");
                            }
                            if (AskPos + i < count)
                            {
                                s.Append(l.DepthList[AskPos + i].Price);
                                s.Append(",");
                                s.Append(l.DepthList[AskPos + i].Size);
                                s.Append(",");
                            }
                            else
                            {
                                s.Append(",,");
                            }
                        }
                        else
                        {
                            s.Append(",,,,");
                        }
                    }
                    stream.WriteLine(s);
                }
                stream.Close();
            }
        }
        private DepthMarketDataField PbTick2DepthMarketDataField(PbTickCodec codec, PbTick tick)
        {
            PbTickView tickView = codec.Data2View(tick, false);

            DepthMarketDataField marketData = default(DepthMarketDataField);

            codec.GetUpdateTime(tick, out marketData.UpdateTime, out marketData.UpdateMillisec);

            marketData.TradingDay   = tickView.TradingDay;
            marketData.ActionDay    = tickView.ActionDay;
            marketData.LastPrice    = tickView.LastPrice;
            marketData.Volume       = tickView.Volume;
            marketData.Turnover     = tickView.Turnover;
            marketData.OpenInterest = tickView.OpenInterest;
            marketData.AveragePrice = tickView.AveragePrice;
            if (tickView.Bar != null)
            {
                marketData.OpenPrice    = tickView.Bar.Open;
                marketData.HighestPrice = tickView.Bar.High;
                marketData.LowestPrice  = tickView.Bar.Low;
                marketData.ClosePrice   = tickView.Bar.Close;
            }
            if (tickView.Static != null)
            {
                marketData.LowerLimitPrice = tickView.Static.LowerLimitPrice;
                marketData.UpperLimitPrice = tickView.Static.UpperLimitPrice;
                marketData.SettlementPrice = tickView.Static.SettlementPrice;
                marketData.Symbol          = tickView.Static.Symbol;
                if (!string.IsNullOrWhiteSpace(tickView.Static.Exchange))
                {
                    marketData.Exchange = Enum <ExchangeType> .Parse(tickView.Static.Exchange);
                }
            }

            int count = tickView.DepthList == null ? 0 : tickView.DepthList.Count;

            if (count > 0)
            {
                int AskPos  = DepthListHelper.FindAsk1Position(tickView.DepthList, tickView.AskPrice1);
                int BidPos  = AskPos - 1;
                int _BidPos = BidPos;
                if (_BidPos >= 0)
                {
                    marketData.BidPrice1  = tickView.DepthList[_BidPos].Price;
                    marketData.BidVolume1 = tickView.DepthList[_BidPos].Size;
                    --_BidPos;
                    if (_BidPos >= 0)
                    {
                        marketData.BidPrice2  = tickView.DepthList[_BidPos].Price;
                        marketData.BidVolume2 = tickView.DepthList[_BidPos].Size;
                        --_BidPos;
                        if (_BidPos >= 0)
                        {
                            marketData.BidPrice3  = tickView.DepthList[_BidPos].Price;
                            marketData.BidVolume3 = tickView.DepthList[_BidPos].Size;
                            --_BidPos;
                            if (_BidPos >= 0)
                            {
                                marketData.BidPrice4  = tickView.DepthList[_BidPos].Price;
                                marketData.BidVolume4 = tickView.DepthList[_BidPos].Size;
                                --_BidPos;
                                if (_BidPos >= 0)
                                {
                                    marketData.BidPrice5  = tickView.DepthList[_BidPos].Price;
                                    marketData.BidVolume5 = tickView.DepthList[_BidPos].Size;
                                }
                            }
                        }
                    }
                }

                int _AskPos = AskPos;
                if (_AskPos < count)
                {
                    marketData.AskPrice1  = tickView.DepthList[_AskPos].Price;
                    marketData.AskVolume1 = tickView.DepthList[_AskPos].Size;
                    ++_AskPos;
                    if (_AskPos < count)
                    {
                        marketData.AskPrice2  = tickView.DepthList[_AskPos].Price;
                        marketData.AskVolume2 = tickView.DepthList[_AskPos].Size;
                        ++_AskPos;
                        if (_AskPos < count)
                        {
                            marketData.AskPrice3  = tickView.DepthList[_AskPos].Price;
                            marketData.AskVolume3 = tickView.DepthList[_AskPos].Size;
                            ++_AskPos;
                            if (_AskPos < count)
                            {
                                marketData.AskPrice4  = tickView.DepthList[_AskPos].Price;
                                marketData.AskVolume4 = tickView.DepthList[_AskPos].Size;
                                ++_AskPos;
                                if (_AskPos < count)
                                {
                                    marketData.AskPrice5  = tickView.DepthList[_AskPos].Price;
                                    marketData.AskVolume5 = tickView.DepthList[_AskPos].Size;
                                }
                            }
                        }
                    }
                }
            }
            return(marketData);
        }
        public static PbTickStruct5 toStruct(PbTickView input)
        {
            var o = default(PbTickStruct5);

            codec.GetUpdateTime(input, out o.UpdateTime, out o.UpdateMillisec);

            o.TradingDay = input.TradingDay;
            o.ActionDay  = input.ActionDay;
            o.LastPrice  = (float)input.LastPrice;
            o.Volume     = input.Volume;

            o.Turnover     = (float)input.Turnover;
            o.OpenInterest = (float)input.OpenInterest;
            o.AveragePrice = (float)input.AveragePrice;

            if (input.Bar != null)
            {
                o.OpenPrice    = (float)input.Bar.Open;
                o.HighestPrice = (float)input.Bar.High;
                o.LowestPrice  = (float)input.Bar.Low;
                o.ClosePrice   = (float)input.Bar.Close;
            }

            if (input.Static != null)
            {
                o.LowerLimitPrice = (float)input.Static.LowerLimitPrice;
                o.UpperLimitPrice = (float)input.Static.UpperLimitPrice;
                o.SettlementPrice = (float)input.Static.SettlementPrice;
                o.Symbol          = input.Static.Symbol;
                o.Exchange        = input.Static.Exchange;
                //o.Symbol = "IF";
                //o.Exchange = "CFFEX";
                o.PreClosePrice      = (float)input.Static.PreClosePrice;
                o.PreSettlementPrice = (float)input.Static.PreSettlementPrice;
                o.PreOpenInterest    = input.Static.PreOpenInterest;
            }

            o.Price = new float[10];
            o.Size  = new int[10];

            int count = input.DepthList == null ? 0 : input.DepthList.Count;

            if (count > 0)
            {
                int AskPos   = DepthListHelper.FindAsk1Position(input.DepthList, input.AskPrice1); // 卖一位置
                int BidPos   = AskPos - 1;                                                         // 买一位置,在数组中的位置
                int BidCount = BidPos + 1;
                int AskCount = count - AskPos;


                if (BidCount > 0)
                {
                    int j = 0;
                    for (int i = BidPos; i >= 0; --i)
                    {
                        o.Price[4 - j] = (float)input.DepthList[i].Price;
                        o.Size[4 - j]  = input.DepthList[i].Size;

                        ++j;
                    }
                }

                if (AskCount > 0)
                {
                    int j = 0;
                    for (int i = AskPos; i < count; ++i)
                    {
                        o.Price[5 + j] = (float)input.DepthList[i].Price;
                        o.Size[5 + j]  = input.DepthList[i].Size;

                        ++j;
                    }
                }
            }

            return(o);
        }
        static void Main(string[] args)
        {
            List <DepthItem> oldPrevList = new List <DepthItem>();
            List <DepthItem> oldCurrList = new List <DepthItem>();

            List <DepthItem> newPrevList = new List <DepthItem>();
            List <DepthItem> newCurrList = new List <DepthItem>();

            DepthListHelper test = new DepthListHelper();

            // 处理老的多一条
            oldPrevList.Add(new DepthItem(1, 100, 0));
            oldPrevList.Add(new DepthItem(7, 100, 0));
            oldPrevList.Add(new DepthItem(12, 100, 0));

            int j11 = DepthListHelper.FindAsk1Position(oldPrevList, 0);
            int j12 = DepthListHelper.FindAsk1Position(oldPrevList, 1);
            int j13 = DepthListHelper.FindAsk1Position(oldPrevList, 12);
            int j14 = DepthListHelper.FindAsk1Position(oldPrevList, 15);

            List <DepthItemView> oldPrevListV = new List <DepthItemView>();

            oldPrevListV.Add(new DepthItemView()
            {
                Price = 10
            });
            oldPrevListV.Add(new DepthItemView()
            {
                Price = 5
            });
            oldPrevListV.Add(new DepthItemView()
            {
                Price = 1
            });

            int j21 = DepthListHelper.FindAsk1PositionDescending(oldPrevListV, 12);
            int j22 = DepthListHelper.FindAsk1PositionDescending(oldPrevListV, 10);
            int j23 = DepthListHelper.FindAsk1PositionDescending(oldPrevListV, 1);
            int j24 = DepthListHelper.FindAsk1PositionDescending(oldPrevListV, 0);

            //oldCurrList.Add(new DepthItem(5, 100, 0));
            //oldCurrList.Add(new DepthItem(8, 100, 0));
            //oldCurrList.Add(new DepthItem(13, 100, 0));

            DepthListHelper.ExpandTwoListsToSameLength(oldPrevList, oldCurrList, 5, 13, newPrevList, newCurrList);

            List <DepthItem> newList = new List <DepthItem>();

            DepthListHelper.SizeMinusInTwoLists(newPrevList, newCurrList, newList);

            List <DepthItem> newList2 = new List <DepthItem>();

            DepthListHelper.SizeAddInTwoLists(newPrevList, newList, newList2);

            List <DepthItem> newList3 = new List <DepthItem>();

            DepthListHelper.PriceMinusInOneList(newList2, newList3);

            List <DepthItem> newList4 = new List <DepthItem>();

            DepthListHelper.PriceAddInOneList(newList3, newList4);

            DepthTick tick = new DepthTick();

            DepthListHelper.SetDepthTick(tick, 4, 100);
            DepthListHelper.SetDepthTick(tick, 10, 200);
            DepthListHelper.SetDepthTick(tick, 15, 300);
            DepthListHelper.SetDepthTick(tick, 28, 400);
            DepthListHelper.SetDepthTick(tick, 30, 500);

            int i1 = DepthListHelper.GetDepthTick(tick, 4);
            int i2 = DepthListHelper.GetDepthTick(tick, 10);
            int i3 = DepthListHelper.GetDepthTick(tick, 15);
            int i4 = DepthListHelper.GetDepthTick(tick, 28);
            int i5 = DepthListHelper.GetDepthTick(tick, 30);
            int i6 = DepthListHelper.GetDepthTick(tick, 45);

            DepthTick tick2 = new DepthTick();

            DepthListHelper.ListToStruct(newPrevList, 2, tick2);

            List <DepthItem> newList5 = new List <DepthItem>();

            DepthListHelper.StructToList(tick2, 2, newList5);


            //int j1 = test.FindAsk1Position(oldCurrList, 5);

            //int j2 = test.FindAsk1Position(oldCurrList, 8);
            //int j3 = test.FindAsk1Position(oldCurrList, 12);
            //int j4 = test.FindAsk1Position(oldCurrList, 13);

            //int j5 = test.FindAsk1Position(oldCurrList, 14);
            //int j6 = test.FindAsk1Position(oldCurrList, 15);

            //int j7 = test.FindAsk1Position(oldCurrList, 4);
            //int j8 = test.FindAsk1Position(oldCurrList, 7);


            //oldPrevList = new List<DepthItem>();
            //oldPrevList.Add(new DepthItem(3, 100, 0));
            //oldPrevList.Add(new DepthItem(7, 100, 0));
            //oldPrevList.Add(new DepthItem(12, 100, 0));
            //int x1 = test.FindAsk1Position(oldPrevList, 3);
            //int x2 = test.FindAsk1Position(oldPrevList, 7);
            //int x3 = test.FindAsk1Position(oldPrevList, 12);
            //int x4 = test.FindAsk1Position(oldPrevList, 5);
            //int x5 = test.FindAsk1Position(oldPrevList, 13);
            //int x6 = test.FindAsk1Position(oldPrevList, 2);



            // 处理新的多一条
            //oldPrevList.Add(new DepthItem(1, 100, 0));

            //oldCurrList.Add(new DepthItem(1, 100, 0));
            //oldCurrList.Add(new DepthItem(5, 100, 0));

            //test.Do(oldPrevList, oldCurrList, newPrevList, newCurrList);

            //test.
        }
Beispiel #6
0
        private DepthMarketDataNClass PbTick2DepthMarketDataNClass(PbTickCodec codec, PbTickView tickView)
        {
            DepthMarketDataNClass marketData = new DepthMarketDataNClass();

            codec.GetUpdateTime(tickView, out marketData.UpdateTime, out marketData.UpdateMillisec);

            marketData.TradingDay = tickView.TradingDay;
            marketData.ActionDay  = tickView.ActionDay;
            marketData.LastPrice  = tickView.LastPrice;
            marketData.Volume     = tickView.Volume;
            if (SubscribeExternData)
            {
                marketData.Turnover     = tickView.Turnover;
                marketData.OpenInterest = tickView.OpenInterest;
                marketData.AveragePrice = tickView.AveragePrice;
                if (tickView.Bar != null)
                {
                    marketData.OpenPrice    = tickView.Bar.Open;
                    marketData.HighestPrice = tickView.Bar.High;
                    marketData.LowestPrice  = tickView.Bar.Low;
                    marketData.ClosePrice   = tickView.Bar.Close;
                }
                if (tickView.Static != null)
                {
                    marketData.LowerLimitPrice = tickView.Static.LowerLimitPrice;
                    marketData.UpperLimitPrice = tickView.Static.UpperLimitPrice;
                    marketData.SettlementPrice = tickView.Static.SettlementPrice;
                    marketData.Symbol          = tickView.Static.Symbol;
                    if (!string.IsNullOrWhiteSpace(tickView.Static.Exchange))
                    {
                        marketData.Exchange = Enum <ExchangeType> .Parse(tickView.Static.Exchange);
                    }
                    marketData.PreClosePrice      = tickView.Static.PreClosePrice;
                    marketData.PreSettlementPrice = tickView.Static.PreSettlementPrice;
                    marketData.PreOpenInterest    = tickView.Static.PreOpenInterest;
                }
            }

            int count = tickView.DepthList == null ? 0 : tickView.DepthList.Count;

            if (count > 0)
            {
                int AskPos   = DepthListHelper.FindAsk1Position(tickView.DepthList, tickView.AskPrice1);
                int BidPos   = AskPos - 1;
                int BidCount = BidPos + 1;
                int AskCount = count - AskPos;

                marketData.Bids = new DepthField[0];
                marketData.Asks = new DepthField[0];

                if (SubscribeBid)
                {
                    if (BidCount > 0)
                    {
                        marketData.Bids = new DepthField[BidCount];
                        int j = 0;
                        for (int i = BidPos; i >= 0; --i)
                        {
                            marketData.Bids[j] = new DepthField()
                            {
                                Price = tickView.DepthList[i].Price,
                                Size  = tickView.DepthList[i].Size,
                                Count = tickView.DepthList[i].Count,
                            };
                            ++j;
                        }
                    }
                }
                if (SubscribeAsk)
                {
                    if (AskCount > 0)
                    {
                        marketData.Asks = new DepthField[AskCount];

                        int j = 0;
                        for (int i = AskPos; i < count; ++i)
                        {
                            marketData.Asks[j] = new DepthField()
                            {
                                Price = tickView.DepthList[i].Price,
                                Size  = tickView.DepthList[i].Size,
                                Count = tickView.DepthList[i].Count,
                            };
                            ++j;
                        }
                    }
                }
            }
            return(marketData);
        }
        public bool Enqueue()
        {
            if (EventQueue.IsFull() || EndOfSeries)
            {
                return(false);
            }

            if (_current == null || !_current.HasNext && _files.Count > 0)
            {
                OpenReader();
                _lastTradeSize = 0;
            }

            // 这种每次只取一个数据方式比较慢,下一阶段可以改成一次读完一个文件
            if (_current != null && _current.HasNext)
            {
                var tick     = _current.Next();
                var dateTime = _current.Codec.GetDateTime(tick.ActionDay == 0?tick.TradingDay:tick.ActionDay).Add(_current.Codec.GetUpdateTime(tick));

                // 在这个地方式可以试着生成自己的TradeEx,这样在策略中,模拟与实盘的效果就完全一样了
                if (_info.SubscribeBidAsk)
                {
                    int count = tick.DepthList.Count;
                    if (count > 0)
                    {
                        int AskPos = DepthListHelper.FindAsk1Position(tick.DepthList, tick.AskPrice1);
                        int BidPos = AskPos - 1;
                        if (BidPos >= 0 && BidPos < count)
                        {
                            var bid = new Bid(dateTime, 0, _info.InstrumentId, _current.Codec.TickToPrice(tick.DepthList[BidPos].Price), tick.DepthList[BidPos].Size);
                            EventQueue.Enqueue(bid);
                        }
                        if (AskPos >= 0 && AskPos < count)
                        {
                            var ask = new Ask(dateTime, 0, _info.InstrumentId, _current.Codec.TickToPrice(tick.DepthList[AskPos].Price), tick.DepthList[AskPos].Size);
                            EventQueue.Enqueue(ask);
                        }
                    }
                }

                if (_info.SubscribeTrade)
                {
                    var trade = new Trade(dateTime, 0, _info.InstrumentId, _current.Codec.GetLastPrice(tick), (int)_current.Codec.GetVolume(tick));
                    trade.Size -= _lastTradeSize;
                    EventQueue.Enqueue(trade);
                    _lastTradeSize = (int)_current.Codec.GetVolume(tick);
                }

                _completed += _current.Setp;
                if (_completed >= _progressCount)
                {
                    _progressCount += _progressDelta;
                    _progressPercent++;
                    EventQueue.Enqueue(new OnSimulatorProgress(_progressCount, _progressPercent));
                }
                return(true);
            }
            else
            {
                EndOfSeries = true;
                return(false);
            }
        }